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