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

« back to all changes in this revision

Viewing changes to clients/upsset.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2005-07-20 19:48:50 UTC
  • mto: (16.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20050720194850-oo61wjr33rrx2mre
Tags: upstream-2.0.2
ImportĀ upstreamĀ versionĀ 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
137
137
        }
138
138
}
139
139
 
140
 
static void do_enum(const char *varname)
141
 
{
142
 
        char    out[SMALLBUF], temp[SMALLBUF], *val, *sel, *ptr;
143
 
 
144
 
        snprintf(out, sizeof(out),
145
 
                (upsname ? "ENUM %s@%s\n" : "ENUM %s\n"), varname, upsname);
146
 
        
147
 
        if (upscli_sendline(&ups, out, strlen(out)) < 0) {
148
 
                fprintf(stderr, "ENUM %s failed: %s\n", varname,
149
 
                        upscli_strerror(&ups));
150
 
                printf("Unable to get ENUM list\n");
151
 
                return;
152
 
        }
153
 
 
154
 
        if (upscli_readline(&ups, temp, sizeof(temp)) < 0) {
155
 
                fprintf(stderr, "Read ENUM %s failed: %s\n", varname,
156
 
                        upscli_strerror(&ups));
157
 
                printf("Unable to get ENUM list\n");
158
 
                return;
159
 
        }
160
 
 
161
 
        if (strncmp(temp, "ENUM", 4) != 0) {
162
 
                printf("Bogus reply from server for %s\n", varname);
163
 
                return;
164
 
        }
165
 
 
166
 
        printf("<SELECT NAME=\"UPSVAR_%s\">\n", varname);
167
 
 
168
 
        /* 'OPTION "<val>" [SELECTED]' || 'END' */
169
 
        if (upscli_readline(&ups, temp, sizeof(temp)) < 0) {
170
 
                printf("<!-- Error reading OPTION list -->\n");
171
 
 
172
 
                fprintf(stderr, "Can't read OPTION list: %s\n",
173
 
                        upscli_strerror(&ups));
174
 
                printf("</SELECT>\n");
175
 
                return;
176
 
        }
177
 
 
178
 
        while (strcmp(temp, "END") != 0) {
179
 
 
180
 
                /* split into value and selected */
181
 
                val = strchr(temp, '"');
182
 
 
183
 
                /* sanity check - no " means a protocol problem */
184
 
                if (!val) {
185
 
                        printf("<!-- Error: no quote detected in ENUM response -->\n");
186
 
                        printf("</SELECT>\n");
187
 
                        return;
188
 
                }
189
 
 
190
 
                /* move off the " */
191
 
                val++;
192
 
 
193
 
                sel = strchr(val, ' ');
194
 
                if (sel)
195
 
                        sel++;
196
 
                ptr = strchr(val, '"');
197
 
 
198
 
                /* same idea */
199
 
                if (!ptr) {
200
 
                        printf("<!-- Error: no quote detected in ENUM response -->\n");
201
 
                        printf("</SELECT>\n");
202
 
                        return;
203
 
                }
204
 
 
205
 
                val [ptr - val] = '\0';
206
 
                        
207
 
                printf("<OPTION VALUE=\"%s\" ", val);
208
 
                        
209
 
                if ((sel != NULL) && (!strcmp(sel, "SELECTED")))
210
 
                        printf("SELECTED");
211
 
 
212
 
                printf(">%s</OPTION>\n", val);  
213
 
 
214
 
                if (upscli_readline(&ups, temp, sizeof(temp)) < 0) {
215
 
                        printf("<!-- Error reading OPTION list -->\n");
216
 
 
217
 
                        fprintf(stderr, "Can't read OPTION list: %s\n",
218
 
                                upscli_strerror(&ups));
219
 
                        printf("</SELECT>\n");
220
 
                        return;
221
 
                }
222
 
        }
223
 
 
224
 
        printf("</SELECT>\n");
225
 
}
226
 
 
227
 
static void do_string(const char *varname, int typelen)
228
 
{
229
 
        char    temp[SMALLBUF];
230
 
 
231
 
        if (upscli_getvar(&ups, upsname, varname, temp, sizeof(temp)) == 0)
232
 
                printf("<INPUT TYPE=\"TEXT\" NAME=\"UPSVAR_%s\" VALUE=\"%s\" "
233
 
                        "SIZE=\"%i\">\n",
234
 
                        varname, temp, typelen);
235
 
}
236
 
 
237
140
static void do_header(const char *title)
238
141
{
239
142
        printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
252
155
        printf("<TABLE CELLPADDING=\"5\" CELLSPACING=\"0\" ALIGN=\"CENTER\" WIDTH=\"100%%\">\n");
253
156
        printf("<TR><TH COLSPAN=2 BGCOLOR=\"#60B0B0\">\n");
254
157
        printf("<FONT SIZE=\"+2\">Network UPS Tools upsset %s</FONT>\n", 
255
 
                UPS_VERSION);
 
158
                UPS_VERSION);
256
159
        printf("</TH></TR>\n");
257
160
}
258
161
 
260
163
static void do_hidden(const char *next)
261
164
{
262
165
        printf("<INPUT TYPE=\"HIDDEN\" NAME=\"username\" VALUE=\"%s\">\n",
263
 
               username);
 
166
                username);
264
167
        printf("<INPUT TYPE=\"HIDDEN\" NAME=\"password\" VALUE=\"%s\">\n",
265
 
               password);
 
