xref: /onnv-gate/usr/src/cmd/oamuser/user/homedir.c (revision 634:263e46dc023d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22108Sbasabi 
23108Sbasabi /*
24108Sbasabi  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25108Sbasabi  * Use is subject to license terms.
26108Sbasabi  */
27108Sbasabi 
280Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
290Sstevel@tonic-gate /*	  All Rights Reserved  	*/
300Sstevel@tonic-gate 
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <sys/types.h>
350Sstevel@tonic-gate #include <stdio.h>
360Sstevel@tonic-gate #include <userdefs.h>
370Sstevel@tonic-gate #include <errno.h>
38*634Sdp #include <strings.h>
390Sstevel@tonic-gate #include "messages.h"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #define 	SBUFSZ	256
420Sstevel@tonic-gate 
43*634Sdp extern int rm_homedir();
440Sstevel@tonic-gate 
450Sstevel@tonic-gate static char cmdbuf[ SBUFSZ ];	/* buffer for system call */
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /*
480Sstevel@tonic-gate 	Create a home directory and populate with files from skeleton
490Sstevel@tonic-gate 	directory.
500Sstevel@tonic-gate */
510Sstevel@tonic-gate int
create_home(char * homedir,char * skeldir,uid_t uid,gid_t gid)52108Sbasabi create_home(char *homedir, char *skeldir, uid_t uid, gid_t gid)
53108Sbasabi 		/* home directory to create */
54108Sbasabi 		/* skel directory to copy if indicated */
55108Sbasabi 		/* uid of new user */
56108Sbasabi 		/* group id of new user */
570Sstevel@tonic-gate {
580Sstevel@tonic-gate 	if( mkdir(homedir, 0775) != 0 ) {
59*634Sdp 		errmsg(M_OOPS, "create the home directory", strerror(errno));
600Sstevel@tonic-gate 		return( EX_HOMEDIR );
610Sstevel@tonic-gate 	}
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	if( chown(homedir, uid, gid) != 0 ) {
64*634Sdp 		errmsg(M_OOPS, "change ownership of home directory",
65*634Sdp 		    strerror(errno));
660Sstevel@tonic-gate 		return( EX_HOMEDIR );
670Sstevel@tonic-gate 	}
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	if(skeldir) {
700Sstevel@tonic-gate 		/* copy the skel_dir into the home directory */
710Sstevel@tonic-gate 		(void) sprintf( cmdbuf, "cd %s && find . -print | cpio -pd %s",
720Sstevel@tonic-gate 			skeldir, homedir);
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 		if( system( cmdbuf ) != 0 ) {
75*634Sdp 			errmsg(M_OOPS, "copy skeleton directory into home "
76*634Sdp 			    "directory", strerror(errno));
770Sstevel@tonic-gate 			(void) rm_homedir( homedir );
780Sstevel@tonic-gate 			return( EX_HOMEDIR );
790Sstevel@tonic-gate 		}
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 		/* make sure contents in the home dirctory have correct owner */
820Sstevel@tonic-gate 		(void) sprintf( cmdbuf,"cd %s && find . -exec chown %ld {} \\;",
830Sstevel@tonic-gate 			homedir, uid );
840Sstevel@tonic-gate 		if( system( cmdbuf ) != 0) {
85*634Sdp 			errmsg(M_OOPS, "change owner of files home directory",
86*634Sdp 			    strerror(errno));
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 			(void) rm_homedir( homedir );
890Sstevel@tonic-gate 			return( EX_HOMEDIR );
900Sstevel@tonic-gate 		}
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 		/* and group....... */
930Sstevel@tonic-gate 		(void) sprintf( cmdbuf, "cd %s && find . -exec chgrp %ld {} \\;",
940Sstevel@tonic-gate 			homedir, gid );
950Sstevel@tonic-gate 		if( system( cmdbuf ) != 0) {
96*634Sdp 			errmsg(M_OOPS, "change group of files home directory",
97*634Sdp 			    strerror(errno));
980Sstevel@tonic-gate 			(void) rm_homedir( homedir );
990Sstevel@tonic-gate 			return( EX_HOMEDIR );
1000Sstevel@tonic-gate 		}
1010Sstevel@tonic-gate 	}
1020Sstevel@tonic-gate 	return( EX_SUCCESS );
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /* Remove a home directory structure */
106108Sbasabi int
rm_homedir(char * dir)107108Sbasabi rm_homedir(char *dir)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate 	(void) sprintf( cmdbuf, "rm -rf %s", dir );
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	return( system( cmdbuf ) );
1120Sstevel@tonic-gate }
113