3 * Bare-bones Moira client
5 * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
6 * For copying and distribution information, please see the file
10 #include <mit-copyright.h>
33 #define isatty _isatty
36 #define sigjmp_buf jmp_buf
37 #define siglongjmp longjmp
38 #define sigsetjmp(env, save) setjmp(env)
42 #include "readline/readline.h"
43 #include "readline/history.h"
48 int recursion = 0, quote_output = 0, interactive;
49 int count, quit = 0, cancel = 0;
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);
62 void test_connect(int argc, char **argv);
63 void test_disconnect(void);
66 void test_query(int argc, char **argv);
68 void test_proxy(int argc, char **argv);
69 void test_access(int argc, char **argv);
71 void test_script(int argc, char **argv);
72 void test_list_requests(void);
73 void test_version(int argc, char **argv);
74 void set_signal_handler(int, void (*handler)(int));
75 void set_signal_blocking(int, int);
77 int main(int argc, char **argv)
83 interactive = (isatty(0) && isatty(1));
85 while ((c = getopt(argc, argv, "q")) != -1)
94 fprintf(stderr, "Usage: mrtest [-q]\n");
99 initialize_sms_error_table();
100 initialize_krb_error_table();
103 /* we don't want filename completion */
104 rl_bind_key('\t', rl_insert);
107 set_signal_handler(SIGINT, discard_input);
112 if (!mr_gets("moira: ", cmdbuf, BUFSIZ))
114 execute_line(cmdbuf);
120 void discard_input(int sig)
124 /* if we're inside a script, we have to clean up file descriptors,
125 so don't jump out yet */
132 char *mr_gets(char *prompt, char *buf, size_t len)
138 in = readline(prompt);
144 strncpy(buf, in, len - 1);
151 printf("%s", prompt);
153 in = fgets(buf, len, stdin);
156 if (strchr(buf, '\n'))
157 *(strchr(buf, '\n')) = '\0';
161 void execute_line(char *cmdbuf)
166 argc = parse(cmdbuf, argv);
169 if (!strcmp(argv[0], "noop"))
171 else if (!strcmp(argv[0], "connect") || !strcmp(argv[0], "c"))
172 test_connect(argc, argv);
173 else if (!strcmp(argv[0], "disconnect") || !strcmp(argv[0], "d"))
175 else if (!strcmp(argv[0], "host"))
177 else if (!strcmp(argv[0], "motd") || !strcmp(argv[0], "m"))
179 else if (!strcmp(argv[0], "query") || !strcmp(argv[0], "qy"))
180 test_query(argc, argv);
181 else if (!strcmp(argv[0], "auth") || !strcmp(argv[0], "a"))
183 else if (!strcmp(argv[0], "proxy") || !strcmp(argv[0], "p"))
184 test_proxy(argc, argv);
185 else if (!strcmp(argv[0], "access"))
186 test_access(argc, argv);
187 else if (!strcmp(argv[0], "dcm"))
189 else if (!strcmp(argv[0], "script") || !strcmp(argv[0], "s"))
190 test_script(argc, argv);
191 else if (!strcmp(argv[0], "list_requests") ||
192 !strcmp(argv[0], "lr") || !strcmp(argv[0], "?"))
193 test_list_requests();
194 else if (!strcmp(argv[0], "quit") || !strcmp(argv[0], "Q"))
196 else if (!strcmp(argv[0], "version") || !strcmp(argv[0], "v"))
197 test_version(argc, argv);
200 fprintf(stderr, "moira: Unknown request \"%s\". "
201 "Type \"?\" for a request list.\n", argv[0]);
205 int parse(char *buf, char *argv[MAXARGS])
213 for (p = buf, argc = 0, argv[0] = buf; *p && *p != '\n'; p++)
218 /* skip to close-quote, copying back over open-quote */
221 if (!*p || *p == '\n')
224 "moira: Unbalanced quotes in command line\n");
227 /* deal with \### or \\ */
230 if (*++p != '"' && (*p < '0' || *p > '9') && (*p != '\\'))
232 fprintf(stderr, "moira: Bad use of \\\n");
235 else if (*p >= '0' && *p <= '9')
237 num = (*p - '0') * 64 + (*++p - '0') * 8 + (*++p - '0');
254 if (*p == ' ' || *p == '\t')
256 /* skip whitespace */
257 for (*p++ = '\0'; *p == ' ' || *p == '\t'; p++)
259 if (*p && *p != '\n')
270 int status = mr_noop();
272 com_err("moira (noop)", status, "");
275 void test_connect(int argc, char *argv[])
282 status = mr_connect(server);
284 com_err("moira (connect)", status, "");
288 void test_disconnect(void)
290 int status = mr_disconnect();
292 com_err("moira (disconnect)", status, "");
300 memset(host, 0, sizeof(host));
302 if ((status = mr_host(host, sizeof(host) - 1)))
303 com_err("moira (host)", status, "");
305 printf("You are connected to host %s\n", host);
312 status = mr_auth("mrtest");
314 com_err("moira (auth)", status, "");
317 void test_proxy(int argc, char *argv[])
323 com_err("moira (proxy)", 0, "Usage: proxy principal authtype");
326 status = mr_proxy(argv[1], argv[2]);
328 com_err("moira (proxy)", status, "");
331 void test_script(int argc, char *argv[])
334 char input[BUFSIZ], *cp;
335 int status, oldstdout, oldstderr;
339 com_err("moira (script)", 0, "too many levels deep in script files\n");
345 com_err("moira (script)", 0, "Usage: script input_file [ output_file ]");
349 inp = fopen(argv[1], "r");
352 sprintf(input, "Cannot open input file %s", argv[1]);
353 com_err("moira (script)", 0, input);
359 printf("Redirecting output to %s\n", argv[2]);
363 status = open(argv[2], O_CREAT|O_WRONLY|O_APPEND, 0664);
369 sprintf(input, "Unable to redirect output to %s\n", argv[2]);
370 com_err("moira (script)", errno, input);
385 if (!fgets(input, BUFSIZ, inp))
387 if ((cp = strchr(input, '\n')))
396 for (cp = &input[1]; *cp && isspace(*cp); cp++)
398 printf("Comment: %s\n", cp);
401 printf("Executing: %s\n", input);
423 int print_reply(int argc, char **argv, void *help)
426 for (i = 0; i < argc; i++)
430 if (quote_output && !*(int *)help)
434 for (p = (unsigned char *)argv[i]; *p; p++)
436 if (isprint(*p) && *p != '\\' && *p != ',')
439 printf("\\%03o", *p);
443 printf("%s", argv[i]);
450 void test_query(int argc, char **argv)
456 com_err("moira (query)", 0, "Usage: query handle [ args ... ]");
459 help = !strcmp(argv[1], "_help");
462 /* Don't allow ^C during the query: it will confuse libmoira's
463 internal state. (Yay static variables) */
464 set_signal_blocking(SIGINT, 1);
465 status = mr_query(argv[1], argc - 2, argv + 2, print_reply, &help);
466 set_signal_blocking(SIGINT, 0);
467 printf("%d tuple%s\n", count, ((count == 1) ? "" : "s"));
469 com_err("moira (query)", status, "");
472 void test_access(int argc, char **argv)
477 com_err("moira (access)", 0, "Usage: access handle [ args ... ]");
480 status = mr_access(argv[1], argc - 2, argv + 2);
482 com_err("moira (access)", status, "");
489 if ((status = mr_do_update()))
490 com_err("moira (dcm)", status, " while triggering dcm");
498 if ((status = mr_motd(&motd)))
499 com_err("moira (motd)", status, " while getting motd");
501 printf("%s\n", motd);
503 printf("No message of the day.\n");
506 void test_list_requests(void)
508 printf("Available moira requests:\n");
510 printf("noop\t\t\tAsk Moira to do nothing\n");
511 printf("connect, c\t\tConnect to Moira server\n");
512 printf("disconnect, d\t\tDisconnect from server\n");
513 printf("host\t\t\tIdentify the server host\n");
514 printf("motd, m\t\t\tGet the Message of the Day\n");
515 printf("query, qy\t\tMake a query.\n");
516 printf("auth, a\t\t\tAuthenticate to Moira.\n");
517 printf("proxy, p\t\tProxy authenticate to Moira.\n");
518 printf("access\t\t\tCheck access to a Moira query.\n");
519 printf("dcm\t\t\tTrigger the DCM\n");
520 printf("script, s\t\tRead commands from a script.\n");
521 printf("list_requests, lr, ?\tList available commands.\n");
522 printf("quit, Q\t\t\tLeave the subsystem.\n");
525 void test_version(int argc, char **argv)
531 com_err("moira (version)", 0, "Usage: version versionnumber");
534 status = mr_version(atoi(argv[1]));
536 com_err("moira (version)", status, "");
539 #ifdef HAVE_POSIX_SIGNALS
541 void set_signal_handler(int sig, void (*handler)(int))
543 struct sigaction action;
545 sigemptyset(&action.sa_mask);
547 action.sa_handler = handler;
548 sigaction(sig, &action, NULL);
551 void set_signal_blocking(int sig, int block)
555 sigaddset(&sigs, sig);
556 sigprocmask(block ? SIG_BLOCK : SIG_UNBLOCK, &sigs, NULL);
561 void set_signal_handler(int sig, void (*handler)(int))
563 signal(sig, handler);
567 BOOL WINAPI blocking_handler(DWORD dwCtrlType)
572 void set_signal_blocking(int sig, int block)
574 SetConsoleCtrlHandler(blocking_handler, block ? TRUE : FALSE);
578 #endif /* HAVE_POSIX_SIGNALS */