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