]> andersk Git - moira.git/blob - update/update_test.c
e3fefcb5b6bdd763c83a3ac7daebae0bb50c279b
[moira.git] / update / update_test.c
1 /* $Header$
2  *
3  * Test client for update_server protocol.
4  *
5  * Reads commands from the command line:
6  * test host [commands...]
7  *      -s file file    sends file to host
8  *      -S file file    sends encrypted file to host
9  *      -i file         sends instruction file to host
10  *      -x file         executes instructions
11  *      -n              nop
12  */
13
14 #include <mit-copyright.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <gdb.h>
18 #include <sys/param.h>
19 #include <sys/wait.h>
20 #include <sys/socket.h>
21 #include <update.h>
22 #include <errno.h>
23 #include <moira.h>
24 #include <moira_site.h>
25 #include <krb.h>
26
27 CONNECTION conn;
28 char *whoami;
29
30 int main(int argc, char **argv)
31 {
32   char *host, service_address[256], *file, *rfile, buf[256];
33   int code, i, count = 0;
34
35   whoami = argv[0];
36   initialize_sms_error_table();
37   initialize_krb_error_table();
38   gdb_init();
39
40   if (argc < 2)
41     usage();
42   host = argv[1];
43
44   sprintf(service_address, "%s:%s", host, SERVICE_NAME);
45   conn = start_server_connection(service_address, "");
46   if (!conn || (connection_status(conn) == CON_STOPPED))
47     {
48       com_err(whoami, connection_errno(conn),
49               " can't connect to update %s", service_address);
50       return MR_CANT_CONNECT;
51     }
52   code = send_auth(host);
53   if (code)
54     com_err(whoami, code, " authorization attempt failed");
55
56   for (i = 2; i < argc; i++)
57     {
58       if (argv[i][0] != '-')
59         usage();
60       switch (argv[i][1])
61         {
62         case 's':
63           if (i + 2 >= argc)
64             usage();
65           file = argv[++i];
66           rfile = argv[++i];
67           fprintf(stderr, "Sending file %s to %s as %s\n", file, host, rfile);
68           send_file(file, rfile, 0);
69           break;
70         case 'S':
71           if (i + 2 >= argc)
72             usage();
73           file = argv[++i];
74           rfile = argv[++i];
75           fprintf(stderr, "Sending (encrypted) file %s to %s as %s\n",
76                   file, host, rfile);
77           send_file(file, rfile, 1);
78           break;
79         case 'i':
80           if (i + 1 >= argc)
81             usage();
82           file = argv[++i];
83           strcpy(buf, "/tmp/moira-updateXXXXX");
84           mktemp(buf);
85           fprintf(stderr, "Sending instructions %s to %s as %s\n",
86                   file, host, buf);
87           send_file(file, buf, 0);
88           break;
89         case 'I':
90           if (i + 2 >= argc)
91             usage();
92           file = argv[++i];
93           rfile = argv[++i];
94           strcpy(buf, rfile);
95           fprintf(stderr, "Sending instructions %s to %s as %s\n",
96                   file, host, buf);
97           send_file(file, buf, 0);
98           break;
99         case 'x':
100           fprintf(stderr, "Executing instructions %s on %s\n", buf, host);
101           code = execute(buf);
102           if (code)
103             com_err(whoami, code, "executing");
104           break;
105         case 'X':
106           if (i + 1 >= argc)
107             usage();
108           file = argv[++i];
109           fprintf(stderr, "Executing instructions %s on %s\n", file, host);
110           code = execute(file);
111           if (code)
112             com_err(whoami, code, "executing");
113           break;
114         case 'n':
115           break;
116         default:
117           usage();
118         }
119     }
120   send_quit();
121   conn = sever_connection(conn);
122   exit(code);
123 }
124
125 usage(void)
126 {
127   fprintf(stderr, "Usage: test host [commands...]\n");
128   fprintf(stderr, "  Commands are:\n");
129   fprintf(stderr, "\t-s srcfile dstfile\tsends file\n");
130   fprintf(stderr, "\t-S srcfile dstfile\tsends encrypted file\n");
131   fprintf(stderr, "\t-i srcfile\t\tsends instructions\n");
132   fprintf(stderr, "\t-I srcfile dstfile\tsends instructions\n");
133   fprintf(stderr, "\t-x\t\texecutes last instructions\n");
134   fprintf(stderr, "\t-X file\t\texecutes file\n");
135   exit(1);
136 }
This page took 0.032684 seconds and 3 git commands to generate.