xref: /onnv-gate/usr/src/cmd/tsol/updatehome/updatehome.c (revision 4746:0bc0c48f4304)
1*4746Srica /*
2*4746Srica  * CDDL HEADER START
3*4746Srica  *
4*4746Srica  * The contents of this file are subject to the terms of the
5*4746Srica  * Common Development and Distribution License (the "License").
6*4746Srica  * You may not use this file except in compliance with the License.
7*4746Srica  *
8*4746Srica  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*4746Srica  * or http://www.opensolaris.org/os/licensing.
10*4746Srica  * See the License for the specific language governing permissions
11*4746Srica  * and limitations under the License.
12*4746Srica  *
13*4746Srica  * When distributing Covered Code, include this CDDL HEADER in each
14*4746Srica  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*4746Srica  * If applicable, add the following below this CDDL HEADER, with the
16*4746Srica  * fields enclosed by brackets "[]" replaced with your own identifying
17*4746Srica  * information: Portions Copyright [yyyy] [name of copyright owner]
18*4746Srica  *
19*4746Srica  * CDDL HEADER END
20*4746Srica  */
21*4746Srica 
22*4746Srica /*
23*4746Srica  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24*4746Srica  * Use is subject to license terms.
25*4746Srica  */
26*4746Srica 
27*4746Srica #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*4746Srica 
29*4746Srica /*
30*4746Srica  *	updatehome - Update the current label's $HOME copy and link files.
31*4746Srica  *
32*4746Srica  *		Update home reads the user's minimum label copy and link
33*4746Srica  *	control files (.copy_files and .link_files) which contain a list
34*4746Srica  *	of files to be copied and symbolically linked from the user's minimum
35*4746Srica  *	label $HOME to the user's current label's $HOME.
36*4746Srica  *
37*4746Srica  *		This is done by the Trusted Solaris dtsession whenever a
38*4746Srica  *	newly labeled workspace is created so that the user's favorite
39*4746Srica  *	files are available for use.  For example the user probably
40*4746Srica  *	wants a symlink to .profile, .login, .cshrc, .exrc, .mailrc, ~/bin,
41*4746Srica  *	... .  updatehome provides a convient mechanism for accomplishing
42*4746Srica  *	this.  The user may add any set of files either to be copied
43*4746Srica  *	(.copy_files), or symbolically linked (.link_files).
44*4746Srica  *
45*4746Srica  *		Files should not include embedded MLDs.
46*4746Srica  *
47*4746Srica  *	Entry	options = c, if replace existing current label $HOME copies
48*4746Srica  *			     (default is to ignore existing).
49*4746Srica  *			  d, if to print debug trace msgs (internal use only).
50*4746Srica  *			  i, if to ignore errors encountered (default is to
51*4746Srica  *			     abort).
52*4746Srica  *			  m, if to suppress error diagnostics -- perror
53*4746Srica  *			     (internal use only).
54*4746Srica  *			  r, if replace existing current label $HOME copies or
55*4746Srica  *			     symbolic links  -- implies c and s (default is to
56*4746Srica  *			     ignore existing).
57*4746Srica  *			  s, if replace existing current label $HOME symbolic
58*4746Srica  *			     links (default is to ignore existing).
59*4746Srica  *
60*4746Srica  *	Exit	stderr = diagnostic messages.
61*4746Srica  *		exis status = 0, no errors noted.
62*4746Srica  *			      1, if errors noted.
63*4746Srica  *
64*4746Srica  *	Calls	__setupfiles (which does all the real work).
65*4746Srica  */
66*4746Srica 
67*4746Srica 
68*4746Srica /*
69*4746Srica  *		There is a private contract between __setupfiles in this
70*4746Srica  *	directory and login.  Changes made to __setupfiles may need to be
71*4746Srica  *	reflected in the source for login.
72*4746Srica  *
73*4746Srica  *	G.Winiger 96/11/03
74*4746Srica  */
75*4746Srica 
76*4746Srica 
77*4746Srica #include <locale.h>
78*4746Srica #include <pwd.h>
79*4746Srica #include <stdio.h>
80*4746Srica #include <stdlib.h>
81*4746Srica #include <unistd.h>
82*4746Srica 
83*4746Srica #include <sys/types.h>
84*4746Srica 
85*4746Srica #include <tsol/label.h>
86*4746Srica #include <sys/tsol/label_macro.h>
87*4746Srica #include <user_attr.h>
88*4746Srica 
89*4746Srica #include "setupfiles.h"
90*4746Srica 
91*4746Srica #if !defined(TEXT_DOMAIN)
92*4746Srica #define	TEXT_DOMAIN	"SYS_TEST"
93*4746Srica #endif	/* !defined(TEXT_DOMAIN) */
94*4746Srica 
95*4746Srica int
96*4746Srica main(int argc, char **argv)
97*4746Srica {
98*4746Srica 	int		opt;		/* option switch value */
99*4746Srica 	int		flags;		/* setupfiles flags */
100*4746Srica 	uid_t		uid;
101*4746Srica 	extern int	opterr;		/* getopt error flag */
102*4746Srica 	char		*kv_str = NULL;
103*4746Srica 	struct passwd	*pwd;		/* current user's password file entry */
104*4746Srica 	userattr_t	*userp = NULL;	/* current user's user_attr entry */
105*4746Srica 	m_label_t	*min_sl;
106*4746Srica 	m_label_t	*clearance;
107*4746Srica 
108*4746Srica 	(void) setlocale(LC_ALL, "");
109*4746Srica 	(void) textdomain(TEXT_DOMAIN);
110*4746Srica 
111*4746Srica 	flags = DIAG;
112*4746Srica 	opterr = 0;	/* handle errors here */
113*4746Srica 
114*4746Srica 	while ((opt = getopt(argc, argv, "cdimrs")) != EOF) {
115*4746Srica 		switch (opt) {
116*4746Srica 		case 'c':	/* replace existing copy */
117*4746Srica 			flags |= REPC;
118*4746Srica 			break;
119*4746Srica 
120*4746Srica 		case 'd':	/* debug */
121*4746Srica 			flags |= DBUG;
122*4746Srica 			break;
123*4746Srica 
124*4746Srica 		case 'i':	/* ignore copy/link errors */
125*4746Srica 			flags |= IGNE;
126*4746Srica 			break;
127*4746Srica 
128*4746Srica 		case 'm':	/* suppress error diagnostic (perror) */
129*4746Srica 				/* prints */
130*4746Srica 			flags &= ~DIAG;
131*4746Srica 			break;
132*4746Srica 
133*4746Srica 		case 'r':		/* replace existing */
134*4746Srica 			flags |= (REPC | REPL);
135*4746Srica 			break;
136*4746Srica 
137*4746Srica 		case 's':	/* replace existing symbolic links */
138*4746Srica 			flags |= REPL;
139*4746Srica 			break;
140*4746Srica 
141*4746Srica 		case '?':		/* switch error */
142*4746Srica 			(void) fprintf(stderr, gettext("Bad option -%c.\n"),
143*4746Srica 			    (char)optopt);
144*4746Srica 
145*4746Srica 		default:
146*4746Srica 			(void) fprintf(stderr, gettext("usage: %s [-cirs].\n"),
147*4746Srica 			    argv[0]);
148*4746Srica 			exit(1);
149*4746Srica 			/*NOTREACHED*/
150*4746Srica 		}  /* switch (opt) */
151*4746Srica 	}  /* while ((opt = getopt()) */
152*4746Srica 
153*4746Srica 	uid = getuid();
154*4746Srica 
155*4746Srica 	if ((pwd = getpwuid(uid)) == (struct passwd *)0) {
156*4746Srica 
157*4746Srica 		(void) fprintf(stderr,
158*4746Srica 		    gettext("Unable to get password entry for uid %d.\n"), uid);
159*4746Srica 		exit(1);
160*4746Srica 	}
161*4746Srica 
162*4746Srica 	min_sl = m_label_alloc(MAC_LABEL);
163*4746Srica 	clearance = m_label_alloc(USER_CLEAR);
164*4746Srica 
165*4746Srica 	if (((userp = getusernam(pwd->pw_name)) == NULL) ||
166*4746Srica 	    ((kv_str = kva_match(userp->attr, USERATTR_MINLABEL)) == NULL)) {
167*4746Srica 
168*4746Srica 		if (userdefs(min_sl, clearance) == -1) {
169*4746Srica 			(void) fprintf(stderr,
170*4746Srica 			    gettext("Unable to get default user labels.\n"));
171*4746Srica 			exit(1);
172*4746Srica 		}
173*4746Srica 	}
174*4746Srica 
175*4746Srica 	if (kv_str != NULL) {
176*4746Srica 
177*4746Srica 		if (str_to_label(kv_str, &min_sl, MAC_LABEL, L_NO_CORRECTION,
178*4746Srica 		    NULL) == -1) {
179*4746Srica 			(void) fprintf(stderr,
180*4746Srica 			    gettext("stobsl failure on min_label for user"
181*4746Srica 			    " %s.\n"), pwd->pw_name);
182*4746Srica 			exit(1);
183*4746Srica 		}
184*4746Srica 	}
185*4746Srica 
186*4746Srica 	if (__setupfiles(pwd, min_sl, flags) != 0) {
187*4746Srica 
188*4746Srica 		(void) fprintf(stderr, gettext("%s failed.\n"), argv[0]);
189*4746Srica 		exit(1);
190*4746Srica 	}
191*4746Srica 
192*4746Srica 	return (0);
193*4746Srica }  /* update home */
194