]>
Commit | Line | Data |
---|---|---|
6e6374cb | 1 | /* |
2 | * $Source$ | |
3 | * $Author$ | |
4 | * $Header$ | |
5 | * | |
6 | * Copyright (C) 1987 by the Massachusetts Institute of Technology | |
c801de4c | 7 | * For copying and distribution information, please see the file |
8 | * <mit-copyright.h>. | |
6e6374cb | 9 | * |
6e6374cb | 10 | */ |
11 | ||
12 | #ifndef lint | |
13 | static char *rcsid_test_c = "$Header$"; | |
14 | #endif lint | |
15 | ||
c801de4c | 16 | #include <mit-copyright.h> |
6e6374cb | 17 | #include <stdio.h> |
39de499f | 18 | #include <sys/types.h> |
58b825a4 | 19 | #include <sys/file.h> |
a4fafd85 | 20 | #include <fcntl.h> |
58b825a4 | 21 | #include <ctype.h> |
f071d8a7 | 22 | #include <string.h> |
8defc06b | 23 | #include <moira.h> |
1a5c774c | 24 | #include <com_err.h> |
6e6374cb | 25 | |
58b825a4 | 26 | int recursion = 0; |
1a5c774c | 27 | extern int errno; |
59af7b90 | 28 | extern int sending_version_no; |
1a5c774c | 29 | int count, quit=0; |
30 | char *whoami; | |
31 | ||
32 | #define MAXARGS 20 | |
6e6374cb | 33 | |
6e6374cb | 34 | main(argc, argv) |
35 | int argc; | |
36 | char **argv; | |
6e6374cb | 37 | { |
38 | int status; | |
1a5c774c | 39 | char cmdbuf[BUFSIZ]; |
6e6374cb | 40 | |
6e6374cb | 41 | whoami = argv[0]; |
6e6374cb | 42 | |
365b5cce | 43 | initialize_sms_error_table(); |
44 | initialize_krb_error_table(); | |
6e6374cb | 45 | |
1a5c774c | 46 | while(!quit) { |
47 | printf("moira: "); | |
48 | fflush(stdout); | |
49 | if(!fgets(cmdbuf,BUFSIZ,stdin)) break; | |
50 | execute_line(cmdbuf); | |
6e6374cb | 51 | } |
1a5c774c | 52 | mr_disconnect(); |
53 | exit(0); | |
54 | } | |
55 | ||
56 | execute_line(cmdbuf) | |
57 | char *cmdbuf; | |
58 | { | |
59 | int argc; | |
60 | char *argv[MAXARGS]; | |
61 | ||
62 | argc=parse(cmdbuf, argv); | |
63 | if(argc==0) return; | |
64 | if(!strcmp(argv[0],"noop")) | |
65 | test_noop(); | |
66 | else if(!strcmp(argv[0],"connect") || !strcmp(argv[0],"c")) | |
67 | test_connect(argc, argv); | |
68 | else if(!strcmp(argv[0],"disconnect") || !strcmp(argv[0],"d")) | |
69 | test_disconnect(); | |
70 | else if(!strcmp(argv[0],"host")) | |
71 | test_host(); | |
72 | else if(!strcmp(argv[0],"new") || !strcmp(argv[0],"2")) | |
73 | test_new(); | |
74 | else if(!strcmp(argv[0],"old") || !strcmp(argv[0],"1")) | |
75 | test_old(); | |
76 | else if(!strcmp(argv[0],"motd")) | |
77 | test_motd(); | |
78 | else if(!strcmp(argv[0],"query") || !strcmp(argv[0],"qy")) | |
79 | test_query(argc, argv); | |
80 | else if(!strcmp(argv[0],"auth") || !strcmp(argv[0],"a")) | |
81 | test_auth(argc, argv); | |
82 | else if(!strcmp(argv[0],"access")) | |
83 | test_access(argc, argv); | |
84 | else if(!strcmp(argv[0],"dcm")) | |
85 | test_dcm(); | |
86 | else if(!strcmp(argv[0],"script") || !strcmp(argv[0],"s")) | |
87 | test_script(argc, argv); | |
88 | else if(!strcmp(argv[0],"list_requests") || | |
89 | !strcmp(argv[0],"lr") || !strcmp(argv[0],"?")) | |
90 | test_list_requests(); | |
91 | else if(!strcmp(argv[0],"quit") || !strcmp(argv[0],"Q")) | |
92 | quit=1; | |
125f757c | 93 | else fprintf(stderr, "moira: Unknown request \"%s\". Type \"?\" for a request list.\n", argv[0]); |
1a5c774c | 94 | } |
95 | ||
96 | int | |
97 | parse(buf, argv) | |
98 | char *buf, *argv[MAXARGS]; | |
99 | { | |
100 | char *p; | |
101 | int argc, num; | |
102 | ||
103 | for(p=buf, argc=0, argv[0]=buf; *p && *p!='\n'; p++) { | |
104 | if(*p=='"') { | |
105 | char *d=p++; | |
106 | /* skip to close-quote, copying back over open-quote */ | |
107 | while(*p!='"') { | |
108 | if(!*p || *p=='\n') { | |
109 | fprintf(stderr, "moira: Unbalanced quotes in command line\n"); | |
110 | return 0; | |
2c031a2a | 111 | } |
1a5c774c | 112 | if(*p=='\\') { |
113 | if(*++p!='"' && (*p<'0' || *p>'9')) { | |
114 | fprintf(stderr, "moira: Bad use of \\\n"); | |
115 | return 0; | |
116 | } else if (*p!='"') { | |
117 | num=(*p-'0')*64 + (*++p-'0')*8 + (*++p-'0'); | |
118 | *p=num; | |
119 | } | |
6e6374cb | 120 | } |
1a5c774c | 121 | *d++=*p++; |
122 | } | |
123 | if(p==d+1) {*d='\0'; p++;} | |
124 | else while(p>=d) *p--=' '; | |
125 | } | |
126 | if(*p==' ' || *p=='\t') { | |
127 | /* skip whitespace */ | |
128 | for(*p++='\0'; *p==' ' || *p=='\t'; p++); | |
129 | if(*p && *p!='\n') argv[++argc]=p--; | |
130 | } | |
131 | } | |
132 | if(*p=='\n') *p='\0'; | |
133 | return argc+1; | |
6e6374cb | 134 | } |
135 | ||
136 | test_noop() | |
137 | { | |
8defc06b | 138 | int status = mr_noop(); |
1a5c774c | 139 | if (status) com_err("moira (noop)", status, ""); |
6e6374cb | 140 | } |
141 | ||
59af7b90 | 142 | test_new() |
143 | { | |
8defc06b | 144 | sending_version_no = MR_VERSION_2; |
59af7b90 | 145 | } |
146 | ||
147 | test_old() | |
148 | { | |
8defc06b | 149 | sending_version_no = MR_VERSION_1; |
59af7b90 | 150 | } |
151 | ||
011e0a54 | 152 | test_connect(argc, argv) |
153 | int argc; | |
154 | char *argv[]; | |
6e6374cb | 155 | { |
f071d8a7 | 156 | char *server = ""; |
011e0a54 | 157 | int status; |
158 | ||
94551bbc | 159 | if (argc > 1) { |
160 | server = argv[1]; | |
94551bbc | 161 | } |
8defc06b | 162 | status = mr_connect(server); |
1a5c774c | 163 | if (status) com_err("moira (connect)", status, ""); |
6e6374cb | 164 | } |
165 | ||
166 | test_disconnect() | |
167 | { | |
8defc06b | 168 | int status = mr_disconnect(); |
1a5c774c | 169 | if (status) com_err("moira (disconnect)", status, ""); |
6e6374cb | 170 | } |
171 | ||
66848930 | 172 | test_host() |
173 | { | |
174 | char host[BUFSIZ]; | |
175 | int status; | |
176 | ||
f071d8a7 | 177 | memset(host, 0, sizeof(host)); |
66848930 | 178 | |
8defc06b | 179 | if (status = mr_host(host, sizeof(host) - 1)) |
1a5c774c | 180 | com_err("moira (host)", status, ""); |
66848930 | 181 | else |
182 | printf("You are connected to host %s\n", host); | |
183 | } | |
184 | ||
1a5c774c | 185 | test_auth(argc, argv) |
186 | int argc; | |
187 | char *argv[]; | |
6e6374cb | 188 | { |
59af7b90 | 189 | int status; |
190 | ||
8defc06b | 191 | status = mr_auth("mrtest"); |
1a5c774c | 192 | if (status) com_err("moira (auth)", status, ""); |
6e6374cb | 193 | } |
194 | ||
58b825a4 | 195 | test_script(argc, argv) |
196 | int argc; | |
197 | char *argv[]; | |
198 | { | |
199 | FILE *inp; | |
f071d8a7 | 200 | char input[BUFSIZ], *cp; |
58b825a4 | 201 | int status, oldstdout, oldstderr; |
202 | ||
203 | if (recursion > 8) { | |
1a5c774c | 204 | com_err("moira (script)", 0, "too many levels deep in script files\n"); |
58b825a4 | 205 | return; |
206 | } | |
207 | ||
208 | if (argc < 2) { | |
1a5c774c | 209 | com_err("moira (script)", 0, "Usage: script input_file [ output_file ]"); |
58b825a4 | 210 | return; |
211 | } | |
212 | ||
213 | inp = fopen(argv[1], "r"); | |
214 | if (inp == NULL) { | |
bddd8492 | 215 | sprintf(input, "Cannot open input file %s", argv[1]); |
1a5c774c | 216 | com_err("moira (script)", 0, input); |
58b825a4 | 217 | return; |
218 | } | |
219 | ||
220 | if (argc == 3) { | |
221 | printf("Redirecting output to %s\n", argv[2]); | |
222 | fflush(stdout); | |
223 | oldstdout = dup(1); | |
224 | close(1); | |
225 | status = open(argv[2], O_CREAT|O_WRONLY|O_APPEND, 0664); | |
226 | if (status != 1) { | |
227 | close(status); | |
228 | dup2(oldstdout, 1); | |
229 | argc = 2; | |
bddd8492 | 230 | sprintf(input, "Unable to redirect output to %s\n", argv[2]); |
1a5c774c | 231 | com_err("moira (script)", errno, input); |
58b825a4 | 232 | } else { |
233 | fflush(stderr); | |
234 | oldstderr = dup(2); | |
235 | close(2); | |
236 | dup2(1, 2); | |
237 | } | |
238 | } | |
239 | ||
240 | recursion++; | |
241 | ||
242 | for(;;) { | |
243 | if (fgets(input, BUFSIZ, inp) == NULL) | |
244 | break; | |
f071d8a7 | 245 | if ((cp = strchr(input, '\n')) != (char *)NULL) |
58b825a4 | 246 | *cp = 0; |
6a592314 | 247 | if (input[0] == 0) { |
248 | printf("\n"); | |
249 | continue; | |
250 | } | |
58b825a4 | 251 | if (input[0] == '%') { |
252 | for (cp = &input[1]; *cp && isspace(*cp); cp++); | |
253 | printf("Comment: %s\n", cp); | |
254 | continue; | |
255 | } | |
256 | printf("Executing: %s\n", input); | |
1a5c774c | 257 | execute_line(input); |
58b825a4 | 258 | } |
259 | ||
260 | recursion--; | |
261 | ||
262 | fclose(inp); | |
263 | if (argc == 3) { | |
264 | fflush(stdout); | |
265 | close(1); | |
266 | dup2(oldstdout, 1); | |
267 | close(oldstdout); | |
268 | fflush(stderr); | |
269 | close(2); | |
270 | dup2(oldstderr, 2); | |
271 | close(oldstderr); | |
272 | } | |
273 | } | |
274 | ||
6e6374cb | 275 | print_reply(argc, argv) |
276 | int argc; | |
277 | char **argv; | |
278 | { | |
279 | int i; | |
280 | for (i = 0; i < argc; i++) { | |
281 | if (i != 0) printf(", "); | |
282 | printf("%s", argv[i]); | |
283 | } | |
284 | printf("\n"); | |
285 | count++; | |
8defc06b | 286 | return(MR_CONT); |
6e6374cb | 287 | } |
288 | ||
289 | test_query(argc, argv) | |
290 | int argc; | |
291 | char **argv; | |
292 | { | |
293 | int status; | |
294 | if (argc < 2) { | |
1a5c774c | 295 | com_err("moira (query)", 0, "Usage: query handle [ args ... ]"); |
6e6374cb | 296 | return; |
297 | } | |
59af7b90 | 298 | |
6e6374cb | 299 | count = 0; |
8defc06b | 300 | status = mr_query(argv[1], argc-2, argv+2, print_reply, (char *)NULL); |
303cb414 | 301 | printf("%d tuple%s\n", count, ((count == 1) ? "" : "s")); |
1a5c774c | 302 | if (status) com_err("moira (query)", status, ""); |
6e6374cb | 303 | } |
304 | ||
305 | test_access(argc, argv) | |
306 | int argc; | |
307 | char **argv; | |
308 | { | |
309 | int status; | |
310 | if (argc < 2) { | |
1a5c774c | 311 | com_err("moira (access)", 0, "Usage: access handle [ args ... ]"); |
6e6374cb | 312 | return; |
313 | } | |
8defc06b | 314 | status = mr_access(argv[1], argc-2, argv+2); |
1a5c774c | 315 | if (status) com_err("moira (access)", status, ""); |
6e6374cb | 316 | } |
fc3ac28e | 317 | |
318 | ||
319 | test_dcm(argc, argv) | |
320 | int argc; | |
321 | char **argv; | |
322 | { | |
323 | int status; | |
324 | ||
8defc06b | 325 | if (status = mr_do_update()) |
1a5c774c | 326 | com_err("moira (dcm)", status, " while triggering dcm"); |
fc3ac28e | 327 | } |
093b57b1 | 328 | |
329 | ||
330 | test_motd(argc, argv) | |
331 | int argc; | |
332 | char **argv; | |
333 | { | |
334 | int status; | |
335 | char *motd; | |
336 | ||
8defc06b | 337 | if (status = mr_motd(&motd)) |
1a5c774c | 338 | com_err("moira (motd)", status, " while getting motd"); |
093b57b1 | 339 | if (motd) |
340 | printf("%s\n", motd); | |
341 | else | |
342 | printf("No message of the day.\n"); | |
343 | } | |
1a5c774c | 344 | |
345 | test_list_requests() | |
346 | { | |
347 | printf("Available moira requests:\n"); | |
348 | printf("\n"); | |
349 | printf("noop\t\t\tAsk Moira to do nothing\n"); | |
350 | printf("connect, c\t\tConnect to Moira server\n"); | |
351 | printf("disconnect, d\t\tDisconnect from server\n"); | |
352 | printf("host\t\t\tIdentify the server host\n"); | |
353 | printf("new, 2\t\t\tUse new protocol\n"); | |
354 | printf("old, 1\t\t\tUse old protocol\n"); | |
355 | printf("motd, m\t\t\tGet the Message of the Day\n"); | |
356 | printf("query, qy\t\tMake a query.\n"); | |
357 | printf("auth, a\t\t\tAuthenticate to Moira.\n"); | |
358 | printf("access\t\t\tCheck access to a Moira query.\n"); | |
359 | printf("dcm\t\t\tTrigger the DCM\n"); | |
360 | printf("script, s\t\tRead commands from a script.\n"); | |
361 | printf("list_requests, lr, ?\tList available commands.\n"); | |
362 | printf("quit, Q\t\t\tLeave the subsystem.\n"); | |
363 | } |