10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7729SJohn.Sonnenschein@Sun.COM * Common Development and Distribution License (the "License").
6*7729SJohn.Sonnenschein@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
220Sstevel@tonic-gate /* All Rights Reserved */
230Sstevel@tonic-gate
240Sstevel@tonic-gate
250Sstevel@tonic-gate /*
26*7729SJohn.Sonnenschein@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
270Sstevel@tonic-gate * Use is subject to license terms.
280Sstevel@tonic-gate */
290Sstevel@tonic-gate
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/stat.h>
320Sstevel@tonic-gate #include <stdio.h>
330Sstevel@tonic-gate #include <ctype.h>
340Sstevel@tonic-gate #include <limits.h>
350Sstevel@tonic-gate #include <pwd.h>
360Sstevel@tonic-gate #include <project.h>
370Sstevel@tonic-gate #include <string.h>
380Sstevel@tonic-gate #include <sys/types.h>
390Sstevel@tonic-gate #include <sys/stat.h>
400Sstevel@tonic-gate #include <userdefs.h>
410Sstevel@tonic-gate #include <stdlib.h>
420Sstevel@tonic-gate #include <errno.h>
43634Sdp #include <unistd.h>
44634Sdp #include <strings.h>
450Sstevel@tonic-gate #include "users.h"
460Sstevel@tonic-gate #include "messages.h"
470Sstevel@tonic-gate #include "funcs.h"
480Sstevel@tonic-gate
490Sstevel@tonic-gate /*******************************************************************************
500Sstevel@tonic-gate * userdel [-r] login
510Sstevel@tonic-gate *
520Sstevel@tonic-gate * This command deletes user logins. Arguments are:
530Sstevel@tonic-gate *
540Sstevel@tonic-gate * -r - when given, this option removes home directory & its contents
550Sstevel@tonic-gate *
560Sstevel@tonic-gate * login - a string of printable chars except colon (:)
570Sstevel@tonic-gate ******************************************************************************/
580Sstevel@tonic-gate
590Sstevel@tonic-gate extern int check_perm(), isbusy();
600Sstevel@tonic-gate extern int rm_files(), call_passmgmt(), edit_group();
610Sstevel@tonic-gate
620Sstevel@tonic-gate static char *logname; /* login name to delete */
630Sstevel@tonic-gate static char *nargv[20]; /* arguments for execvp of passmgmt */
640Sstevel@tonic-gate
650Sstevel@tonic-gate char *cmdname;
660Sstevel@tonic-gate
670Sstevel@tonic-gate int
main(int argc,char ** argv)680Sstevel@tonic-gate main(int argc, char **argv)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate int ch, ret = 0, rflag = 0, argindex, tries;
710Sstevel@tonic-gate struct passwd *pstruct;
720Sstevel@tonic-gate struct stat statbuf;
730Sstevel@tonic-gate #ifndef att
740Sstevel@tonic-gate FILE *pwf; /* fille ptr for opened passwd file */
750Sstevel@tonic-gate #endif
760Sstevel@tonic-gate char *usertype = NULL;
770Sstevel@tonic-gate int rc;
780Sstevel@tonic-gate
790Sstevel@tonic-gate cmdname = argv[0];
800Sstevel@tonic-gate
810Sstevel@tonic-gate if( geteuid() != 0 ) {
820Sstevel@tonic-gate errmsg( M_PERM_DENIED );
830Sstevel@tonic-gate exit( EX_NO_PERM );
840Sstevel@tonic-gate }
850Sstevel@tonic-gate
860Sstevel@tonic-gate opterr = 0; /* no print errors from getopt */
870Sstevel@tonic-gate usertype = getusertype(argv[0]);
880Sstevel@tonic-gate
890Sstevel@tonic-gate while( (ch = getopt(argc, argv, "r")) != EOF ) {
900Sstevel@tonic-gate switch(ch) {
910Sstevel@tonic-gate case 'r':
920Sstevel@tonic-gate rflag++;
930Sstevel@tonic-gate break;
940Sstevel@tonic-gate case '?':
950Sstevel@tonic-gate if (is_role(usertype))
960Sstevel@tonic-gate errmsg( M_DRUSAGE );
970Sstevel@tonic-gate else
980Sstevel@tonic-gate errmsg( M_DUSAGE );
990Sstevel@tonic-gate exit( EX_SYNTAX );
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate if( optind != argc - 1 ) {
1040Sstevel@tonic-gate if (is_role(usertype))
1050Sstevel@tonic-gate errmsg( M_DRUSAGE );
1060Sstevel@tonic-gate else
1070Sstevel@tonic-gate errmsg( M_DUSAGE );
1080Sstevel@tonic-gate exit( EX_SYNTAX );
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate logname = argv[optind];
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate #ifdef att
1140Sstevel@tonic-gate pstruct = getpwnam(logname);
1150Sstevel@tonic-gate #else
1160Sstevel@tonic-gate /*
1170Sstevel@tonic-gate * Do this with fgetpwent to make sure we are only looking on local
1180Sstevel@tonic-gate * system (since passmgmt only works on local system).
1190Sstevel@tonic-gate */
1200Sstevel@tonic-gate if ((pwf = fopen("/etc/passwd", "r")) == NULL) {
1210Sstevel@tonic-gate errmsg( M_OOPS, "open", "/etc/passwd");
1220Sstevel@tonic-gate exit(EX_FAILURE);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate while ((pstruct = fgetpwent(pwf)) != NULL)
1250Sstevel@tonic-gate if (strcmp(pstruct->pw_name, logname) == 0)
1260Sstevel@tonic-gate break;
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate fclose(pwf);
1290Sstevel@tonic-gate #endif
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate if (pstruct == NULL) {
1320Sstevel@tonic-gate errmsg( M_EXIST, logname );
1330Sstevel@tonic-gate exit( EX_NAME_NOT_EXIST );
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate if( isbusy(logname) ) {
1370Sstevel@tonic-gate errmsg( M_BUSY, logname, "remove" );
1380Sstevel@tonic-gate exit( EX_BUSY );
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate /* that's it for validations - now do the work */
1420Sstevel@tonic-gate /* set up arguments to passmgmt in nargv array */
143*7729SJohn.Sonnenschein@Sun.COM nargv[0] = PASSMGMT;
1440Sstevel@tonic-gate nargv[1] = "-d"; /* delete */
1450Sstevel@tonic-gate argindex = 2; /* next argument */
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate /* finally - login name */
1480Sstevel@tonic-gate nargv[argindex++] = logname;
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate /* set the last to null */
1510Sstevel@tonic-gate nargv[argindex++] = NULL;
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate /* remove home directory */
1540Sstevel@tonic-gate if( rflag ) {
1550Sstevel@tonic-gate /* Check Permissions */
1560Sstevel@tonic-gate if( stat( pstruct->pw_dir, &statbuf ) ) {
157634Sdp errmsg(M_OOPS, "find status about home directory",
158634Sdp strerror(errno));
1590Sstevel@tonic-gate exit( EX_HOMEDIR );
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate if( check_perm( statbuf, pstruct->pw_uid, pstruct->pw_gid,
1630Sstevel@tonic-gate S_IWOTH|S_IXOTH ) != 0 ) {
1640Sstevel@tonic-gate errmsg( M_NO_PERM, logname, pstruct->pw_dir );
1650Sstevel@tonic-gate exit( EX_HOMEDIR );
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate if( rm_files(pstruct->pw_dir, logname) != EX_SUCCESS )
1690Sstevel@tonic-gate exit( EX_HOMEDIR );
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate /* now call passmgmt */
1730Sstevel@tonic-gate ret = PEX_FAILED;
1740Sstevel@tonic-gate for( tries = 3; ret != PEX_SUCCESS && tries--; ) {
1750Sstevel@tonic-gate switch( ret = call_passmgmt( nargv ) ) {
1760Sstevel@tonic-gate case PEX_SUCCESS:
1770Sstevel@tonic-gate ret = edit_group( logname, (char *)0, (int **)0, 1 );
1780Sstevel@tonic-gate if( ret != EX_SUCCESS )
1790Sstevel@tonic-gate errmsg( M_UPDATE, "deleted" );
1800Sstevel@tonic-gate break;
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate case PEX_BUSY:
1830Sstevel@tonic-gate break;
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate case PEX_HOSED_FILES:
1860Sstevel@tonic-gate errmsg( M_HOSED_FILES );
1870Sstevel@tonic-gate exit( EX_INCONSISTENT );
1880Sstevel@tonic-gate break;
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate case PEX_SYNTAX:
1910Sstevel@tonic-gate case PEX_BADARG:
1920Sstevel@tonic-gate /* should NEVER occur that passmgmt usage is wrong */
1930Sstevel@tonic-gate if (is_role(usertype))
1940Sstevel@tonic-gate errmsg( M_DRUSAGE );
1950Sstevel@tonic-gate else
1960Sstevel@tonic-gate errmsg( M_DUSAGE );
1970Sstevel@tonic-gate exit( EX_SYNTAX );
1980Sstevel@tonic-gate break;
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate case PEX_BADUID:
2010Sstevel@tonic-gate /* uid is used - shouldn't happen but print message anyway */
2020Sstevel@tonic-gate errmsg( M_UID_USED, pstruct->pw_uid );
2030Sstevel@tonic-gate exit( EX_ID_EXISTS );
2040Sstevel@tonic-gate break;
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate case PEX_BADNAME:
2070Sstevel@tonic-gate /* invalid loname */
2080Sstevel@tonic-gate errmsg( M_USED, logname);
2090Sstevel@tonic-gate exit( EX_NAME_EXISTS );
2100Sstevel@tonic-gate break;
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate default:
2130Sstevel@tonic-gate errmsg( M_UPDATE, "deleted" );
2140Sstevel@tonic-gate exit( ret );
2150Sstevel@tonic-gate break;
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate if( tries == 0 )
2190Sstevel@tonic-gate errmsg( M_UPDATE, "deleted" );
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate /*
2220Sstevel@tonic-gate * Now, remove this user from all project entries
2230Sstevel@tonic-gate */
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate rc = edit_project(logname, (char *)0, (projid_t **)0, 1);
2260Sstevel@tonic-gate if (rc != EX_SUCCESS) {
2270Sstevel@tonic-gate errmsg(M_UPDATE, "modified");
2280Sstevel@tonic-gate exit(rc);
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate
2310Sstevel@tonic-gate exit( ret );
2320Sstevel@tonic-gate /*NOTREACHED*/
2330Sstevel@tonic-gate }
234