17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * Copyright 1995-2002 Sun Microsystems, Inc. All rights reserved. 37c478bd9Sstevel@tonic-gate * Use is subject to license terms. 4*7140fde5SIlya Usvyatsky * 5*7140fde5SIlya Usvyatsky * Copyright 2013 Nexenta Systems. All rights reserved. 67c478bd9Sstevel@tonic-gate */ 77c478bd9Sstevel@tonic-gate 87c478bd9Sstevel@tonic-gate /* 97c478bd9Sstevel@tonic-gate * Test client for kwarnd. This program is not shipped on the binary 107c478bd9Sstevel@tonic-gate * release. This code was taken and modified from gssdtest.c 117c478bd9Sstevel@tonic-gate */ 127c478bd9Sstevel@tonic-gate 137c478bd9Sstevel@tonic-gate #include <stdio.h> 147c478bd9Sstevel@tonic-gate #include <strings.h> 157c478bd9Sstevel@tonic-gate #include <ctype.h> 167c478bd9Sstevel@tonic-gate #include <stdlib.h> 177c478bd9Sstevel@tonic-gate #include "kwarnd.h" 187c478bd9Sstevel@tonic-gate #include <rpc/rpc.h> 197c478bd9Sstevel@tonic-gate 207c478bd9Sstevel@tonic-gate #define LOOP_COUNTER 100 217c478bd9Sstevel@tonic-gate 227c478bd9Sstevel@tonic-gate #define OCTAL_MACRO "%03.3o." 237c478bd9Sstevel@tonic-gate #define MALLOC(n) malloc(n) 247c478bd9Sstevel@tonic-gate #define CALLOC(n, s) calloc((n), (s)) 257c478bd9Sstevel@tonic-gate #define FREE(x, n) free(x) 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate static void instructs(void); 287c478bd9Sstevel@tonic-gate static void usage(void); 297c478bd9Sstevel@tonic-gate static int parse_input_line(char *, int *, char ***); 307c478bd9Sstevel@tonic-gate extern uid_t getuid(void); 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate static void _kwarnd_add_warning(int, char **); 337c478bd9Sstevel@tonic-gate static void _kwarnd_del_warning(int, char **); 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate static int do_kwarndtest(char *buf); 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate extern OM_UINT32 kwarn_add_warning(); 387c478bd9Sstevel@tonic-gate extern OM_UINT32 kwarn_del_warning(); 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate static int read_line(char *buf, int size) 417c478bd9Sstevel@tonic-gate { 427c478bd9Sstevel@tonic-gate int len; 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate /* read the next line. If cntl-d, return with zero char count */ 457c478bd9Sstevel@tonic-gate printf(gettext("\n> ")); 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate if (fgets(buf, size, stdin) == NULL) 487c478bd9Sstevel@tonic-gate return (0); 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate len = strlen(buf); 517c478bd9Sstevel@tonic-gate buf[--len] = '\0'; 527c478bd9Sstevel@tonic-gate return (len); 537c478bd9Sstevel@tonic-gate } 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate int 567c478bd9Sstevel@tonic-gate main() 577c478bd9Sstevel@tonic-gate { 587c478bd9Sstevel@tonic-gate char buf[512]; 597c478bd9Sstevel@tonic-gate int len, ret; 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate /* Print out usage and instructions to start off the session */ 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate instructs(); 647c478bd9Sstevel@tonic-gate usage(); 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate /* 677c478bd9Sstevel@tonic-gate * Loop, repeatedly calling parse_input_line() to get the 687c478bd9Sstevel@tonic-gate * next line and parse it into argc and argv. Act on the 697c478bd9Sstevel@tonic-gate * arguements found on the line. 707c478bd9Sstevel@tonic-gate */ 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate do { 737c478bd9Sstevel@tonic-gate len = read_line(buf, 512); 747c478bd9Sstevel@tonic-gate if (len) 757c478bd9Sstevel@tonic-gate ret = do_kwarndtest(buf); 767c478bd9Sstevel@tonic-gate } while (len && !ret); 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate return (0); 797c478bd9Sstevel@tonic-gate } 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate static int 827c478bd9Sstevel@tonic-gate do_kwarndtest(char *buf) 837c478bd9Sstevel@tonic-gate { 847c478bd9Sstevel@tonic-gate int argc; 857c478bd9Sstevel@tonic-gate char **argv, **argv_array; 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate char *cmd; 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate argv = 0; 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate if (parse_input_line(buf, &argc, &argv) == 0) { 927c478bd9Sstevel@tonic-gate printf(gettext("\n")); 937c478bd9Sstevel@tonic-gate return (1); 947c478bd9Sstevel@tonic-gate } 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate if (argc == 0) { 977c478bd9Sstevel@tonic-gate usage(); 98*7140fde5SIlya Usvyatsky FREE(argv, (argc+1)*sizeof (char *)); 997c478bd9Sstevel@tonic-gate return (0); 1007c478bd9Sstevel@tonic-gate } 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate /* 1037c478bd9Sstevel@tonic-gate * remember argv_array address, which is memory calloc'd by 1047c478bd9Sstevel@tonic-gate * parse_input_line, so it can be free'd at the end of the loop. 1057c478bd9Sstevel@tonic-gate */ 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate argv_array = argv; 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate cmd = argv[0]; 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate argc--; 1127c478bd9Sstevel@tonic-gate argv++; 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate if (strcmp(cmd, "kwarn_add_warning") == 0 || 1157c478bd9Sstevel@tonic-gate strcmp(cmd, "add") == 0) { 1167c478bd9Sstevel@tonic-gate _kwarnd_add_warning(argc, argv); 1177c478bd9Sstevel@tonic-gate } else if (strcmp(cmd, "kwarn_del_warning") == 0 || 1187c478bd9Sstevel@tonic-gate strcmp(cmd, "delete") == 0) { 1197c478bd9Sstevel@tonic-gate _kwarnd_del_warning(argc, argv); 1207c478bd9Sstevel@tonic-gate } else if (strcmp(cmd, "exit") == 0) { 1217c478bd9Sstevel@tonic-gate printf(gettext("\n")); 1227c478bd9Sstevel@tonic-gate FREE(argv_array, (argc+2) * sizeof (char *)); 1237c478bd9Sstevel@tonic-gate return (1); 1247c478bd9Sstevel@tonic-gate } else 1257c478bd9Sstevel@tonic-gate usage(); 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate /* free argv array */ 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate FREE(argv_array, (argc+2) * sizeof (char *)); 1307c478bd9Sstevel@tonic-gate return (0); 1317c478bd9Sstevel@tonic-gate } 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate static void 1347c478bd9Sstevel@tonic-gate _kwarnd_add_warning(int argc, char **argv) 1357c478bd9Sstevel@tonic-gate { 1367c478bd9Sstevel@tonic-gate OM_UINT32 status; 1377c478bd9Sstevel@tonic-gate time_t exptime; 1387c478bd9Sstevel@tonic-gate time_t now; 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate /* set up the arguments specified in the input parameters */ 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate if (argc == 0) { 1437c478bd9Sstevel@tonic-gate usage(); 1447c478bd9Sstevel@tonic-gate return; 1457c478bd9Sstevel@tonic-gate } 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate if (argc != 2) { 1487c478bd9Sstevel@tonic-gate usage(); 1497c478bd9Sstevel@tonic-gate return; 1507c478bd9Sstevel@tonic-gate } 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate time(&now); 1537c478bd9Sstevel@tonic-gate exptime = atol(argv[1]); 1547c478bd9Sstevel@tonic-gate exptime = now + exptime; 1557c478bd9Sstevel@tonic-gate 1567c478bd9Sstevel@tonic-gate status = kwarn_add_warning(argv[0], exptime); 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate if (status == 0) { 1597c478bd9Sstevel@tonic-gate printf(gettext("\nadd of credential\n\n")); 1607c478bd9Sstevel@tonic-gate printf(gettext("warning message successful for \"%s\"\n\n"), 1617c478bd9Sstevel@tonic-gate argv[0]); 1627c478bd9Sstevel@tonic-gate } else { 1637c478bd9Sstevel@tonic-gate printf(gettext("server ret err (octal) %o (%s)\n"), 1647c478bd9Sstevel@tonic-gate status, gettext("add warning error")); 1657c478bd9Sstevel@tonic-gate } 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate return; 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate } 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate static void 1727c478bd9Sstevel@tonic-gate _kwarnd_del_warning(int argc, char **argv) 1737c478bd9Sstevel@tonic-gate { 1747c478bd9Sstevel@tonic-gate OM_UINT32 status; 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate if (argc != 1) { 1777c478bd9Sstevel@tonic-gate usage(); 1787c478bd9Sstevel@tonic-gate return; 1797c478bd9Sstevel@tonic-gate } 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate status = kwarn_del_warning(argv[0]); 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate if (status == 0) { 1847c478bd9Sstevel@tonic-gate printf(gettext("delete of principal warning message" 1857c478bd9Sstevel@tonic-gate "for %s successful"), 1867c478bd9Sstevel@tonic-gate argv[0]); 1877c478bd9Sstevel@tonic-gate } else { 1887c478bd9Sstevel@tonic-gate printf(gettext("delete of principal %s unsuccessful\n\n"), 1897c478bd9Sstevel@tonic-gate argv[0]); 1907c478bd9Sstevel@tonic-gate } 1917c478bd9Sstevel@tonic-gate } 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate static void 1947c478bd9Sstevel@tonic-gate instructs(void) 1957c478bd9Sstevel@tonic-gate { 1967c478bd9Sstevel@tonic-gate fprintf(stderr, 1977c478bd9Sstevel@tonic-gate gettext( 1987c478bd9Sstevel@tonic-gate "\nThis program will test kwarnd. kwarnd must be running as root. Enter\n" 1997c478bd9Sstevel@tonic-gate "the desired command and the principal to be added/deleted. If adding a\n" 2007c478bd9Sstevel@tonic-gate "principal, also include the expiration time in seconds.\n")); 2017c478bd9Sstevel@tonic-gate } 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate static void 2047c478bd9Sstevel@tonic-gate usage(void) 2057c478bd9Sstevel@tonic-gate { 2067c478bd9Sstevel@tonic-gate fprintf(stderr, 2077c478bd9Sstevel@tonic-gate gettext( 2087c478bd9Sstevel@tonic-gate "\nusage:\t[kwarn_add_warning | add] (principal) (exptime)\n" 2097c478bd9Sstevel@tonic-gate "\t[kwarn_del_warning | delete] (principal)\n" 2107c478bd9Sstevel@tonic-gate "\texit\n\n")); 2117c478bd9Sstevel@tonic-gate } 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate /* Copied from parse_argv(), then modified */ 2147c478bd9Sstevel@tonic-gate 2157c478bd9Sstevel@tonic-gate static int 2167c478bd9Sstevel@tonic-gate parse_input_line(char *input_line, int *argc, char ***argv) 2177c478bd9Sstevel@tonic-gate { 2187c478bd9Sstevel@tonic-gate const char nil = '\0'; 2197c478bd9Sstevel@tonic-gate char *chptr; 2207c478bd9Sstevel@tonic-gate int chr_cnt; 2217c478bd9Sstevel@tonic-gate int arg_cnt = 0; 2227c478bd9Sstevel@tonic-gate int ch_was_space = 1; 2237c478bd9Sstevel@tonic-gate int ch_is_space; 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate chr_cnt = strlen(input_line); 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gate /* Count the arguments in the input_line string */ 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate *argc = 1; 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate for (chptr = &input_line[0]; *chptr != nil; chptr++) { 2327c478bd9Sstevel@tonic-gate ch_is_space = isspace(*chptr); 2337c478bd9Sstevel@tonic-gate if (ch_is_space && !ch_was_space) { 2347c478bd9Sstevel@tonic-gate (*argc)++; 2357c478bd9Sstevel@tonic-gate } 2367c478bd9Sstevel@tonic-gate ch_was_space = ch_is_space; 2377c478bd9Sstevel@tonic-gate } 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate if (ch_was_space) { 2407c478bd9Sstevel@tonic-gate (*argc)--; 2417c478bd9Sstevel@tonic-gate } /* minus trailing spaces */ 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate /* Now that we know how many args calloc the argv array */ 2447c478bd9Sstevel@tonic-gate 2457c478bd9Sstevel@tonic-gate *argv = (char **)CALLOC((*argc)+1, sizeof (char *)); 2467c478bd9Sstevel@tonic-gate chptr = (char *)(&input_line[0]); 2477c478bd9Sstevel@tonic-gate 2487c478bd9Sstevel@tonic-gate for (ch_was_space = 1; *chptr != nil; chptr++) { 2497c478bd9Sstevel@tonic-gate ch_is_space = isspace(*chptr); 2507c478bd9Sstevel@tonic-gate if (ch_is_space) { 2517c478bd9Sstevel@tonic-gate *chptr = nil; /* replace each space with nil */ 2527c478bd9Sstevel@tonic-gate } else if (ch_was_space) { /* begining of word? */ 2537c478bd9Sstevel@tonic-gate (*argv)[arg_cnt++] = chptr; /* new argument ? */ 2547c478bd9Sstevel@tonic-gate } 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate ch_was_space = ch_is_space; 2577c478bd9Sstevel@tonic-gate } 2587c478bd9Sstevel@tonic-gate 2597c478bd9Sstevel@tonic-gate return (chr_cnt); 2607c478bd9Sstevel@tonic-gate } 261