]> andersk Git - moira.git/blob - clients/mrtest/mrtest.c
Merge changes from moira-krb5 CVS branch.
[moira.git] / clients / mrtest / mrtest.c
1 /* $Id$
2  *
3  * Bare-bones Moira client
4  *
5  * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
6  * For copying and distribution information, please see the file
7  * <mit-copyright.h>.
8  */
9
10 #include <mit-copyright.h>
11 #include <moira.h>
12
13 #include <errno.h>
14 #include <fcntl.h>
15 #include <setjmp.h>
16 #include <signal.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #ifdef HAVE_UNISTD_H
21 #include <unistd.h>
22 #endif
23
24 #ifdef HAVE_GETOPT_H
25 #include <getopt.h>
26 #endif
27
28 #ifdef _WIN32
29 #include <windows.h>
30 #include <io.h>
31 #define dup    _dup
32 #define dup2   _dup2
33 #define isatty _isatty
34 #define close  _close
35 #define open   _open
36 #define sigjmp_buf jmp_buf
37 #define siglongjmp longjmp
38 #define sigsetjmp(env, save) setjmp(env)
39 #endif /* _WIN32 */
40
41 #ifdef HAVE_READLINE
42 #include "readline/readline.h"
43 #include "readline/history.h"
44 #endif
45
46 RCSID("$Header$");
47
48 int recursion = 0, quote_output = 0, interactive;
49 int count, quit = 0, cancel = 0;
50 char *whoami;
51
52 sigjmp_buf jb;
53
54 #define MAXARGS 20
55
56 void discard_input(int sig);
57 char *mr_gets(char *prompt, char *buf, size_t len);
58 void execute_line(char *cmdbuf);
59 int parse(char *buf, char *argv[MAXARGS]);
60 int print_reply(int argc, char **argv, void *help);
61 void test_noop(void);
62 void test_connect(int argc, char **argv);
63 void test_disconnect(void);
64 void test_host(void);
65 void test_motd(void);
66 void test_query(int argc, char **argv);
67 void test_auth(void);
68 void test_proxy(int argc, char **argv);
69 void test_access(int argc, char **argv);
70 void test_dcm(void);
71 void test_script(int argc, char **argv);
72 void test_list_requests(void);
73 void test_version(int argc, char **argv);
74 void test_krb5_auth(void);
75 void set_signal_handler(int, void (*handler)(int));
76 void set_signal_blocking(int, int);
77
78 int main(int argc, char **argv)
79 {
80   char cmdbuf[BUFSIZ];
81   int c;
82
83   whoami = argv[0];
84   interactive = (isatty(0) && isatty(1));
85
86   while ((c = getopt(argc, argv, "q")) != -1)
87     {
88       switch (c)
89         {
90         case 'q':
91           quote_output = 1;
92           break;
93
94         default:
95           fprintf(stderr, "Usage: mrtest [-q]\n");
96           exit (1);
97         }
98     }
99
100   initialize_sms_error_table();
101   initialize_krb_error_table();
102
103 #ifdef HAVE_READLINE
104   /* we don't want filename completion */
105   rl_bind_key('\t', rl_insert);
106 #endif
107
108   set_signal_handler(SIGINT, discard_input);
109   sigsetjmp(jb, 1);
110
111   while (!quit)
112     {
113       if (!mr_gets("moira:  ", cmdbuf, BUFSIZ))
114         break;
115       execute_line(cmdbuf);
116     }
117   mr_disconnect();
118   exit(0);
119 }
120
121 void discard_input(int sig)
122 {
123   putc('\n', stdout);
124
125   /* if we're inside a script, we have to clean up file descriptors,
126      so don't jump out yet */
127   if (recursion)
128     cancel = 1;
129   else
130     siglongjmp(jb, 1);
131 }
132
133 char *mr_gets(char *prompt, char *buf, size_t len)
134 {
135   char *in;
136 #ifdef HAVE_READLINE
137   if (interactive)
138     {
139       in = readline(prompt);
140
141       if (!in)
142         return NULL;
143       if (*in)
144         add_history(in);
145       strncpy(buf, in, len - 1);
146       buf[len] = 0;
147       free(in);
148
149       return buf;
150     }
151 #endif
152   printf("%s", prompt);
153   fflush(stdout);
154   in = fgets(buf, len, stdin);
155   if (!in)
156     return in;
157   if (strchr(buf, '\n'))
158     *(strchr(buf, '\n')) = '\0';
159   return buf;
160 }
161
162 void execute_line(char *cmdbuf)
163 {
164   int argc;
165   char *argv[MAXARGS];
166
167   argc = parse(cmdbuf, argv);
168   if (argc == 0)
169     return;
170   if (!strcmp(argv[0], "noop"))
171     test_noop();
172   else if (!strcmp(argv[0], "connect") || !strcmp(argv[0], "c"))
173     test_connect(argc, argv);
174   else if (!strcmp(argv[0], "disconnect") || !strcmp(argv[0], "d"))
175     test_disconnect();
176   else if (!strcmp(argv[0], "host"))
177     test_host();
178   else if (!strcmp(argv[0], "motd") || !strcmp(argv[0], "m"))
179     test_motd();
180   else if (!strcmp(argv[0], "query") || !strcmp(argv[0], "qy"))
181     test_query(argc, argv);
182   else if (!strcmp(argv[0], "auth") || !strcmp(argv[0], "a"))
183     test_krb5_auth();
184   else if (!strcmp(argv[0], "proxy") || !strcmp(argv[0], "p"))
185     test_proxy(argc, argv);
186   else if (!strcmp(argv[0], "access"))
187     test_access(argc, argv);
188   else if (!strcmp(argv[0], "dcm"))
189     test_dcm();
190   else if (!strcmp(argv[0], "script") || !strcmp(argv[0], "s"))
191     test_script(argc, argv);
192   else if (!strcmp(argv[0], "list_requests") ||
193           !strcmp(argv[0], "lr") || !strcmp(argv[0], "?"))
194     test_list_requests();
195   else if (!strcmp(argv[0], "quit") || !strcmp(argv[0], "Q"))
196     quit = 1;
197   else if (!strcmp(argv[0], "version") || !strcmp(argv[0], "v"))
198     test_version(argc, argv);
199   else if (!strcmp(argv[0], "krb4_auth") || !strcmp(argv[0], "4"))
200     test_auth();
201   else
202     {
203       fprintf(stderr, "moira: Unknown request \"%s\".  "
204               "Type \"?\" for a request list.\n", argv[0]);
205     }
206 }
207
208 int parse(char *buf, char *argv[MAXARGS])
209 {
210   char *p;
211   int argc, num;
212
213   if (!*buf)
214     return 0;
215
216   for (p = buf, argc = 0, argv[0] = buf; *p && *p != '\n'; p++)
217     {
218       if (*p == '"')
219         {
220           char *d = p++;
221           /* skip to close-quote, copying back over open-quote */
222           while (*p != '"')
223             {
224               if (!*p || *p == '\n')
225                 {
226                   fprintf(stderr,
227                           "moira: Unbalanced quotes in command line\n");
228                   return 0;
229                 }
230               /* deal with \### or \\ */
231               if (*p == '\\')
232                 {
233                   if (*++p != '"' && (*p < '0' || *p > '9') && (*p != '\\'))
234                     {
235                       fprintf(stderr, "moira: Bad use of \\\n");
236                       return 0;
237                     }
238                   else if (*p >= '0' && *p <= '9')
239                     {
240                       num = (*p - '0') * 64 + (*++p - '0') * 8 + (*++p - '0');
241                       *p = num;
242                     }
243                 }
244               *d++ = *p++;
245             }
246           if (p == d + 1)
247             {
248               *d = '\0';
249               p++;
250             }
251           else
252             {
253               while (p >= d)
254                 *p-- = ' ';
255             }
256         }
257       if (*p == ' ' || *p == '\t')
258         {
259           /* skip whitespace */
260           for (*p++ = '\0'; *p == ' ' || *p == '\t'; p++)
261             ;
262           if (*p && *p != '\n')
263             argv[++argc] = p--;
264         }
265     }
266   if (*p == '\n')
267     *p = '\0';
268   return argc + 1;
269 }
270
271 void test_noop(void)
272 {
273   int status = mr_noop();
274   if (status)
275     com_err("moira (noop)", status, "");
276 }
277
278 void test_connect(int argc, char *argv[])
279 {
280   char *server = "";
281   int status;
282
283   if (argc > 1)
284     server = argv[1];
285   status = mr_connect(server);
286   if (status)
287     com_err("moira (connect)", status, "");
288   mr_version(-1);
289 }
290
291 void test_disconnect(void)
292 {
293   int status = mr_disconnect();
294   if (status)
295     com_err("moira (disconnect)", status, "");
296 }
297
298 void test_host(void)
299 {
300   char host[BUFSIZ];
301   int status;
302
303   memset(host, 0, sizeof(host));
304
305   if ((status = mr_host(host, sizeof(host) - 1)))
306     com_err("moira (host)", status, "");
307   else
308     printf("You are connected to host %s\n", host);
309 }
310
311 void test_auth(void)
312 {
313   int status;
314
315   status = mr_auth("mrtest");
316   if (status)
317     com_err("moira (auth)", status, "");
318 }
319
320 void test_krb5_auth(void)
321 {
322   int status;
323
324   status = mr_krb5_auth("mrtest");
325   if (status)
326     com_err("moira (krb5_auth)", status, "");
327 }
328
329 void test_proxy(int argc, char *argv[])
330 {
331   int status;
332
333   if (argc != 3)
334     {
335       com_err("moira (proxy)", 0, "Usage: proxy principal authtype");
336       return;
337     }
338   status = mr_proxy(argv[1], argv[2]);
339   if (status)
340     com_err("moira (proxy)", status, "");
341 }
342
343 void test_script(int argc, char *argv[])
344 {
345   FILE *inp;
346   char input[BUFSIZ], *cp;
347   int status, oldstdout, oldstderr;
348
349   if (recursion > 8)
350     {
351       com_err("moira (script)", 0, "too many levels deep in script files\n");
352       return;
353     }
354
355   if (argc < 2)
356     {
357       com_err("moira (script)", 0, "Usage: script input_file [ output_file ]");
358       return;
359     }
360
361   inp = fopen(argv[1], "r");
362   if (!inp)
363     {
364       sprintf(input, "Cannot open input file %s", argv[1]);
365       com_err("moira (script)", 0, input);
366       return;
367     }
368
369   if (argc == 3)
370     {
371       printf("Redirecting output to %s\n", argv[2]);
372       fflush(stdout);
373       oldstdout = dup(1);
374       close(1);
375       status = open(argv[2], O_CREAT|O_WRONLY|O_APPEND, 0664);
376       if (status != 1)
377         {
378           close(status);
379           dup2(oldstdout, 1);
380           argc = 2;
381           sprintf(input, "Unable to redirect output to %s\n", argv[2]);
382           com_err("moira (script)", errno, input);
383         }
384       else
385         {
386           fflush(stderr);
387           oldstderr = dup(2);
388           close(2);
389           dup2(1, 2);
390         }
391     }
392
393   recursion++;
394
395   while (!cancel)
396     {
397       if (!fgets(input, BUFSIZ, inp))
398         break;
399       if ((cp = strchr(input, '\n')))
400         *cp = '\0';
401       if (input[0] == 0)
402         {
403           printf("\n");
404           continue;
405         }
406       if (input[0] == '%')
407         {
408           for (cp = &input[1]; *cp && isspace(*cp); cp++)
409             ;
410           printf("Comment: %s\n", cp);
411           continue;
412         }
413       printf("Executing: %s\n", input);
414       execute_line(input);
415     }
416
417   recursion--;
418   if (!recursion)
419     cancel = 0;
420
421   fclose(inp);
422   if (argc == 3)
423     {
424       fflush(stdout);
425       close(1);
426       dup2(oldstdout, 1);
427       close(oldstdout);
428       fflush(stderr);
429       close(2);
430       dup2(oldstderr, 2);
431       close(oldstderr);
432     }
433 }
434
435 int print_reply(int argc, char **argv, void *help)
436 {
437   int i;
438   for (i = 0; i < argc; i++)
439     {
440       if (i != 0)
441         printf(", ");
442       if (quote_output && !*(int *)help)
443         {
444           unsigned char *p;
445
446           for (p = (unsigned char *)argv[i]; *p; p++)
447             {
448               if (isprint(*p) && *p != '\\' && *p != ',')
449                 putc(*p, stdout);
450               else
451                 printf("\\%03o", *p);
452             }
453         }
454       else
455         printf("%s", argv[i]);
456     }
457   printf("\n");
458   count++;
459   return MR_CONT;
460 }
461
462 void test_query(int argc, char **argv)
463 {
464   int status, help;
465
466   if (argc < 2)
467     {
468       com_err("moira (query)", 0, "Usage: query handle [ args ... ]");
469       return;
470     }
471   help = !strcmp(argv[1], "_help");
472
473   count = 0;
474   /* Don't allow ^C during the query: it will confuse libmoira's
475      internal state. (Yay static variables) */
476   set_signal_blocking(SIGINT, 1);
477   status = mr_query(argv[1], argc - 2, argv + 2, print_reply, &help);
478   set_signal_blocking(SIGINT, 0);
479   printf("%d tuple%s\n", count, ((count == 1) ? "" : "s"));
480   if (status)
481     com_err("moira (query)", status, "");
482 }
483
484 void test_access(int argc, char **argv)
485 {
486   int status;
487   if (argc < 2)
488     {
489       com_err("moira (access)", 0, "Usage: access handle [ args ... ]");
490       return;
491     }
492   status = mr_access(argv[1], argc - 2, argv + 2);
493   if (status)
494     com_err("moira (access)", status, "");
495 }
496
497 void test_dcm()
498 {
499   int status;
500
501   if ((status = mr_do_update()))
502     com_err("moira (dcm)", status, " while triggering dcm");
503 }
504
505 void test_motd()
506 {
507   int status;
508   char *motd;
509
510   if ((status = mr_motd(&motd)))
511     com_err("moira (motd)", status, " while getting motd");
512   if (motd)
513     printf("%s\n", motd);
514   else
515     printf("No message of the day.\n");
516 }
517
518 void test_list_requests(void)
519 {
520   printf("Available moira requests:\n");
521   printf("\n");
522   printf("noop\t\t\tAsk Moira to do nothing\n");
523   printf("connect, c\t\tConnect to Moira server\n");
524   printf("disconnect, d\t\tDisconnect from server\n");
525   printf("host\t\t\tIdentify the server host\n");
526   printf("motd, m\t\t\tGet the Message of the Day\n");
527   printf("query, qy\t\tMake a query.\n");
528   printf("auth, a\t\t\tAuthenticate to Moira.\n");
529   printf("proxy, p\t\tProxy authenticate to Moira.\n");
530   printf("access\t\t\tCheck access to a Moira query.\n");
531   printf("dcm\t\t\tTrigger the DCM\n");
532   printf("script, s\t\tRead commands from a script.\n");
533   printf("list_requests, lr, ?\tList available commands.\n");
534   printf("quit, Q\t\t\tLeave the subsystem.\n");
535 }
536
537 void test_version(int argc, char **argv)
538 {
539   int status;
540
541   if (argc != 2)
542     {
543       com_err("moira (version)", 0, "Usage: version versionnumber");
544       return;
545     }
546   status = mr_version(atoi(argv[1]));
547   if (status)
548     com_err("moira (version)", status, "");
549 }
550
551 #ifdef HAVE_POSIX_SIGNALS
552
553 void set_signal_handler(int sig, void (*handler)(int))
554 {
555   struct sigaction action;
556
557   sigemptyset(&action.sa_mask);
558   action.sa_flags = 0;
559   action.sa_handler = handler;
560   sigaction(sig, &action, NULL);
561 }
562
563 void set_signal_blocking(int sig, int block)
564 {
565   sigset_t sigs;
566   sigemptyset(&sigs);
567   sigaddset(&sigs, sig);
568   sigprocmask(block ? SIG_BLOCK : SIG_UNBLOCK, &sigs, NULL);
569 }
570
571 #else
572
573 void set_signal_handler(int sig, void (*handler)(int))
574 {
575   signal(sig, handler);
576 }
577
578 #ifdef _WIN32
579 BOOL WINAPI blocking_handler(DWORD dwCtrlType)
580 {
581   return(TRUE);
582 }
583
584 void set_signal_blocking(int sig, int block)
585 {
586   SetConsoleCtrlHandler(blocking_handler, block ? TRUE : FALSE);
587 }
588 #endif /* _WIN32 */
589
590 #endif /* HAVE_POSIX_SIGNALS */
This page took 0.599659 seconds and 5 git commands to generate.