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