xref: /titanic_51/usr/src/cmd/oamuser/user/homedir.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
1 /*
2  * CDDL HEADER START
3  *
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
7  * with the License.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include <sys/types.h>
34 #include <stdio.h>
35 #include <userdefs.h>
36 #include <errno.h>
37 #include "messages.h"
38 
39 #define 	SBUFSZ	256
40 
41 extern int mkdir(), chown(), rm_homedir();
42 extern char *prerrno();
43 
44 static char cmdbuf[ SBUFSZ ];	/* buffer for system call */
45 
46 /*
47 	Create a home directory and populate with files from skeleton
48 	directory.
49 */
50 int
51 create_home( homedir, skeldir, uid, gid)
52 char *homedir;			/* home directory to create */
53 char *skeldir;			/* skel directory to copy if indicated */
54 uid_t uid;			/* uid of new user */
55 gid_t gid;			/* group id of new user */
56 {
57 	if( mkdir(homedir, 0775) != 0 ) {
58 		errmsg( M_OOPS, "create the home directory", prerrno( errno ) );
59 		return( EX_HOMEDIR );
60 	}
61 
62 	if( chown(homedir, uid, gid) != 0 ) {
63 		errmsg( M_OOPS, "change ownership of home directory",
64 			prerrno( errno ) );
65 		return( EX_HOMEDIR );
66 	}
67 
68 	if(skeldir) {
69 		/* copy the skel_dir into the home directory */
70 		(void) sprintf( cmdbuf, "cd %s && find . -print | cpio -pd %s",
71 			skeldir, homedir);
72 
73 		if( system( cmdbuf ) != 0 ) {
74 			errmsg( M_OOPS, "copy skeleton directory into home directory",
75 				prerrno( errno ) );
76 			(void) rm_homedir( homedir );
77 			return( EX_HOMEDIR );
78 		}
79 
80 		/* make sure contents in the home dirctory have correct owner */
81 		(void) sprintf( cmdbuf,"cd %s && find . -exec chown %ld {} \\;",
82 			homedir, uid );
83 		if( system( cmdbuf ) != 0) {
84 			errmsg( M_OOPS, "change owner of files home directory",
85 				prerrno( errno ) );
86 
87 			(void) rm_homedir( homedir );
88 			return( EX_HOMEDIR );
89 		}
90 
91 		/* and group....... */
92 		(void) sprintf( cmdbuf, "cd %s && find . -exec chgrp %ld {} \\;",
93 			homedir, gid );
94 		if( system( cmdbuf ) != 0) {
95 			errmsg( M_OOPS, "change group of files home directory",
96 				prerrno( errno ) );
97 			(void) rm_homedir( homedir );
98 			return( EX_HOMEDIR );
99 		}
100 	}
101 	return( EX_SUCCESS );
102 }
103 
104 /* Remove a home directory structure */
105 rm_homedir( dir )
106 char *dir;
107 {
108 	(void) sprintf( cmdbuf, "rm -rf %s", dir );
109 
110 	return( system( cmdbuf ) );
111 }
112