1
/* upsrw - simple client for read/write variable access (formerly upsct2)
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
21
#include "nut_platform.h"
25
#include <netinet/in.h>
26
#include <sys/socket.h>
28
#include "upsclient.h"
30
static char *upsname = NULL, *hostname = NULL;
31
static UPSCONN_t *ups = NULL;
38
static void usage(const char *prog)
40
printf("Network UPS Tools %s %s\n\n", prog, UPS_VERSION);
41
printf("usage: %s [-h]\n", prog);
42
printf(" %s [-s <variable>] [-u <username>] [-p <password>] <ups>\n\n", prog);
43
printf("Demo program to set variables within UPS hardware.\n");
45
printf(" -h display this help text\n");
46
printf(" -s <variable> specify variable to be changed\n");
47
printf(" use -s VAR=VALUE to avoid prompting for value\n");
48
printf(" -u <username> set username for command authentication\n");
49
printf(" -p <password> set password for command authentication\n");
51
printf(" <ups> UPS identifier - <upsname>[@<hostname>[:<port>]]\n");
53
printf("Call without -s to show all possible read/write variables.\n");
56
static void clean_exit(void)
59
upscli_disconnect(ups);
67
static void do_set(const char *varname, const char *newval)
69
char buf[SMALLBUF], enc[SMALLBUF];
71
snprintf(buf, sizeof(buf), "SET VAR %s %s \"%s\"\n", upsname, varname, pconf_encode(newval, enc, sizeof(enc)));
73
if (upscli_sendline(ups, buf, strlen(buf)) < 0) {
74
fatalx(EXIT_FAILURE, "Can't set variable: %s", upscli_strerror(ups));
77
if (upscli_readline(ups, buf, sizeof(buf)) < 0) {
78
fatalx(EXIT_FAILURE, "Set variable failed: %s", upscli_strerror(ups));
81
/* FUTURE: status cookies will tie in here */
82
if (strncmp(buf, "OK", 2) != 0) {
83
fatalx(EXIT_FAILURE, "Unexpected response from upsd: %s", buf);
86
fprintf(stderr, "%s\n", buf);
89
static void do_setvar(const char *varname, char *uin, const char *pass)
91
char newval[SMALLBUF], temp[SMALLBUF], user[SMALLBUF], *ptr;
95
snprintf(user, sizeof(user), "%s", uin);
97
memset(user, '\0', sizeof(user));
99
pw = getpwuid(getuid());
102
printf("Username (%s): ", pw->pw_name);
104
printf("Username: ");
107
if (fgets(user, sizeof(user), stdin) == NULL) {
108
upsdebug_with_errno(LOG_INFO, "%s", __func__);
111
/* deal with that pesky newline */
112
if (strlen(user) > 1) {
113
user[strlen(user) - 1] = '\0';
116
fatalx(EXIT_FAILURE, "No username available - even tried getpwuid");
119
snprintf(user, sizeof(user), "%s", pw->pw_name);
123
/* leaks - use -p when running in valgrind */
125
pass = GETPASS("Password: " );
128
fatal_with_errno(EXIT_FAILURE, "getpass failed");
132
/* Check if varname is in VAR=VALUE form */
133
if ((ptr = strchr(varname, '=')) != NULL) {
135
snprintf(newval, sizeof(newval), "%s", ptr);
137
printf("Enter new value for %s: ", varname);
139
if (fgets(newval, sizeof(newval), stdin) == NULL) {
140
upsdebug_with_errno(LOG_INFO, "%s", __func__);
142
newval[strlen(newval) - 1] = '\0';
145
snprintf(temp, sizeof(temp), "USERNAME %s\n", user);
147
if (upscli_sendline(ups, temp, strlen(temp)) < 0) {
148
fatalx(EXIT_FAILURE, "Can't set username: %s", upscli_strerror(ups));
151
if (upscli_readline(ups, temp, sizeof(temp)) < 0) {
153
if (upscli_upserror(ups) == UPSCLI_ERR_UNKCOMMAND) {
154
fatalx(EXIT_FAILURE, "Set username failed due to an unknown command. You probably need to upgrade upsd.");
157
fatalx(EXIT_FAILURE, "Set username failed: %s", upscli_strerror(ups));
160
snprintf(temp, sizeof(temp), "PASSWORD %s\n", pass);
162
if (upscli_sendline(ups, temp, strlen(temp)) < 0) {
163
fatalx(EXIT_FAILURE, "Can't set password: %s", upscli_strerror(ups));
166
if (upscli_readline(ups, temp, sizeof(temp)) < 0) {
167
fatalx(EXIT_FAILURE, "Set password failed: %s", upscli_strerror(ups));
170
/* no upsname means die */
172
fatalx(EXIT_FAILURE, "Error: a UPS name must be specified (upsname[@hostname[:port]])");
175
/* old variable names are no longer supported */
176
if (!strchr(varname, '.')) {
177
fatalx(EXIT_FAILURE, "Error: old variable names are not supported");
180
do_set(varname, newval);
183
static const char *get_data(const char *type, const char *varname)
186
unsigned int numq, numa;
188
const char *query[4];
196
ret = upscli_get(ups, numq, query, &numa, &answer);
198
if ((ret < 0) || (numa < numq)) {
202
/* <type> <upsname> <varname> <desc> */
206
static void do_string(const char *varname, const int len)
210
val = get_data("VAR", varname);
213
fatalx(EXIT_FAILURE, "do_string: can't get current value of %s", varname);
216
printf("Type: STRING\n");
217
printf("Maximum length: %d\n", len);
218
printf("Value: %s\n", val);
221
static void do_enum(const char *varname)
224
unsigned int numq, numa;
225
char **answer, buf[SMALLBUF];
226
const char *query[4], *val;
228
/* get current value */
229
val = get_data("VAR", varname);
232
fatalx(EXIT_FAILURE, "do_enum: can't get current value of %s", varname);
235
snprintf(buf, sizeof(buf), "%s", val);
242
ret = upscli_list_start(ups, numq, query);
245
fatalx(EXIT_FAILURE, "Error: %s", upscli_strerror(ups));
248
ret = upscli_list_next(ups, numq, query, &numa, &answer);
250
printf("Type: ENUM\n");
254
/* ENUM <upsname> <varname> <value> */
257
fatalx(EXIT_FAILURE, "Error: insufficient data (got %d args, need at least 4)", numa);
260
printf("Option: \"%s\"", answer[3]);
262
if (!strcmp(answer[3], buf)) {
268
ret = upscli_list_next(ups, numq, query, &numa, &answer);
272
static void do_range(const char *varname)
275
unsigned int numq, numa;
277
const char *query[4], *val;
280
/* get current value */
281
val = get_data("VAR", varname);
284
fatalx(EXIT_FAILURE, "do_range: can't get current value of %s", varname);
294
ret = upscli_list_start(ups, numq, query);
297
fatalx(EXIT_FAILURE, "Error: %s", upscli_strerror(ups));
300
ret = upscli_list_next(ups, numq, query, &numa, &answer);
302
printf("Type: RANGE\n");
306
/* RANGE <upsname> <varname> <min> <max> */
309
fatalx(EXIT_FAILURE, "Error: insufficient data (got %d args, need at least 4)", numa);
312
min = atoi(answer[3]);
313
max = atoi(answer[4]);
315
printf("Option: \"%i-%i\"", min, max);
317
if ((ival >= min) && (ival <= max)) {
323
ret = upscli_list_next(ups, numq, query, &numa, &answer);
327
static void do_type(const char *varname)
330
unsigned int i, numq, numa;
332
const char *query[4];
339
ret = upscli_get(ups, numq, query, &numa, &answer);
341
if ((ret < 0) || (numa < numq)) {
342
printf("Unknown type\n");
346
/* TYPE <upsname> <varname> <type>... */
347
for (i = 3; i < numa; i++) {
349
if (!strcasecmp(answer[i], "ENUM")) {
354
if (!strcasecmp(answer[i], "RANGE")) {
359
if (!strncasecmp(answer[i], "STRING:", 7)) {
361
char *len = answer[i] + 7;
362
int length = strtol(len, NULL, 10);
364
do_string(varname, length);
369
/* ignore this one */
370
if (!strcasecmp(answer[i], "RW")) {
374
printf("Type: %s (unrecognized)\n", answer[i]);
378
static void print_rw(const char *varname)
382
printf("[%s]\n", varname);
384
tmp = get_data("DESC", varname);
389
printf("Description unavailable\n");
397
static void print_rwlist(void)
400
unsigned int numq, numa;
401
const char *query[2];
403
struct list_t *lhead, *llast, *ltmp, *lnext;
405
/* the upsname is now required */
407
fatalx(EXIT_FAILURE, "Error: a UPS name must be specified (upsname[@hostname[:port]])");
410
llast = lhead = NULL;
416
ret = upscli_list_start(ups, numq, query);
420
/* old upsd --> fall back on old LISTRW technique */
421
if (upscli_upserror(ups) == UPSCLI_ERR_UNKCOMMAND) {
422
fatalx(EXIT_FAILURE, "Error: upsd is too old to support this query");
425
fatalx(EXIT_FAILURE, "Error: %s", upscli_strerror(ups));
428
ret = upscli_list_next(ups, numq, query, &numa, &answer);
432
/* RW <upsname> <varname> <value> */
434
fatalx(EXIT_FAILURE, "Error: insufficient data (got %d args, need at least 4)", numa);
437
/* sock this entry away for later */
439
ltmp = xmalloc(sizeof(struct list_t));
440
ltmp->name = xstrdup(answer[2]);
451
ret = upscli_list_next(ups, numq, query, &numa, &answer);
454
/* use the list to get descriptions and types */
461
print_rw(ltmp->name);
469
int main(int argc, char **argv)
472
const char *prog = xbasename(argv[0]);
473
char *password = NULL, *username = NULL, *setvar = NULL;
475
while ((i = getopt(argc, argv, "+hs:p:u:V")) != -1) {
488
printf("Network UPS Tools %s %s\n", prog, UPS_VERSION);
505
/* be a good little client that cleans up after itself */
508
if (upscli_splitname(argv[0], &upsname, &hostname, &port) != 0) {
509
fatalx(EXIT_FAILURE, "Error: invalid UPS definition. Required format: upsname[@hostname[:port]]");
512
ups = xcalloc(1, sizeof(*ups));
514
if (upscli_connect(ups, hostname, port, 0) < 0) {
515
fatalx(EXIT_FAILURE, "Error: %s", upscli_strerror(ups));
519
/* setting a variable */
520
do_setvar(setvar, username, password);
522
/* if not, get the list of supported read/write variables */
530
/* Formal do_upsconf_args implementation to satisfy linker on AIX */
531
#if (defined NUT_PLATFORM_AIX)
532
void do_upsconf_args(char *upsname, char *var, char *val) {
533
fatalx(EXIT_FAILURE, "INTERNAL ERROR: formal do_upsconf_args called");
535
#endif /* end of #if (defined NUT_PLATFORM_AIX) */