1
/* upsct - simple "client" to test read/write variable access
3
Copyright (C) 1999 Russell Kroll <rkroll@exploits.org>
5
This program is free software; you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation; either version 2 of the License, or
8
(at your option) any later version.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with this program; if not, write to the Free Software
17
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
#include <netinet/in.h>
24
#include <sys/socket.h>
30
void do_enum(char *varname)
32
char out[SMALLBUF], temp[SMALLBUF];
34
snprintf(out, sizeof(out), "ENUM %s\n", varname);
36
if (upssendraw(fd, out) < 0) {
37
printf("Enumerate %s failed: %s\n",
38
varname, upsstrerror(upserror));
42
if (upsreadraw(fd, temp, sizeof(temp)) < 0) {
43
printf("Enumerate %s failed: %s\n",
44
varname, upsstrerror(upserror));
48
if (strncmp(temp, "ENUM", 4) != 0) {
49
printf("Bogus reply from server for %s\n", varname);
53
if (upsreadraw(fd, temp, sizeof(temp)) < 0) {
54
printf("Can't get next ENUM value: %s\n",
55
upsstrerror(upserror));
59
while (strcmp(temp, "END") != 0) {
60
printf("Option: %s\n", strstr(temp, "\""));
62
if (upsreadraw(fd, temp, sizeof(temp)) < 0) {
63
printf("Can't get next ENUM value: %s\n",
64
upsstrerror(upserror));
70
void usage(char *prog)
72
printf("Network UPS Tools upsct2 %s\n\n", UPS_VERSION);
73
printf("usage: %s [-h]\n", prog);
74
printf(" %s [-s <variable>] [-u <username>] [-p <password>] <ups>\n\n", prog);
75
printf("Demo program to set variables within UPS hardware.\n");
77
printf(" -h display this help text\n");
78
printf(" -s <variable> specify variable to be changed\n");
79
printf(" use -s VAR=VALUE to avoid prompting for value\n");
80
printf(" -u <username> set username for command authentication\n");
81
printf(" -p <password> set password for command authentication\n");
83
printf(" <ups> UPS identifier - myups@localhost, etc.\n");
85
printf("Call without -s to show all possible read/write variables.\n");
89
void do_setvar(char *varname, int fd, char *upsname, char *user, char *pass)
91
char newval[SMALLBUF], temp[SMALLBUF], *ptr;
95
user = xmalloc(SMALLBUF);
96
fgets(user, SMALLBUF, stdin);
98
/* deal with that pesky newline */
100
user[strlen(user) - 1] = '\0';
104
pass = getpass("Password: " );
106
/* Check if varname is in VAR=VALUE form */
107
if ((ptr = strchr(varname, '=')) != NULL) {
109
strlcpy(newval, ptr, sizeof(newval));
111
printf("Enter new value for %s: ", varname);
113
fgets(newval, sizeof(newval), stdin);
114
newval[strlen(newval) - 1] = '\0';
117
snprintf(temp, sizeof(temp), "USERNAME %s\n", user);
119
if (upssendraw(fd, temp) < 0) {
120
printf("Can't set username: %s\n", upsstrerror(upserror));
124
if (upsreadraw(fd, temp, sizeof(temp)) < 0) {
125
if (upserror == UPSF_UNKNOWNCMD) {
126
printf("Set username failed due to an unknown command.\n");
127
printf("You probably need to upgrade upsd.\n");
131
printf("Set username failed: %s\n", upsstrerror(upserror));
135
snprintf(temp, sizeof(temp), "PASSWORD %s\n", pass);
137
if (upssendraw(fd, temp) < 0) {
138
printf("Can't set password: %s\n", upsstrerror(upserror));
142
if (upsreadraw(fd, temp, sizeof(temp)) < 0) {
143
printf("Set password failed: %s\n", upsstrerror(upserror));
147
snprintf(temp, sizeof(temp), "SET %s %s\n", varname, newval);
149
if (upssendraw(fd, temp) < 0) {
150
printf("Can't set variable: %s\n", upsstrerror(upserror));
154
if (upsreadraw(fd, temp, sizeof(temp)) < 0) {
155
printf("Set variable failed: %s\n", upsstrerror(upserror));
162
int main(int argc, char **argv)
164
char *upsname, *ptr, *host, vars[SMALLBUF], temp[SMALLBUF], out[SMALLBUF], *v,
165
type[16], *setvar, *prog, *password = NULL, *username = NULL;
171
while ((i = getopt(argc, argv, "+s:p:u:")) != EOF) {
174
setvar = xstrdup(optarg);
194
/* handle upsname@hostname syntax and split up parts */
195
ptr = strstr(argv[0], "@");
206
fd = upsconnect(host);
208
printf("Unable to connect to %s - %s\n", host,
209
upsstrerror(upserror));
213
/* setting a variable? */
215
do_setvar(setvar, fd, upsname, username, password);
217
/* if not, get the list of supported read/write variables */
219
printf("host: %s\n", host);
224
snprintf(buf, sizeof(buf), "LISTRW %s\n", upsname);
225
ret = upssendraw(fd, buf);
228
ret = upssendraw(fd, "LISTRW\n");
231
printf("Can't list read/write variables: %s\n",
232
upsstrerror(upserror));
236
if (upsreadraw(fd, vars, sizeof(vars)) < 0) {
237
printf("Unable to get variable list - %s\n",
238
upsstrerror(upserror));
243
ptr = strchr(v, ' ');
245
printf("Broken string from server: %s\n", vars);
252
/* possibly skip '@ups' */
254
ptr = strchr(ptr, ' ');
256
printf("No R/W variables available on this UPS.\n");
264
ptr = strchr(v, ' ');
268
/* get description */
269
snprintf(out, sizeof(out), "VARDESC %s\n", v);
271
if (upssendraw(fd, out) < 0) {
272
printf("Can't get description of %s: %s\n",
273
v, upsstrerror(upserror));
277
if (upsreadraw(fd, temp, sizeof(temp)) < 0) {
278
printf("Can't get description of %s: %s\n",
279
v, upsstrerror(upserror));
283
printf("[%s] %s ", v, strstr(temp, "\""));
286
snprintf(out, sizeof(out), "VARTYPE %s\n", v);
288
if (upssendraw(fd, out) < 0) {
289
printf("Can't get type of %s: %s\n",
290
v, upsstrerror(upserror));
294
if (upsreadraw(fd, temp, sizeof(temp)) < 0) {
295
printf("Can't get type of %s: %s\n",
296
v, upsstrerror(upserror));
300
sscanf(temp, "%*s %s %i", type, &typelen);
301
printf("(%s:%i)\n", type, typelen);
303
if (!strcmp(type, "ENUM"))
306
if (!strcmp(type, "STRING")) {
307
if (getupsvarfd(fd, upsname, v, temp,
310
printf("Can't get value of %s: %s\n",
311
temp, upsstrerror(upserror));
315
printf("Value: %s\n", temp);