1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * Copyright 1995-2002 Sun Microsystems, Inc. All rights reserved.
3*0Sstevel@tonic-gate * Use is subject to license terms.
4*0Sstevel@tonic-gate */
5*0Sstevel@tonic-gate
6*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
7*0Sstevel@tonic-gate
8*0Sstevel@tonic-gate /*
9*0Sstevel@tonic-gate * Test client for kwarnd. This program is not shipped on the binary
10*0Sstevel@tonic-gate * release. This code was taken and modified from gssdtest.c
11*0Sstevel@tonic-gate */
12*0Sstevel@tonic-gate
13*0Sstevel@tonic-gate #include <stdio.h>
14*0Sstevel@tonic-gate #include <strings.h>
15*0Sstevel@tonic-gate #include <ctype.h>
16*0Sstevel@tonic-gate #include <stdlib.h>
17*0Sstevel@tonic-gate #include "kwarnd.h"
18*0Sstevel@tonic-gate #include <rpc/rpc.h>
19*0Sstevel@tonic-gate
20*0Sstevel@tonic-gate #define LOOP_COUNTER 100
21*0Sstevel@tonic-gate
22*0Sstevel@tonic-gate #define OCTAL_MACRO "%03.3o."
23*0Sstevel@tonic-gate #define MALLOC(n) malloc(n)
24*0Sstevel@tonic-gate #define CALLOC(n, s) calloc((n), (s))
25*0Sstevel@tonic-gate #define FREE(x, n) free(x)
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate static void instructs(void);
28*0Sstevel@tonic-gate static void usage(void);
29*0Sstevel@tonic-gate static int parse_input_line(char *, int *, char ***);
30*0Sstevel@tonic-gate extern uid_t getuid(void);
31*0Sstevel@tonic-gate
32*0Sstevel@tonic-gate static void _kwarnd_add_warning(int, char **);
33*0Sstevel@tonic-gate static void _kwarnd_del_warning(int, char **);
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gate static int do_kwarndtest(char *buf);
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gate extern OM_UINT32 kwarn_add_warning();
38*0Sstevel@tonic-gate extern OM_UINT32 kwarn_del_warning();
39*0Sstevel@tonic-gate
read_line(char * buf,int size)40*0Sstevel@tonic-gate static int read_line(char *buf, int size)
41*0Sstevel@tonic-gate {
42*0Sstevel@tonic-gate int len;
43*0Sstevel@tonic-gate
44*0Sstevel@tonic-gate /* read the next line. If cntl-d, return with zero char count */
45*0Sstevel@tonic-gate printf(gettext("\n> "));
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gate if (fgets(buf, size, stdin) == NULL)
48*0Sstevel@tonic-gate return (0);
49*0Sstevel@tonic-gate
50*0Sstevel@tonic-gate len = strlen(buf);
51*0Sstevel@tonic-gate buf[--len] = '\0';
52*0Sstevel@tonic-gate return (len);
53*0Sstevel@tonic-gate }
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gate int
main()56*0Sstevel@tonic-gate main()
57*0Sstevel@tonic-gate {
58*0Sstevel@tonic-gate char buf[512];
59*0Sstevel@tonic-gate int len, ret;
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate /* Print out usage and instructions to start off the session */
62*0Sstevel@tonic-gate
63*0Sstevel@tonic-gate instructs();
64*0Sstevel@tonic-gate usage();
65*0Sstevel@tonic-gate
66*0Sstevel@tonic-gate /*
67*0Sstevel@tonic-gate * Loop, repeatedly calling parse_input_line() to get the
68*0Sstevel@tonic-gate * next line and parse it into argc and argv. Act on the
69*0Sstevel@tonic-gate * arguements found on the line.
70*0Sstevel@tonic-gate */
71*0Sstevel@tonic-gate
72*0Sstevel@tonic-gate do {
73*0Sstevel@tonic-gate len = read_line(buf, 512);
74*0Sstevel@tonic-gate if (len)
75*0Sstevel@tonic-gate ret = do_kwarndtest(buf);
76*0Sstevel@tonic-gate } while (len && !ret);
77*0Sstevel@tonic-gate
78*0Sstevel@tonic-gate return (0);
79*0Sstevel@tonic-gate }
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gate static int
do_kwarndtest(char * buf)82*0Sstevel@tonic-gate do_kwarndtest(char *buf)
83*0Sstevel@tonic-gate {
84*0Sstevel@tonic-gate int argc;
85*0Sstevel@tonic-gate char **argv, **argv_array;
86*0Sstevel@tonic-gate
87*0Sstevel@tonic-gate char *cmd;
88*0Sstevel@tonic-gate
89*0Sstevel@tonic-gate argv = 0;
90*0Sstevel@tonic-gate
91*0Sstevel@tonic-gate if (parse_input_line(buf, &argc, &argv) == 0) {
92*0Sstevel@tonic-gate printf(gettext("\n"));
93*0Sstevel@tonic-gate return (1);
94*0Sstevel@tonic-gate }
95*0Sstevel@tonic-gate
96*0Sstevel@tonic-gate if (argc == 0) {
97*0Sstevel@tonic-gate usage();
98*0Sstevel@tonic-gate /*LINTED*/
99*0Sstevel@tonic-gate FREE(argv_array, (argc+1)*sizeof (char *));
100*0Sstevel@tonic-gate return (0);
101*0Sstevel@tonic-gate }
102*0Sstevel@tonic-gate
103*0Sstevel@tonic-gate /*
104*0Sstevel@tonic-gate * remember argv_array address, which is memory calloc'd by
105*0Sstevel@tonic-gate * parse_input_line, so it can be free'd at the end of the loop.
106*0Sstevel@tonic-gate */
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gate argv_array = argv;
109*0Sstevel@tonic-gate
110*0Sstevel@tonic-gate cmd = argv[0];
111*0Sstevel@tonic-gate
112*0Sstevel@tonic-gate argc--;
113*0Sstevel@tonic-gate argv++;
114*0Sstevel@tonic-gate
115*0Sstevel@tonic-gate if (strcmp(cmd, "kwarn_add_warning") == 0 ||
116*0Sstevel@tonic-gate strcmp(cmd, "add") == 0) {
117*0Sstevel@tonic-gate _kwarnd_add_warning(argc, argv);
118*0Sstevel@tonic-gate } else if (strcmp(cmd, "kwarn_del_warning") == 0 ||
119*0Sstevel@tonic-gate strcmp(cmd, "delete") == 0) {
120*0Sstevel@tonic-gate _kwarnd_del_warning(argc, argv);
121*0Sstevel@tonic-gate } else if (strcmp(cmd, "exit") == 0) {
122*0Sstevel@tonic-gate printf(gettext("\n"));
123*0Sstevel@tonic-gate FREE(argv_array, (argc+2) * sizeof (char *));
124*0Sstevel@tonic-gate return (1);
125*0Sstevel@tonic-gate } else
126*0Sstevel@tonic-gate usage();
127*0Sstevel@tonic-gate
128*0Sstevel@tonic-gate /* free argv array */
129*0Sstevel@tonic-gate
130*0Sstevel@tonic-gate FREE(argv_array, (argc+2) * sizeof (char *));
131*0Sstevel@tonic-gate return (0);
132*0Sstevel@tonic-gate }
133*0Sstevel@tonic-gate
134*0Sstevel@tonic-gate static void
_kwarnd_add_warning(int argc,char ** argv)135*0Sstevel@tonic-gate _kwarnd_add_warning(int argc, char **argv)
136*0Sstevel@tonic-gate {
137*0Sstevel@tonic-gate OM_UINT32 status;
138*0Sstevel@tonic-gate time_t exptime;
139*0Sstevel@tonic-gate time_t now;
140*0Sstevel@tonic-gate
141*0Sstevel@tonic-gate /* set up the arguments specified in the input parameters */
142*0Sstevel@tonic-gate
143*0Sstevel@tonic-gate if (argc == 0) {
144*0Sstevel@tonic-gate usage();
145*0Sstevel@tonic-gate return;
146*0Sstevel@tonic-gate }
147*0Sstevel@tonic-gate
148*0Sstevel@tonic-gate if (argc != 2) {
149*0Sstevel@tonic-gate usage();
150*0Sstevel@tonic-gate return;
151*0Sstevel@tonic-gate }
152*0Sstevel@tonic-gate
153*0Sstevel@tonic-gate time(&now);
154*0Sstevel@tonic-gate exptime = atol(argv[1]);
155*0Sstevel@tonic-gate exptime = now + exptime;
156*0Sstevel@tonic-gate
157*0Sstevel@tonic-gate status = kwarn_add_warning(argv[0], exptime);
158*0Sstevel@tonic-gate
159*0Sstevel@tonic-gate if (status == 0) {
160*0Sstevel@tonic-gate printf(gettext("\nadd of credential\n\n"));
161*0Sstevel@tonic-gate printf(gettext("warning message successful for \"%s\"\n\n"),
162*0Sstevel@tonic-gate argv[0]);
163*0Sstevel@tonic-gate } else {
164*0Sstevel@tonic-gate printf(gettext("server ret err (octal) %o (%s)\n"),
165*0Sstevel@tonic-gate status, gettext("add warning error"));
166*0Sstevel@tonic-gate }
167*0Sstevel@tonic-gate
168*0Sstevel@tonic-gate return;
169*0Sstevel@tonic-gate
170*0Sstevel@tonic-gate }
171*0Sstevel@tonic-gate
172*0Sstevel@tonic-gate static void
_kwarnd_del_warning(int argc,char ** argv)173*0Sstevel@tonic-gate _kwarnd_del_warning(int argc, char **argv)
174*0Sstevel@tonic-gate {
175*0Sstevel@tonic-gate OM_UINT32 status;
176*0Sstevel@tonic-gate
177*0Sstevel@tonic-gate if (argc != 1) {
178*0Sstevel@tonic-gate usage();
179*0Sstevel@tonic-gate return;
180*0Sstevel@tonic-gate }
181*0Sstevel@tonic-gate
182*0Sstevel@tonic-gate status = kwarn_del_warning(argv[0]);
183*0Sstevel@tonic-gate
184*0Sstevel@tonic-gate if (status == 0) {
185*0Sstevel@tonic-gate printf(gettext("delete of principal warning message"
186*0Sstevel@tonic-gate "for %s successful"),
187*0Sstevel@tonic-gate argv[0]);
188*0Sstevel@tonic-gate } else {
189*0Sstevel@tonic-gate printf(gettext("delete of principal %s unsuccessful\n\n"),
190*0Sstevel@tonic-gate argv[0]);
191*0Sstevel@tonic-gate }
192*0Sstevel@tonic-gate }
193*0Sstevel@tonic-gate
194*0Sstevel@tonic-gate static void
instructs(void)195*0Sstevel@tonic-gate instructs(void)
196*0Sstevel@tonic-gate {
197*0Sstevel@tonic-gate fprintf(stderr,
198*0Sstevel@tonic-gate gettext(
199*0Sstevel@tonic-gate "\nThis program will test kwarnd. kwarnd must be running as root. Enter\n"
200*0Sstevel@tonic-gate "the desired command and the principal to be added/deleted. If adding a\n"
201*0Sstevel@tonic-gate "principal, also include the expiration time in seconds.\n"));
202*0Sstevel@tonic-gate }
203*0Sstevel@tonic-gate
204*0Sstevel@tonic-gate static void
usage(void)205*0Sstevel@tonic-gate usage(void)
206*0Sstevel@tonic-gate {
207*0Sstevel@tonic-gate fprintf(stderr,
208*0Sstevel@tonic-gate gettext(
209*0Sstevel@tonic-gate "\nusage:\t[kwarn_add_warning | add] (principal) (exptime)\n"
210*0Sstevel@tonic-gate "\t[kwarn_del_warning | delete] (principal)\n"
211*0Sstevel@tonic-gate "\texit\n\n"));
212*0Sstevel@tonic-gate }
213*0Sstevel@tonic-gate
214*0Sstevel@tonic-gate /* Copied from parse_argv(), then modified */
215*0Sstevel@tonic-gate
216*0Sstevel@tonic-gate static int
parse_input_line(char * input_line,int * argc,char *** argv)217*0Sstevel@tonic-gate parse_input_line(char *input_line, int *argc, char ***argv)
218*0Sstevel@tonic-gate {
219*0Sstevel@tonic-gate const char nil = '\0';
220*0Sstevel@tonic-gate char *chptr;
221*0Sstevel@tonic-gate int chr_cnt;
222*0Sstevel@tonic-gate int arg_cnt = 0;
223*0Sstevel@tonic-gate int ch_was_space = 1;
224*0Sstevel@tonic-gate int ch_is_space;
225*0Sstevel@tonic-gate
226*0Sstevel@tonic-gate chr_cnt = strlen(input_line);
227*0Sstevel@tonic-gate
228*0Sstevel@tonic-gate /* Count the arguments in the input_line string */
229*0Sstevel@tonic-gate
230*0Sstevel@tonic-gate *argc = 1;
231*0Sstevel@tonic-gate
232*0Sstevel@tonic-gate for (chptr = &input_line[0]; *chptr != nil; chptr++) {
233*0Sstevel@tonic-gate ch_is_space = isspace(*chptr);
234*0Sstevel@tonic-gate if (ch_is_space && !ch_was_space) {
235*0Sstevel@tonic-gate (*argc)++;
236*0Sstevel@tonic-gate }
237*0Sstevel@tonic-gate ch_was_space = ch_is_space;
238*0Sstevel@tonic-gate }
239*0Sstevel@tonic-gate
240*0Sstevel@tonic-gate if (ch_was_space) {
241*0Sstevel@tonic-gate (*argc)--;
242*0Sstevel@tonic-gate } /* minus trailing spaces */
243*0Sstevel@tonic-gate
244*0Sstevel@tonic-gate /* Now that we know how many args calloc the argv array */
245*0Sstevel@tonic-gate
246*0Sstevel@tonic-gate *argv = (char **)CALLOC((*argc)+1, sizeof (char *));
247*0Sstevel@tonic-gate chptr = (char *)(&input_line[0]);
248*0Sstevel@tonic-gate
249*0Sstevel@tonic-gate for (ch_was_space = 1; *chptr != nil; chptr++) {
250*0Sstevel@tonic-gate ch_is_space = isspace(*chptr);
251*0Sstevel@tonic-gate if (ch_is_space) {
252*0Sstevel@tonic-gate *chptr = nil; /* replace each space with nil */
253*0Sstevel@tonic-gate } else if (ch_was_space) { /* begining of word? */
254*0Sstevel@tonic-gate (*argv)[arg_cnt++] = chptr; /* new argument ? */
255*0Sstevel@tonic-gate }
256*0Sstevel@tonic-gate
257*0Sstevel@tonic-gate ch_was_space = ch_is_space;
258*0Sstevel@tonic-gate }
259*0Sstevel@tonic-gate
260*0Sstevel@tonic-gate return (chr_cnt);
261*0Sstevel@tonic-gate }
262