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
5*c651b32eSJohn Sonnenschein * Common Development and Distribution License (the "License").
6*c651b32eSJohn 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 /*
26*c651b32eSJohn 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
497c478bd9Sstevel@tonic-gate /*******************************************************************************
507c478bd9Sstevel@tonic-gate * 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 (:)
577c478bd9Sstevel@tonic-gate ******************************************************************************/
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate extern int check_perm(), isbusy();
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 {
707c478bd9Sstevel@tonic-gate int ch, ret = 0, rflag = 0, argindex, tries;
717c478bd9Sstevel@tonic-gate struct passwd *pstruct;
727c478bd9Sstevel@tonic-gate struct stat statbuf;
737c478bd9Sstevel@tonic-gate #ifndef att
747c478bd9Sstevel@tonic-gate FILE *pwf; /* fille ptr for opened passwd file */
757c478bd9Sstevel@tonic-gate #endif
767c478bd9Sstevel@tonic-gate char *usertype = NULL;
777c478bd9Sstevel@tonic-gate int rc;
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate cmdname = argv[0];
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate if( geteuid() != 0 ) {
827c478bd9Sstevel@tonic-gate errmsg( M_PERM_DENIED );
837c478bd9Sstevel@tonic-gate exit( EX_NO_PERM );
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate opterr = 0; /* no print errors from getopt */
877c478bd9Sstevel@tonic-gate usertype = getusertype(argv[0]);
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate while( (ch = getopt(argc, argv, "r")) != EOF ) {
907c478bd9Sstevel@tonic-gate switch(ch) {
917c478bd9Sstevel@tonic-gate case 'r':
927c478bd9Sstevel@tonic-gate rflag++;
937c478bd9Sstevel@tonic-gate break;
947c478bd9Sstevel@tonic-gate case '?':
957c478bd9Sstevel@tonic-gate if (is_role(usertype))
967c478bd9Sstevel@tonic-gate errmsg( M_DRUSAGE );
977c478bd9Sstevel@tonic-gate else
987c478bd9Sstevel@tonic-gate errmsg( M_DUSAGE );
997c478bd9Sstevel@tonic-gate exit( EX_SYNTAX );
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate if( optind != argc - 1 ) {
1047c478bd9Sstevel@tonic-gate if (is_role(usertype))
1057c478bd9Sstevel@tonic-gate errmsg( M_DRUSAGE );
1067c478bd9Sstevel@tonic-gate else
1077c478bd9Sstevel@tonic-gate errmsg( M_DUSAGE );
1087c478bd9Sstevel@tonic-gate exit( EX_SYNTAX );
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate logname = argv[optind];
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate #ifdef att
1147c478bd9Sstevel@tonic-gate pstruct = getpwnam(logname);
1157c478bd9Sstevel@tonic-gate #else
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate * Do this with fgetpwent to make sure we are only looking on local
1187c478bd9Sstevel@tonic-gate * system (since passmgmt only works on local system).
1197c478bd9Sstevel@tonic-gate */
1207c478bd9Sstevel@tonic-gate if ((pwf = fopen("/etc/passwd", "r")) == NULL) {
1217c478bd9Sstevel@tonic-gate errmsg( M_OOPS, "open", "/etc/passwd");
1227c478bd9Sstevel@tonic-gate exit(EX_FAILURE);
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate while ((pstruct = fgetpwent(pwf)) != NULL)
1257c478bd9Sstevel@tonic-gate if (strcmp(pstruct->pw_name, logname) == 0)
1267c478bd9Sstevel@tonic-gate break;
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate fclose(pwf);
1297c478bd9Sstevel@tonic-gate #endif
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate if (pstruct == NULL) {
1327c478bd9Sstevel@tonic-gate errmsg( M_EXIST, logname );
1337c478bd9Sstevel@tonic-gate exit( EX_NAME_NOT_EXIST );
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate if( isbusy(logname) ) {
1377c478bd9Sstevel@tonic-gate errmsg( M_BUSY, logname, "remove" );
1387c478bd9Sstevel@tonic-gate exit( EX_BUSY );
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate /* that's it for validations - now do the work */
1427c478bd9Sstevel@tonic-gate /* set up arguments to passmgmt in nargv array */
143*c651b32eSJohn Sonnenschein nargv[0] = PASSMGMT;
1447c478bd9Sstevel@tonic-gate nargv[1] = "-d"; /* delete */
1457c478bd9Sstevel@tonic-gate argindex = 2; /* next argument */
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate /* finally - login name */
1487c478bd9Sstevel@tonic-gate nargv[argindex++] = logname;
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate /* set the last to null */
1517c478bd9Sstevel@tonic-gate nargv[argindex++] = NULL;
1527c478bd9Sstevel@tonic-gate
1537c478bd9Sstevel@tonic-gate /* remove home directory */
1547c478bd9Sstevel@tonic-gate if( rflag ) {
1557c478bd9Sstevel@tonic-gate /* Check Permissions */
1567c478bd9Sstevel@tonic-gate if( stat( pstruct->pw_dir, &statbuf ) ) {
1577c478bd9Sstevel@tonic-gate errmsg(M_OOPS, "find status about home directory",
158ace1a5f1Sdp strerror(errno));
1597c478bd9Sstevel@tonic-gate exit( EX_HOMEDIR );
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate if( check_perm( statbuf, pstruct->pw_uid, pstruct->pw_gid,
1637c478bd9Sstevel@tonic-gate S_IWOTH|S_IXOTH ) != 0 ) {
1647c478bd9Sstevel@tonic-gate errmsg( M_NO_PERM, logname, pstruct->pw_dir );
1657c478bd9Sstevel@tonic-gate exit( EX_HOMEDIR );
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate if( rm_files(pstruct->pw_dir, logname) != EX_SUCCESS )
1697c478bd9Sstevel@tonic-gate exit( EX_HOMEDIR );
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate /* now call passmgmt */
1737c478bd9Sstevel@tonic-gate ret = PEX_FAILED;
1747c478bd9Sstevel@tonic-gate for( tries = 3; ret != PEX_SUCCESS && tries--; ) {
1757c478bd9Sstevel@tonic-gate switch( ret = call_passmgmt( nargv ) ) {
1767c478bd9Sstevel@tonic-gate case PEX_SUCCESS:
1777c478bd9Sstevel@tonic-gate ret = edit_group( logname, (char *)0, (int **)0, 1 );
1787c478bd9Sstevel@tonic-gate if( ret != EX_SUCCESS )
1797c478bd9Sstevel@tonic-gate errmsg( M_UPDATE, "deleted" );
1807c478bd9Sstevel@tonic-gate break;
1817c478bd9Sstevel@tonic-gate
1827c478bd9Sstevel@tonic-gate case PEX_BUSY:
1837c478bd9Sstevel@tonic-gate break;
1847c478bd9Sstevel@tonic-gate
1857c478bd9Sstevel@tonic-gate case PEX_HOSED_FILES:
1867c478bd9Sstevel@tonic-gate errmsg( M_HOSED_FILES );
1877c478bd9Sstevel@tonic-gate exit( EX_INCONSISTENT );
1887c478bd9Sstevel@tonic-gate break;
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate case PEX_SYNTAX:
1917c478bd9Sstevel@tonic-gate case PEX_BADARG:
1927c478bd9Sstevel@tonic-gate /* should NEVER occur that passmgmt usage is wrong */
1937c478bd9Sstevel@tonic-gate if (is_role(usertype))
1947c478bd9Sstevel@tonic-gate errmsg( M_DRUSAGE );
1957c478bd9Sstevel@tonic-gate else
1967c478bd9Sstevel@tonic-gate errmsg( M_DUSAGE );
1977c478bd9Sstevel@tonic-gate exit( EX_SYNTAX );
1987c478bd9Sstevel@tonic-gate break;
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate case PEX_BADUID:
2017c478bd9Sstevel@tonic-gate /* uid is used - shouldn't happen but print message anyway */
2027c478bd9Sstevel@tonic-gate errmsg( M_UID_USED, pstruct->pw_uid );
2037c478bd9Sstevel@tonic-gate exit( EX_ID_EXISTS );
2047c478bd9Sstevel@tonic-gate break;
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate case PEX_BADNAME:
2077c478bd9Sstevel@tonic-gate /* invalid loname */
2087c478bd9Sstevel@tonic-gate errmsg( M_USED, logname);
2097c478bd9Sstevel@tonic-gate exit( EX_NAME_EXISTS );
2107c478bd9Sstevel@tonic-gate break;
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate default:
2137c478bd9Sstevel@tonic-gate errmsg( M_UPDATE, "deleted" );
2147c478bd9Sstevel@tonic-gate exit( ret );
2157c478bd9Sstevel@tonic-gate break;
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate if( tries == 0 )
2197c478bd9Sstevel@tonic-gate errmsg( M_UPDATE, "deleted" );
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate /*
2227c478bd9Sstevel@tonic-gate * Now, remove this user from all project entries
2237c478bd9Sstevel@tonic-gate */
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate rc = edit_project(logname, (char *)0, (projid_t **)0, 1);
2267c478bd9Sstevel@tonic-gate if (rc != EX_SUCCESS) {
2277c478bd9Sstevel@tonic-gate errmsg(M_UPDATE, "modified");
2287c478bd9Sstevel@tonic-gate exit(rc);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate exit( ret );
2327c478bd9Sstevel@tonic-gate /*NOTREACHED*/
2337c478bd9Sstevel@tonic-gate }
234