168
                password);
266
169
 
267
170
        if (next)
268
171
                printf("<INPUT TYPE=\"HIDDEN\" NAME=\"function\" VALUE=\"%s\">\n", 
269
 
                       next);
 
172
                        next);
270
173
}
271
174
 
272
175
/* generate SELECT chooser from hosts.conf entries */
288
191
}
289
192
 
290
193
/* called for fatal errors in parseconf like malloc failures */
291
 
void upsset_hosts_err(const char *errmsg)
 
194
static void upsset_hosts_err(const char *errmsg)
292
195
{
293
196
        upslogx(LOG_ERR, "Fatal error in parseconf(hosts.conf): %s", errmsg);
294
197
}
325
228
                if (pconf_parse_error(&ctx)) {
326
229
                        upslogx(LOG_ERR, "Parse error: %s:%d: %s",
327
230
                                hostfn, ctx.linenum, ctx.errmsg);
328
 
                        
 
231
 
329
232
                        continue;
330
233
                }
331
234
 
386
289
        printf("</BODY></HTML>\n");
387
290
 
388
291
        upscli_disconnect(&ups);
389
 
        exit(0);
 
292
        exit(EXIT_SUCCESS);
390
293
}
391
294
 
392
295
static void loginscreen(void)
415
318
        printf("</BODY></HTML>\n");
416
319
 
417
320
        upscli_disconnect(&ups);
418
 
        exit(0);
 
321
        exit(EXIT_SUCCESS);
419
322
}
420
323
 
