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