xref: /onnv-gate/usr/src/cmd/tsol/updatehome/updatehome.c (revision 11561:e0d5740d4722)
14746Srica /*
24746Srica  * CDDL HEADER START
34746Srica  *
44746Srica  * The contents of this file are subject to the terms of the
54746Srica  * Common Development and Distribution License (the "License").
64746Srica  * You may not use this file except in compliance with the License.
74746Srica  *
84746Srica  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
94746Srica  * or http://www.opensolaris.org/os/licensing.
104746Srica  * See the License for the specific language governing permissions
114746Srica  * and limitations under the License.
124746Srica  *
134746Srica  * When distributing Covered Code, include this CDDL HEADER in each
144746Srica  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
154746Srica  * If applicable, add the following below this CDDL HEADER, with the
164746Srica  * fields enclosed by brackets "[]" replaced with your own identifying
174746Srica  * information: Portions Copyright [yyyy] [name of copyright owner]
184746Srica  *
194746Srica  * CDDL HEADER END
204746Srica  */
214746Srica 
224746Srica /*
23*11561SRic.Aleshire@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
244746Srica  * Use is subject to license terms.
254746Srica  */
264746Srica 
274746Srica /*
284746Srica  *	updatehome - Update the current label's $HOME copy and link files.
294746Srica  *
304746Srica  *		Update home reads the user's minimum label copy and link
314746Srica  *	control files (.copy_files and .link_files) which contain a list
324746Srica  *	of files to be copied and symbolically linked from the user's minimum
334746Srica  *	label $HOME to the user's current label's $HOME.
344746Srica  *
354746Srica  *		This is done by the Trusted Solaris dtsession whenever a
364746Srica  *	newly labeled workspace is created so that the user's favorite
374746Srica  *	files are available for use.  For example the user probably
384746Srica  *	wants a symlink to .profile, .login, .cshrc, .exrc, .mailrc, ~/bin,
394746Srica  *	... .  updatehome provides a convient mechanism for accomplishing
404746Srica  *	this.  The user may add any set of files either to be copied
414746Srica  *	(.copy_files), or symbolically linked (.link_files).
424746Srica  *
434746Srica  *		Files should not include embedded MLDs.
444746Srica  *
454746Srica  *	Entry	options = c, if replace existing current label $HOME copies
464746Srica  *			     (default is to ignore existing).
474746Srica  *			  d, if to print debug trace msgs (internal use only).
484746Srica  *			  i, if to ignore errors encountered (default is to
494746Srica  *			     abort).
504746Srica  *			  m, if to suppress error diagnostics -- perror
514746Srica  *			     (internal use only).
524746Srica  *			  r, if replace existing current label $HOME copies or
534746Srica  *			     symbolic links  -- implies c and s (default is to
544746Srica  *			     ignore existing).
554746Srica  *			  s, if replace existing current label $HOME symbolic
564746Srica  *			     links (default is to ignore existing).
574746Srica  *
584746Srica  *	Exit	stderr = diagnostic messages.
594746Srica  *		exis status = 0, no errors noted.
604746Srica  *			      1, if errors noted.
614746Srica  *
624746Srica  *	Calls	__setupfiles (which does all the real work).
634746Srica  */
644746Srica 
654746Srica 
664746Srica /*
674746Srica  *		There is a private contract between __setupfiles in this
684746Srica  *	directory and login.  Changes made to __setupfiles may need to be
694746Srica  *	reflected in the source for login.
704746Srica  *
714746Srica  *	G.Winiger 96/11/03
724746Srica  */
734746Srica 
744746Srica 
754746Srica #include <locale.h>
764746Srica #include <pwd.h>
774746Srica #include <stdio.h>
784746Srica #include <stdlib.h>
794746Srica #include <unistd.h>
804746Srica 
814746Srica #include <sys/types.h>
824746Srica 
834746Srica #include <tsol/label.h>
844746Srica #include <sys/tsol/label_macro.h>
854746Srica #include <user_attr.h>
864746Srica 
874746Srica #include "setupfiles.h"
884746Srica 
894746Srica #if !defined(TEXT_DOMAIN)
904746Srica #define	TEXT_DOMAIN	"SYS_TEST"
914746Srica #endif	/* !defined(TEXT_DOMAIN) */
924746Srica 
934746Srica int
main(int argc,char ** argv)944746Srica main(int argc, char **argv)
954746Srica {
964746Srica 	int		opt;		/* option switch value */
974746Srica 	int		flags;		/* setupfiles flags */
984746Srica 	uid_t		uid;
994746Srica 	extern int	opterr;		/* getopt error flag */
1004746Srica 	char		*kv_str = NULL;
1014746Srica 	struct passwd	*pwd;		/* current user's password file entry */
1024746Srica 	userattr_t	*userp = NULL;	/* current user's user_attr entry */
1034746Srica 	m_label_t	*min_sl;
1044746Srica 	m_label_t	*clearance;
1054746Srica 
1064746Srica 	(void) setlocale(LC_ALL, "");
1074746Srica 	(void) textdomain(TEXT_DOMAIN);
1084746Srica 
1094746Srica 	flags = DIAG;
1104746Srica 	opterr = 0;	/* handle errors here */
1114746Srica 
1124746Srica 	while ((opt = getopt(argc, argv, "cdimrs")) != EOF) {
1134746Srica 		switch (opt) {
1144746Srica 		case 'c':	/* replace existing copy */
1154746Srica 			flags |= REPC;
1164746Srica 			break;
1174746Srica 
1184746Srica 		case 'd':	/* debug */
1194746Srica 			flags |= DBUG;
1204746Srica 			break;
1214746Srica 
1224746Srica 		case 'i':	/* ignore copy/link errors */
1234746Srica 			flags |= IGNE;
1244746Srica 			break;
1254746Srica 
1264746Srica 		case 'm':	/* suppress error diagnostic (perror) */
1274746Srica 				/* prints */
1284746Srica 			flags &= ~DIAG;
1294746Srica 			break;
1304746Srica 
1314746Srica 		case 'r':		/* replace existing */
1324746Srica 			flags |= (REPC | REPL);
1334746Srica 			break;
1344746Srica 
1354746Srica 		case 's':	/* replace existing symbolic links */
1364746Srica 			flags |= REPL;
1374746Srica 			break;
1384746Srica 
1394746Srica 		case '?':		/* switch error */
1404746Srica 			(void) fprintf(stderr, gettext("Bad option -%c.\n"),
1414746Srica 			    (char)optopt);
1424746Srica 
1434746Srica 		default:
1444746Srica 			(void) fprintf(stderr, gettext("usage: %s [-cirs].\n"),
1454746Srica 			    argv[0]);
1464746Srica 			exit(1);
1474746Srica 			/*NOTREACHED*/
1484746Srica 		}  /* switch (opt) */
1494746Srica 	}  /* while ((opt = getopt()) */
1504746Srica 
1514746Srica 	uid = getuid();
1524746Srica 
1534746Srica 	if ((pwd = getpwuid(uid)) == (struct passwd *)0) {
1544746Srica 
1554746Srica 		(void) fprintf(stderr,
1564746Srica 		    gettext("Unable to get password entry for uid %d.\n"), uid);
1574746Srica 		exit(1);
1584746Srica 	}
1594746Srica 
1604746Srica 	min_sl = m_label_alloc(MAC_LABEL);
1614746Srica 	clearance = m_label_alloc(USER_CLEAR);
1624746Srica 
1634746Srica 	if (((userp = getusernam(pwd->pw_name)) == NULL) ||
1644746Srica 	    ((kv_str = kva_match(userp->attr, USERATTR_MINLABEL)) == NULL)) {
1654746Srica 
1664746Srica 		if (userdefs(min_sl, clearance) == -1) {
1674746Srica 			(void) fprintf(stderr,
1684746Srica 			    gettext("Unable to get default user labels.\n"));
1694746Srica 			exit(1);
1704746Srica 		}
1714746Srica 	}
1724746Srica 
1734746Srica 	if (kv_str != NULL) {
1744746Srica 		if (str_to_label(kv_str, &min_sl, MAC_LABEL, L_NO_CORRECTION,
1754746Srica 		    NULL) == -1) {
1764746Srica 			(void) fprintf(stderr,
177*11561SRic.Aleshire@Sun.COM 			    gettext("str_to_label failure on min_label for"
178*11561SRic.Aleshire@Sun.COM 			    " user %s.\n"), pwd->pw_name);
1794746Srica 			exit(1);
1804746Srica 		}
1814746Srica 	}
1824746Srica 
1834746Srica 	if (__setupfiles(pwd, min_sl, flags) != 0) {
1844746Srica 
1854746Srica 		(void) fprintf(stderr, gettext("%s failed.\n"), argv[0]);
1864746Srica 		exit(1);
1874746Srica 	}
1884746Srica 
1894746Srica 	return (0);
1904746Srica }  /* update home */
191