xref: /illumos-gate/usr/src/cmd/gss/gsscred/gsscred.c (revision 2a8bcb4e)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1997-2002 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate  *  gsscred utility
29*7c478bd9Sstevel@tonic-gate  *  Manages mapping between a security principal name and unix uid
30*7c478bd9Sstevel@tonic-gate  */
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #include <stdio.h>
33*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
34*7c478bd9Sstevel@tonic-gate #include <pwd.h>
35*7c478bd9Sstevel@tonic-gate #include <unistd.h>
36*7c478bd9Sstevel@tonic-gate #include <string.h>
37*7c478bd9Sstevel@tonic-gate #include <gssapi/gssapi_ext.h>
38*7c478bd9Sstevel@tonic-gate #include "gsscred.h"
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #define	MAX_STR_LEN	1024
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate /*
44*7c478bd9Sstevel@tonic-gate  * Internal Functions
45*7c478bd9Sstevel@tonic-gate  */
46*7c478bd9Sstevel@tonic-gate static void usage(void);
47*7c478bd9Sstevel@tonic-gate static void addUser(const char *name, const char *oid, const char *userUid,
48*7c478bd9Sstevel@tonic-gate 		const char *userComment, const char *userMech);
49*7c478bd9Sstevel@tonic-gate static int file_listUsers(const gss_OID mechOid, const char *userUid,
50*7c478bd9Sstevel@tonic-gate 		char **errDetails);
51*7c478bd9Sstevel@tonic-gate static int listUsers(const char *name, const char *nameTypeOid,
52*7c478bd9Sstevel@tonic-gate 		const char *uid, const char *mechOid);
53*7c478bd9Sstevel@tonic-gate static int file_removeUsers(const gss_OID mechOid, const char *userUid,
54*7c478bd9Sstevel@tonic-gate 		char **errDetails);
55*7c478bd9Sstevel@tonic-gate static int removeUsers(const char *name, const char *nameTypeOid,
56*7c478bd9Sstevel@tonic-gate 		const char *uid, const char *mechOid);
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate /*
59*7c478bd9Sstevel@tonic-gate  * Global variables
60*7c478bd9Sstevel@tonic-gate  */
61*7c478bd9Sstevel@tonic-gate static int tableSource;
62*7c478bd9Sstevel@tonic-gate static char *PROG_NAME = NULL;
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate int
main(int argc,char * args[])65*7c478bd9Sstevel@tonic-gate main(int argc, char *args[])
66*7c478bd9Sstevel@tonic-gate {
67*7c478bd9Sstevel@tonic-gate 	char *userName = NULL, *nameTypeOID = NULL,
68*7c478bd9Sstevel@tonic-gate 		*uid = NULL, *comment = NULL, *mech = NULL,
69*7c478bd9Sstevel@tonic-gate 		operation = '0';
70*7c478bd9Sstevel@tonic-gate 	int c, errflag = 0;
71*7c478bd9Sstevel@tonic-gate 	extern char *optarg;
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	PROG_NAME = *args;
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	/* set locale and domain for internationalization */
76*7c478bd9Sstevel@tonic-gate 	setlocale(LC_ALL, "");
77*7c478bd9Sstevel@tonic-gate 	textdomain(TEXT_DOMAIN);
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 	if (argc < 2)
80*7c478bd9Sstevel@tonic-gate 		usage();
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 	/* Process the input arguments */
83*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, args, "arln:o:u:m:c:")) != EOF) {
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 		switch (c) {
86*7c478bd9Sstevel@tonic-gate 		case 'n':
87*7c478bd9Sstevel@tonic-gate 			userName = optarg;
88*7c478bd9Sstevel@tonic-gate 			break;
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 		case 'o':
91*7c478bd9Sstevel@tonic-gate 			nameTypeOID = optarg;
92*7c478bd9Sstevel@tonic-gate 			break;
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 		case 'u':
95*7c478bd9Sstevel@tonic-gate 			uid = optarg;
96*7c478bd9Sstevel@tonic-gate 			break;
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 		case 'm':
99*7c478bd9Sstevel@tonic-gate 			mech = optarg;
100*7c478bd9Sstevel@tonic-gate 			break;
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 		case 'c':
103*7c478bd9Sstevel@tonic-gate 			comment = optarg;
104*7c478bd9Sstevel@tonic-gate 			break;
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 		case 'a':
107*7c478bd9Sstevel@tonic-gate 		case 'r':
108*7c478bd9Sstevel@tonic-gate 		case 'l':
109*7c478bd9Sstevel@tonic-gate 			operation = c;
110*7c478bd9Sstevel@tonic-gate 			errflag++;
111*7c478bd9Sstevel@tonic-gate 			if (errflag > 1)
112*7c478bd9Sstevel@tonic-gate 				usage();
113*7c478bd9Sstevel@tonic-gate 			break;
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 		default:
116*7c478bd9Sstevel@tonic-gate 			usage();
117*7c478bd9Sstevel@tonic-gate 		}
118*7c478bd9Sstevel@tonic-gate 	}
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 	/* determine which back-end to use as the gsscred store */
121*7c478bd9Sstevel@tonic-gate 	tableSource = gsscred_read_config_file();
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	/* perform the requested operation */
124*7c478bd9Sstevel@tonic-gate 	switch (operation) {
125*7c478bd9Sstevel@tonic-gate 		case 'a':
126*7c478bd9Sstevel@tonic-gate 			addUser(userName, nameTypeOID, uid, comment, mech);
127*7c478bd9Sstevel@tonic-gate 			break;
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 		case 'r':
130*7c478bd9Sstevel@tonic-gate 			removeUsers(userName, nameTypeOID, uid, mech);
131*7c478bd9Sstevel@tonic-gate 			break;
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 		case 'l':
134*7c478bd9Sstevel@tonic-gate 			listUsers(userName, nameTypeOID, uid, mech);
135*7c478bd9Sstevel@tonic-gate 			break;
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 		default:
138*7c478bd9Sstevel@tonic-gate 			usage();
139*7c478bd9Sstevel@tonic-gate 	}
140*7c478bd9Sstevel@tonic-gate 	fprintf(stdout, "\n");
141*7c478bd9Sstevel@tonic-gate 	return (0);
142*7c478bd9Sstevel@tonic-gate }  /* main */
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate /*
145*7c478bd9Sstevel@tonic-gate  * Handles the addition of users to the gsscred table.
146*7c478bd9Sstevel@tonic-gate  */
147*7c478bd9Sstevel@tonic-gate static void
addUser(const char * name,const char * nameOidStr,const char * userUid,const char * userComment,const char * mechOidStr)148*7c478bd9Sstevel@tonic-gate addUser(const char *name, const char *nameOidStr,
149*7c478bd9Sstevel@tonic-gate 	    const char *userUid, const char *userComment,
150*7c478bd9Sstevel@tonic-gate 	    const char *mechOidStr)
151*7c478bd9Sstevel@tonic-gate {
152*7c478bd9Sstevel@tonic-gate 	gss_OID mechOid;
153*7c478bd9Sstevel@tonic-gate 	gss_buffer_desc fullName = GSS_C_EMPTY_BUFFER,
154*7c478bd9Sstevel@tonic-gate 		hexBufDesc = GSS_C_EMPTY_BUFFER,
155*7c478bd9Sstevel@tonic-gate 		hexMechOid = GSS_C_EMPTY_BUFFER;
156*7c478bd9Sstevel@tonic-gate 	char comment[MAX_STR_LEN+1], hexBuf[MAX_STR_LEN+MAX_STR_LEN+1],
157*7c478bd9Sstevel@tonic-gate 		hexMechOidBuf[MAX_STR_LEN+1], *commentPtr = NULL,
158*7c478bd9Sstevel@tonic-gate 		*errDetail = NULL, uidStr[256], *uidPtr;
159*7c478bd9Sstevel@tonic-gate 	struct passwd *aUser;
160*7c478bd9Sstevel@tonic-gate 	OM_uint32 minor;
161*7c478bd9Sstevel@tonic-gate 	int count = 0, retCode;
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 	hexMechOid.length = MAX_STR_LEN;
164*7c478bd9Sstevel@tonic-gate 	hexMechOid.value = (void*)hexMechOidBuf;
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 	/* addition of users can only be performed by super users */
167*7c478bd9Sstevel@tonic-gate 	if (getuid()) {
168*7c478bd9Sstevel@tonic-gate 		fprintf(stderr,
169*7c478bd9Sstevel@tonic-gate 			gettext("\nUser addition requires"
170*7c478bd9Sstevel@tonic-gate 				" root privileges."));
171*7c478bd9Sstevel@tonic-gate 		return;
172*7c478bd9Sstevel@tonic-gate 	}
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	/* the mechanism OID is required */
175*7c478bd9Sstevel@tonic-gate 	if (mechOidStr == NULL) {
176*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("\nUnspecified mechanism."));
177*7c478bd9Sstevel@tonic-gate 		usage();
178*7c478bd9Sstevel@tonic-gate 	}
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	/* Convert from string mechanism Oid to ASN.1 oid and then hex */
181*7c478bd9Sstevel@tonic-gate 	if (__gss_mech_to_oid(mechOidStr, &mechOid) != GSS_S_COMPLETE) {
182*7c478bd9Sstevel@tonic-gate 		fprintf(stderr,
183*7c478bd9Sstevel@tonic-gate 			gettext("\nInvalid mechanism specified [%s]."),
184*7c478bd9Sstevel@tonic-gate 			mechOidStr);
185*7c478bd9Sstevel@tonic-gate 		return;
186*7c478bd9Sstevel@tonic-gate 	}
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 	hexBufDesc.length = mechOid->length;
189*7c478bd9Sstevel@tonic-gate 	hexBufDesc.value = mechOid->elements;
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 	if (!gsscred_AsHex(&hexBufDesc, &hexMechOid)) {
192*7c478bd9Sstevel@tonic-gate 		fprintf(stderr,
193*7c478bd9Sstevel@tonic-gate 			gettext("\nInternal error.  "
194*7c478bd9Sstevel@tonic-gate 				"Conversion to hex failed."));
195*7c478bd9Sstevel@tonic-gate 		return;
196*7c478bd9Sstevel@tonic-gate 	}
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 	/*
199*7c478bd9Sstevel@tonic-gate 	 * if the name is specified, then do single addition.
200*7c478bd9Sstevel@tonic-gate 	 * Might have to look up the uid.
201*7c478bd9Sstevel@tonic-gate 	 */
202*7c478bd9Sstevel@tonic-gate 	if (name != NULL) {
203*7c478bd9Sstevel@tonic-gate 		hexBufDesc.length = sizeof (hexBuf);
204*7c478bd9Sstevel@tonic-gate 		hexBufDesc.value = hexBuf;
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 		/* build the name as needed */
207*7c478bd9Sstevel@tonic-gate 		if (!gsscred_MakeName(mechOid, name, nameOidStr, &fullName)) {
208*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
209*7c478bd9Sstevel@tonic-gate 				gettext("\nError adding user [%s]."), name);
210*7c478bd9Sstevel@tonic-gate 			return;
211*7c478bd9Sstevel@tonic-gate 		}
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 		/* convert it to hex */
214*7c478bd9Sstevel@tonic-gate 		if (!gsscred_AsHex(&fullName, &hexBufDesc)) {
215*7c478bd9Sstevel@tonic-gate 			gss_release_buffer(&minor, &fullName);
216*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
217*7c478bd9Sstevel@tonic-gate 				gettext("\nInternal error.  "
218*7c478bd9Sstevel@tonic-gate 					"Conversion to hex failed."));
219*7c478bd9Sstevel@tonic-gate 			return;
220*7c478bd9Sstevel@tonic-gate 		}
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 		/* might require the lookup of the uid if one not specified */
223*7c478bd9Sstevel@tonic-gate 		if (userUid == NULL) {
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate 			if ((aUser = getpwnam(name)) == NULL) {
226*7c478bd9Sstevel@tonic-gate 				fprintf(stderr,
227*7c478bd9Sstevel@tonic-gate 					gettext("\nUnable to obtain password"
228*7c478bd9Sstevel@tonic-gate 						" information for [%s]."),
229*7c478bd9Sstevel@tonic-gate 					name);
230*7c478bd9Sstevel@tonic-gate 				gss_release_buffer(&minor, &fullName);
231*7c478bd9Sstevel@tonic-gate 				return;
232*7c478bd9Sstevel@tonic-gate 			}
233*7c478bd9Sstevel@tonic-gate 			sprintf(uidStr, "%ld", aUser->pw_uid);
234*7c478bd9Sstevel@tonic-gate 			uidPtr = uidStr;
235*7c478bd9Sstevel@tonic-gate 		}
236*7c478bd9Sstevel@tonic-gate 		else
237*7c478bd9Sstevel@tonic-gate 			uidPtr = (char *)userUid;
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate 		if (userComment == NULL) {
240*7c478bd9Sstevel@tonic-gate 			sprintf(comment, "%s, %s", name, mechOidStr);
241*7c478bd9Sstevel@tonic-gate 			commentPtr = comment;
242*7c478bd9Sstevel@tonic-gate 		} else
243*7c478bd9Sstevel@tonic-gate 			commentPtr = (char *)userComment;
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate 		if (tableSource == GSSCRED_FLAT_FILE)
246*7c478bd9Sstevel@tonic-gate 			retCode = file_addGssCredEntry(&hexBufDesc,
247*7c478bd9Sstevel@tonic-gate 					uidPtr, commentPtr, &errDetail);
248*7c478bd9Sstevel@tonic-gate 		else
249*7c478bd9Sstevel@tonic-gate 			/* other backends (ldap, dss) coming soon */
250*7c478bd9Sstevel@tonic-gate 			retCode	= 0;
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate 		if (!retCode) {
253*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("\nError adding user [%s]."),
254*7c478bd9Sstevel@tonic-gate 				commentPtr);
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate 			if (errDetail) {
257*7c478bd9Sstevel@tonic-gate 				fprintf(stderr, "\n%s\n", errDetail);
258*7c478bd9Sstevel@tonic-gate 				free(errDetail);
259*7c478bd9Sstevel@tonic-gate 				errDetail = NULL;
260*7c478bd9Sstevel@tonic-gate 			}
261*7c478bd9Sstevel@tonic-gate 		}
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 		gss_release_buffer(&minor, &fullName);
264*7c478bd9Sstevel@tonic-gate 		return;
265*7c478bd9Sstevel@tonic-gate 	}
266*7c478bd9Sstevel@tonic-gate 
267*7c478bd9Sstevel@tonic-gate 	/*
268*7c478bd9Sstevel@tonic-gate 	 * since no name specified, then we will load everyone from
269*7c478bd9Sstevel@tonic-gate 	 * password table.  This means that -u and -o options are invalid.
270*7c478bd9Sstevel@tonic-gate 	 * We just ignore it, but we could flag it as error.
271*7c478bd9Sstevel@tonic-gate 	 */
272*7c478bd9Sstevel@tonic-gate 	setpwent();
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate 	while ((aUser = getpwent()) != NULL) {
275*7c478bd9Sstevel@tonic-gate 		hexBufDesc.length = sizeof (hexBuf);
276*7c478bd9Sstevel@tonic-gate 		hexBufDesc.value = hexBuf;
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate 		if (!gsscred_MakeName(mechOid, aUser->pw_name,
279*7c478bd9Sstevel@tonic-gate 			nameOidStr, &fullName)) {
280*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
281*7c478bd9Sstevel@tonic-gate 				gettext("\nError adding user [%s]."),
282*7c478bd9Sstevel@tonic-gate 				aUser->pw_name);
283*7c478bd9Sstevel@tonic-gate 			continue;
284*7c478bd9Sstevel@tonic-gate 		}
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate 		if (!gsscred_AsHex(&fullName, &hexBufDesc)) {
287*7c478bd9Sstevel@tonic-gate 			gss_release_buffer(&minor, &fullName);
288*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
289*7c478bd9Sstevel@tonic-gate 				gettext("\nInternal error.  "
290*7c478bd9Sstevel@tonic-gate 					"Conversion to hex failed."));
291*7c478bd9Sstevel@tonic-gate 			continue;
292*7c478bd9Sstevel@tonic-gate 		}
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate 		sprintf(uidStr, "%ld", aUser->pw_uid);
295*7c478bd9Sstevel@tonic-gate 		sprintf(comment, "%s, %s", aUser->pw_name, mechOidStr);
296*7c478bd9Sstevel@tonic-gate 		if (tableSource == GSSCRED_FLAT_FILE)
297*7c478bd9Sstevel@tonic-gate 			retCode = file_addGssCredEntry(&hexBufDesc,
298*7c478bd9Sstevel@tonic-gate 					uidStr, comment, &errDetail);
299*7c478bd9Sstevel@tonic-gate 		else
300*7c478bd9Sstevel@tonic-gate 			retCode	= 0;
301*7c478bd9Sstevel@tonic-gate 
302*7c478bd9Sstevel@tonic-gate 		if (!retCode) {
303*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
304*7c478bd9Sstevel@tonic-gate 				gettext("\nError adding user [%s]."),
305*7c478bd9Sstevel@tonic-gate 				comment);
306*7c478bd9Sstevel@tonic-gate 
307*7c478bd9Sstevel@tonic-gate 			if (errDetail) {
308*7c478bd9Sstevel@tonic-gate 				fprintf(stderr, "\n%s\n", errDetail);
309*7c478bd9Sstevel@tonic-gate 				free(errDetail);
310*7c478bd9Sstevel@tonic-gate 				errDetail = NULL;
311*7c478bd9Sstevel@tonic-gate 			}
312*7c478bd9Sstevel@tonic-gate 		} else {
313*7c478bd9Sstevel@tonic-gate 			count++;
314*7c478bd9Sstevel@tonic-gate 			if ((count % 50) == 0)
315*7c478bd9Sstevel@tonic-gate 				fprintf(stdout,
316*7c478bd9Sstevel@tonic-gate 					gettext("\n[%d] users added..."),
317*7c478bd9Sstevel@tonic-gate 					count);
318*7c478bd9Sstevel@tonic-gate 		}
319*7c478bd9Sstevel@tonic-gate 		gss_release_buffer(&minor, &fullName);
320*7c478bd9Sstevel@tonic-gate 	}
321*7c478bd9Sstevel@tonic-gate 	endpwent();
322*7c478bd9Sstevel@tonic-gate }  /* addUser */
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate 
325*7c478bd9Sstevel@tonic-gate /*
326*7c478bd9Sstevel@tonic-gate  *  Handles the searching of the gsscred table.
327*7c478bd9Sstevel@tonic-gate  */
listUsers(const char * name,const char * nameOidStr,const char * uidStr,const char * mechOidStr)328*7c478bd9Sstevel@tonic-gate static int listUsers(const char *name, const char *nameOidStr,
329*7c478bd9Sstevel@tonic-gate 		const char *uidStr, const char *mechOidStr)
330*7c478bd9Sstevel@tonic-gate {
331*7c478bd9Sstevel@tonic-gate 	GssCredEntry *entryPtr, *entryTmpPtr;
332*7c478bd9Sstevel@tonic-gate 	char hexMech[256],
333*7c478bd9Sstevel@tonic-gate 		hexName[(MAX_STR_LEN *2) + 1];
334*7c478bd9Sstevel@tonic-gate 	gss_OID anOid = NULL, userMechOid = NULL;
335*7c478bd9Sstevel@tonic-gate 	gss_OID_set mechSet = NULL;
336*7c478bd9Sstevel@tonic-gate 	gss_buffer_desc inBufDesc = GSS_C_EMPTY_BUFFER,
337*7c478bd9Sstevel@tonic-gate 		outBufDesc = GSS_C_EMPTY_BUFFER,
338*7c478bd9Sstevel@tonic-gate 		searchName = GSS_C_EMPTY_BUFFER;
339*7c478bd9Sstevel@tonic-gate 	int status = 1, numOfMechs, i;
340*7c478bd9Sstevel@tonic-gate 	OM_uint32 minor;
341*7c478bd9Sstevel@tonic-gate 	char *errDetails = NULL;
342*7c478bd9Sstevel@tonic-gate 
343*7c478bd9Sstevel@tonic-gate 	/* Do we need to convert the mechanism oid? */
344*7c478bd9Sstevel@tonic-gate 	if (mechOidStr != NULL) {
345*7c478bd9Sstevel@tonic-gate 
346*7c478bd9Sstevel@tonic-gate 		if (__gss_mech_to_oid(mechOidStr, &userMechOid) !=
347*7c478bd9Sstevel@tonic-gate 			GSS_S_COMPLETE) {
348*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
349*7c478bd9Sstevel@tonic-gate 				gettext("\nInvalid mechanism specified [%s]."),
350*7c478bd9Sstevel@tonic-gate 				mechOidStr);
351*7c478bd9Sstevel@tonic-gate 			return (0);
352*7c478bd9Sstevel@tonic-gate 		}
353*7c478bd9Sstevel@tonic-gate 		inBufDesc.length = userMechOid->length;
354*7c478bd9Sstevel@tonic-gate 		inBufDesc.value = userMechOid->elements;
355*7c478bd9Sstevel@tonic-gate 		outBufDesc.length = sizeof (hexMech);
356*7c478bd9Sstevel@tonic-gate 		outBufDesc.value = hexMech;
357*7c478bd9Sstevel@tonic-gate 
358*7c478bd9Sstevel@tonic-gate 		if (!gsscred_AsHex(&inBufDesc, &outBufDesc)) {
359*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
360*7c478bd9Sstevel@tonic-gate 				gettext("\nInternal error.  "
361*7c478bd9Sstevel@tonic-gate 					"Conversion to hex failed."));
362*7c478bd9Sstevel@tonic-gate 			status = 0;
363*7c478bd9Sstevel@tonic-gate 			goto cleanup;
364*7c478bd9Sstevel@tonic-gate 		}
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate 	}	/* mechOidStr != NULL */
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate 	/* are we retrieving everyone ? or searching by mech ? */
369*7c478bd9Sstevel@tonic-gate 	if ((name == NULL && uidStr == NULL && mechOidStr == NULL) ||
370*7c478bd9Sstevel@tonic-gate 	    (name == NULL && uidStr == NULL)) {
371*7c478bd9Sstevel@tonic-gate 
372*7c478bd9Sstevel@tonic-gate 		if (tableSource == GSSCRED_FLAT_FILE) {
373*7c478bd9Sstevel@tonic-gate 			file_listUsers(userMechOid, NULL, &errDetails);
374*7c478bd9Sstevel@tonic-gate 
375*7c478bd9Sstevel@tonic-gate 			if (errDetails) {
376*7c478bd9Sstevel@tonic-gate 				fprintf(stderr,
377*7c478bd9Sstevel@tonic-gate 					gettext("\nError searching gsscred"
378*7c478bd9Sstevel@tonic-gate 						" table [%s]."),
379*7c478bd9Sstevel@tonic-gate 					errDetails);
380*7c478bd9Sstevel@tonic-gate 				free(errDetails);
381*7c478bd9Sstevel@tonic-gate 				errDetails = NULL;
382*7c478bd9Sstevel@tonic-gate 				return (0);
383*7c478bd9Sstevel@tonic-gate 			}
384*7c478bd9Sstevel@tonic-gate 			return (1);
385*7c478bd9Sstevel@tonic-gate 		}
386*7c478bd9Sstevel@tonic-gate 
387*7c478bd9Sstevel@tonic-gate 	}
388*7c478bd9Sstevel@tonic-gate 
389*7c478bd9Sstevel@tonic-gate 	/* Are we searching by uid or uid and mech? */
390*7c478bd9Sstevel@tonic-gate 	if (name == NULL && uidStr != NULL) {
391*7c478bd9Sstevel@tonic-gate 
392*7c478bd9Sstevel@tonic-gate 		if (tableSource == GSSCRED_FLAT_FILE)
393*7c478bd9Sstevel@tonic-gate 			file_listUsers(userMechOid, uidStr, &errDetails);
394*7c478bd9Sstevel@tonic-gate 		else {
395*7c478bd9Sstevel@tonic-gate 			entryPtr = NULL;
396*7c478bd9Sstevel@tonic-gate 			while (entryPtr != NULL) {
397*7c478bd9Sstevel@tonic-gate 				fprintf(stdout, "\n%s\t%d\t%s",
398*7c478bd9Sstevel@tonic-gate 					entryPtr->principal_name,
399*7c478bd9Sstevel@tonic-gate 					entryPtr->unix_uid, entryPtr->comment);
400*7c478bd9Sstevel@tonic-gate 				free(entryPtr->principal_name);
401*7c478bd9Sstevel@tonic-gate 				free(entryPtr->comment);
402*7c478bd9Sstevel@tonic-gate 				entryTmpPtr = entryPtr->next;
403*7c478bd9Sstevel@tonic-gate 				free(entryPtr);
404*7c478bd9Sstevel@tonic-gate 				entryPtr = entryTmpPtr;
405*7c478bd9Sstevel@tonic-gate 			}
406*7c478bd9Sstevel@tonic-gate 		}
407*7c478bd9Sstevel@tonic-gate 
408*7c478bd9Sstevel@tonic-gate 		/* check for any errors */
409*7c478bd9Sstevel@tonic-gate 		if (errDetails) {
410*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
411*7c478bd9Sstevel@tonic-gate 				gettext("\nError searching gsscred table "
412*7c478bd9Sstevel@tonic-gate 					"[%s]."),
413*7c478bd9Sstevel@tonic-gate 				errDetails);
414*7c478bd9Sstevel@tonic-gate 			free(errDetails);
415*7c478bd9Sstevel@tonic-gate 			errDetails = NULL;
416*7c478bd9Sstevel@tonic-gate 			status = 0;
417*7c478bd9Sstevel@tonic-gate 		}
418*7c478bd9Sstevel@tonic-gate 
419*7c478bd9Sstevel@tonic-gate 		goto cleanup;
420*7c478bd9Sstevel@tonic-gate 	}
421*7c478bd9Sstevel@tonic-gate 
422*7c478bd9Sstevel@tonic-gate 	/*
423*7c478bd9Sstevel@tonic-gate 	 * We are searching by name;
424*7c478bd9Sstevel@tonic-gate 	 * how many mechs must we check?
425*7c478bd9Sstevel@tonic-gate 	 */
426*7c478bd9Sstevel@tonic-gate 	if (mechOidStr == NULL) {
427*7c478bd9Sstevel@tonic-gate 
428*7c478bd9Sstevel@tonic-gate 		if (gss_indicate_mechs(&minor, &mechSet) != GSS_S_COMPLETE) {
429*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
430*7c478bd9Sstevel@tonic-gate 				gettext("\nInternal error.  "
431*7c478bd9Sstevel@tonic-gate 					"GSS-API call failed."));
432*7c478bd9Sstevel@tonic-gate 			return (0);
433*7c478bd9Sstevel@tonic-gate 		}
434*7c478bd9Sstevel@tonic-gate 		numOfMechs = mechSet->count;
435*7c478bd9Sstevel@tonic-gate 	}
436*7c478bd9Sstevel@tonic-gate 	else
437*7c478bd9Sstevel@tonic-gate 		numOfMechs = 1;
438*7c478bd9Sstevel@tonic-gate 
439*7c478bd9Sstevel@tonic-gate 	/* now look through all the mechs searching */
440*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < numOfMechs; i++) {
441*7c478bd9Sstevel@tonic-gate 
442*7c478bd9Sstevel@tonic-gate 		if (mechOidStr == NULL) {
443*7c478bd9Sstevel@tonic-gate 			anOid = &mechSet->elements[i];
444*7c478bd9Sstevel@tonic-gate 			inBufDesc.length = anOid->length;
445*7c478bd9Sstevel@tonic-gate 			inBufDesc.value = anOid->elements;
446*7c478bd9Sstevel@tonic-gate 			outBufDesc.length = sizeof (hexMech);
447*7c478bd9Sstevel@tonic-gate 			outBufDesc.value = hexMech;
448*7c478bd9Sstevel@tonic-gate 
449*7c478bd9Sstevel@tonic-gate 			if (!gsscred_AsHex(&inBufDesc, &outBufDesc))
450*7c478bd9Sstevel@tonic-gate 				continue;
451*7c478bd9Sstevel@tonic-gate 		} else
452*7c478bd9Sstevel@tonic-gate 			anOid = userMechOid;
453*7c478bd9Sstevel@tonic-gate 
454*7c478bd9Sstevel@tonic-gate 		/* create a gss name */
455*7c478bd9Sstevel@tonic-gate 		if (!gsscred_MakeName(anOid, name, nameOidStr, &outBufDesc))
456*7c478bd9Sstevel@tonic-gate 			continue;
457*7c478bd9Sstevel@tonic-gate 
458*7c478bd9Sstevel@tonic-gate 		/* now convert it to hex, and find it */
459*7c478bd9Sstevel@tonic-gate 		searchName.value = hexName;
460*7c478bd9Sstevel@tonic-gate 		searchName.length = sizeof (hexName);
461*7c478bd9Sstevel@tonic-gate 		status = gsscred_AsHex(&outBufDesc, &searchName);
462*7c478bd9Sstevel@tonic-gate 		free(outBufDesc.value);
463*7c478bd9Sstevel@tonic-gate 
464*7c478bd9Sstevel@tonic-gate 		if (!status)
465*7c478bd9Sstevel@tonic-gate 			continue;
466*7c478bd9Sstevel@tonic-gate 
467*7c478bd9Sstevel@tonic-gate 		if (tableSource == GSSCRED_FLAT_FILE)
468*7c478bd9Sstevel@tonic-gate 			file_getGssCredEntry(&searchName, uidStr, &errDetails);
469*7c478bd9Sstevel@tonic-gate 		else {
470*7c478bd9Sstevel@tonic-gate 			entryPtr = NULL;  /* other backends coming soon */
471*7c478bd9Sstevel@tonic-gate 			while (entryPtr != NULL) {
472*7c478bd9Sstevel@tonic-gate 				fprintf(stdout, "\n%s\t%d\t%s",
473*7c478bd9Sstevel@tonic-gate 					entryPtr->principal_name,
474*7c478bd9Sstevel@tonic-gate 					entryPtr->unix_uid, entryPtr->comment);
475*7c478bd9Sstevel@tonic-gate 				free(entryPtr->principal_name);
476*7c478bd9Sstevel@tonic-gate 				free(entryPtr->comment);
477*7c478bd9Sstevel@tonic-gate 				entryTmpPtr = entryPtr->next;
478*7c478bd9Sstevel@tonic-gate 				free(entryPtr);
479*7c478bd9Sstevel@tonic-gate 				entryPtr = entryTmpPtr;
480*7c478bd9Sstevel@tonic-gate 			}
481*7c478bd9Sstevel@tonic-gate 		}
482*7c478bd9Sstevel@tonic-gate 
483*7c478bd9Sstevel@tonic-gate 		/* any errors to display */
484*7c478bd9Sstevel@tonic-gate 		if (errDetails) {
485*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
486*7c478bd9Sstevel@tonic-gate 				gettext("\nError searching gsscred table "
487*7c478bd9Sstevel@tonic-gate 					"[%s]."),
488*7c478bd9Sstevel@tonic-gate 				errDetails);
489*7c478bd9Sstevel@tonic-gate 			free(errDetails);
490*7c478bd9Sstevel@tonic-gate 			errDetails = NULL;
491*7c478bd9Sstevel@tonic-gate 			status = 0;
492*7c478bd9Sstevel@tonic-gate 		}
493*7c478bd9Sstevel@tonic-gate 	}	/* for */
494*7c478bd9Sstevel@tonic-gate 
495*7c478bd9Sstevel@tonic-gate cleanup:
496*7c478bd9Sstevel@tonic-gate 	if (mechSet != NULL)
497*7c478bd9Sstevel@tonic-gate 		gss_release_oid_set(&minor, &mechSet);
498*7c478bd9Sstevel@tonic-gate 
499*7c478bd9Sstevel@tonic-gate 	return (status);
500*7c478bd9Sstevel@tonic-gate }  /* listUsers */
501*7c478bd9Sstevel@tonic-gate 
502*7c478bd9Sstevel@tonic-gate /*
503*7c478bd9Sstevel@tonic-gate  * Performs additional handling while searching for users
504*7c478bd9Sstevel@tonic-gate  * stored in the flat file table.
505*7c478bd9Sstevel@tonic-gate  */
506*7c478bd9Sstevel@tonic-gate int
file_listUsers(const gss_OID mechOid,const char * unixUid,char ** errDetails)507*7c478bd9Sstevel@tonic-gate file_listUsers(const gss_OID mechOid, const char *unixUid,
508*7c478bd9Sstevel@tonic-gate 		char **errDetails)
509*7c478bd9Sstevel@tonic-gate {
510*7c478bd9Sstevel@tonic-gate 	gss_buffer_desc mechBufDesc = GSS_C_EMPTY_BUFFER,
511*7c478bd9Sstevel@tonic-gate 		mechHexBufDesc = GSS_C_EMPTY_BUFFER;
512*7c478bd9Sstevel@tonic-gate 	char mechBuf[128], mechHexBuf[256];
513*7c478bd9Sstevel@tonic-gate 
514*7c478bd9Sstevel@tonic-gate 	if (mechOid != NULL) {
515*7c478bd9Sstevel@tonic-gate 		/* must make the name header whic contains mech oid */
516*7c478bd9Sstevel@tonic-gate 		mechBufDesc.value = (void *) mechBuf;
517*7c478bd9Sstevel@tonic-gate 		mechBufDesc.length = sizeof (mechBuf);
518*7c478bd9Sstevel@tonic-gate 		mechHexBufDesc.value = (void*) mechHexBuf;
519*7c478bd9Sstevel@tonic-gate 		mechHexBufDesc.length = sizeof (mechHexBuf);
520*7c478bd9Sstevel@tonic-gate 
521*7c478bd9Sstevel@tonic-gate 		if ((!gsscred_MakeNameHeader(mechOid, &mechBufDesc)) ||
522*7c478bd9Sstevel@tonic-gate 			(!gsscred_AsHex(&mechBufDesc, &mechHexBufDesc))) {
523*7c478bd9Sstevel@tonic-gate 			(*errDetails) = strdup(
524*7c478bd9Sstevel@tonic-gate 					gettext("\nInternal error. "
525*7c478bd9Sstevel@tonic-gate 					" Conversion to hex failed."));
526*7c478bd9Sstevel@tonic-gate 			return (0);
527*7c478bd9Sstevel@tonic-gate 		}
528*7c478bd9Sstevel@tonic-gate 
529*7c478bd9Sstevel@tonic-gate 		return (file_getGssCredEntry(&mechHexBufDesc,
530*7c478bd9Sstevel@tonic-gate 				unixUid, errDetails));
531*7c478bd9Sstevel@tonic-gate 	}
532*7c478bd9Sstevel@tonic-gate 
533*7c478bd9Sstevel@tonic-gate 	return (file_getGssCredEntry(NULL, unixUid, errDetails));
534*7c478bd9Sstevel@tonic-gate }  /* file_listUsers */
535*7c478bd9Sstevel@tonic-gate 
536*7c478bd9Sstevel@tonic-gate 
537*7c478bd9Sstevel@tonic-gate /*
538*7c478bd9Sstevel@tonic-gate  *  Handles the deletion of users.
539*7c478bd9Sstevel@tonic-gate  */
removeUsers(const char * name,const char * nameOidStr,const char * uidStr,const char * mechOidStr)540*7c478bd9Sstevel@tonic-gate static int removeUsers(const char *name, const char *nameOidStr,
541*7c478bd9Sstevel@tonic-gate 		const char *uidStr, const char *mechOidStr)
542*7c478bd9Sstevel@tonic-gate {
543*7c478bd9Sstevel@tonic-gate 	char hexMech[256],
544*7c478bd9Sstevel@tonic-gate 		hexName[(MAX_STR_LEN *2) + 1],
545*7c478bd9Sstevel@tonic-gate 		*errDetails = NULL;
546*7c478bd9Sstevel@tonic-gate 	gss_OID anOid = NULL, userMechOid = NULL;
547*7c478bd9Sstevel@tonic-gate 	gss_OID_set mechSet = NULL;
548*7c478bd9Sstevel@tonic-gate 	gss_buffer_desc inBufDesc = GSS_C_EMPTY_BUFFER,
549*7c478bd9Sstevel@tonic-gate 		outBufDesc = GSS_C_EMPTY_BUFFER,
550*7c478bd9Sstevel@tonic-gate 		searchName = GSS_C_EMPTY_BUFFER;
551*7c478bd9Sstevel@tonic-gate 	int status = 0, numOfMechs, i;
552*7c478bd9Sstevel@tonic-gate 	OM_uint32 minor;
553*7c478bd9Sstevel@tonic-gate 
554*7c478bd9Sstevel@tonic-gate 
555*7c478bd9Sstevel@tonic-gate 	/* user deletion can only be performed by super user */
556*7c478bd9Sstevel@tonic-gate 	if (getuid()) {
557*7c478bd9Sstevel@tonic-gate 
558*7c478bd9Sstevel@tonic-gate 		fprintf(stderr,
559*7c478bd9Sstevel@tonic-gate 			gettext("\nUser deletion requires"
560*7c478bd9Sstevel@tonic-gate 				" root privileges."));
561*7c478bd9Sstevel@tonic-gate 		return (0);
562*7c478bd9Sstevel@tonic-gate 	}
563*7c478bd9Sstevel@tonic-gate 
564*7c478bd9Sstevel@tonic-gate 	/* do we need to convert the mechanism oid? */
565*7c478bd9Sstevel@tonic-gate 	if (mechOidStr != NULL) {
566*7c478bd9Sstevel@tonic-gate 		if (__gss_mech_to_oid(mechOidStr, &userMechOid) !=
567*7c478bd9Sstevel@tonic-gate 		GSS_S_COMPLETE) {
568*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
569*7c478bd9Sstevel@tonic-gate 				gettext("\nInvalid mechanism specified [%s]."),
570*7c478bd9Sstevel@tonic-gate 				mechOidStr);
571*7c478bd9Sstevel@tonic-gate 			return (0);
572*7c478bd9Sstevel@tonic-gate 		}
573*7c478bd9Sstevel@tonic-gate 
574*7c478bd9Sstevel@tonic-gate 		inBufDesc.length = userMechOid->length;
575*7c478bd9Sstevel@tonic-gate 		inBufDesc.value = userMechOid->elements;
576*7c478bd9Sstevel@tonic-gate 		outBufDesc.length = sizeof (hexMech);
577*7c478bd9Sstevel@tonic-gate 		outBufDesc.value = hexMech;
578*7c478bd9Sstevel@tonic-gate 
579*7c478bd9Sstevel@tonic-gate 		if (!gsscred_AsHex(&inBufDesc, &outBufDesc)) {
580*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
581*7c478bd9Sstevel@tonic-gate 				gettext("\nInternal error."
582*7c478bd9Sstevel@tonic-gate 					"  Conversion to hex failed."));
583*7c478bd9Sstevel@tonic-gate 			status = 0;
584*7c478bd9Sstevel@tonic-gate 			goto cleanup;
585*7c478bd9Sstevel@tonic-gate 		}
586*7c478bd9Sstevel@tonic-gate 
587*7c478bd9Sstevel@tonic-gate 	}	 /* mechOidStr != NULL */
588*7c478bd9Sstevel@tonic-gate 
589*7c478bd9Sstevel@tonic-gate 	/* are we deleting the entire table or an entire mech ? */
590*7c478bd9Sstevel@tonic-gate 	if (name == NULL && uidStr == NULL) {
591*7c478bd9Sstevel@tonic-gate 
592*7c478bd9Sstevel@tonic-gate 		if (tableSource == GSSCRED_FLAT_FILE)
593*7c478bd9Sstevel@tonic-gate 			status = file_removeUsers(userMechOid,
594*7c478bd9Sstevel@tonic-gate 					NULL, &errDetails);
595*7c478bd9Sstevel@tonic-gate 		else
596*7c478bd9Sstevel@tonic-gate 			status = 0;
597*7c478bd9Sstevel@tonic-gate 
598*7c478bd9Sstevel@tonic-gate 		/* display any errors */
599*7c478bd9Sstevel@tonic-gate 		if (errDetails) {
600*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
601*7c478bd9Sstevel@tonic-gate 				gettext("\nError deleting gsscred entry "
602*7c478bd9Sstevel@tonic-gate 					"[%s]."),
603*7c478bd9Sstevel@tonic-gate 				errDetails);
604*7c478bd9Sstevel@tonic-gate 			free(errDetails);
605*7c478bd9Sstevel@tonic-gate 			errDetails = NULL;
606*7c478bd9Sstevel@tonic-gate 		}
607*7c478bd9Sstevel@tonic-gate 		goto cleanup;
608*7c478bd9Sstevel@tonic-gate 	}
609*7c478bd9Sstevel@tonic-gate 
610*7c478bd9Sstevel@tonic-gate 	/* are we deleting by uid or uid and mech? */
611*7c478bd9Sstevel@tonic-gate 	if (name == NULL && uidStr != NULL) {
612*7c478bd9Sstevel@tonic-gate 
613*7c478bd9Sstevel@tonic-gate 		if (tableSource == GSSCRED_FLAT_FILE)
614*7c478bd9Sstevel@tonic-gate 			status = file_removeUsers(userMechOid, uidStr,
615*7c478bd9Sstevel@tonic-gate 						&errDetails);
616*7c478bd9Sstevel@tonic-gate 		else
617*7c478bd9Sstevel@tonic-gate 			status = 0;
618*7c478bd9Sstevel@tonic-gate 
619*7c478bd9Sstevel@tonic-gate 		/* check for any errors */
620*7c478bd9Sstevel@tonic-gate 		if (errDetails) {
621*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
622*7c478bd9Sstevel@tonic-gate 				gettext("\nError deleting gsscred entry "
623*7c478bd9Sstevel@tonic-gate 					"[%s]."),
624*7c478bd9Sstevel@tonic-gate 				errDetails);
625*7c478bd9Sstevel@tonic-gate 			free(errDetails);
626*7c478bd9Sstevel@tonic-gate 			errDetails = NULL;
627*7c478bd9Sstevel@tonic-gate 		}
628*7c478bd9Sstevel@tonic-gate 		goto cleanup;
629*7c478bd9Sstevel@tonic-gate 	}
630*7c478bd9Sstevel@tonic-gate 
631*7c478bd9Sstevel@tonic-gate 	/*
632*7c478bd9Sstevel@tonic-gate 	 * We are deleting by name;
633*7c478bd9Sstevel@tonic-gate 	 * how many mechs must we check?
634*7c478bd9Sstevel@tonic-gate 	 */
635*7c478bd9Sstevel@tonic-gate 	if (mechOidStr == NULL) {
636*7c478bd9Sstevel@tonic-gate 
637*7c478bd9Sstevel@tonic-gate 		if (gss_indicate_mechs(&minor, &mechSet) != GSS_S_COMPLETE) {
638*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
639*7c478bd9Sstevel@tonic-gate 				gettext("\nInternal error.  "
640*7c478bd9Sstevel@tonic-gate 					"GSS-API call failed."));
641*7c478bd9Sstevel@tonic-gate 			status = 0;
642*7c478bd9Sstevel@tonic-gate 			goto cleanup;
643*7c478bd9Sstevel@tonic-gate 		}
644*7c478bd9Sstevel@tonic-gate 		numOfMechs = mechSet->count;
645*7c478bd9Sstevel@tonic-gate 	}
646*7c478bd9Sstevel@tonic-gate 	else
647*7c478bd9Sstevel@tonic-gate 		numOfMechs = 1;
648*7c478bd9Sstevel@tonic-gate 
649*7c478bd9Sstevel@tonic-gate 	/* now look through all the mechs, deleting */
650*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < numOfMechs; i++) {
651*7c478bd9Sstevel@tonic-gate 
652*7c478bd9Sstevel@tonic-gate 		if (mechOidStr == NULL) {
653*7c478bd9Sstevel@tonic-gate 			anOid = &mechSet->elements[i];
654*7c478bd9Sstevel@tonic-gate 			inBufDesc.length = anOid->length;
655*7c478bd9Sstevel@tonic-gate 			inBufDesc.value = anOid->elements;
656*7c478bd9Sstevel@tonic-gate 			outBufDesc.length = sizeof (hexMech);
657*7c478bd9Sstevel@tonic-gate 			outBufDesc.value = hexMech;
658*7c478bd9Sstevel@tonic-gate 			if (!gsscred_AsHex(&inBufDesc, &outBufDesc))
659*7c478bd9Sstevel@tonic-gate 				continue;
660*7c478bd9Sstevel@tonic-gate 		} else
661*7c478bd9Sstevel@tonic-gate 			anOid = userMechOid;
662*7c478bd9Sstevel@tonic-gate 
663*7c478bd9Sstevel@tonic-gate 		/* create a gss name */
664*7c478bd9Sstevel@tonic-gate 		if (!gsscred_MakeName(anOid, name, nameOidStr, &outBufDesc))
665*7c478bd9Sstevel@tonic-gate 			continue;
666*7c478bd9Sstevel@tonic-gate 
667*7c478bd9Sstevel@tonic-gate 		/* now convert it to hex, and delete it */
668*7c478bd9Sstevel@tonic-gate 		searchName.value = hexName;
669*7c478bd9Sstevel@tonic-gate 		searchName.length = sizeof (hexName);
670*7c478bd9Sstevel@tonic-gate 		status = gsscred_AsHex(&outBufDesc, &searchName);
671*7c478bd9Sstevel@tonic-gate 		free(outBufDesc.value);
672*7c478bd9Sstevel@tonic-gate 
673*7c478bd9Sstevel@tonic-gate 		if (!status)
674*7c478bd9Sstevel@tonic-gate 			continue;
675*7c478bd9Sstevel@tonic-gate 
676*7c478bd9Sstevel@tonic-gate 		if (tableSource == GSSCRED_FLAT_FILE)
677*7c478bd9Sstevel@tonic-gate 			status = file_deleteGssCredEntry(&searchName,
678*7c478bd9Sstevel@tonic-gate 					uidStr, &errDetails);
679*7c478bd9Sstevel@tonic-gate 		else
680*7c478bd9Sstevel@tonic-gate 			status = 0;
681*7c478bd9Sstevel@tonic-gate 
682*7c478bd9Sstevel@tonic-gate 		/* check for any errors */
683*7c478bd9Sstevel@tonic-gate 		if (errDetails) {
684*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
685*7c478bd9Sstevel@tonic-gate 				gettext("\nError deleting gsscred entry"
686*7c478bd9Sstevel@tonic-gate 					" [%s]."),
687*7c478bd9Sstevel@tonic-gate 				errDetails);
688*7c478bd9Sstevel@tonic-gate 			free(errDetails);
689*7c478bd9Sstevel@tonic-gate 			errDetails = NULL;
690*7c478bd9Sstevel@tonic-gate 		}
691*7c478bd9Sstevel@tonic-gate 	}	 /* for */
692*7c478bd9Sstevel@tonic-gate 
693*7c478bd9Sstevel@tonic-gate cleanup:
694*7c478bd9Sstevel@tonic-gate 	if (mechSet != NULL)
695*7c478bd9Sstevel@tonic-gate 		gss_release_oid_set(&minor, &mechSet);
696*7c478bd9Sstevel@tonic-gate 
697*7c478bd9Sstevel@tonic-gate 	return (status);
698*7c478bd9Sstevel@tonic-gate }  /* removeUsers */
699*7c478bd9Sstevel@tonic-gate 
700*7c478bd9Sstevel@tonic-gate 
701*7c478bd9Sstevel@tonic-gate /*
702*7c478bd9Sstevel@tonic-gate  * Performs additional handling while deleting users
703*7c478bd9Sstevel@tonic-gate  * stored in the flat file table.
704*7c478bd9Sstevel@tonic-gate  */
file_removeUsers(const gss_OID mechOid,const char * unixUid,char ** errDetails)705*7c478bd9Sstevel@tonic-gate int file_removeUsers(const gss_OID mechOid, const char *unixUid,
706*7c478bd9Sstevel@tonic-gate 		char **errDetails)
707*7c478bd9Sstevel@tonic-gate {
708*7c478bd9Sstevel@tonic-gate 	gss_buffer_desc mechBufDesc = GSS_C_EMPTY_BUFFER,
709*7c478bd9Sstevel@tonic-gate 		mechHexBufDesc = GSS_C_EMPTY_BUFFER;
710*7c478bd9Sstevel@tonic-gate 	char mechBuf[128], mechHexBuf[256];
711*7c478bd9Sstevel@tonic-gate 
712*7c478bd9Sstevel@tonic-gate 	if (mechOid != NULL) {
713*7c478bd9Sstevel@tonic-gate 		/*
714*7c478bd9Sstevel@tonic-gate 		 * need to create the buffer header which contains
715*7c478bd9Sstevel@tonic-gate 		 * the mechanism oid.
716*7c478bd9Sstevel@tonic-gate 		 */
717*7c478bd9Sstevel@tonic-gate 		mechBufDesc.value = (void*) mechBuf;
718*7c478bd9Sstevel@tonic-gate 		mechBufDesc.length = sizeof (mechBuf);
719*7c478bd9Sstevel@tonic-gate 		mechHexBufDesc.value = (void *) mechHexBuf;
720*7c478bd9Sstevel@tonic-gate 		mechHexBufDesc.length = sizeof (mechHexBuf);
721*7c478bd9Sstevel@tonic-gate 
722*7c478bd9Sstevel@tonic-gate 		if ((!gsscred_MakeNameHeader(mechOid, &mechBufDesc)) ||
723*7c478bd9Sstevel@tonic-gate 		    (!gsscred_AsHex(&mechBufDesc, &mechHexBufDesc))) {
724*7c478bd9Sstevel@tonic-gate 			(*errDetails) = strdup(
725*7c478bd9Sstevel@tonic-gate 				gettext("\nInternal error."
726*7c478bd9Sstevel@tonic-gate 					"  Conversion to hex failed."));
727*7c478bd9Sstevel@tonic-gate 			return (0);
728*7c478bd9Sstevel@tonic-gate 		}
729*7c478bd9Sstevel@tonic-gate 
730*7c478bd9Sstevel@tonic-gate 		return (file_deleteGssCredEntry(&mechHexBufDesc, unixUid,
731*7c478bd9Sstevel@tonic-gate 						errDetails));
732*7c478bd9Sstevel@tonic-gate 	}
733*7c478bd9Sstevel@tonic-gate 
734*7c478bd9Sstevel@tonic-gate 	return (file_deleteGssCredEntry(NULL, unixUid, errDetails));
735*7c478bd9Sstevel@tonic-gate }  /* file_removeUsers */
736*7c478bd9Sstevel@tonic-gate 
737*7c478bd9Sstevel@tonic-gate 
738*7c478bd9Sstevel@tonic-gate /*
739*7c478bd9Sstevel@tonic-gate  * Prints the usage string, and terminates.
740*7c478bd9Sstevel@tonic-gate  */
usage(void)741*7c478bd9Sstevel@tonic-gate static void usage(void)
742*7c478bd9Sstevel@tonic-gate {
743*7c478bd9Sstevel@tonic-gate 
744*7c478bd9Sstevel@tonic-gate 	fprintf(stderr,
745*7c478bd9Sstevel@tonic-gate 		gettext("\nUsage:\t %s [-n user [-o oid] [-u uid]]"
746*7c478bd9Sstevel@tonic-gate 			" [-c comment] -m mech -a"
747*7c478bd9Sstevel@tonic-gate 			"\n\t %s [-n user [-o oid]] [-u uid] [-m mech] -r"
748*7c478bd9Sstevel@tonic-gate 			"\n\t %s [-n user [-o oid]] [-u uid] [-m mech] -l\n"),
749*7c478bd9Sstevel@tonic-gate 		PROG_NAME, PROG_NAME, PROG_NAME);
750*7c478bd9Sstevel@tonic-gate 	exit(1);
751*7c478bd9Sstevel@tonic-gate }  /* usage */
752