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