xref: /titanic_44/usr/src/cmd/oamuser/user/homedir.c (revision ace1a5f11236a072fca1b5e0ea1416a083a9f2aa)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
2249335bdeSbasabi 
2349335bdeSbasabi /*
2449335bdeSbasabi  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
2549335bdeSbasabi  * Use is subject to license terms.
2649335bdeSbasabi  */
2749335bdeSbasabi 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <userdefs.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
38*ace1a5f1Sdp #include <strings.h>
397c478bd9Sstevel@tonic-gate #include "messages.h"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #define 	SBUFSZ	256
427c478bd9Sstevel@tonic-gate 
43*ace1a5f1Sdp extern int rm_homedir();
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate static char cmdbuf[ SBUFSZ ];	/* buffer for system call */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate 	Create a home directory and populate with files from skeleton
497c478bd9Sstevel@tonic-gate 	directory.
507c478bd9Sstevel@tonic-gate */
517c478bd9Sstevel@tonic-gate int
create_home(char * homedir,char * skeldir,uid_t uid,gid_t gid)5249335bdeSbasabi create_home(char *homedir, char *skeldir, uid_t uid, gid_t gid)
5349335bdeSbasabi 		/* home directory to create */
5449335bdeSbasabi 		/* skel directory to copy if indicated */
5549335bdeSbasabi 		/* uid of new user */
5649335bdeSbasabi 		/* group id of new user */
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 	if( mkdir(homedir, 0775) != 0 ) {
59*ace1a5f1Sdp 		errmsg(M_OOPS, "create the home directory", strerror(errno));
607c478bd9Sstevel@tonic-gate 		return( EX_HOMEDIR );
617c478bd9Sstevel@tonic-gate 	}
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	if( chown(homedir, uid, gid) != 0 ) {
647c478bd9Sstevel@tonic-gate 		errmsg(M_OOPS, "change ownership of home directory",
65*ace1a5f1Sdp 		    strerror(errno));
667c478bd9Sstevel@tonic-gate 		return( EX_HOMEDIR );
677c478bd9Sstevel@tonic-gate 	}
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	if(skeldir) {
707c478bd9Sstevel@tonic-gate 		/* copy the skel_dir into the home directory */
717c478bd9Sstevel@tonic-gate 		(void) sprintf( cmdbuf, "cd %s && find . -print | cpio -pd %s",
727c478bd9Sstevel@tonic-gate 			skeldir, homedir);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 		if( system( cmdbuf ) != 0 ) {
75*ace1a5f1Sdp 			errmsg(M_OOPS, "copy skeleton directory into home "
76*ace1a5f1Sdp 			    "directory", strerror(errno));
777c478bd9Sstevel@tonic-gate 			(void) rm_homedir( homedir );
787c478bd9Sstevel@tonic-gate 			return( EX_HOMEDIR );
797c478bd9Sstevel@tonic-gate 		}
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 		/* make sure contents in the home dirctory have correct owner */
827c478bd9Sstevel@tonic-gate 		(void) sprintf( cmdbuf,"cd %s && find . -exec chown %ld {} \\;",
837c478bd9Sstevel@tonic-gate 			homedir, uid );
847c478bd9Sstevel@tonic-gate 		if( system( cmdbuf ) != 0) {
857c478bd9Sstevel@tonic-gate 			errmsg(M_OOPS, "change owner of files home directory",
86*ace1a5f1Sdp 			    strerror(errno));
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 			(void) rm_homedir( homedir );
897c478bd9Sstevel@tonic-gate 			return( EX_HOMEDIR );
907c478bd9Sstevel@tonic-gate 		}
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 		/* and group....... */
937c478bd9Sstevel@tonic-gate 		(void) sprintf( cmdbuf, "cd %s && find . -exec chgrp %ld {} \\;",
947c478bd9Sstevel@tonic-gate 			homedir, gid );
957c478bd9Sstevel@tonic-gate 		if( system( cmdbuf ) != 0) {
967c478bd9Sstevel@tonic-gate 			errmsg(M_OOPS, "change group of files home directory",
97*ace1a5f1Sdp 			    strerror(errno));
987c478bd9Sstevel@tonic-gate 			(void) rm_homedir( homedir );
997c478bd9Sstevel@tonic-gate 			return( EX_HOMEDIR );
1007c478bd9Sstevel@tonic-gate 		}
1017c478bd9Sstevel@tonic-gate 	}
1027c478bd9Sstevel@tonic-gate 	return( EX_SUCCESS );
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /* Remove a home directory structure */
10649335bdeSbasabi int
rm_homedir(char * dir)10749335bdeSbasabi rm_homedir(char *dir)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate 	(void) sprintf( cmdbuf, "rm -rf %s", dir );
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	return( system( cmdbuf ) );
1127c478bd9Sstevel@tonic-gate }
113