xref: /illumos-gate/usr/src/cmd/valtools/ckuid.c (revision 2a8bcb4e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #include <stdio.h>
31 #include <string.h>
32 #include <signal.h>
33 #include <stdlib.h>
34 #include <locale.h>
35 #include <libintl.h>
36 #include <limits.h>
37 #include "usage.h"
38 #include "libadm.h"
39 
40 #define	BADPID	(-2)
41 
42 static char	*prog;
43 static char	*deflt = NULL, *prompt = NULL, *error = NULL, *help = NULL;
44 static int	kpid = BADPID;
45 static int	signo;
46 
47 static const char	husage[] = "mWh";
48 static const char	eusage[] = "mWe";
49 
50 static void
usage(void)51 usage(void)
52 {
53 	switch (*prog) {
54 	default:
55 		(void) fprintf(stderr,
56 			gettext("usage: %s [options] [-m]\n"),
57 			prog);
58 		(void) fprintf(stderr, gettext(OPTMESG));
59 		(void) fprintf(stderr, gettext(STDOPTS));
60 		break;
61 
62 	case 'd':
63 		(void) fprintf(stderr,
64 			gettext("usage: %s\n"), prog);
65 		break;
66 
67 	case 'v':
68 		(void) fprintf(stderr,
69 			gettext("usage: %s input\n"), prog);
70 		break;
71 
72 	case 'h':
73 		(void) fprintf(stderr,
74 			gettext("usage: %s [options] [-m]\n"),
75 			prog);
76 		(void) fprintf(stderr, gettext(OPTMESG));
77 		(void) fprintf(stderr,
78 			gettext("\t-W width\n\t-h help\n"));
79 		break;
80 
81 	case 'e':
82 		(void) fprintf(stderr,
83 			gettext("usage: %s [options] [-m]\n"),
84 			prog);
85 		(void) fprintf(stderr, gettext(OPTMESG));
86 		(void) fprintf(stderr,
87 			gettext("\t-W width\n\t-e error\n"));
88 		break;
89 	}
90 	exit(1);
91 }
92 
93 /*
94  * Given argv[0], return a pointer to the basename of the program.
95  */
96 static char *
prog_name(char * arg0)97 prog_name(char *arg0)
98 {
99 	char *str;
100 
101 	/* first strip trailing '/' characters (exec() allows these!) */
102 	str = arg0 + strlen(arg0);
103 	while (str > arg0 && *--str == '/')
104 		*str = '\0';
105 	if ((str = strrchr(arg0, '/')) != NULL)
106 		return (str + 1);
107 	return (arg0);
108 }
109 
110 int
main(int argc,char ** argv)111 main(int argc, char **argv)
112 {
113 	int	c, n;
114 	char	*strval;
115 	size_t	len;
116 	int	disp = 0;	/* display set to no-display */
117 
118 	(void) setlocale(LC_ALL, "");
119 
120 #if	!defined(TEXT_DOMAIN)
121 #define	TEXT_DOMAIN	"SYS_TEST"
122 #endif
123 	(void) textdomain(TEXT_DOMAIN);
124 
125 	prog = prog_name(argv[0]);
126 
127 	while ((c = getopt(argc, argv, "md:p:e:h:k:s:QW:?")) != EOF) {
128 		/* check for invalid option */
129 		if ((*prog == 'v') || (*prog == 'd'))
130 			usage();
131 		if ((*prog == 'e') && !strchr(eusage, c))
132 			usage();
133 		if ((*prog == 'h') && !strchr(husage, c))
134 			usage();
135 
136 		switch (c) {
137 		case 'Q':
138 			ckquit = 0;
139 			break;
140 
141 		case 'W':
142 			ckwidth = atoi(optarg);
143 			if (ckwidth < 0) {
144 				(void) fprintf(stderr,
145 		gettext("%s: ERROR: negative display width specified\n"),
146 					prog);
147 				exit(1);
148 			}
149 			break;
150 
151 		case 'm':
152 			disp = 1;
153 			break;
154 
155 		case 'd':
156 			deflt = optarg;
157 			break;
158 
159 		case 'p':
160 			prompt = optarg;
161 			break;
162 
163 		case 'e':
164 			error = optarg;
165 			break;
166 
167 		case 'h':
168 			help = optarg;
169 			break;
170 
171 		case 'k':
172 			kpid = atoi(optarg);
173 			break;
174 
175 		case 's':
176 			signo = atoi(optarg);
177 			break;
178 
179 		default:
180 			usage();
181 		}
182 	}
183 
184 	if (signo) {
185 		if (kpid == BADPID)
186 			usage();
187 	} else
188 		signo = SIGTERM;
189 
190 	if (*prog == 'v') {
191 		if (argc != (optind+1))
192 			usage();
193 		exit(ckuid_val(argv[optind]));
194 	}
195 
196 	if (optind != argc)
197 		usage();
198 
199 	if (*prog == 'e') {
200 		ckindent = 0;
201 		ckuid_err(disp, error);
202 		exit(0);
203 	} else if (*prog == 'h') {
204 		ckindent = 0;
205 		ckuid_hlp(disp, help);
206 		exit(0);
207 	} else if (*prog == 'd') {
208 		exit(ckuid_dsp());
209 	}
210 
211 	if (deflt) {
212 		len = strlen(deflt) + 1;
213 		if (len < MAX_INPUT)
214 			len = MAX_INPUT;
215 	} else {
216 		len = MAX_INPUT;
217 	}
218 	strval = (char *)malloc(len);
219 	if (!strval) {
220 		(void) fprintf(stderr,
221 			gettext("Not enough memory\n"));
222 		exit(1);
223 	}
224 	n = ckuid(strval, disp, deflt, error, help, prompt);
225 	if (n == 3) {
226 		if (kpid > -2)
227 			(void) kill(kpid, signo);
228 		(void) puts("q");
229 	} else if (n == 0)
230 		(void) fputs(strval, stdout);
231 	return (n);
232 }
233