xref: /illumos-gate/usr/src/cmd/oamuser/user/userdel.c (revision 7fc68ddf)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5c651b32eSJohn Sonnenschein  * Common Development and Distribution License (the "License").
6c651b32eSJohn Sonnenschein  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
227c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
26c651b32eSJohn Sonnenschein  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
277c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/stat.h>
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <ctype.h>
347c478bd9Sstevel@tonic-gate #include <limits.h>
357c478bd9Sstevel@tonic-gate #include <pwd.h>
367c478bd9Sstevel@tonic-gate #include <project.h>
377c478bd9Sstevel@tonic-gate #include <string.h>
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <sys/stat.h>
407c478bd9Sstevel@tonic-gate #include <userdefs.h>
417c478bd9Sstevel@tonic-gate #include <stdlib.h>
427c478bd9Sstevel@tonic-gate #include <errno.h>
43ace1a5f1Sdp #include <unistd.h>
44ace1a5f1Sdp #include <strings.h>
457c478bd9Sstevel@tonic-gate #include "users.h"
467c478bd9Sstevel@tonic-gate #include "messages.h"
477c478bd9Sstevel@tonic-gate #include "funcs.h"
487c478bd9Sstevel@tonic-gate 
49*7fc68ddfSAlbert Lee /*
50*7fc68ddfSAlbert Lee  *  userdel [-r ] login
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  *	This command deletes user logins.  Arguments are:
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  *	-r - when given, this option removes home directory & its contents
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  *	login - a string of printable chars except colon (:)
57*7fc68ddfSAlbert Lee  */
587c478bd9Sstevel@tonic-gate 
59*7fc68ddfSAlbert Lee extern int check_perm(), isbusy(), get_default_zfs_flags();
607c478bd9Sstevel@tonic-gate extern int rm_files(), call_passmgmt(), edit_group();
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate static char *logname;			/* login name to delete */
637c478bd9Sstevel@tonic-gate static char *nargv[20];		/* arguments for execvp of passmgmt */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate char *cmdname;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)687c478bd9Sstevel@tonic-gate main(int argc, char **argv)
697c478bd9Sstevel@tonic-gate {
70*7fc68ddfSAlbert Lee 	int ch, ret = 0, rflag = 0;
71*7fc68ddfSAlbert Lee 	int zfs_flags = 0, argindex, tries;
727c478bd9Sstevel@tonic-gate 	struct passwd *pstruct;
737c478bd9Sstevel@tonic-gate 	struct stat statbuf;
747c478bd9Sstevel@tonic-gate #ifndef att
757c478bd9Sstevel@tonic-gate 	FILE *pwf;		/* fille ptr for opened passwd file */
767c478bd9Sstevel@tonic-gate #endif
777c478bd9Sstevel@tonic-gate 	char *usertype = NULL;
787c478bd9Sstevel@tonic-gate 	int rc;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	cmdname = argv[0];
817c478bd9Sstevel@tonic-gate 
82*7fc68ddfSAlbert Lee 	if (geteuid() != 0) {
83*7fc68ddfSAlbert Lee 		errmsg(M_PERM_DENIED);
84*7fc68ddfSAlbert Lee 		exit(EX_NO_PERM);
857c478bd9Sstevel@tonic-gate 	}
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	opterr = 0;			/* no print errors from getopt */
887c478bd9Sstevel@tonic-gate 	usertype = getusertype(argv[0]);
89*7fc68ddfSAlbert Lee 
90*7fc68ddfSAlbert Lee 	while ((ch = getopt(argc, argv, "r")) != EOF) {
91*7fc68ddfSAlbert Lee 		switch (ch) {
927c478bd9Sstevel@tonic-gate 			case 'r':
937c478bd9Sstevel@tonic-gate 				rflag++;
947c478bd9Sstevel@tonic-gate 				break;
957c478bd9Sstevel@tonic-gate 			case '?':
967c478bd9Sstevel@tonic-gate 				if (is_role(usertype))
97*7fc68ddfSAlbert Lee 					errmsg(M_DRUSAGE);
987c478bd9Sstevel@tonic-gate 				else
99*7fc68ddfSAlbert Lee 					errmsg(M_DUSAGE);
100*7fc68ddfSAlbert Lee 				exit(EX_SYNTAX);
1017c478bd9Sstevel@tonic-gate 		}
1027c478bd9Sstevel@tonic-gate 	}
1037c478bd9Sstevel@tonic-gate 
104*7fc68ddfSAlbert Lee 	if (optind != argc - 1) {
1057c478bd9Sstevel@tonic-gate 		if (is_role(usertype))
106*7fc68ddfSAlbert Lee 			errmsg(M_DRUSAGE);
1077c478bd9Sstevel@tonic-gate 		else
108*7fc68ddfSAlbert Lee 			errmsg(M_DUSAGE);
109*7fc68ddfSAlbert Lee 		exit(EX_SYNTAX);
1107c478bd9Sstevel@tonic-gate 	}
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	logname = argv[optind];
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate #ifdef att
1157c478bd9Sstevel@tonic-gate 	pstruct = getpwnam(logname);
1167c478bd9Sstevel@tonic-gate #else
1177c478bd9Sstevel@tonic-gate 	/*
1187c478bd9Sstevel@tonic-gate 	 * Do this with fgetpwent to make sure we are only looking on local
1197c478bd9Sstevel@tonic-gate 	 * system (since passmgmt only works on local system).
1207c478bd9Sstevel@tonic-gate 	 */
1217c478bd9Sstevel@tonic-gate 	if ((pwf = fopen("/etc/passwd", "r")) == NULL) {
122*7fc68ddfSAlbert Lee 		errmsg(M_OOPS, "open", "/etc/passwd");
1237c478bd9Sstevel@tonic-gate 		exit(EX_FAILURE);
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 	while ((pstruct = fgetpwent(pwf)) != NULL)
1267c478bd9Sstevel@tonic-gate 		if (strcmp(pstruct->pw_name, logname) == 0)
1277c478bd9Sstevel@tonic-gate 			break;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	fclose(pwf);
1307c478bd9Sstevel@tonic-gate #endif
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	if (pstruct == NULL) {
133*7fc68ddfSAlbert Lee 		errmsg(M_EXIST, logname);
134*7fc68ddfSAlbert Lee 		exit(EX_NAME_NOT_EXIST);
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 
137*7fc68ddfSAlbert Lee 	if (isbusy(logname)) {
138*7fc68ddfSAlbert Lee 		errmsg(M_BUSY, logname, "remove");
139*7fc68ddfSAlbert Lee 		exit(EX_BUSY);
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	/* that's it for validations - now do the work */
1437c478bd9Sstevel@tonic-gate 	/* set up arguments to  passmgmt in nargv array */
144c651b32eSJohn Sonnenschein 	nargv[0] = PASSMGMT;
1457c478bd9Sstevel@tonic-gate 	nargv[1] = "-d";	/* delete */
1467c478bd9Sstevel@tonic-gate 	argindex = 2;		/* next argument */
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	/* finally - login name */
1497c478bd9Sstevel@tonic-gate 	nargv[argindex++] = logname;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	/* set the last to null */
1527c478bd9Sstevel@tonic-gate 	nargv[argindex++] = NULL;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	/* remove home directory */
155*7fc68ddfSAlbert Lee 	if (rflag) {
1567c478bd9Sstevel@tonic-gate 		/* Check Permissions */
157*7fc68ddfSAlbert Lee 		if (stat(pstruct->pw_dir, &statbuf)) {
158*7fc68ddfSAlbert Lee 			errmsg(M_OOPS, "find status about home directory",
159ace1a5f1Sdp 			    strerror(errno));
160*7fc68ddfSAlbert Lee 			exit(EX_HOMEDIR);
1617c478bd9Sstevel@tonic-gate 		}
162*7fc68ddfSAlbert Lee 
163*7fc68ddfSAlbert Lee 		if (check_perm(statbuf, pstruct->pw_uid, pstruct->pw_gid,
164*7fc68ddfSAlbert Lee 		    S_IWOTH|S_IXOTH) != 0) {
165*7fc68ddfSAlbert Lee 			errmsg(M_NO_PERM, logname, pstruct->pw_dir);
166*7fc68ddfSAlbert Lee 			exit(EX_HOMEDIR);
1677c478bd9Sstevel@tonic-gate 		}
168*7fc68ddfSAlbert Lee 		zfs_flags = get_default_zfs_flags();
1697c478bd9Sstevel@tonic-gate 
170*7fc68ddfSAlbert Lee 		if (rm_files(pstruct->pw_dir, logname, zfs_flags) != EX_SUCCESS)
171*7fc68ddfSAlbert Lee 			exit(EX_HOMEDIR);
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	/* now call passmgmt */
1757c478bd9Sstevel@tonic-gate 	ret = PEX_FAILED;
176*7fc68ddfSAlbert Lee 	for (tries = 3; ret != PEX_SUCCESS && tries--; ) {
177*7fc68ddfSAlbert Lee 		switch (ret = call_passmgmt(nargv)) {
1787c478bd9Sstevel@tonic-gate 		case PEX_SUCCESS:
179*7fc68ddfSAlbert Lee 			ret = edit_group(logname, (char *)0, (int **)0, 1);
180*7fc68ddfSAlbert Lee 			if (ret != EX_SUCCESS)
181*7fc68ddfSAlbert Lee 				errmsg(M_UPDATE, "deleted");
1827c478bd9Sstevel@tonic-gate 			break;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 		case PEX_BUSY:
1857c478bd9Sstevel@tonic-gate 			break;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 		case PEX_HOSED_FILES:
188*7fc68ddfSAlbert Lee 			errmsg(M_HOSED_FILES);
189*7fc68ddfSAlbert Lee 			exit(EX_INCONSISTENT);
1907c478bd9Sstevel@tonic-gate 			break;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 		case PEX_SYNTAX:
1937c478bd9Sstevel@tonic-gate 		case PEX_BADARG:
1947c478bd9Sstevel@tonic-gate 			/* should NEVER occur that passmgmt usage is wrong */
1957c478bd9Sstevel@tonic-gate 			if (is_role(usertype))
196*7fc68ddfSAlbert Lee 				errmsg(M_DRUSAGE);
1977c478bd9Sstevel@tonic-gate 			else
198*7fc68ddfSAlbert Lee 				errmsg(M_DUSAGE);
199*7fc68ddfSAlbert Lee 			exit(EX_SYNTAX);
2007c478bd9Sstevel@tonic-gate 			break;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 		case PEX_BADUID:
203*7fc68ddfSAlbert Lee 		/*
204*7fc68ddfSAlbert Lee 		 * uid is used - shouldn't happen but print message anyway
205*7fc68ddfSAlbert Lee 		 */
206*7fc68ddfSAlbert Lee 			errmsg(M_UID_USED, pstruct->pw_uid);
207*7fc68ddfSAlbert Lee 			exit(EX_ID_EXISTS);
2087c478bd9Sstevel@tonic-gate 			break;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 		case PEX_BADNAME:
2117c478bd9Sstevel@tonic-gate 			/* invalid loname */
212*7fc68ddfSAlbert Lee 			errmsg(M_USED, logname);
213*7fc68ddfSAlbert Lee 			exit(EX_NAME_EXISTS);
2147c478bd9Sstevel@tonic-gate 			break;
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 		default:
217*7fc68ddfSAlbert Lee 			errmsg(M_UPDATE, "deleted");
218*7fc68ddfSAlbert Lee 			exit(ret);
2197c478bd9Sstevel@tonic-gate 			break;
2207c478bd9Sstevel@tonic-gate 		}
2217c478bd9Sstevel@tonic-gate 	}
222*7fc68ddfSAlbert Lee 	if (tries == 0)
223*7fc68ddfSAlbert Lee 		errmsg(M_UPDATE, "deleted");
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /*
2267c478bd9Sstevel@tonic-gate  * Now, remove this user from all project entries
2277c478bd9Sstevel@tonic-gate  */
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	rc = edit_project(logname, (char *)0, (projid_t **)0, 1);
2307c478bd9Sstevel@tonic-gate 	if (rc != EX_SUCCESS) {
2317c478bd9Sstevel@tonic-gate 		errmsg(M_UPDATE, "modified");
2327c478bd9Sstevel@tonic-gate 		exit(rc);
2337c478bd9Sstevel@tonic-gate 	}
234*7fc68ddfSAlbert Lee 
235*7fc68ddfSAlbert Lee 	exit(ret);
2367c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
2377c478bd9Sstevel@tonic-gate }
238