421
324
/* try to connect to upsd - generate an error page if it fails */
422
325
static void upsd_connect(void)
423
326
{
424
 
        upscli_splitname(monups, &upsname, &hostname, &port);
 
327
        if (upscli_splitname(monups, &upsname, &hostname, &port) != 0) {
 
328
                error_page("showsettings", "UPS name is unusable",
 
329
                        "Unable to split UPS name [%s]", monups);
 
330
                /* NOTREACHED */
 
331
        }
425
332
 
426
333
        if (upscli_connect(&ups, hostname, port, 0) < 0) {
427
334
                error_page("showsettings", "Connect failure",
431
338
        }
432
339
}
433
340
 
434
 
static void old_showcmds(const char *desc)
435
 
{
436
 
        char    *ptr, cmds[SMALLBUF], *tmp;
437
 
 
438
 
        if (upscli_getlist(&ups, upsname, UPSCLI_LIST_CMDS, cmds, 
439
 
                sizeof(cmds)) < 0) {
440
 
 
441
 
                error_page("showcmds", "Protocol failure",
442
 
                         "Unable to get list of commands - %s\n",
443
 
                         upscli_strerror(&ups));
444
 
 
445
 
                /* NOTREACHED */
446
 
        }
447
 
 
448
 
        if (strlen(cmds) == 0)
449
 
                error_page("showcmds", "No instant commands supported",
450
 
                          "This UPS doesn't support any instant commands.");
451
 
 
452
 
        do_header("Instant commands");
453
 
        printf("<FORM ACTION=\"upsset.cgi\" METHOD=\"POST\">\n");
454
 
        start_table();
455
 
 
456
 
        /* include the description from checkhost() if present */
457
 
        if (desc)
458
 
                printf("<TR><TH BGCOLOR=\"#60B0B0\"COLSPAN=2>%s</TH></TR>\n",
459
 
                       desc);
460
 
 
461
 
        printf("<TR BGCOLOR=\"#60B0B0\" ALIGN=\"CENTER\">\n");
462
 
        printf("<TD>Instant commands</TD>\n");
463
 
 
464
 
        printf("<TD>\n");
465
 
        printf("<SELECT NAME=\"upscommand\">\n");
466
 
 
467
 
        /* provide a dummy do-nothing default choice */
468
 
        printf("<OPTION VALUE=\"\" SELECTED></OPTION>\n");
469
 
 
470
 
        ptr = cmds;
471
 
 
472
 
        while (ptr) {
473
 
                char    buf[SMALLBUF], *q, *desc;
474
 
 
475
 
                tmp = ptr;
476
 
                ptr = strchr(tmp, ' ');
477
 
                if (ptr) {
478
 
                        *ptr = '\0';
479
 
                        ptr++;
480
 
                }
481
 
 
482
 
                /* get a description for this command from the server */
483
 
                snprintf(buf, sizeof(buf), "INSTCMDDESC %s\n", tmp);
484
 
 
485
 
                /* if description retrieval fails, go with the cmd name */
486
 
                if (upscli_sendline(&ups, buf, strlen(buf)) < 0) {
487
 
                        printf("<OPTION VALUE=\"%s\">%s</OPTION>\n", tmp, tmp);
488
 
                        continue;
489
 
                }
490
 
 
491
 
                if (upscli_readline(&ups, buf, sizeof(buf)) < 0) {
492
 
                        printf("<OPTION VALUE=\"%s\">%s</OPTION>\n", tmp, tmp);
493
 
                        continue;
494
 
                }
495
 
 
496
 
                q = strchr(buf, '"');
497
 
 
498
 
                if (!q) {       /* invalid/missing description, use cmd name */
499
 
                        printf("<OPTION VALUE=\"%s\">%s</OPTION>\n", tmp, tmp);
500
 
                        continue;
501
 
                }
502
 
 
503
 
                desc = ++q;
504
 
                
505
 
                q = strchr(desc, '"');
506
 
                if (!q) {       /* missing second quote - don't use it */
507
 
                        printf("<OPTION VALUE=\"%s\">%s</OPTION>\n", tmp, tmp);
508
 
                        continue;
509
 
                }
510
 
 
511
 
                *q = '\0';
512
 
                printf("<OPTION VALUE=\"%s\">%s</OPTION>\n", tmp, desc);
513
 
        }
514
 
 
515
 
        printf("</SELECT>\n");
516
 
        printf("</TD></TR>\n");
517
 
 
518
 
        printf("<TR BGCOLOR=\"#60B0B0\">\n");
519
 
        printf("<TD COLSPAN=\"2\" ALIGN=\"CENTER\">\n");
520
 
        do_hidden("docmd");
521
 
        printf("<INPUT TYPE=\"HIDDEN\" NAME=\"monups\" VALUE=\"%s\">\n", monups);
522
 
        printf("<INPUT TYPE=\"SUBMIT\" VALUE=\"Issue command\">\n");
523
 
        printf("<INPUT TYPE=\"RESET\" VALUE=\"Reset\">\n");
524
 
        printf("</TD></TR>\n");
525
 
        printf("</TABLE>\n");
526
 
        printf("</FORM>\n");
527
 
 
528
 
        printf("<TR><TD ALIGN=\"CENTER\">\n");
529
 
        do_pickups("showcmds");
530
 
        printf("</TD></TR>\n");
531
 
 
532
 
        printf("</TABLE>\n");
533
 
        printf("</BODY></HTML>\n");
534
 
 
535
 
        upscli_disconnect(&ups);
536
 
        exit(0);
537
 
}
538
 
 
539
341
static void print_cmd(const char *cmd)
540
342
{
541
 
        int     ret, numq, numa;
 
343
        int     ret;
 
344
        unsigned int    numq, numa;
542
345
        char    **answer;
543
346
        const   char    *query[4];
544
347
 
557
360
        printf("<OPTION VALUE=\"%s\">%s</OPTION>\n", cmd, answer[3]);
558
361
}
559
362
 
560
 
static void new_showcmds(const char *desc)
 
363
/* generate a list of instant commands */
 
364
static void showcmds(void)
561
365
{
562
 
        int     ret, numq, numa;
 
366
        int     ret;
 
367
        unsigned int    numq, numa;
563
368
        const   char    *query[2];
564
369
        char    **answer;
565
370
        struct  list_t  *lhead, *llast, *ltmp, *lnext;
 
371
        char    *desc;
 
372
 
 
373
        if (!checkhost(monups, &desc))
 
374
                error_page("showsettings", "Access denied",
 
375
                        "Access to that host is not authorized");
 
376
 
 
377
        upsd_connect();
566
378
 
567
379
        llast = lhead = NULL;
568
380
 
573
385
        ret = upscli_list_start(&ups, numq, query);
574
386
 
575
387
        if (ret < 0) {
576
 
 
577
 
                /* old upsd: fall back */
578
 
 
579
 
                if (upscli_upserror(&ups) == UPSCLI_ERR_UNKCOMMAND)
580
 
                        old_showcmds(desc);
581
 
 
582
388
                fprintf(stderr, "LIST CMD %s failed: %s\n",
583
389
                        upsname, upscli_strerror(&ups));
584
390
 
616
422
 
617
423
        if (!lhead)
618
424
                error_page("showcmds", "No instant commands supported",
619
 
                          "This UPS doesn't support any instant commands.");
 
425
                        "This UPS doesn't support any instant commands.");
620
426
 
621
427
        do_header("Instant commands");
622
428
        printf("<FORM ACTION=\"upsset.cgi\" METHOD=\"POST\">\n");
625
431
        /* include the description from checkhost() if present */
626
432
        if (desc)
627
433
                printf("<TR><TH BGCOLOR=\"#60B0B0\"COLSPAN=2>%s</TH></TR>\n",
628
 
                       desc);
 
434
                        desc);
629
435
 
630
436
        printf("<TR BGCOLOR=\"#60B0B0\" ALIGN=\"CENTER\">\n");
631
437
        printf("<TD>Instant commands</TD>\n");
669
475
        printf("</BODY></HTML>\n");
670
476
 
671
477
        upscli_disconnect(&ups);
672
 
        exit(0);
 
478
        exit(EXIT_SUCCESS);
673
479
}       
674
480
 
675
 
/* generate a list of instant commands */
676
 
static void showcmds(void)
677
 
{
678
 
        char    *desc;
679
 
 
680
 
        if (!checkhost(monups, &desc))
681
 
                error_page("showsettings", "Access denied",
682
 
                          "Access to that host is not authorized");
683
 
 
684
 
        upsd_connect();
685
 
 
686
 
        if (!upsname)
687
 
                old_showcmds(desc);
688
 
        else
689
 
                new_showcmds(desc);
690
 
 
691
 
        /* NOTREACHED */
692
 
}
693
 
 
694
 
static void old_docmd(const char *desc, const char *cmd)
695
 
