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 /* 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 (:) 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 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 827c478bd9Sstevel@tonic-gate if (geteuid() != 0) { 837c478bd9Sstevel@tonic-gate errmsg(M_PERM_DENIED); 847c478bd9Sstevel@tonic-gate 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]); 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate while ((ch = getopt(argc, argv, "r")) != EOF) { 917c478bd9Sstevel@tonic-gate 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)) 977c478bd9Sstevel@tonic-gate errmsg(M_DRUSAGE); 987c478bd9Sstevel@tonic-gate else 997c478bd9Sstevel@tonic-gate errmsg(M_DUSAGE); 1007c478bd9Sstevel@tonic-gate exit(EX_SYNTAX); 1017c478bd9Sstevel@tonic-gate } 1027c478bd9Sstevel@tonic-gate } 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate if (optind != argc - 1) { 1057c478bd9Sstevel@tonic-gate if (is_role(usertype)) 1067c478bd9Sstevel@tonic-gate errmsg(M_DRUSAGE); 1077c478bd9Sstevel@tonic-gate else 1087c478bd9Sstevel@tonic-gate errmsg(M_DUSAGE); 1097c478bd9Sstevel@tonic-gate 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) { 1227c478bd9Sstevel@tonic-gate 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) { 1337c478bd9Sstevel@tonic-gate errmsg(M_EXIST, logname); 1347c478bd9Sstevel@tonic-gate exit(EX_NAME_NOT_EXIST); 1357c478bd9Sstevel@tonic-gate } 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate if (isbusy(logname)) { 1387c478bd9Sstevel@tonic-gate errmsg(M_BUSY, logname, "remove"); 1397c478bd9Sstevel@tonic-gate 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 */ 1557c478bd9Sstevel@tonic-gate if (rflag) { 1567c478bd9Sstevel@tonic-gate /* Check Permissions */ 1577c478bd9Sstevel@tonic-gate if (stat(pstruct->pw_dir, &statbuf)) { 1587c478bd9Sstevel@tonic-gate errmsg(M_OOPS, "find status about home directory", 159ace1a5f1Sdp strerror(errno)); 1607c478bd9Sstevel@tonic-gate exit(EX_HOMEDIR); 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate if (check_perm(statbuf, pstruct->pw_uid, pstruct->pw_gid, 1647c478bd9Sstevel@tonic-gate S_IWOTH|S_IXOTH) != 0) { 1657c478bd9Sstevel@tonic-gate errmsg(M_NO_PERM, logname, pstruct->pw_dir); 1667c478bd9Sstevel@tonic-gate 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) 1717c478bd9Sstevel@tonic-gate exit(EX_HOMEDIR); 1727c478bd9Sstevel@tonic-gate } 1737c478bd9Sstevel@tonic-gate 1747c478bd9Sstevel@tonic-gate /* now call passmgmt */ 1757c478bd9Sstevel@tonic-gate ret = PEX_FAILED; 1767c478bd9Sstevel@tonic-gate for (tries = 3; ret != PEX_SUCCESS && tries--; ) { 1777c478bd9Sstevel@tonic-gate switch (ret = call_passmgmt(nargv)) { 1787c478bd9Sstevel@tonic-gate case PEX_SUCCESS: 1797c478bd9Sstevel@tonic-gate ret = edit_group(logname, (char *)0, (int **)0, 1); 1807c478bd9Sstevel@tonic-gate if (ret != EX_SUCCESS) 1817c478bd9Sstevel@tonic-gate 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: 1887c478bd9Sstevel@tonic-gate errmsg(M_HOSED_FILES); 1897c478bd9Sstevel@tonic-gate 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)) 1967c478bd9Sstevel@tonic-gate errmsg(M_DRUSAGE); 1977c478bd9Sstevel@tonic-gate else 1987c478bd9Sstevel@tonic-gate errmsg(M_DUSAGE); 1997c478bd9Sstevel@tonic-gate 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 */ 2067c478bd9Sstevel@tonic-gate errmsg(M_UID_USED, pstruct->pw_uid); 2077c478bd9Sstevel@tonic-gate exit(EX_ID_EXISTS); 2087c478bd9Sstevel@tonic-gate break; 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate case PEX_BADNAME: 2117c478bd9Sstevel@tonic-gate /* invalid loname */ 2127c478bd9Sstevel@tonic-gate errmsg(M_USED, logname); 2137c478bd9Sstevel@tonic-gate exit(EX_NAME_EXISTS); 2147c478bd9Sstevel@tonic-gate break; 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate default: 2177c478bd9Sstevel@tonic-gate errmsg(M_UPDATE, "deleted"); 2187c478bd9Sstevel@tonic-gate exit(ret); 2197c478bd9Sstevel@tonic-gate break; 2207c478bd9Sstevel@tonic-gate } 2217c478bd9Sstevel@tonic-gate } 2227c478bd9Sstevel@tonic-gate if (tries == 0) 2237c478bd9Sstevel@tonic-gate 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 } 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate exit(ret); 2367c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 2377c478bd9Sstevel@tonic-gate } 238