30
31
struct list_t *next;
33
static int old_do_enum(UPSCONN *ups, const char *varname)
35
char out[SMALLBUF], temp[SMALLBUF];
37
snprintf(out, sizeof(out), "ENUM %s\n", varname);
39
if (upscli_sendline(ups, out, strlen(out)) < 0) {
40
fprintf(stderr, "Enumerate %s failed: %s\n",
41
varname, upscli_strerror(ups));
43
return -1; /* failed */
46
if (upscli_readline(ups, temp, sizeof(temp)) < 0) {
47
fprintf(stderr, "Enumerate %s failed: %s\n",
48
varname, upscli_strerror(ups));
53
if (strncmp(temp, "ENUM", 4) != 0) {
54
fprintf(stderr, "Bogus reply from server for %s\n", varname);
58
if (upscli_readline(ups, temp, sizeof(temp)) < 0) {
59
fprintf(stderr, "Can't get next ENUM value: %s\n",
60
upscli_strerror(ups));
65
while (strcmp(temp, "END") != 0) {
66
printf("Option: %s\n", strstr(temp, "\""));
68
if (upscli_readline(ups, temp, sizeof(temp)) < 0) {
69
fprintf(stderr, "Can't get next ENUM value: %s\n",
70
upscli_strerror(ups));
79
static void usage(char *prog)
34
static void usage(const char *prog)
81
36
printf("Network UPS Tools upsrw %s\n\n", UPS_VERSION);
82
printf("usage: %s [-h]\n", prog);
37
printf("usage: %s [-h]\n", prog);
83
38
printf(" %s [-s <variable>] [-u <username>] [-p <password>] <ups>\n\n", prog);
84
39
printf("Demo program to set variables within UPS hardware.\n");
86
printf(" -h display this help text\n");
41
printf(" -h display this help text\n");
87
42
printf(" -s <variable> specify variable to be changed\n");
88
43
printf(" use -s VAR=VALUE to avoid prompting for value\n");
89
printf(" -u <username> set username for command authentication\n");
90
printf(" -p <password> set password for command authentication\n");
92
printf(" <ups> UPS identifier - myups@localhost, etc.\n");
44
printf(" -u <username> set username for command authentication\n");
45
printf(" -p <password> set password for command authentication\n");
47
printf(" <ups> UPS identifier - <upsname>@<hostname>[:<port>]\n");
94
49
printf("Call without -s to show all possible read/write variables.\n");
99
54
static void clean_exit(UPSCONN *ups, char *upsname, char *hostname, int code)
112
static int old_set(UPSCONN *ups, const char *upsname, const char *varname,
118
snprintf(temp, sizeof(temp), "SET %s@%s %s\n",
119
varname, upsname, newval);
121
snprintf(temp, sizeof(temp), "SET %s %s\n", varname, newval);
123
if (upscli_sendline(ups, temp, strlen(temp)) < 0) {
124
fprintf(stderr, "Can't set variable: %s\n",
125
upscli_strerror(ups));
130
if (upscli_readline(ups, temp, sizeof(temp)) < 0) {
131
fprintf(stderr, "Set variable failed: %s\n",
132
upscli_strerror(ups));
140
static int new_set(UPSCONN *ups, const char *upsname, const char *varname,
67
static int do_set(UPSCONN *ups, const char *upsname, const char *varname,
141
68
const char *newval)
143
70
char buf[SMALLBUF], enc[SMALLBUF];
149
76
fprintf(stderr, "Can't set variable: %s\n",
150
77
upscli_strerror(ups));
155
82
if (upscli_readline(ups, buf, sizeof(buf)) < 0) {
156
83
fprintf(stderr, "Set variable failed: %s\n",
157
84
upscli_strerror(ups));
162
89
/* FUTURE: status cookies will tie in here */
163
90
if (strncmp(buf, "OK", 2) != 0) {
164
91
printf("Unexpected response from upsd: %s\n", buf);
171
98
static int do_setvar(UPSCONN *ups, const char *varname, char *uin,
172
99
const char *pass, char *upsname, char *hostname)
174
101
char newval[SMALLBUF], temp[SMALLBUF], user[SMALLBUF], *ptr;
177
105
snprintf(user, sizeof(user), "%s", uin);
180
printf("Username: ");
107
memset(user, sizeof(user), '\0');
109
pw = getpwuid(getuid());
112
printf("Username (%s): ", pw->pw_name);
114
printf("Username: ");
181
116
fgets(user, sizeof(user), stdin);
183
118
/* deal with that pesky newline */
184
119
if (strlen(user) > 1)
185
120
user[strlen(user) - 1] = '\0';
123
fatalx("No username available - even tried getpwuid");
125
snprintf(user, sizeof(user), "%s", pw->pw_name);
188
129
/* leaks - use -p when running in valgrind */
239
181
fprintf(stderr, "Can't set password: %s\n",
240
182
upscli_strerror(ups));
245
187
if (upscli_readline(ups, temp, sizeof(temp)) < 0) {
246
188
fprintf(stderr, "Set password failed: %s\n",
247
189
upscli_strerror(ups));
252
/* no upsname = fallback until 2.0 */
254
return old_set(ups, upsname, varname, newval);
256
/* also fallback for old variable names */
257
if (!strchr(varname, '.'))
258
return old_set(ups, upsname, varname, newval);
260
return new_set(ups, upsname, varname, newval);
194
/* no upsname means die */
196
fprintf(stderr, "Error: a UPS name must be specified (upsname@hostname)\n");
200
/* old variable names are no longer supported */
201
if (!strchr(varname, '.')) {
202
fprintf(stderr, "Error: old variable names are not supported\n");
206
return do_set(ups, upsname, varname, newval);
263
static int old_print_rwlist(UPSCONN *ups, char *upsname, char *hostname)
266
char *v, *sp, *ptr, vars[SMALLBUF], out[SMALLBUF], temp[SMALLBUF],
269
printf("host: %s\n", hostname);
271
if (upscli_getlist(ups, upsname, UPSCLI_LIST_RW, vars,
273
fprintf(stderr, "Unable to get variable list - %s\n",
274
upscli_strerror(ups));
279
if (strlen(vars) == 0) {
280
fprintf(stderr, "Error: no read/write variables available\n");
285
ptr = strchr(v, ' ');
287
fprintf(stderr, "Broken string from server: %s\n", vars);
292
ptr = strchr(v, ' ');
296
/* get description */
297
snprintf(out, sizeof(out), "VARDESC %s\n", v);
299
if (upscli_sendline(ups, out, strlen(out)) < 0) {
300
fprintf(stderr, "Can't get description of %s: %s\n",
301
v, upscli_strerror(ups));
305
if (upscli_readline(ups, temp, sizeof(temp)) < 0) {
306
fprintf(stderr, "Can't get description of %s: %s\n",
307
v, upscli_strerror(ups));
311
printf("[%s] %s ", v, strstr(temp, "\""));
314
snprintf(out, sizeof(out),
315
(upsname ? "VARTYPE %s@%s\n" : "VARTYPE %s\n"), v, upsname);
317
if (upscli_sendline(ups, out, strlen(out)) < 0) {
318
fprintf(stderr, "Can't get type of %s: %s\n",
319
v, upscli_strerror(ups));
323
if (upscli_readline(ups, temp, sizeof(temp)) < 0) {
324
fprintf(stderr, "Can't get type of %s: %s\n",
325
v, upscli_strerror(ups));
330
/* TYPE (ENUM|STRING) <num> */
332
sp = strchr(temp, ' ');
333
if ((!sp) || (strncmp(temp, "TYPE ", 5) != 0)) {
334
fprintf(stderr, "Unexpected response from server [%s]\n",
344
/* now make sure we can handle this vartype */
346
sp = strchr(type, ' ');
349
fprintf(stderr, "Unexpected vartype from server [%s]\n",
357
typelen = strtol(sp, (char **) NULL, 10);
359
printf("(%s:%d)\n", type, typelen);
361
if (!strcmp(type, "ENUM"))
362
if (old_do_enum(ups, v) != 0)
365
if (!strcmp(type, "STRING")) {
366
if (upscli_getvar(ups, upsname, v, temp,
369
fprintf(stderr, "Can't get value of %s: %s\n",
370
temp, upscli_strerror(ups));
375
printf("Value: %s\n", temp);
385
209
static const char *get_data(const char *type, UPSCONN *ups,
386
210
const char *upsname, const char *varname)
213
unsigned int numq, numa;
390
215
const char *query[4];
653
upscli_splitname(argv[0], &upsname, &hostname, &port);
494
check_upsdef(argv[0]);
496
upsname = hostname = NULL;
498
if (upscli_splitname(argv[0], &upsname, &hostname, &port) != 0)
499
clean_exit(&ups, upsname, hostname, EXIT_FAILURE);
655
501
if (upscli_connect(&ups, hostname, port, 0) < 0) {
656
502
fprintf(stderr, "Can't connect: %s\n", upscli_strerror(&ups));
657
clean_exit(&ups, upsname, hostname, 1);
503
clean_exit(&ups, upsname, hostname, EXIT_FAILURE);
660
506
/* setting a variable? */