4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
24 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
28 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
32 #include <sys/types.h>
39 #include <sys/mntent.h>
40 #include <sys/mnttab.h>
51 #define DEFAULT_USERADD "/etc/default/useradd"
53 static int rm_homedir();
54 static char *get_mnt_special();
56 static char cmdbuf
[ SBUFSZ
]; /* buffer for system call */
57 static char dhome
[ PATH_MAX
+ 1 ]; /* buffer for dirname */
58 static char bhome
[ PATH_MAX
+ 1 ]; /* buffer for basename */
59 static char pdir
[ PATH_MAX
+ 1 ]; /* parent directory */
60 static libzfs_handle_t
*g_zfs
= NULL
;
63 * Create a home directory and populate with files from skeleton
67 create_home(char *homedir
, char *skeldir
, uid_t uid
, gid_t gid
, int flags
)
68 /* home directory to create */
69 /* skel directory to copy if indicated */
71 /* group id of new user */
72 /* miscellaneous flags */
76 char *dname
, *bname
, *rp
;
79 rp
= realpath(homedir
, NULL
);
80 if (rp
&& (strcmp(rp
, "/") == 0)) {
84 (void) strcpy(dhome
, homedir
);
85 (void) strcpy(bhome
, homedir
);
86 dname
= dirname(dhome
);
87 bname
= basename(bhome
);
88 (void) strcpy(pdir
, dname
);
90 if ((stat(pdir
, &stbuf
) != 0) || !S_ISDIR(stbuf
.st_mode
)) {
91 errmsg(M_OOPS
, "access the parent directory", strerror(errno
));
95 if ((strcmp(stbuf
.st_fstype
, MNTTYPE_ZFS
) == 0) &&
96 (flags
& MANAGE_ZFS
)) {
98 g_zfs
= libzfs_init();
100 errmsg(M_OOPS
, "libzfs_init failure", strerror(errno
));
103 if ((dataset
= get_mnt_special(pdir
, stbuf
.st_fstype
))
105 char nm
[ZFS_MAX_DATASET_NAME_LEN
];
108 (void) snprintf(nm
, sizeof (nm
), "%s/%s",
111 if ((zfs_create(g_zfs
, nm
, ZFS_TYPE_FILESYSTEM
, NULL
)
113 ((zhp
= zfs_open(g_zfs
, nm
, ZFS_TYPE_FILESYSTEM
)) ==
115 errmsg(M_OOPS
, "create the home directory",
116 libzfs_error_description(g_zfs
));
122 if (zfs_mount(zhp
, NULL
, 0) != 0) {
123 errmsg(M_OOPS
, "mount the home directory",
124 libzfs_error_description(g_zfs
));
125 (void) zfs_destroy(zhp
, B_FALSE
);
135 S_IRWXU
|S_IRGRP
|S_IXGRP
|S_IROTH
|S_IXOTH
) != 0) {
137 "change permissions of home directory",
146 errmsg(M_NO_ZFS_MOUNTPOINT
, pdir
);
151 if (mkdir(homedir
, S_IRWXU
|S_IRGRP
|S_IXGRP
|S_IROTH
|S_IXOTH
)
153 errmsg(M_OOPS
, "create the home directory",
163 if (chown(homedir
, uid
, gid
) != 0) {
164 errmsg(M_OOPS
, "change ownership of home directory",
173 if (skeldir
!= NULL
) {
174 /* copy the skel_dir into the home directory */
175 (void) sprintf(cmdbuf
, "cd %s && find . -print | cpio -pd %s",
178 if (system(cmdbuf
) != 0) {
179 errmsg(M_OOPS
, "copy skeleton directory into home "
180 "directory", strerror(errno
));
181 (void) rm_homedir(homedir
, flags
);
189 /* make sure contents in the home dirctory have correct owner */
190 (void) sprintf(cmdbuf
,
191 "cd %s && find . -exec chown %ld:%ld {} \\;",
193 if (system(cmdbuf
) != 0) {
195 "change owner and group of files home directory",
197 (void) rm_homedir(homedir
, flags
);
213 /* Remove a home directory structure */
215 rm_homedir(char *dir
, int flags
)
220 rp
= realpath(dir
, NULL
);
221 if (rp
&& (strcmp(rp
, "/") == 0)) {
225 if ((stat(dir
, &stbuf
) != 0) || !S_ISDIR(stbuf
.st_mode
))
228 if ((strcmp(stbuf
.st_fstype
, MNTTYPE_ZFS
) == 0) &&
229 (flags
& MANAGE_ZFS
)) {
231 g_zfs
= libzfs_init();
234 errmsg(M_OOPS
, "libzfs_init failure", strerror(errno
));
238 if ((nm
= get_mnt_special(dir
, stbuf
.st_fstype
)) != NULL
) {
241 if ((zhp
= zfs_open(g_zfs
, nm
, ZFS_TYPE_FILESYSTEM
))
243 if ((zfs_unmount(zhp
, NULL
, 0) == 0) &&
244 (zfs_destroy(zhp
, B_FALSE
) == 0)) {
251 errmsg(M_OOPS
, "destroy the home directory",
252 libzfs_error_description(g_zfs
));
254 (void) zfs_mount(zhp
, NULL
, 0);
264 (void) sprintf(cmdbuf
, "rm -rf %s", dir
);
271 return (system(cmdbuf
));
275 rm_files(char *homedir
, char *user
, int flags
)
277 if (rm_homedir(homedir
, flags
) != 0) {
286 get_default_zfs_flags()
290 if (defopen(DEFAULT_USERADD
) == 0) {
293 if ((defptr
= defread(MANAGE_ZFS_OPT
)) != NULL
) {
294 char let
= tolower(*defptr
);
303 (void) defopen((char *)NULL
);
308 /* Get the name of a mounted filesytem */
310 get_mnt_special(char *mountp
, char *fstype
)
312 struct mnttab entry
, search
;
313 char *special
= NULL
;
316 search
.mnt_special
= search
.mnt_mntopts
= search
.mnt_time
= NULL
;
317 search
.mnt_mountp
= mountp
;
318 search
.mnt_fstype
= fstype
;
320 if ((fp
= fopen(MNTTAB
, "r")) != NULL
) {
321 if (getmntany(fp
, &entry
, &search
) == 0)
322 special
= entry
.mnt_special
;