xref: /titanic_51/usr/src/cmd/hal/hald/util_helper.c (revision 80feccabd0bc0fca1df2b60bfa50449d9a6e3e09)
1 /***************************************************************************
2  *
3  * util_helper.c - HAL utilities for helper (as e.g. prober/addons) et al.
4  *
5  * Copyright (C) 2006 David Zeuthen, <david@fubar.dk>
6  *
7  * Licensed under the Academic Free License version 2.1
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22  *
23  **************************************************************************/
24 
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28 
29 #include <grp.h>
30 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/time.h>
34 #include <time.h>
35 #include <pwd.h>
36 #include <unistd.h>
37 
38 #include "logger.h"
39 
40 #include "util_helper.h"
41 
42 #ifdef __linux__
43 extern char **environ;
44 #endif
45 
46 static char **argv_buffer = NULL;
47 static size_t argv_size = 0;
48 
49 #ifdef sun
50 #include <priv.h>
51 void
52 drop_privileges (int keep_auxgroups)
53 {
54 	priv_set_t *pPrivSet = NULL;
55 	priv_set_t *lPrivSet = NULL;
56 
57 	/*
58 	 * Start with the 'basic' privilege set and then remove any
59 	 * of the 'basic' privileges that will not be needed.
60 	 */
61 	if ((pPrivSet = priv_str_to_set("basic", ",", NULL)) == NULL) {
62 		return;
63 	}
64 
65 	/* Clear privileges we will not need from the 'basic' set */
66 	(void) priv_delset(pPrivSet, PRIV_FILE_LINK_ANY);
67 	(void) priv_delset(pPrivSet, PRIV_PROC_INFO);
68 	(void) priv_delset(pPrivSet, PRIV_PROC_SESSION);
69 
70 	/* for sysevent need to be root and have this privilege */
71 	(void) priv_addset(pPrivSet, PRIV_SYS_CONFIG);
72 
73 	/* Set the permitted privilege set. */
74 	if (setppriv(PRIV_SET, PRIV_PERMITTED, pPrivSet) != 0) {
75 		return;
76 	}
77 
78 	/* Clear the limit set. */
79 	if ((lPrivSet = priv_allocset()) == NULL) {
80 		return;
81 	}
82 
83 	priv_emptyset(lPrivSet);
84 
85 	if (setppriv(PRIV_SET, PRIV_LIMIT, lPrivSet) != 0) {
86 		return;
87 	}
88 
89 	priv_freeset(lPrivSet);
90 }
91 #else /* !sun */
92 
93 /** Drop root privileges: Set the running user id to HAL_USER and
94  *  group to HAL_GROUP, and optionally retain auxiliary groups of HAL_USER.
95  */
96 void
97 drop_privileges (int keep_auxgroups)
98 {
99 	struct passwd *pw = NULL;
100 	struct group *gr = NULL;
101 
102 	/* determine user id */
103 	pw = getpwnam (HAL_USER);
104 	if (!pw)  {
105 		HAL_DEBUG (("drop_privileges: user " HAL_USER " does not exist"));
106 		exit (-1);
107 	}
108 
109 	/* determine primary group id */
110 	gr = getgrnam (HAL_GROUP);
111 	if (!gr) {
112 		HAL_DEBUG (("drop_privileges: group " HAL_GROUP " does not exist"));
113 		exit (-1);
114 	}
115 
116 	if (keep_auxgroups) {
117 		if (initgroups (HAL_USER, gr->gr_gid)) {
118 			HAL_DEBUG(("drop_privileges: could not initialize groups"));
119 			exit (-1);
120 		}
121 	}
122 
123 	if (setgid (gr->gr_gid)) {
124 		HAL_DEBUG (("drop_privileges: could not set group id"));
125 		exit (-1);
126 	}
127 
128 	if (setuid (pw->pw_uid)) {
129 		HAL_DEBUG (("drop_privileges: could not set user id"));
130 		exit (-1);
131 	}
132 }
133 #endif /* !sun */
134 
135 void
136 hal_set_proc_title_init (int argc, char *argv[])
137 {
138 #ifdef __linux__
139 	unsigned int i;
140 	char **new_environ, *endptr;
141 
142 	/* This code is really really ugly. We make some memory layout
143 	 * assumptions and reuse the environment array as memory to store
144 	 * our process title in */
145 
146 	for (i = 0; environ[i] != NULL; i++)
147 		;
148 
149 	endptr = i ? environ[i-1] + strlen (environ[i-1]) : argv[argc-1] + strlen (argv[argc-1]);
150 
151 	argv_buffer = argv;
152 	argv_size = endptr - argv_buffer[0];
153 
154 	/* Make a copy of environ */
155 
156 	new_environ = malloc (sizeof(char*) * (i + 1));
157 	for (i = 0; environ[i] != NULL; i++)
158 		new_environ[i] = strdup (environ[i]);
159 	new_environ[i] = NULL;
160 
161 	environ = new_environ;
162 #endif
163 }
164 
165 /* this code borrowed from avahi-daemon's setproctitle.c (LGPL v2) */
166 void
167 hal_set_proc_title (const char *format, ...)
168 {
169 #ifdef __linux__
170 	size_t len;
171 	va_list ap;
172 
173 	if (argv_buffer == NULL)
174 		goto out;
175 
176 	va_start (ap, format);
177 	vsnprintf (argv_buffer[0], argv_size, format, ap);
178 	va_end (ap);
179 
180 	len = strlen (argv_buffer[0]);
181 
182 	memset (argv_buffer[0] + len, 0, argv_size - len);
183 	argv_buffer[1] = NULL;
184 out:
185 	;
186 #endif
187 }
188 
189