]>
Commit | Line | Data |
---|---|---|
d8e20246 | 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 | |
ca63c94e | 8 | * -S file file sends encrypted file to host |
d8e20246 | 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 <strings.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 <dcm.h> | |
24 | #include <moira.h> | |
25 | #include <moira_site.h> | |
26 | #include <krb.h> | |
27 | ||
28 | CONNECTION conn; | |
29 | char *whoami; | |
30 | ||
31 | ||
32 | main(argc, argv) | |
33 | int argc; | |
34 | char **argv; | |
35 | { | |
36 | char *host, service_address[256], *file, *rfile, buf[256]; | |
37 | int code, i, count=0; | |
38 | ||
39 | whoami = argv[0]; | |
40 | initialize_sms_error_table(); | |
41 | initialize_krb_error_table(); | |
42 | gdb_init(); | |
43 | ||
44 | if (argc < 2) usage(); | |
45 | host = argv[1]; | |
46 | ||
47 | sprintf(service_address, "%s:%s", host, SERVICE_NAME); | |
48 | conn = start_server_connection(service_address, ""); | |
49 | if (!conn || (connection_status(conn) == CON_STOPPED)) { | |
50 | com_err(whoami, connection_errno(conn), | |
51 | " can't connect to update %s", service_address); | |
52 | return(MR_CANT_CONNECT); | |
53 | } | |
54 | code = send_auth(host); | |
55 | if (code) { | |
56 | com_err(whoami, code, " authorization attempt failed"); | |
57 | } | |
58 | ||
59 | for (i = 2; i < argc; i++) { | |
60 | if (argv[i][0] != '-') usage(); | |
61 | switch (argv[i][1]) { | |
62 | case 's': | |
63 | if (i+2 >= argc) usage(); | |
64 | file = argv[++i]; | |
65 | rfile = argv[++i]; | |
66 | fprintf(stderr, "Sending file %s to %s as %s\n", file, host, rfile); | |
ca63c94e | 67 | send_file(file, rfile, 0); |
68 | break; | |
69 | case 'S': | |
70 | if (i+2 >= argc) usage(); | |
71 | file = argv[++i]; | |
72 | rfile = argv[++i]; | |
73 | fprintf(stderr, "Sending (encrypted) file %s to %s as %s\n", file, host, rfile); | |
74 | send_file(file, rfile, 1); | |
d8e20246 | 75 | break; |
76 | case 'i': | |
77 | if (i+1 >= argc) usage(); | |
78 | file = argv[++i]; | |
79 | strcpy(buf, "/tmp/moira-updateXXXXX"); | |
80 | mktemp(buf); | |
81 | fprintf(stderr, "Sending instructions %s to %s as %s\n", | |
82 | file, host, buf); | |
ca63c94e | 83 | send_file(file, buf, 0); |
d8e20246 | 84 | break; |
85 | case 'I': | |
86 | if (i+2 >= argc) usage(); | |
87 | file = argv[++i]; | |
88 | rfile = argv[++i]; | |
89 | strcpy(buf, rfile); | |
90 | fprintf(stderr, "Sending instructions %s to %s as %s\n", | |
91 | file, host, buf); | |
ca63c94e | 92 | send_file(file, buf, 0); |
d8e20246 | 93 | break; |
94 | case 'x': | |
95 | fprintf(stderr, "Executing instructions %s on %s\n", buf, host); | |
96 | code = execute(buf); | |
97 | if (code) com_err(whoami, code, "executing"); | |
98 | break; | |
99 | case 'X': | |
100 | if (i+1 >= argc) usage(); | |
101 | file = argv[++i]; | |
102 | fprintf(stderr, "Executing instructions %s on %s\n", file, host); | |
103 | code = execute(file); | |
104 | if (code) com_err(whoami, code, "executing"); | |
105 | break; | |
106 | case 'n': | |
107 | break; | |
108 | default: | |
109 | usage(); | |
110 | } | |
111 | } | |
112 | send_quit(); | |
113 | conn = sever_connection(conn); | |
114 | exit(code); | |
115 | } | |
116 | ||
117 | usage() | |
118 | { | |
119 | fprintf(stderr, "Usage: test host [commands...]\n"); | |
120 | fprintf(stderr, " Commands are:\n"); | |
121 | fprintf(stderr, "\t-s srcfile dstfile\tsends file\n"); | |
ca63c94e | 122 | fprintf(stderr, "\t-S srcfile dstfile\tsends encrypted file\n"); |
d8e20246 | 123 | fprintf(stderr, "\t-i srcfile\t\tsends instructions\n"); |
124 | fprintf(stderr, "\t-I srcfile dstfile\tsends instructions\n"); | |
125 | fprintf(stderr, "\t-x\t\texecutes last instructions\n"); | |
126 | fprintf(stderr, "\t-X file\t\texecutes file\n"); | |
127 | exit(1); | |
128 | } | |
129 | ||
130 | static | |
131 | send_auth(host_name) | |
132 | char *host_name; | |
133 | { | |
134 | KTEXT_ST ticket_st; | |
135 | KTEXT ticket = &ticket_st; | |
136 | STRING data; | |
137 | register int code; | |
138 | int response; | |
139 | ||
140 | code = get_mr_update_ticket(host_name, ticket); | |
141 | if (code) { | |
142 | return(code); | |
143 | } | |
144 | STRING_DATA(data) = "AUTH_001"; | |
145 | MAX_STRING_SIZE(data) = 9; | |
146 | code = send_object(conn, (char *)&data, STRING_T); | |
147 | if (code) { | |
148 | return(connection_errno(conn)); | |
149 | } | |
150 | code = receive_object(conn, (char *)&response, INTEGER_T); | |
151 | if (code) { | |
152 | return(connection_errno(conn)); | |
153 | } | |
154 | if (response) { | |
155 | return(response); | |
156 | } | |
157 | STRING_DATA(data) = (char *)ticket->dat; | |
158 | MAX_STRING_SIZE(data) = ticket->length; | |
159 | code = send_object(conn, (char *)&data, STRING_T); | |
160 | if (code) { | |
161 | return(connection_errno(conn)); | |
162 | } | |
163 | code = receive_object(conn, (char *)&response, INTEGER_T); | |
164 | if (code) { | |
165 | return(connection_errno(conn)); | |
166 | } | |
167 | if (response) { | |
168 | com_err(whoami, response, "Permission to connect denied"); | |
169 | return(response); | |
170 | } | |
171 | return(MR_SUCCESS); | |
172 | } | |
173 | ||
174 | static | |
175 | execute(path) | |
176 | char *path; | |
177 | { | |
178 | int response; | |
179 | STRING data; | |
180 | register int code; | |
181 | ||
182 | string_alloc(&data, BUFSIZ); | |
183 | sprintf(STRING_DATA(data), "EXEC_002 %s", path); | |
184 | code = send_object(conn, (char *)&data, STRING_T); | |
185 | if (code) | |
186 | return(connection_errno(conn)); | |
187 | code = receive_object(conn, (char *)&response, INTEGER_T); | |
188 | if (code) | |
189 | return(connection_errno(conn)); | |
190 | if (response) | |
191 | return(response); | |
192 | return(MR_SUCCESS); | |
193 | } | |
194 | ||
195 | send_quit() | |
196 | { | |
197 | STRING str; | |
198 | if (!conn) | |
199 | return; | |
200 | string_alloc(&str, 5); | |
201 | (void) strcpy(STRING_DATA(str), "quit"); | |
202 | (void) send_object(conn, (char *)&str, STRING_T); | |
203 | string_free(&str); | |
204 | } |