cmd/oamuser: drop -Wno-implicit-function-declaration
[unleashed.git] / usr / src / cmd / oamuser / user / userdel.c
blobc46169a97cd30d79418641e5b689e3f1cee4a41e
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 int edit_project();
62 extern struct passwd *fgetpwent(FILE *);
64 static char *logname; /* login name to delete */
65 static char *nargv[20]; /* arguments for execvp of passmgmt */
67 char *cmdname;
69 int
70 main(int argc, char **argv)
72 int ch, ret = 0, rflag = 0;
73 int zfs_flags = 0, argindex, tries;
74 struct passwd *pstruct;
75 struct stat statbuf;
76 #ifndef att
77 FILE *pwf; /* fille ptr for opened passwd file */
78 #endif
79 char *usertype = NULL;
80 int rc;
82 cmdname = argv[0];
84 if (geteuid() != 0) {
85 errmsg(M_PERM_DENIED);
86 exit(EX_NO_PERM);
89 opterr = 0; /* no print errors from getopt */
90 usertype = getusertype(argv[0]);
92 while ((ch = getopt(argc, argv, "r")) != EOF) {
93 switch (ch) {
94 case 'r':
95 rflag++;
96 break;
97 case '?':
98 if (is_role(usertype))
99 errmsg(M_DRUSAGE);
100 else
101 errmsg(M_DUSAGE);
102 exit(EX_SYNTAX);
106 if (optind != argc - 1) {
107 if (is_role(usertype))
108 errmsg(M_DRUSAGE);
109 else
110 errmsg(M_DUSAGE);
111 exit(EX_SYNTAX);
114 logname = argv[optind];
116 #ifdef att
117 pstruct = getpwnam(logname);
118 #else
120 * Do this with fgetpwent to make sure we are only looking on local
121 * system (since passmgmt only works on local system).
123 if ((pwf = fopen("/etc/passwd", "r")) == NULL) {
124 errmsg(M_OOPS, "open", "/etc/passwd");
125 exit(EX_FAILURE);
127 while ((pstruct = fgetpwent(pwf)) != NULL)
128 if (strcmp(pstruct->pw_name, logname) == 0)
129 break;
131 fclose(pwf);
132 #endif
134 if (pstruct == NULL) {
135 errmsg(M_EXIST, logname);
136 exit(EX_NAME_NOT_EXIST);
139 if (isbusy(logname)) {
140 errmsg(M_BUSY, logname, "remove");
141 exit(EX_BUSY);
144 /* that's it for validations - now do the work */
145 /* set up arguments to passmgmt in nargv array */
146 nargv[0] = PASSMGMT;
147 nargv[1] = "-d"; /* delete */
148 argindex = 2; /* next argument */
150 /* finally - login name */
151 nargv[argindex++] = logname;
153 /* set the last to null */
154 nargv[argindex++] = NULL;
156 /* remove home directory */
157 if (rflag) {
158 /* Check Permissions */
159 if (stat(pstruct->pw_dir, &statbuf)) {
160 errmsg(M_OOPS, "find status about home directory",
161 strerror(errno));
162 exit(EX_HOMEDIR);
165 if (check_perm(statbuf, pstruct->pw_uid, pstruct->pw_gid,
166 S_IWOTH|S_IXOTH) != 0) {
167 errmsg(M_NO_PERM, logname, pstruct->pw_dir);
168 exit(EX_HOMEDIR);
170 zfs_flags = get_default_zfs_flags();
172 if (rm_files(pstruct->pw_dir, logname, zfs_flags) != EX_SUCCESS)
173 exit(EX_HOMEDIR);
176 /* now call passmgmt */
177 ret = PEX_FAILED;
178 for (tries = 3; ret != PEX_SUCCESS && tries--; ) {
179 switch (ret = call_passmgmt(nargv)) {
180 case PEX_SUCCESS:
181 ret = edit_group(logname, (char *)0, (int **)0, 1);
182 if (ret != EX_SUCCESS)
183 errmsg(M_UPDATE, "deleted");
184 break;
186 case PEX_BUSY:
187 break;
189 case PEX_HOSED_FILES:
190 errmsg(M_HOSED_FILES);
191 exit(EX_INCONSISTENT);
192 break;
194 case PEX_SYNTAX:
195 case PEX_BADARG:
196 /* should NEVER occur that passmgmt usage is wrong */
197 if (is_role(usertype))
198 errmsg(M_DRUSAGE);
199 else
200 errmsg(M_DUSAGE);
201 exit(EX_SYNTAX);
202 break;
204 case PEX_BADUID:
206 * uid is used - shouldn't happen but print message anyway
208 errmsg(M_UID_USED, pstruct->pw_uid);
209 exit(EX_ID_EXISTS);
210 break;
212 case PEX_BADNAME:
213 /* invalid loname */
214 errmsg(M_USED, logname);
215 exit(EX_NAME_EXISTS);
216 break;
218 default:
219 errmsg(M_UPDATE, "deleted");
220 exit(ret);
221 break;
224 if (tries == 0)
225 errmsg(M_UPDATE, "deleted");
228 * Now, remove this user from all project entries
231 rc = edit_project(logname, (char *)0, (projid_t **)0, 1);
232 if (rc != EX_SUCCESS) {
233 errmsg(M_UPDATE, "modified");
234 exit(rc);
237 exit(ret);
238 /*NOTREACHED*/