{
696
 
        char    buf[SMALLBUF];
697
 
 
698
 
        /* send command, get response, show it */
699
 
        if (upsname)
700
 
                snprintf(buf, sizeof(buf), "INSTCMD %s@%s\n", cmd, upsname);
701
 
        else
702
 
                snprintf(buf, sizeof(buf), "INSTCMD %s\n", cmd);
703
 
 
704
 
        if (upscli_sendline(&ups, buf, strlen(buf)) < 0) {
705
 
                do_header("Error while issuing command");
706
 
 
707
 
                start_table();    
708
 
 
709
 
                printf("<TR><TD>Error sending command: %s\n</TD></TR>",
710
 
                        upscli_strerror(&ups));
711
 
 
712
 
                printf("<TR><TD ALIGN=\"CENTER\" COLSPAN=2>\n");
713
 
                do_pickups("showcmds");
714
 
                printf("</TD></TR>\n");
715
 
 
716
 
                printf("</TABLE>\n");
717
 
 
718
 
                printf("</TD></TR></TABLE>\n");
719
 
                printf("</BODY></HTML>\n");
720
 
                upscli_disconnect(&ups);
721
 
                exit(0);
722
 
        }
723
 
 
724
 
        if (upscli_readline(&ups, buf, sizeof(buf)) < 0) {
725
 
                do_header("Error while reading command response");
726
 
 
727
 
                start_table();    
728
 
 
729
 
                printf("<TR><TD>Error reading command response: %s\n</TD></TR>",
730
 
                        upscli_strerror(&ups));
731
 
 
732
 
                printf("<TR><TD ALIGN=\"CENTER\" COLSPAN=2>\n");
733
 
                do_pickups("showcmds");
734
 
                printf("</TD></TR>\n");
735
 
 
736
 
                printf("</TABLE>\n");
737
 
 
738
 
                printf("</TD></TR></TABLE>\n");
739
 
                printf("</BODY></HTML>\n");
740
 
                upscli_disconnect(&ups);
741
 
                exit(0);
742
 
        }
743
 
 
744
 
        do_header("Issuing commands");
745
 
        start_table();
746
 
 
747
 
        printf("<TR><TD><PRE>\n");
748
 
        printf("Sending command: %s\n", upscommand);
749
 
        printf("Response: %s\n", buf);
750
 
        printf("</PRE></TD></TR>\n");
751
 
 
752
 
        printf("<TR><TD ALIGN=\"CENTER\" COLSPAN=2>\n");
753
 
        do_pickups("showcmds");
754
 
        printf("</TD></TR>\n");
755
 
 
756
 
        printf("</TABLE>\n");
757
 
        printf("</TD></TR></TABLE>\n");
758
 
        printf("</BODY></HTML>\n");
759
 
 
760
 
        upscli_disconnect(&ups);
761
 
        exit(0);
762
 
 
763
 
}
764
 
 
765
 
static void new_docmd(const char *desc, const char *cmd)
766
 
{
767
 
        char    buf[SMALLBUF];
768
 
 
769
 
        snprintf(buf, sizeof(buf), "INSTCMD %s %s\n", upsname, cmd);
770
 
 
771
 
        if (upscli_sendline(&ups, buf, strlen(buf)) < 0) {
772
 
                do_header("Error while issuing command");
773
 
 
774
 
                start_table();    
775
 
 
776
 
                printf("<TR><TD>Error sending command: %s\n</TD></TR>",
777
 
                        upscli_strerror(&ups));
778
 
 
779
 
                printf("<TR><TD ALIGN=\"CENTER\" COLSPAN=2>\n");
780
 
                do_pickups("showcmds");
781
 
                printf("</TD></TR>\n");
782
 
 
783
 
                printf("</TABLE>\n");
784
 
 
785
 
                printf("</TD></TR></TABLE>\n");
786
 
                printf("</BODY></HTML>\n");
787
 
                upscli_disconnect(&ups);
788
 
                exit(0);
789
 
        }
790
 
 
791
 
        if (upscli_readline(&ups, buf, sizeof(buf)) < 0) {
792
 
                do_header("Error while reading command response");
793
 
 
794
 
                start_table();    
795
 
 
796
 
                printf("<TR><TD>Error reading command response: %s\n</TD></TR>",
797
 
                        upscli_strerror(&ups));
798
 
 
799
 
                printf("<TR><TD ALIGN=\"CENTER\" COLSPAN=2>\n");
800
 
                do_pickups("showcmds");
801
 
                printf("</TD></TR>\n");
802
 
 
803
 
                printf("</TABLE>\n");
804
 
 
805
 
                printf("</TD></TR></TABLE>\n");
806
 
                printf("</BODY></HTML>\n");
807
 
                upscli_disconnect(&ups);
808
 
                exit(0);
809
 
        }
810
 
 
811
 
        do_header("Issuing command");
812
 
        start_table();
813
 
 
814
 
        printf("<TR><TD><PRE>\n");
815
 
        printf("Sending command: %s\n", upscommand);
816
 
        printf("Response: %s\n", buf);
817
 
        printf("</PRE></TD></TR>\n");
818
 
 
819
 
        printf("<TR><TD ALIGN=\"CENTER\" COLSPAN=2>\n");
820
 
        do_pickups("showcmds");
821
 
        printf("</TD></TR>\n");
822
 
 
823
 
        printf("</TABLE>\n");
824
 
        printf("</TD></TR></TABLE>\n");
825
 
        printf("</BODY></HTML>\n");
826
 
 
827
 
        upscli_disconnect(&ups);
828
 
        exit(0);
829
 
}
830
 
 
831
481
/* handle setting authentication data in the server */
832
482
static void send_auth(const char *next)
833
483
{
848
498
                /* test for old upsd that doesn't do USERNAME */
849
499
                if (upscli_upserror(&ups) == UPSCLI_ERR_UNKCOMMAND) {
850
500
                        error_page(next, "Protocol mismatch",
851
 
                                  "upsd version too old - USERNAME not supported");
 
501
                                "upsd version too old - USERNAME not supported");
852
502
                }
