xref: /illumos-gate/usr/src/cmd/oamuser/user/homedir.c (revision 49335bde)
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  */
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate /*
24*49335bdeSbasabi  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
28*49335bdeSbasabi /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29*49335bdeSbasabi /*	  All Rights Reserved  	*/
30*49335bdeSbasabi 
31*49335bdeSbasabi 
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>
387c478bd9Sstevel@tonic-gate #include "messages.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #define 	SBUFSZ	256
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate extern int mkdir(), chown(), rm_homedir();
437c478bd9Sstevel@tonic-gate extern char *prerrno();
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
52*49335bdeSbasabi create_home(char *homedir, char *skeldir, uid_t uid, gid_t gid)
53*49335bdeSbasabi 		/* home directory to create */
54*49335bdeSbasabi 		/* skel directory to copy if indicated */
55*49335bdeSbasabi 		/* uid of new user */
56*49335bdeSbasabi 		/* group id of new user */
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 	if( mkdir(homedir, 0775) != 0 ) {
597c478bd9Sstevel@tonic-gate 		errmsg( M_OOPS, "create the home directory", prerrno( 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",
657c478bd9Sstevel@tonic-gate 			prerrno( 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 ) {
757c478bd9Sstevel@tonic-gate 			errmsg( M_OOPS, "copy skeleton directory into home directory",
767c478bd9Sstevel@tonic-gate 				prerrno( 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",
867c478bd9Sstevel@tonic-gate 				prerrno( 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",
977c478bd9Sstevel@tonic-gate 				prerrno( 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 */
106*49335bdeSbasabi int
107*49335bdeSbasabi 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