~ubuntu-branches/ubuntu/saucy/nut/saucy

« back to all changes in this revision

Viewing changes to clients/upsct2.c

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Quette
  • Date: 2004-05-28 13:10:01 UTC
  • mto: (16.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20040528131001-yj2m9qcez4ya2w14
Tags: upstream-1.4.2
ImportĀ upstreamĀ versionĀ 1.4.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* upsct - simple "client" to test read/write variable access
2
 
 
3
 
   Copyright (C) 1999  Russell Kroll <rkroll@exploits.org>
4
 
 
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.
9
 
 
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.
14
 
 
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
18
 
*/
19
 
 
20
 
#include "common.h"
21
 
 
22
 
#include <netdb.h>
23
 
#include <netinet/in.h>
24
 
#include <sys/socket.h>
25
 
 
26
 
#include "upsfetch.h"
27
 
 
28
 
        int     fd;
29
 
 
30
 
void do_enum(char *varname)
31
 
{
32
 
        char    out[SMALLBUF], temp[SMALLBUF];
33
 
 
34
 
        snprintf(out, sizeof(out), "ENUM %s\n", varname);
35
 
 
36
 
        if (upssendraw(fd, out) < 0) {
37
 
                printf("Enumerate %s failed: %s\n", 
38
 
                        varname, upsstrerror(upserror));
39
 
                exit(1);
40
 
        }
41
 
 
42
 
        if (upsreadraw(fd, temp, sizeof(temp)) < 0) {
43
 
                printf("Enumerate %s failed: %s\n",
44
 
                        varname, upsstrerror(upserror));
45
 
                exit(1);
46
 
        }
47
 
 
48
 
        if (strncmp(temp, "ENUM", 4) != 0) {
49
 
                printf("Bogus reply from server for %s\n", varname);
50
 
                return;
51
 
        }
52
 
 
53
 
        if (upsreadraw(fd, temp, sizeof(temp)) < 0) {
54
 
                printf("Can't get next ENUM value: %s\n",
55
 
                        upsstrerror(upserror));
56
 
                exit(1);
57
 
        }
58
 
 
59
 
        while (strcmp(temp, "END") != 0) {
60
 
                printf("Option: %s\n", strstr(temp, "\""));
61
 
 
62
 
                if (upsreadraw(fd, temp, sizeof(temp)) < 0) {
63
 
                        printf("Can't get next ENUM value: %s\n",
64
 
                                upsstrerror(upserror));
65
 
                        exit(1);
66
 
                }
67
 
        }
68
 
}
69
 
 
70
 
void usage(char *prog)
71
 
{
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");
76
 
        printf("\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");
82
 
        printf("\n");
83
 
        printf("  <ups>         UPS identifier - myups@localhost, etc.\n");
84
 
        printf("\n");
85
 
        printf("Call without -s to show all possible read/write variables.\n");
86
 
        exit(1);
87
 
}
88
 
 
89
 
void do_setvar(char *varname, int fd, char *upsname, char *user, char *pass)
90
 
{
91
 
        char    newval[SMALLBUF], temp[SMALLBUF], *ptr;
92
 
 
93
 
        if (!user) {
94
 
                printf("Username: ");
95
 
                user = xmalloc(SMALLBUF);
96
 
                fgets(user, SMALLBUF, stdin);
97
 
 
98
 
                /* deal with that pesky newline */
99
 
                if (strlen(user) > 1)
100
 
                        user[strlen(user) - 1] = '\0';
101
 
        }
102
 
 
103
 
        if (!pass)
104
 
                pass = getpass("Password: " );
105
 
 
106
 
        /* Check if varname is in VAR=VALUE form */
107
 
        if ((ptr = strchr(varname, '=')) != NULL) {
108
 
                *ptr++ = 0;
109
 
                strlcpy(newval, ptr, sizeof(newval));
110
 
        } else {
111
 
                printf("Enter new value for %s: ", varname);
112
 
                fflush(stdout);
113
 
                fgets(newval, sizeof(newval), stdin);
114
 
                newval[strlen(newval) - 1] = '\0';
115
 
        }
116
 
 
117
 
        snprintf(temp, sizeof(temp), "USERNAME %s\n", user);
118
 
 
119
 
        if (upssendraw(fd, temp) < 0) {
120
 
                printf("Can't set username: %s\n", upsstrerror(upserror));
121
 
                exit(1);
122
 
        }
123
 
 
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");
128
 
                        exit(1);
129
 
                }
130
 
 
131
 
                printf("Set username failed: %s\n", upsstrerror(upserror));
132
 
                exit(1);
133
 
        }