853
503
 
854
504
                error_page(next, "Can't set user name", 
866
516
                        "Password set failed: %s", upscli_strerror(&ups));
867
517
}       
868
518
 
869
 
/* take a command from the client and send it to upsd */
870
519
static void docmd(void)
871
520
{
872
 
        char    *desc;
 
521
        char    buf[SMALLBUF], *desc;
873
522
 
874
523
        if (!checkhost(monups, &desc))
875
524
                error_page("showsettings", "Access denied",
876
 
                          "Access to that host is not authorized");
 
525
                        "Access to that host is not authorized");
877
526
 
878
527
        /* the user is messing with us */
879
528
        if (!upscommand)        
889
538
 
890
539
        send_auth("showcmds");
891
540
 
892
 
        if (!upsname)
893
 
                old_docmd(desc, upscommand);
894
 
 
895
 
        if (!strchr(upscommand, '.'))
896
 
                old_docmd(desc, upscommand);
897
 
 
898
 
        new_docmd(desc, upscommand);
899
 
 
900
 
        /* NOTREACHED */
901
 
}
902
 
 
903
 
/* dump a table of all read/write variables and their current values */
904
 
static void old_showsettings(const char *desc)
905
 
{
906
 
        char    vars[SMALLBUF], temp[SMALLBUF], out[SMALLBUF], *v, 
907
 
                *type, *ptr, *vptr;
908
 
        int     typelen;
909
 
 
910
 
        if (upscli_getlist(&ups, upsname, UPSCLI_LIST_RW, vars, 
911
 
                sizeof(vars)) < 0) {
912
 
 
913
 
                error_page("showsettings", "Server protocol error",
914
 
                         "Unable to get variable list - %s\n",
915
 
                         upscli_strerror(&ups));
916
 
 
917
 
                /* NOTREACHED */
918
 
        }
919
 
 
920
 
        if (strlen(vars) == 0) {
921
 
                error_page("showsettings", "No read/write variables", 
922
 
                          "This UPS has no read/write variables.");
923
 
                /* NOTREACHED */
924
 
        }
925
 
 
926
 
        /* no spaces = seriously broken string */
927
 
        if (!strchr(vars, ' ')) {               
928
 
                error_page("showsettings", "Server protocol error", 
929
 
                          "Unable to get variable list - broken string");
930
 
                /* NOTREACHED */
931
 
        }
932
 
 
933
 
        do_header("Current settings");
934
 
        printf("<FORM ACTION=\"upsset.cgi\" METHOD=\"POST\">\n");
 
541
        snprintf(buf, sizeof(buf), "INSTCMD %s %s\n", upsname, upscommand);
 
542
 
 
543
        if (upscli_sendline(&ups, buf, strlen(buf)) < 0) {
 
544
                do_header("Error while issuing command");
 
545
 
 
546
                start_table();
 
547
 
 
548
                printf("<TR><TD>Error sending command: %s\n</TD></TR>",
 
549
                        upscli_strerror(&ups));
 
550
 
 
551
                printf("<TR><TD ALIGN=\"CENTER\" COLSPAN=2>\n");
 
552
                do_pickups("showcmds");
 
553
                printf("</TD></TR>\n");
 
554
 
 
555
                printf("</TABLE>\n");
 
556
 
 
557
                printf("</TD></TR></TABLE>\n");
 
558
                printf("</BODY></HTML>\n");
 
559
                upscli_disconnect(&ups);
 
560
                exit(EXIT_SUCCESS);
 
561
        }
 
562
 
 
563
        if (upscli_readline(&ups, buf, sizeof(buf)) < 0) {
 
564
                do_header("Error while reading command response");
 
565
 
 
566
                start_table();
 
567
 
 
568
                printf("<TR><TD>Error reading command response: %s\n</TD></TR>",
 
569
                        upscli_strerror(&ups));
 
570
 
 
571
                printf("<TR><TD ALIGN=\"CENTER\" COLSPAN=2>\n");
 
572
                do_pickups("showcmds");
 
573
                printf("</TD></TR>\n");
 
574
 
 
575
                printf("</TABLE>\n");
 
576
 
 
577
                printf("</TD></TR></TABLE>\n");
 
578
                printf("</BODY></HTML>\n");
 
579
                upscli_disconnect(&ups);
 
580
                exit(EXIT_SUCCESS);
 
581
        }
 
582
 
 
583
        do_header("Issuing command");
935
584
        start_table();
936
585
 
937
 
        /* include the description from checkhost() if present */
938
 
        if (desc)
939
 
                printf("<TR><TH BGCOLOR=\"#60B0B0\"COLSPAN=2>%s</TH></TR>\n",
940
 
                        desc);
941
 
 
942
 
        printf("<TR BGCOLOR=\"#60B0B0\">\n");
943
 
        printf("<TH>Setting</TH>\n");
944
 
        printf("<TH>Value</TH></TR>\n");
945
 
 
946
 
        v = vars;
947
 
 
948
 
        while (v != NULL) {
949
 
                vptr = strchr(v, ' ');
950
 
                if (vptr)
951
 
                        *vptr++ = '\0';
952
 
 
953
 
                /* get description */
954
 
                snprintf(out, sizeof(out), "VARDESC %s\n", v);
955
 
                if (upscli_sendline(&ups, out, strlen(out)) < 0) {
956
 
                        printf("Can't get VARDESC\n");
957
 
 
958
 
                        fprintf(stderr, "Can't get VARDESC of %s: %s\n",
959
 
                                v, upscli_strerror(&ups));
960
 
                        return;
961
 
                }
962
 
 
963
 
                if (upscli_readline(&ups, temp, sizeof(temp)) < 0) {
964
 
                        printf("Can't get VARDESC\n");
965
 
 
966
 
                        fprintf(stderr, "Can't get VARDESC of %s: %s\n",
967
 
                                v, upscli_strerror(&ups));
968
 
                        return;
969
 
                }
970
 
 
971
 
                printf("<TR BGCOLOR=\"#60B0B0\" ALIGN=\"CENTER\">\n");
972
 
 
973
 
                /* strip off leading/trailing quotes */
974
 
                ptr = strchr(temp, '"');
975
 
                if (ptr != NULL) {
976
 
                        char    *ptr2;
977
 
 
978
 
                        ptr++;
979
 
                        ptr2 = strchr(ptr, '"');
980
 
 
981
 
                        if (ptr2)
982
 
                                *ptr2 = '\0';
983
 
                        else {
984
 
                                printf("Unbalanced quotes from server\n");
985
 
                                exit(0);
986
 
                        }
987
 
                }
988
 
 
989
 
                printf("<TD>%s</TD>\n", ptr);
990
 
 
991
 
                /* now get variable type */
992
 
                snprintf(out, sizeof(out),
993
 
                        (upsname ? "VARTYPE %s@%s\n" : "VARTYPE %s\n"), v, upsname);
994
 
 
995
 
                if (upscli_sendline(&ups, out, strlen(out)) < 0) {
996
 
                        printf("Can't get VARTYPE\n");
997
 
 
998
 
                        fprintf(stderr, "Can't get VARTYPE %s: %s\n",
999
 
                                v, upscli_strerror(&ups));
1000
 
                        return;
1001
 
                }
1002
 
 
1003
 
                if (upscli_readline(&ups, temp, sizeof(temp)) < 0) {
1004
 
                        printf("Can't get VARTYPE\n");
1005
 
 
1006
 
                        fprintf(stderr, "Can't get VARTYPE %s: %s\n",
1007
 
                                v, upscli_strerror(&ups));
1008
 
                        return;
1009
 
                }
1010
 
 
1011
 
                type = NULL;
1012
 
                typelen = 0;
1013
 
 
1014
 
                ptr = strchr(temp, ' ');
1015
 
                if (ptr) {
1016
 
                        *ptr++ = '\0';
1017
 
                        type = ptr;
1018
 
                        ptr = strchr(type, ' ');
1019
 
                        if (ptr) {
1020
 
                                *ptr++ = '\0';
1021
 
                                typelen = strtol(ptr, (char **) NULL, 10);
1022
 
                        }
1023
 
                }
1024
 
 
1025
 
                if (type) {
1026
 
                        printf("<TD>\n");
1027
 
 
1028
 
                        if (!strcmp(type, "ENUM"))
1029
 
                                do_enum(v);
1030
 
 
1031
 
                        if (!strcmp(type, "STRING"))
1032
 
                                do_string(v, typelen);
1033
 
                }
1034
 
                else
1035
 
                        printf("Broken type received from server\n");
1036
 
 
1037
 
                printf("</TD></TR>\n");
1038
 
 
1039
 
                v = vptr;               
1040
 
        }
1041
 
 
1042
 
        printf("<TR BGCOLOR=\"#60B0B0\">\n");
1043
 
        printf("<TD COLSPAN=\"2\" ALIGN=\"CENTER\">\n");
1044
 
        do_hidden("savesettings");
1045
 
        printf("<INPUT TYPE=\"HIDDEN\" NAME=\"monups\" VALUE=\"%s\">\n", monups);
1046
 
        printf("<INPUT TYPE=\"SUBMIT\" VALUE=\"Save changes\">\n");
1047
 
        printf("<INPUT TYPE=\"RESET\" VALUE=\"Reset\">\n");
1048
 
        printf("</TD></TR>\n");
1049
 
        printf("</TABLE>\n");
1050
 
        printf("</FORM>\n");
1051
 
 
1052
 
        printf("<TR><TD ALIGN=\"CENTER\">\n");
1053
 
        do_pickups("showsettings");
1054
 
        printf("</TD></TR>\n");
1055
 
 
1056
 
        printf("</TABLE>\n");
 
586
        printf("<TR><TD><PRE>\n");
 
587
        printf("Sending command: %s\n", upscommand);
 
588
        printf("Response: %s\n", buf);
 
589
        printf("</PRE></TD></TR>\n");
 
590
 
 
591
        printf("<TR><TD ALIGN=\"CENTER\" COLSPAN=2>\n");
 
592
        do_pickups("showcmds");
 
593
        printf("</TD></TR>\n");
 
594
 
 
595
        printf("</TABLE>\n");
 
596
        printf("</TD></TR></TABLE>\n");
1057
597
        printf("</BODY></HTML>\n");
1058
598
 
1059
599
        upscli_disconnect(&ups);
1060
 
        exit(0);
 
600
        exit(EXIT_SUCCESS);
1061
601
}
1062
602
 
