xref: /titanic_54/usr/src/cmd/oamuser/user/userdel.c (revision ace1a5f11236a072fca1b5e0ea1416a083a9f2aa)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
237c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
27*ace1a5f1Sdp  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
287c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/stat.h>
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <ctype.h>
377c478bd9Sstevel@tonic-gate #include <limits.h>
387c478bd9Sstevel@tonic-gate #include <pwd.h>
397c478bd9Sstevel@tonic-gate #include <project.h>
407c478bd9Sstevel@tonic-gate #include <string.h>
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <sys/stat.h>
437c478bd9Sstevel@tonic-gate #include <userdefs.h>
447c478bd9Sstevel@tonic-gate #include <stdlib.h>
457c478bd9Sstevel@tonic-gate #include <errno.h>
46*ace1a5f1Sdp #include <unistd.h>
47*ace1a5f1Sdp #include <strings.h>
487c478bd9Sstevel@tonic-gate #include "users.h"
497c478bd9Sstevel@tonic-gate #include "messages.h"
507c478bd9Sstevel@tonic-gate #include "funcs.h"
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*******************************************************************************
537c478bd9Sstevel@tonic-gate  *  userdel [-r] login
547c478bd9Sstevel@tonic-gate  *
557c478bd9Sstevel@tonic-gate  *	This command deletes user logins.  Arguments are:
567c478bd9Sstevel@tonic-gate  *
577c478bd9Sstevel@tonic-gate  *	-r - when given, this option removes home directory & its contents
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  *	login - a string of printable chars except colon (:)
607c478bd9Sstevel@tonic-gate  ******************************************************************************/
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate extern int check_perm(), isbusy();
637c478bd9Sstevel@tonic-gate extern int rm_files(), call_passmgmt(), edit_group();
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static char *logname;			/* login name to delete */
667c478bd9Sstevel@tonic-gate static char *nargv[20];		/* arguments for execvp of passmgmt */
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate char *cmdname;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate int
717c478bd9Sstevel@tonic-gate main(int argc, char **argv)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate 	int ch, ret = 0, rflag = 0, argindex, tries;
747c478bd9Sstevel@tonic-gate 	struct passwd *pstruct;
757c478bd9Sstevel@tonic-gate 	struct stat statbuf;
767c478bd9Sstevel@tonic-gate #ifndef att
777c478bd9Sstevel@tonic-gate 	FILE *pwf;		/* fille ptr for opened passwd file */
787c478bd9Sstevel@tonic-gate #endif
797c478bd9Sstevel@tonic-gate 	char *usertype = NULL;
807c478bd9Sstevel@tonic-gate 	int rc;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	cmdname = argv[0];
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	if( geteuid() != 0 ) {
857c478bd9Sstevel@tonic-gate 		errmsg( M_PERM_DENIED );
867c478bd9Sstevel@tonic-gate 		exit( EX_NO_PERM );
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	opterr = 0;			/* no print errors from getopt */
907c478bd9Sstevel@tonic-gate 	usertype = getusertype(argv[0]);
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	while( (ch = getopt(argc, argv, "r")) != EOF ) {
937c478bd9Sstevel@tonic-gate 		switch(ch) {
947c478bd9Sstevel@tonic-gate 			case 'r':
957c478bd9Sstevel@tonic-gate 				rflag++;
967c478bd9Sstevel@tonic-gate 				break;
977c478bd9Sstevel@tonic-gate 			case '?':
987c478bd9Sstevel@tonic-gate 				if (is_role(usertype))
997c478bd9Sstevel@tonic-gate 					errmsg( M_DRUSAGE );
1007c478bd9Sstevel@tonic-gate 				else
1017c478bd9Sstevel@tonic-gate 					errmsg( M_DUSAGE );
1027c478bd9Sstevel@tonic-gate 				exit( EX_SYNTAX );
1037c478bd9Sstevel@tonic-gate 		}
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	if( optind != argc - 1 ) {
1077c478bd9Sstevel@tonic-gate 		if (is_role(usertype))
1087c478bd9Sstevel@tonic-gate 			errmsg( M_DRUSAGE );
1097c478bd9Sstevel@tonic-gate 		else
1107c478bd9Sstevel@tonic-gate 			errmsg( M_DUSAGE );
1117c478bd9Sstevel@tonic-gate 		exit( EX_SYNTAX );
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	logname = argv[optind];
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate #ifdef att
1177c478bd9Sstevel@tonic-gate 	pstruct = getpwnam(logname);
1187c478bd9Sstevel@tonic-gate #else
1197c478bd9Sstevel@tonic-gate 	/*
1207c478bd9Sstevel@tonic-gate 	 * Do this with fgetpwent to make sure we are only looking on local
1217c478bd9Sstevel@tonic-gate 	 * system (since passmgmt only works on local system).
1227c478bd9Sstevel@tonic-gate 	 */
1237c478bd9Sstevel@tonic-gate 	if ((pwf = fopen("/etc/passwd", "r")) == NULL) {
1247c478bd9Sstevel@tonic-gate 		errmsg( M_OOPS, "open", "/etc/passwd");
1257c478bd9Sstevel@tonic-gate 		exit(EX_FAILURE);
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 	while ((pstruct = fgetpwent(pwf)) != NULL)
1287c478bd9Sstevel@tonic-gate 		if (strcmp(pstruct->pw_name, logname) == 0)
1297c478bd9Sstevel@tonic-gate 			break;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	fclose(pwf);
1327c478bd9Sstevel@tonic-gate #endif
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	if (pstruct == NULL) {
1357c478bd9Sstevel@tonic-gate 		errmsg( M_EXIST, logname );
1367c478bd9Sstevel@tonic-gate 		exit( EX_NAME_NOT_EXIST );
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	if( isbusy(logname) ) {
1407c478bd9Sstevel@tonic-gate 		errmsg( M_BUSY, logname, "remove" );
1417c478bd9Sstevel@tonic-gate 		exit( EX_BUSY );
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	/* that's it for validations - now do the work */
1457c478bd9Sstevel@tonic-gate 	/* set up arguments to  passmgmt in nargv array */
1467c478bd9Sstevel@tonic-gate 	nargv[0] = "passmgmt";
1477c478bd9Sstevel@tonic-gate 	nargv[1] = "-d";	/* delete */
1487c478bd9Sstevel@tonic-gate 	argindex = 2;		/* next argument */
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	/* finally - login name */
1517c478bd9Sstevel@tonic-gate 	nargv[argindex++] = logname;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	/* set the last to null */
1547c478bd9Sstevel@tonic-gate 	nargv[argindex++] = NULL;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	/* remove home directory */
1577c478bd9Sstevel@tonic-gate 	if( rflag ) {
1587c478bd9Sstevel@tonic-gate 		/* Check Permissions */
1597c478bd9Sstevel@tonic-gate 		if( stat( pstruct->pw_dir, &statbuf ) ) {
1607c478bd9Sstevel@tonic-gate 			errmsg(M_OOPS, "find status about home directory",
161*ace1a5f1Sdp 			    strerror(errno));
1627c478bd9Sstevel@tonic-gate 			exit( EX_HOMEDIR );
1637c478bd9Sstevel@tonic-gate 		}
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 		if( check_perm( statbuf, pstruct->pw_uid, pstruct->pw_gid,
1667c478bd9Sstevel@tonic-gate 		    S_IWOTH|S_IXOTH ) != 0 ) {
1677c478bd9Sstevel@tonic-gate 			errmsg( M_NO_PERM, logname, pstruct->pw_dir );
1687c478bd9Sstevel@tonic-gate 			exit( EX_HOMEDIR );
1697c478bd9Sstevel@tonic-gate 		}
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 		if( rm_files(pstruct->pw_dir, logname) != EX_SUCCESS )
1727c478bd9Sstevel@tonic-gate 			exit( EX_HOMEDIR );
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	/* now call passmgmt */
1767c478bd9Sstevel@tonic-gate 	ret = PEX_FAILED;
1777c478bd9Sstevel@tonic-gate 	for( tries = 3; ret != PEX_SUCCESS && tries--; ) {
1787c478bd9Sstevel@tonic-gate 		switch( ret = call_passmgmt( nargv ) ) {
1797c478bd9Sstevel@tonic-gate 		case PEX_SUCCESS:
1807c478bd9Sstevel@tonic-gate 			ret = edit_group( logname, (char *)0, (int **)0, 1 );
1817c478bd9Sstevel@tonic-gate 			if( ret != EX_SUCCESS )
1827c478bd9Sstevel@tonic-gate 				errmsg( M_UPDATE, "deleted" );
1837c478bd9Sstevel@tonic-gate 			break;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 		case PEX_BUSY:
1867c478bd9Sstevel@tonic-gate 			break;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 		case PEX_HOSED_FILES:
1897c478bd9Sstevel@tonic-gate 			errmsg( M_HOSED_FILES );
1907c478bd9Sstevel@tonic-gate 			exit( EX_INCONSISTENT );
1917c478bd9Sstevel@tonic-gate 			break;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 		case PEX_SYNTAX:
1947c478bd9Sstevel@tonic-gate 		case PEX_BADARG:
1957c478bd9Sstevel@tonic-gate 			/* should NEVER occur that passmgmt usage is wrong */
1967c478bd9Sstevel@tonic-gate 			if (is_role(usertype))
1977c478bd9Sstevel@tonic-gate 				errmsg( M_DRUSAGE );
1987c478bd9Sstevel@tonic-gate 			else
1997c478bd9Sstevel@tonic-gate 				errmsg( M_DUSAGE );
2007c478bd9Sstevel@tonic-gate 			exit( EX_SYNTAX );
2017c478bd9Sstevel@tonic-gate 			break;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 		case PEX_BADUID:
2047c478bd9Sstevel@tonic-gate 			/* uid is used - shouldn't happen but print message anyway */
2057c478bd9Sstevel@tonic-gate 			errmsg( M_UID_USED, pstruct->pw_uid );
2067c478bd9Sstevel@tonic-gate 			exit( EX_ID_EXISTS );
2077c478bd9Sstevel@tonic-gate 			break;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 		case PEX_BADNAME:
2107c478bd9Sstevel@tonic-gate 			/* invalid loname */
2117c478bd9Sstevel@tonic-gate 			errmsg( M_USED, logname);
2127c478bd9Sstevel@tonic-gate 			exit( EX_NAME_EXISTS );
2137c478bd9Sstevel@tonic-gate 			break;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 		default:
2167c478bd9Sstevel@tonic-gate 			errmsg( M_UPDATE, "deleted" );
2177c478bd9Sstevel@tonic-gate 			exit( ret );
2187c478bd9Sstevel@tonic-gate 			break;
2197c478bd9Sstevel@tonic-gate 		}
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 	if( tries == 0 )
2227c478bd9Sstevel@tonic-gate 		errmsg( M_UPDATE, "deleted" );
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate /*
2257c478bd9Sstevel@tonic-gate  * Now, remove this user from all project entries
2267c478bd9Sstevel@tonic-gate  */
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	rc = edit_project(logname, (char *)0, (projid_t **)0, 1);
2297c478bd9Sstevel@tonic-gate 	if (rc != EX_SUCCESS) {
2307c478bd9Sstevel@tonic-gate 		errmsg(M_UPDATE, "modified");
2317c478bd9Sstevel@tonic-gate 		exit(rc);
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	exit( ret );
2357c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
2367c478bd9Sstevel@tonic-gate }
237