134
 
 
135
 
        snprintf(temp, sizeof(temp), "PASSWORD %s\n", pass);
136
 
 
137
 
        if (upssendraw(fd, temp) < 0) {
138
 
                printf("Can't set password: %s\n", upsstrerror(upserror));
139
 
                exit(1);
140
 
        }
141
 
 
142
 
        if (upsreadraw(fd, temp, sizeof(temp)) < 0) {
143
 
                printf("Set password failed: %s\n", upsstrerror(upserror));
144
 
                exit(1);
145
 
        }
146
 
 
147
 
        snprintf(temp, sizeof(temp), "SET %s %s\n", varname, newval);
148
 
 
149
 
        if (upssendraw(fd, temp) < 0) {
150
 
                printf("Can't set variable: %s\n", upsstrerror(upserror));
151
 
                exit(1);
152
 
        }
153
 
 
154
 
        if (upsreadraw(fd, temp, sizeof(temp)) < 0) {
155
 
                printf("Set variable failed: %s\n", upsstrerror(upserror));
156
 
                exit(1);
157
 
        }
158
 
 
159
 
        exit(0);
160
 
}       
161
 
 
162
 
int main(int argc, char **argv)
163
 
{
164
 
        char    *upsname, *ptr, *host, vars[SMALLBUF], temp[SMALLBUF], out[SMALLBUF], *v,
165
 
                type[16], *setvar, *prog, *password = NULL, *username = NULL;
166
 
        int     typelen, i, ret;
167
 
 
168
 
        setvar = NULL;
169
 
        prog = argv[0];
170
 
 
171
 
        while ((i = getopt(argc, argv, "+s:p:u:")) != EOF) {
172
 
                switch (i) {
173
 
                case 's':
174
 
                        setvar = xstrdup(optarg);
175
 
                        break;
176
 
                case 'p':
177
 
                        password = optarg;
178
 
                        break;
179
 
                case 'u':
180
 
                        username = optarg;
181
 
                        break;
182
 
                default:
183
 
                        usage(prog);
184
 
                        break;
185
 
                }
186
 
        }
187
 
 
188
 
        argc -= optind;
189
 
        argv += optind;
190
 
 
191
 
        if (argc < 1)
192
 
                usage(prog);
193
 
 
194
 
        /* handle upsname@hostname syntax and split up parts */
195
 
        ptr = strstr(argv[0], "@");
196
 
        if (ptr != NULL) {
197
 
                ptr[0] = 0;
198
 
                upsname = argv[0];
199
 
                host = ptr + 1;
200
 
        }
201
 
        else {
202
 
                upsname = NULL;
203
 
                host = argv[0];
204
 
        }
205
 
 
206
 
        fd = upsconnect(host);
207
 
        if (fd < 0) {
208
 
                printf("Unable to connect to %s - %s\n", host,
209
 
                       upsstrerror(upserror));
210
 
                exit(1);
211
 
        }
212
 
 
213
 
        /* setting a variable? */
214
 
        if (setvar)
215
 
                do_setvar(setvar, fd, upsname, username, password);
216
 
 
217
 
        /* if not, get the list of supported read/write variables */
218
 
 
219
 
        printf("host: %s\n", host);
220
 
 
221
 
        if (upsname) {
222
 
                char    buf[SMALLBUF];
223
 
 
224
 
                snprintf(buf, sizeof(buf), "LISTRW %s\n", upsname);
225
 
                ret = upssendraw(fd, buf);
226
 
        }
227
 
        else
228
 
                ret = upssendraw(fd, "LISTRW\n");
229
 
 
230
 
        if (ret < 0) {
231
 
                printf("Can't list read/write variables: %s\n",
232
 
                        upsstrerror(upserror));
233
 
                exit(1);
234
 
        }
235
 
        
236
 
        if (upsreadraw(fd, vars, sizeof(vars)) < 0) {
237
 
                printf("Unable to get variable list - %s\n",
238
 
                       upsstrerror(upserror));
239
 
                exit(1);
240
 
        }
241
 
 
242
 
        v = vars;
243
 
        ptr = strchr(v, ' ');
244
 
        if (!ptr) {
245
 
                printf("Broken string from server: %s\n", vars);
246
 
                exit(1);
247
 
        }
248
 
 
249
 
        *ptr++ = '\0';
250
 
        v = ptr;
251
 
 
252
 
        /* possibly skip '@ups' */
253
 
        if (ptr[0] == '@') {
254
 
                ptr = strchr(ptr, ' ');
255
 
                if (!ptr) {
256
 
                        printf("No R/W variables available on this UPS.\n");
257
 
                        exit(0);
258
 
                }
259
 
                ptr++;
260
 
                v = ptr;
261
 
        }
262
 
 
263
 
        while (v != NULL) {
264
 
                ptr = strchr(v, ' ');
265
 
                if (ptr)
266
 
                        *ptr++ = '\0';
267
 
 
268
 
                /* get description */
269
 
                snprintf(out, sizeof(out), "VARDESC %s\n", v);
270
 
 
271
 
                if (upssendraw(fd, out) < 0) {
272
 
                        printf("Can't get description of %s: %s\n",
273
 
                                v, upsstrerror(upserror));
274
 
                        exit(1);
275
 
                }
276
 
 
277
 
                if (upsreadraw(fd, temp, sizeof(temp)) < 0) {
278
 
                        printf("Can't get description of %s: %s\n",
279
 
                                v, upsstrerror(upserror));
280
 
                        exit(1);
281
 
                }
282
 
 
283
 
                printf("[%s] %s ", v, strstr(temp, "\""));
284
 
 
285
 
                /* now get type */
286
 
                snprintf(out, sizeof(out), "VARTYPE %s\n", v);
287
 
 
288
 
                if (upssendraw(fd, out) < 0) {
289
 
                        printf("Can't get type of %s: %s\n",
290
 
                                v, upsstrerror(upserror));
291
 
                        exit(1);
292
 
                }
293
 
 
294
 
                if (upsreadraw(fd, temp, sizeof(temp)) < 0) {
295
 
                        printf("Can't get type of %s: %s\n",
296
 
                                v, upsstrerror(upserror));
297
 
                        exit(1);
298
 
                }
299
 
 
300
 
                sscanf(temp, "%*s %s %i", type, &typelen); 
301
 
                printf("(%s:%i)\n", type, typelen);
302
 
 
303
 
                if (!strcmp(type, "ENUM"))
304
 
                        do_enum(v);
305
 
 
306
 
                if (!strcmp(type, "STRING")) {
307
 
                        if (getupsvarfd(fd, upsname, v, temp, 
308
 
                                sizeof(temp)) < 0) {
309
 
 
310
 
                                printf("Can't get value of %s: %s\n",
311
 
                                        temp, upsstrerror(upserror));
312
 
                                exit(1);
313
 
                        }
314
 
                                
315
 
                        printf("Value: %s\n", temp);
316
 
                }
317
 
                
318
 
                v = ptr;
319
 
        }
320
 
        
321
 
        return 0;
322
 
}