1063
603
static const char *get_data(const char *type, const char *varname)
1064
604
{
1065
 
        int     ret, numq, numa;
 
605
        int     ret;
 
606
        unsigned int    numq, numa;
1066
607
        char    **answer;
1067
608
        const   char    *query[4];
1068
609
 
1080
621
        return answer[3];
1081
622
}
1082
623
 
1083
 
static void new_do_string(const char *varname, int maxlen)
 
624
static void do_string(const char *varname, int maxlen)
1084
625
{
1085
626
        const   char    *val;
1086
627
 
1088
629
 
1089
630
        if (!val) {
1090
631
                printf("Unavailable\n");
1091
 
                fprintf(stderr, "new_do_string: can't get current value of %s\n",
 
632
                fprintf(stderr, "do_string: can't get current value of %s\n",
1092
633
                        varname);
1093
634
                return;
1094
635
        }
1097
638
                "SIZE=\"%d\">\n", varname, val, maxlen);
1098
639
}
1099
640
 
1100
 
static void new_do_enum(const char *varname)
 
641
static void do_enum(const char *varname)
1101
642
{
1102
 
        int     ret, numq, numa;
 
643
        int     ret;
 
644
        unsigned int    numq, numa;
1103
645
        char    **answer, *val;
1104
646
        const   char    *query[4], *tmp;
1105
647
 
1162
704
 
1163
705
static void do_type(const char *varname)
1164
706
{
1165
 
        int     i, ret, numq, numa;
 
707
        int     ret;
 
708
        unsigned int    i, numq, numa;
1166
709
        char    **answer;
1167
710
        const   char    *query[4];
1168
711
 
1182
725
        for (i = 3; i < numa; i++) {
1183
726
 
1184
727
                if (!strcasecmp(answer[i], "ENUM")) {
1185
 
                        new_do_enum(varname);
 
728
                        do_enum(varname);
1186
729
                        return;
1187
730
                }
1188
731
 
1194
737
                        *ptr++ = '\0';
1195
738
                        len = strtol(ptr, (char **) NULL, 10);
1196
739
 
1197
 
                        new_do_string(varname, len);
 
740
                        do_string(varname, len);
1198
741
                        return;
1199
742
                }
1200
743
 
1232
775
 
1233
776
static void showsettings(void)
1234
777
{
1235
 
        int     ret, numq, numa;
 
778
        int     ret;
 
779
        unsigned int    numq, numa;
1236
780
        const   char    *query[2];
1237
781
        char    **answer, *desc = NULL;
1238
782
        struct  list_t  *lhead, *llast, *ltmp, *lnext;
1239
783
 
1240
784
        if (!checkhost(monups, &desc))
1241
785
                error_page("showsettings", "Access denied",
1242
 
                          "Access to that host is not authorized");
 
786
                        "Access to that host is not authorized");
1243
787
 
1244
788
        upsd_connect();
1245
789
 
1246
 
        /* old default upsname scheme = use old protocol commands */
1247
 
        if ((!upsname) || (!strcmp(upsname, ""))) {
1248
 
                old_showsettings(desc);
1249
 
                return;
1250
 
        }
1251
 
 
1252
790
        query[0] = "RW";
1253
791
        query[1] = upsname;
1254
792
        numq = 2;
1256
794
        ret = upscli_list_start(&ups, numq, query);
1257
795
 
1258
796
        if (ret < 0) {
1259
 
 
1260
 
                /* old upsd --> fall back on old LISTRW technique */
1261
 
                if (upscli_upserror(&ups) == UPSCLI_ERR_UNKCOMMAND) {
1262
 
                        old_showsettings(desc);
1263
 
                        return;
1264
 
                }
1265
 
 
1266
797
                fprintf(stderr, "LIST RW %s failed: %s\n",
1267
798
                        upsname, upscli_strerror(&ups));
1268
799
 
1299
830
        start_table();
1300
831
 
1301
832
        /* include the description from checkhost() if present */
1302
 
        if (desc)
 
833
        if (desc)
1303
834
                printf("<TR><TH BGCOLOR=\"#60B0B0\"COLSPAN=2>%s</TH></TR>\n",
1304
835
                        desc);
1305
 
        
 
836
 
1306
837
        printf("<TR BGCOLOR=\"#60B0B0\">\n");
1307
838
        printf("<TH>Setting</TH>\n");
1308
839
        printf("<TH>Value</TH></TR>\n");
1339
870
        printf("</BODY></HTML>\n");
1340
871
 
1341
872
        upscli_disconnect(&ups);
1342
 
        exit(0);
1343
 
}
1344
 
 
1345
 
static void old_setvar(const char *var, const char *val)
1346
 
{
1347
 
        char    buf[SMALLBUF];
1348
 
 
1349
 
        upscli_getvar(&ups, upsname, var, buf, sizeof(buf));
1350
 
 
1351
 
        /* don't send a SET if it hasn't chnaged */
1352
 
        if (strcmp(val, buf))
1353
 
                return;
1354
 
 
1355
 
        printf("set %s to %s (was %s)\n", var, val, buf);
1356
 
 
1357
 
        if (upsname)
1358
 
                snprintf(buf, sizeof(buf), "SET %s@%s %s\n", var, upsname, val);
1359
 
        else
1360
 
                snprintf(buf, sizeof(buf), "SET %s %s\n", var, val);
1361
 
 
1362
 
        if (upscli_sendline(&ups, buf, strlen(buf)) < 0) {
1363
 
                printf("Error: SET failed: %s\n", upscli_strerror(&ups));
1364
 
                return;
1365
 
        }
1366
 
 
1367
 
        if (upscli_readline(&ups, buf, sizeof(buf)) < 0) {
1368
 
                printf("Error: SET failed: %s\n", upscli_strerror(&ups));
1369
 
                return;
1370
 
        }
1371
 
 
1372
 
        printf("response: %s\n", buf);
1373
 
}
1374
 
 
1375
 
static void new_setvar(const char *var, const char *val)
 
873
        exit(EXIT_SUCCESS);
 
874
}
 
875
 
 
876
static int setvar(const char *var, const char *val)
1376
877
{
1377
878
        char    buf[SMALLBUF], enc[SMALLBUF];
1378
879
        const   char    *tmp;
1382
883
 
1383
884
        if (!tmp) {
1384
885
                printf("Can't get old value for %s, aborting SET\n", var);
1385
 
                return;
 
886
                return 0;
1386
887
        }
1387
888
 
1388
889
        /* don't send a SET if it hasn't chnaged */
1389
 
        if (!strcmp(tmp, var))
1390
 
                return;
 
890
        if (!strcmp(tmp, val))
 
891
                return 0;
1391
892
 
1392
893
        printf("set %s to %s (was %s)\n", var, val, tmp);
1393
894
 
1396
897
 
1397
898
        if (upscli_sendline(&ups, buf, strlen(buf)) < 0) {
1398
899
                printf("Error: SET failed: %s\n", upscli_strerror(&ups));
1399
 
                return;
 
900
                return 0;
1400
901
        }
1401
902
 
1402
903
        if (upscli_readline(&ups, buf, sizeof(buf)) < 0) {
1403
904
                printf("Error: SET failed: %s\n", upscli_strerror(&ups));
1404
 
                return;
 
905
                return 0;
1405
906
        }
1406
907
 
1407
908
        if (strncmp(buf, "OK", 2) != 0) {
1408
909
                printf("Unexpected response: %s\n", buf);
1409
 
                return;
 
910
                return 0;
1410
911
        }
1411
912
 
1412
913
        printf("OK\n");
 
914
        return 1;
1413
915
}
1414
916
 
1415
917
/* turn a form submission of settings into SET commands for upsd */
1416
918
static void savesettings(void)
1417
919
{
 
920
        int     changed = 0;
1418
921
        char    *desc;
1419
922
        uvtype  *upsvar;
1420
923
 
1421
924
        if (!checkhost(monups, &desc)) 
1422
925
                error_page("showsettings", "Access denied",
1423
 
                          "Access to that host is not authorized");
 
926
                        "Access to that host is not authorized");
1424
927
 
1425
928
        upsd_connect();
1426
929
 
1434
937
        printf("<TR><TD><PRE>\n");
1435
938
 
1436
939
        while (upsvar) {
1437
 
 
1438
 
                /* fallback on old methods */
1439
 
                if (!upsname) {
1440
 
                        old_setvar(upsvar->var, upsvar->value);
1441
 
                        upsvar = upsvar->next;
1442
 
                        continue;
1443
 
                }
1444
 
 
1445
 
                /* ditto for old variable names */
1446
 
                if (!strchr(upsvar->var, '.')) {
1447
 
                        old_setvar(upsvar->var, upsvar->value);
1448
 
                        upsvar = upsvar->next;
1449
 
                        continue;
1450
 
                }
1451
 
 
1452
 
                new_setvar(upsvar->var, upsvar->value);
 
940
                changed += setvar(upsvar->var, upsvar->value);
1453
941
                upsvar = upsvar->next;
1454
942
        }
1455
943
 
 
944
        if (changed == 0)
 
945
                printf("No settings changed.\n");
 
946
        else
 
947
                printf("Updated %d setting%s.\n",
 
948
                        changed, changed == 1 ? "" : "s");
 
949
 
1456
950
        printf("</PRE></TD></TR>\n");
1457
951
 
1458
952
        printf("<TR><TD ALIGN=\"CENTER\" COLSPAN=\"2\">\n");
1464
958
        printf("</BODY></HTML>\n");
1465
959
 
1466
960
        upscli_disconnect(&ups);
1467
 
        exit(0);
 
961
        exit(EXIT_SUCCESS);
1468
962
}
1469
963
 
1470
964
static void initial_pickups(void)
1481
975
        printf("</BODY></HTML>\n");
1482
976
 
1483
977
        upscli_disconnect(&ups);
1484
 
        exit(0);
 
978
        exit(EXIT_SUCCESS);
1485
979
}
1486
980
 
1487
 
void upsset_conf_err(const char *errmsg)
 
981
static void upsset_conf_err(const char *errmsg)
1488
982
{
1489
983
        upslogx(LOG_ERR, "Fatal error in parseconf(upsset.conf): %s", errmsg);
1490
984
}
1509
1003
 
1510
1004
                /* leave something in the httpd log for the admin */
1511
1005
                fprintf(stderr, "upsset.conf does not exist to permit execution\n");
1512
 
                exit(0);
 
1006
                exit(EXIT_FAILURE);
1513
1007
        }
1514
1008
 
1515
1009
        while (pconf_file_next(&ctx)) {
1540
1034
        /* leave something in the httpd log for the admin */
1541
1035
        fprintf(stderr, "upsset.conf does not permit execution\n");
1542
1036
 
1543
 
        exit(0);
 
1037
        exit(EXIT_FAILURE);
1544
1038
}       
1545
1039
 
1546
1040
int main(int argc, char **argv)