dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / oamuser / user / userdel.c
blobb19fc35cd1bab19d80ce6ed7e73ce237153875bb
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
21 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
22 /* All Rights Reserved */
26 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <stdio.h>
33 #include <ctype.h>
34 #include <limits.h>
35 #include <pwd.h>
36 #include <project.h>
37 #include <string.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <userdefs.h>
41 #include <stdlib.h>
42 #include <errno.h>
43 #include <unistd.h>
44 #include <strings.h>
45 #include "users.h"
46 #include "messages.h"
47 #include "funcs.h"
50 * userdel [-r ] login
52 * This command deletes user logins. Arguments are:
54 * -r - when given, this option removes home directory & its contents
56 * login - a string of printable chars except colon (:)
59 extern int check_perm(), isbusy(), get_default_zfs_flags();
60 extern int rm_files(), call_passmgmt(), edit_group();
61 extern struct passwd *fgetpwent(FILE *);
63 static char *logname; /* login name to delete */
64 static char *nargv[20]; /* arguments for execvp of passmgmt */
66 char *cmdname;
68 int
69 main(int argc, char **argv)
71 int ch, ret = 0, rflag = 0;
72 int zfs_flags = 0, argindex, tries;
73 struct passwd *pstruct;
74 struct stat statbuf;
75 #ifndef att
76 FILE *pwf; /* fille ptr for opened passwd file */
77 #endif
78 char *usertype = NULL;
79 int rc;
81 cmdname = argv[0];
83 if (geteuid() != 0) {
84 errmsg(M_PERM_DENIED);
85 exit(EX_NO_PERM);
88 opterr = 0; /* no print errors from getopt */
89 usertype = getusertype(argv[0]);
91 while ((ch = getopt(argc, argv, "r")) != EOF) {
92 switch (ch) {
93 case 'r':
94 rflag++;
95 break;
96 case '?':
97 if (is_role(usertype))
98 errmsg(M_DRUSAGE);
99 else
100 errmsg(M_DUSAGE);
101 exit(EX_SYNTAX);
105 if (optind != argc - 1) {
106 if (is_role(usertype))
107 errmsg(M_DRUSAGE);
108 else
109 errmsg(M_DUSAGE);
110 exit(EX_SYNTAX);
113 logname = argv[optind];
115 #ifdef att
116 pstruct = getpwnam(logname);
117 #else
119 * Do this with fgetpwent to make sure we are only looking on local
120 * system (since passmgmt only works on local system).
122 if ((pwf = fopen("/etc/passwd", "r")) == NULL) {
123 errmsg(M_OOPS, "open", "/etc/passwd");
124 exit(EX_FAILURE);
126 while ((pstruct = fgetpwent(pwf)) != NULL)
127 if (strcmp(pstruct->pw_name, logname) == 0)
128 break;
130 fclose(pwf);
131 #endif
133 if (pstruct == NULL) {
134 errmsg(M_EXIST, logname);
135 exit(EX_NAME_NOT_EXIST);
138 if (isbusy(logname)) {
139 errmsg(M_BUSY, logname, "remove");
140 exit(EX_BUSY);
143 /* that's it for validations - now do the work */
144 /* set up arguments to passmgmt in nargv array */
145 nargv[0] = PASSMGMT;
146 nargv[1] = "-d"; /* delete */
147 argindex = 2; /* next argument */
149 /* finally - login name */
150 nargv[argindex++] = logname;
152 /* set the last to null */
153 nargv[argindex++] = NULL;
155 /* remove home directory */
156 if (rflag) {
157 /* Check Permissions */
158 if (stat(pstruct->pw_dir, &statbuf)) {
159 errmsg(M_OOPS, "find status about home directory",
160 strerror(errno));
161 exit(EX_HOMEDIR);
164 if (check_perm(statbuf, pstruct->pw_uid, pstruct->pw_gid,
165 S_IWOTH|S_IXOTH) != 0) {
166 errmsg(M_NO_PERM, logname, pstruct->pw_dir);
167 exit(EX_HOMEDIR);
169 zfs_flags = get_default_zfs_flags();
171 if (rm_files(pstruct->pw_dir, logname, zfs_flags) != EX_SUCCESS)
172 exit(EX_HOMEDIR);
175 /* now call passmgmt */
176 ret = PEX_FAILED;
177 for (tries = 3; ret != PEX_SUCCESS && tries--; ) {
178 switch (ret = call_passmgmt(nargv)) {
179 case PEX_SUCCESS:
180 ret = edit_group(logname, (char *)0, (int **)0, 1);
181 if (ret != EX_SUCCESS)
182 errmsg(M_UPDATE, "deleted");
183 break;
185 case PEX_BUSY:
186 break;
188 case PEX_HOSED_FILES:
189 errmsg(M_HOSED_FILES);
190 exit(EX_INCONSISTENT);
191 break;
193 case PEX_SYNTAX:
194 case PEX_BADARG:
195 /* should NEVER occur that passmgmt usage is wrong */
196 if (is_role(usertype))
197 errmsg(M_DRUSAGE);
198 else
199 errmsg(M_DUSAGE);
200 exit(EX_SYNTAX);
201 break;
203 case PEX_BADUID:
205 * uid is used - shouldn't happen but print message anyway
207 errmsg(M_UID_USED, pstruct->pw_uid);
208 exit(EX_ID_EXISTS);
209 break;
211 case PEX_BADNAME:
212 /* invalid loname */
213 errmsg(M_USED, logname);
214 exit(EX_NAME_EXISTS);
215 break;
217 default:
218 errmsg(M_UPDATE, "deleted");
219 exit(ret);
220 break;
223 if (tries == 0)
224 errmsg(M_UPDATE, "deleted");
227 * Now, remove this user from all project entries
230 rc = edit_project(logname, (char *)0, (projid_t **)0, 1);
231 if (rc != EX_SUCCESS) {
232 errmsg(M_UPDATE, "modified");
233 exit(rc);
236 exit(ret);
237 /*NOTREACHED*/