login_cap.c revision 1.6 1 1.6 ad /* $NetBSD: login_cap.c,v 1.6 2000/07/05 11:46:41 ad Exp $ */
2 1.1 mjl
3 1.1 mjl /*-
4 1.1 mjl * Copyright (c) 1995,1997 Berkeley Software Design, Inc. All rights reserved.
5 1.1 mjl *
6 1.1 mjl * Redistribution and use in source and binary forms, with or without
7 1.1 mjl * modification, are permitted provided that the following conditions
8 1.1 mjl * are met:
9 1.1 mjl * 1. Redistributions of source code must retain the above copyright
10 1.1 mjl * notice, this list of conditions and the following disclaimer.
11 1.1 mjl * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 mjl * notice, this list of conditions and the following disclaimer in the
13 1.1 mjl * documentation and/or other materials provided with the distribution.
14 1.1 mjl * 3. All advertising materials mentioning features or use of this software
15 1.1 mjl * must display the following acknowledgement:
16 1.1 mjl * This product includes software developed by Berkeley Software Design,
17 1.1 mjl * Inc.
18 1.1 mjl * 4. The name of Berkeley Software Design, Inc. may not be used to endorse
19 1.1 mjl * or promote products derived from this software without specific prior
20 1.1 mjl * written permission.
21 1.1 mjl *
22 1.1 mjl * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN, INC. ``AS IS'' AND
23 1.1 mjl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 mjl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 mjl * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN, INC. BE LIABLE
26 1.1 mjl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 mjl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 mjl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 mjl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 mjl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 mjl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 mjl * SUCH DAMAGE.
33 1.1 mjl *
34 1.1 mjl * BSDI login_cap.c,v 2.13 1998/02/07 03:17:05 prb Exp
35 1.1 mjl */
36 1.6 ad
37 1.6 ad #include <sys/cdefs.h>
38 1.6 ad #if defined(LIBC_SCCS) && !defined(lint)
39 1.6 ad __RCSID("$NetBSD: login_cap.c,v 1.6 2000/07/05 11:46:41 ad Exp $");
40 1.6 ad #endif /* LIBC_SCCS and not lint */
41 1.1 mjl
42 1.1 mjl #include <sys/types.h>
43 1.1 mjl #include <sys/stat.h>
44 1.1 mjl #include <sys/time.h>
45 1.1 mjl #include <sys/resource.h>
46 1.1 mjl
47 1.4 mjl #include <ctype.h>
48 1.1 mjl #include <err.h>
49 1.1 mjl #include <errno.h>
50 1.1 mjl #include <fcntl.h>
51 1.1 mjl #include <limits.h>
52 1.1 mjl #include <login_cap.h>
53 1.1 mjl #include <paths.h>
54 1.1 mjl #include <pwd.h>
55 1.1 mjl #include <stdio.h>
56 1.1 mjl #include <stdlib.h>
57 1.1 mjl #include <string.h>
58 1.1 mjl #include <syslog.h>
59 1.1 mjl #include <unistd.h>
60 1.1 mjl
61 1.6 ad static char *classfiles[] = { _PATH_LOGIN_CONF, 0 };
62 1.1 mjl
63 1.6 ad static void setuserpath(login_cap_t *, char *);
64 1.6 ad static u_quad_t multiply(u_quad_t, u_quad_t);
65 1.6 ad static u_quad_t strtolimit(char *, char **, int);
66 1.6 ad static u_quad_t strtosize(char *, char **, int);
67 1.6 ad static int gsetrl(login_cap_t *, int, char *, int type);
68 1.6 ad static int setuserenv(login_cap_t *);
69 1.6 ad static int isinfinite(const char *);
70 1.1 mjl
71 1.1 mjl login_cap_t *
72 1.6 ad login_getclass(char *class)
73 1.1 mjl {
74 1.1 mjl login_cap_t *lc;
75 1.1 mjl int res;
76 1.1 mjl
77 1.1 mjl for (res = 0; classfiles[res]; ++res)
78 1.1 mjl if (secure_path(classfiles[res]) < 0)
79 1.1 mjl return (0);
80 1.1 mjl
81 1.1 mjl if ((lc = malloc(sizeof(login_cap_t))) == NULL) {
82 1.1 mjl syslog(LOG_ERR, "%s:%d malloc: %m", __FILE__, __LINE__);
83 1.1 mjl return (0);
84 1.1 mjl }
85 1.1 mjl
86 1.1 mjl lc->lc_cap = 0;
87 1.1 mjl lc->lc_style = 0;
88 1.1 mjl
89 1.1 mjl if (class == NULL || class[0] == '\0')
90 1.1 mjl class = LOGIN_DEFCLASS;
91 1.1 mjl
92 1.1 mjl if ((lc->lc_class = strdup(class)) == NULL) {
93 1.1 mjl syslog(LOG_ERR, "%s:%d strdup: %m", __FILE__, __LINE__);
94 1.1 mjl free(lc);
95 1.1 mjl return (0);
96 1.1 mjl }
97 1.1 mjl
98 1.1 mjl if ((res = cgetent(&lc->lc_cap, classfiles, lc->lc_class)) != 0 ) {
99 1.1 mjl lc->lc_cap = 0;
100 1.1 mjl switch (res) {
101 1.1 mjl case 1:
102 1.1 mjl syslog(LOG_ERR, "%s: couldn't resolve 'tc'",
103 1.1 mjl lc->lc_class);
104 1.1 mjl break;
105 1.1 mjl case -1:
106 1.1 mjl if ((res = open(classfiles[0], 0)) >= 0)
107 1.1 mjl close(res);
108 1.1 mjl if (strcmp(lc->lc_class, LOGIN_DEFCLASS) == NULL &&
109 1.1 mjl res < 0)
110 1.1 mjl return (lc);
111 1.1 mjl syslog(LOG_ERR, "%s: unknown class", lc->lc_class);
112 1.1 mjl break;
113 1.1 mjl case -2:
114 1.1 mjl syslog(LOG_ERR, "%s: getting class information: %m",
115 1.1 mjl lc->lc_class);
116 1.1 mjl break;
117 1.1 mjl case -3:
118 1.1 mjl syslog(LOG_ERR, "%s: 'tc' reference loop",
119 1.1 mjl lc->lc_class);
120 1.1 mjl break;
121 1.1 mjl default:
122 1.1 mjl syslog(LOG_ERR, "%s: unexpected cgetent error",
123 1.1 mjl lc->lc_class);
124 1.1 mjl break;
125 1.1 mjl }
126 1.1 mjl free(lc->lc_class);
127 1.1 mjl free(lc);
128 1.1 mjl return (0);
129 1.1 mjl }
130 1.1 mjl return (lc);
131 1.1 mjl }
132 1.1 mjl
133 1.4 mjl login_cap_t *
134 1.6 ad login_getpwclass(const struct passwd *pwd)
135 1.4 mjl {
136 1.4 mjl return login_getclass(pwd ? pwd->pw_class : NULL);
137 1.4 mjl }
138 1.4 mjl
139 1.1 mjl char *
140 1.6 ad login_getcapstr(login_cap_t *lc, char *cap, char *def, char *e)
141 1.1 mjl {
142 1.1 mjl char *res;
143 1.1 mjl int status;
144 1.1 mjl
145 1.1 mjl errno = 0;
146 1.1 mjl
147 1.2 mjl if (!lc || !lc->lc_cap)
148 1.1 mjl return (def);
149 1.1 mjl
150 1.1 mjl switch (status = cgetstr(lc->lc_cap, cap, &res)) {
151 1.1 mjl case -1:
152 1.1 mjl return (def);
153 1.1 mjl case -2:
154 1.1 mjl syslog(LOG_ERR, "%s: getting capability %s: %m",
155 1.1 mjl lc->lc_class, cap);
156 1.1 mjl return (e);
157 1.1 mjl default:
158 1.1 mjl if (status >= 0)
159 1.1 mjl return (res);
160 1.1 mjl syslog(LOG_ERR, "%s: unexpected error with capability %s",
161 1.1 mjl lc->lc_class, cap);
162 1.1 mjl return (e);
163 1.1 mjl }
164 1.1 mjl }
165 1.1 mjl
166 1.1 mjl quad_t
167 1.6 ad login_getcaptime(login_cap_t *lc, char *cap, quad_t def, quad_t e)
168 1.1 mjl {
169 1.1 mjl char *ep;
170 1.1 mjl char *res, *sres;
171 1.1 mjl int status;
172 1.1 mjl quad_t q, r;
173 1.1 mjl
174 1.1 mjl errno = 0;
175 1.2 mjl if (!lc || !lc->lc_cap)
176 1.1 mjl return (def);
177 1.1 mjl
178 1.1 mjl switch (status = cgetstr(lc->lc_cap, cap, &res)) {
179 1.1 mjl case -1:
180 1.1 mjl return (def);
181 1.1 mjl case -2:
182 1.1 mjl syslog(LOG_ERR, "%s: getting capability %s: %m",
183 1.1 mjl lc->lc_class, cap);
184 1.1 mjl errno = ERANGE;
185 1.1 mjl return (e);
186 1.1 mjl default:
187 1.1 mjl if (status >= 0)
188 1.1 mjl break;
189 1.1 mjl syslog(LOG_ERR, "%s: unexpected error with capability %s",
190 1.1 mjl lc->lc_class, cap);
191 1.1 mjl errno = ERANGE;
192 1.1 mjl return (e);
193 1.1 mjl }
194 1.1 mjl
195 1.5 mjl if (isinfinite(res))
196 1.1 mjl return (RLIM_INFINITY);
197 1.1 mjl
198 1.1 mjl errno = 0;
199 1.1 mjl
200 1.1 mjl q = 0;
201 1.1 mjl sres = res;
202 1.1 mjl while (*res) {
203 1.1 mjl r = strtoq(res, &ep, 0);
204 1.1 mjl if (!ep || ep == res ||
205 1.1 mjl ((r == QUAD_MIN || r == QUAD_MAX) && errno == ERANGE)) {
206 1.1 mjl invalid:
207 1.1 mjl syslog(LOG_ERR, "%s:%s=%s: invalid time",
208 1.1 mjl lc->lc_class, cap, sres);
209 1.1 mjl errno = ERANGE;
210 1.1 mjl return (e);
211 1.1 mjl }
212 1.1 mjl switch (*ep++) {
213 1.1 mjl case '\0':
214 1.1 mjl --ep;
215 1.1 mjl break;
216 1.1 mjl case 's': case 'S':
217 1.1 mjl break;
218 1.1 mjl case 'm': case 'M':
219 1.1 mjl r *= 60;
220 1.1 mjl break;
221 1.1 mjl case 'h': case 'H':
222 1.1 mjl r *= 60 * 60;
223 1.1 mjl break;
224 1.1 mjl case 'd': case 'D':
225 1.1 mjl r *= 60 * 60 * 24;
226 1.1 mjl break;
227 1.1 mjl case 'w': case 'W':
228 1.1 mjl r *= 60 * 60 * 24 * 7;
229 1.1 mjl break;
230 1.1 mjl case 'y': case 'Y': /* Pretty absurd */
231 1.1 mjl r *= 60 * 60 * 24 * 365;
232 1.1 mjl break;
233 1.1 mjl default:
234 1.1 mjl goto invalid;
235 1.1 mjl }
236 1.1 mjl res = ep;
237 1.1 mjl q += r;
238 1.1 mjl }
239 1.1 mjl return (q);
240 1.1 mjl }
241 1.1 mjl
242 1.1 mjl quad_t
243 1.6 ad login_getcapnum(login_cap_t *lc, char *cap, quad_t def, quad_t e)
244 1.1 mjl {
245 1.1 mjl char *ep;
246 1.1 mjl char *res;
247 1.1 mjl int status;
248 1.1 mjl quad_t q;
249 1.1 mjl
250 1.1 mjl errno = 0;
251 1.2 mjl if (!lc || !lc->lc_cap)
252 1.1 mjl return (def);
253 1.1 mjl
254 1.1 mjl switch (status = cgetstr(lc->lc_cap, cap, &res)) {
255 1.1 mjl case -1:
256 1.1 mjl return (def);
257 1.1 mjl case -2:
258 1.1 mjl syslog(LOG_ERR, "%s: getting capability %s: %m",
259 1.1 mjl lc->lc_class, cap);
260 1.1 mjl errno = ERANGE;
261 1.1 mjl return (e);
262 1.1 mjl default:
263 1.1 mjl if (status >= 0)
264 1.1 mjl break;
265 1.1 mjl syslog(LOG_ERR, "%s: unexpected error with capability %s",
266 1.1 mjl lc->lc_class, cap);
267 1.1 mjl errno = ERANGE;
268 1.1 mjl return (e);
269 1.1 mjl }
270 1.1 mjl
271 1.5 mjl if (isinfinite(res))
272 1.1 mjl return (RLIM_INFINITY);
273 1.1 mjl
274 1.1 mjl errno = 0;
275 1.1 mjl q = strtoq(res, &ep, 0);
276 1.1 mjl if (!ep || ep == res || ep[0] ||
277 1.1 mjl ((q == QUAD_MIN || q == QUAD_MAX) && errno == ERANGE)) {
278 1.1 mjl syslog(LOG_ERR, "%s:%s=%s: invalid number",
279 1.1 mjl lc->lc_class, cap, res);
280 1.1 mjl errno = ERANGE;
281 1.1 mjl return (e);
282 1.1 mjl }
283 1.1 mjl return (q);
284 1.1 mjl }
285 1.1 mjl
286 1.1 mjl quad_t
287 1.6 ad login_getcapsize(login_cap_t *lc, char *cap, quad_t def, quad_t e)
288 1.1 mjl {
289 1.1 mjl char *ep;
290 1.1 mjl char *res;
291 1.1 mjl int status;
292 1.1 mjl quad_t q;
293 1.1 mjl
294 1.1 mjl errno = 0;
295 1.1 mjl
296 1.2 mjl if (!lc || !lc->lc_cap)
297 1.1 mjl return (def);
298 1.1 mjl
299 1.1 mjl switch (status = cgetstr(lc->lc_cap, cap, &res)) {
300 1.1 mjl case -1:
301 1.1 mjl return (def);
302 1.1 mjl case -2:
303 1.1 mjl syslog(LOG_ERR, "%s: getting capability %s: %m",
304 1.1 mjl lc->lc_class, cap);
305 1.1 mjl errno = ERANGE;
306 1.1 mjl return (e);
307 1.1 mjl default:
308 1.1 mjl if (status >= 0)
309 1.1 mjl break;
310 1.1 mjl syslog(LOG_ERR, "%s: unexpected error with capability %s",
311 1.1 mjl lc->lc_class, cap);
312 1.1 mjl errno = ERANGE;
313 1.1 mjl return (e);
314 1.1 mjl }
315 1.1 mjl
316 1.1 mjl errno = 0;
317 1.1 mjl q = strtolimit(res, &ep, 0);
318 1.1 mjl if (!ep || ep == res || (ep[0] && ep[1]) ||
319 1.1 mjl ((q == QUAD_MIN || q == QUAD_MAX) && errno == ERANGE)) {
320 1.1 mjl syslog(LOG_ERR, "%s:%s=%s: invalid size",
321 1.1 mjl lc->lc_class, cap, res);
322 1.1 mjl errno = ERANGE;
323 1.1 mjl return (e);
324 1.1 mjl }
325 1.1 mjl return (q);
326 1.1 mjl }
327 1.1 mjl
328 1.1 mjl int
329 1.6 ad login_getcapbool(login_cap_t *lc, char *cap, u_int def)
330 1.1 mjl {
331 1.2 mjl if (!lc || !lc->lc_cap)
332 1.1 mjl return (def);
333 1.1 mjl
334 1.1 mjl return (cgetcap(lc->lc_cap, cap, ':') != NULL);
335 1.1 mjl }
336 1.1 mjl
337 1.1 mjl void
338 1.6 ad login_close(login_cap_t *lc)
339 1.1 mjl {
340 1.1 mjl if (lc) {
341 1.1 mjl if (lc->lc_class)
342 1.1 mjl free(lc->lc_class);
343 1.1 mjl if (lc->lc_cap)
344 1.1 mjl free(lc->lc_cap);
345 1.1 mjl if (lc->lc_style)
346 1.1 mjl free(lc->lc_style);
347 1.1 mjl free(lc);
348 1.1 mjl }
349 1.1 mjl }
350 1.1 mjl
351 1.1 mjl #define CTIME 1
352 1.1 mjl #define CSIZE 2
353 1.1 mjl #define CNUMB 3
354 1.1 mjl
355 1.1 mjl static struct {
356 1.1 mjl int what;
357 1.1 mjl int type;
358 1.1 mjl char * name;
359 1.1 mjl } r_list[] = {
360 1.1 mjl { RLIMIT_CPU, CTIME, "cputime", },
361 1.1 mjl { RLIMIT_FSIZE, CSIZE, "filesize", },
362 1.1 mjl { RLIMIT_DATA, CSIZE, "datasize", },
363 1.1 mjl { RLIMIT_STACK, CSIZE, "stacksize", },
364 1.1 mjl { RLIMIT_RSS, CSIZE, "memoryuse", },
365 1.1 mjl { RLIMIT_MEMLOCK, CSIZE, "memorylocked", },
366 1.1 mjl { RLIMIT_NPROC, CNUMB, "maxproc", },
367 1.1 mjl { RLIMIT_NOFILE, CNUMB, "openfiles", },
368 1.1 mjl { RLIMIT_CORE, CSIZE, "coredumpsize", },
369 1.1 mjl { -1, 0, 0 }
370 1.1 mjl };
371 1.1 mjl
372 1.1 mjl static int
373 1.6 ad gsetrl(login_cap_t *lc, int what, char *name, int type)
374 1.1 mjl {
375 1.1 mjl struct rlimit rl;
376 1.1 mjl struct rlimit r;
377 1.1 mjl char name_cur[32];
378 1.1 mjl char name_max[32];
379 1.1 mjl
380 1.1 mjl sprintf(name_cur, "%s-cur", name);
381 1.1 mjl sprintf(name_max, "%s-max", name);
382 1.1 mjl
383 1.1 mjl if (getrlimit(what, &r)) {
384 1.1 mjl syslog(LOG_ERR, "getting resource limit: %m");
385 1.1 mjl return (-1);
386 1.1 mjl }
387 1.1 mjl
388 1.1 mjl #define RCUR r.rlim_cur
389 1.1 mjl #define RMAX r.rlim_max
390 1.1 mjl
391 1.1 mjl switch (type) {
392 1.1 mjl case CTIME:
393 1.1 mjl RCUR = login_getcaptime(lc, name, RCUR, RCUR);
394 1.1 mjl RMAX = login_getcaptime(lc, name, RMAX, RMAX);
395 1.1 mjl rl.rlim_cur = login_getcaptime(lc, name_cur, RCUR, RCUR);
396 1.1 mjl rl.rlim_max = login_getcaptime(lc, name_max, RMAX, RMAX);
397 1.1 mjl break;
398 1.1 mjl case CSIZE:
399 1.1 mjl RCUR = login_getcapsize(lc, name, RCUR, RCUR);
400 1.1 mjl RMAX = login_getcapsize(lc, name, RMAX, RMAX);
401 1.1 mjl rl.rlim_cur = login_getcapsize(lc, name_cur, RCUR, RCUR);
402 1.1 mjl rl.rlim_max = login_getcapsize(lc, name_max, RMAX, RMAX);
403 1.1 mjl break;
404 1.1 mjl case CNUMB:
405 1.1 mjl RCUR = login_getcapnum(lc, name, RCUR, RCUR);
406 1.1 mjl RMAX = login_getcapnum(lc, name, RMAX, RMAX);
407 1.1 mjl rl.rlim_cur = login_getcapnum(lc, name_cur, RCUR, RCUR);
408 1.1 mjl rl.rlim_max = login_getcapnum(lc, name_max, RMAX, RMAX);
409 1.1 mjl break;
410 1.1 mjl default:
411 1.1 mjl return (-1);
412 1.1 mjl }
413 1.1 mjl
414 1.1 mjl if (setrlimit(what, &rl)) {
415 1.1 mjl syslog(LOG_ERR, "%s: setting resource limit %s: %m",
416 1.1 mjl lc->lc_class, name);
417 1.1 mjl return (-1);
418 1.1 mjl }
419 1.1 mjl #undef RCUR
420 1.1 mjl #undef RMAX
421 1.1 mjl return (0);
422 1.1 mjl }
423 1.1 mjl
424 1.4 mjl static int
425 1.6 ad setuserenv(login_cap_t *lc)
426 1.4 mjl {
427 1.4 mjl char *stop = ", \t";
428 1.4 mjl int i, count;
429 1.4 mjl char *ptr;
430 1.4 mjl char **res;
431 1.4 mjl char *str = login_getcapstr(lc, "setenv", NULL, NULL);
432 1.4 mjl
433 1.4 mjl if(str == NULL || *str == '\0')
434 1.4 mjl return 0;
435 1.4 mjl
436 1.4 mjl /* count the sub-strings */
437 1.4 mjl for (i = 1, ptr = str; *ptr; i++) {
438 1.4 mjl ptr += strcspn(ptr, stop);
439 1.4 mjl if (*ptr)
440 1.4 mjl ptr++;
441 1.4 mjl }
442 1.4 mjl
443 1.4 mjl /* allocate ptr array and string */
444 1.4 mjl count = i;
445 1.4 mjl res = malloc( count * sizeof(char *) + strlen(str) + 1 );
446 1.4 mjl
447 1.4 mjl if(!res)
448 1.4 mjl return -1;
449 1.4 mjl
450 1.4 mjl ptr = (char *)res + count * sizeof(char *);
451 1.4 mjl strcpy(ptr, str);
452 1.4 mjl
453 1.4 mjl /* split string */
454 1.4 mjl for (i = 0; *ptr && i < count; i++) {
455 1.4 mjl res[i] = ptr;
456 1.4 mjl ptr += strcspn(ptr, stop);
457 1.4 mjl if (*ptr)
458 1.4 mjl *ptr++ = '\0';
459 1.4 mjl }
460 1.4 mjl
461 1.4 mjl res[i] = NULL;
462 1.4 mjl
463 1.4 mjl for (i = 0; i < count && res[i]; i++) {
464 1.4 mjl if (*res[i] != '\0') {
465 1.6 ad if ((ptr = strchr(res[i], '=')) != NULL)
466 1.4 mjl *ptr++ = '\0';
467 1.4 mjl else
468 1.4 mjl ptr = "";
469 1.4 mjl setenv(res[i], ptr, 1);
470 1.4 mjl }
471 1.4 mjl }
472 1.4 mjl
473 1.5 mjl free(res);
474 1.4 mjl return 0;
475 1.4 mjl }
476 1.4 mjl
477 1.1 mjl int
478 1.6 ad setclasscontext(char *class, u_int flags)
479 1.1 mjl {
480 1.1 mjl int ret;
481 1.1 mjl login_cap_t *lc;
482 1.1 mjl
483 1.1 mjl flags &= LOGIN_SETRESOURCES | LOGIN_SETPRIORITY | LOGIN_SETUMASK |
484 1.1 mjl LOGIN_SETPATH;
485 1.1 mjl
486 1.1 mjl lc = login_getclass(class);
487 1.1 mjl ret = lc ? setusercontext(lc, NULL, 0, flags) : -1;
488 1.1 mjl login_close(lc);
489 1.1 mjl return (ret);
490 1.1 mjl }
491 1.1 mjl
492 1.1 mjl int
493 1.6 ad setusercontext(login_cap_t *lc, struct passwd *pwd, uid_t uid, u_int flags)
494 1.1 mjl {
495 1.1 mjl login_cap_t *flc;
496 1.1 mjl quad_t p;
497 1.1 mjl int i;
498 1.1 mjl
499 1.1 mjl flc = NULL;
500 1.1 mjl
501 1.3 mjl if (!lc)
502 1.3 mjl flc = lc = login_getclass(pwd ? pwd->pw_class : NULL);
503 1.1 mjl
504 1.1 mjl /*
505 1.1 mjl * Without the pwd entry being passed we cannot set either
506 1.1 mjl * the group or the login. We could complain about it.
507 1.1 mjl */
508 1.1 mjl if (pwd == NULL)
509 1.1 mjl flags &= ~(LOGIN_SETGROUP|LOGIN_SETLOGIN);
510 1.1 mjl
511 1.1 mjl if (flags & LOGIN_SETRESOURCES)
512 1.1 mjl for (i = 0; r_list[i].name; ++i)
513 1.1 mjl if (gsetrl(lc, r_list[i].what, r_list[i].name,
514 1.1 mjl r_list[i].type))
515 1.1 mjl /* XXX - call syslog()? */;
516 1.1 mjl
517 1.1 mjl if (flags & LOGIN_SETPRIORITY) {
518 1.1 mjl p = login_getcapnum(lc, "priority", 0LL, 0LL);
519 1.1 mjl
520 1.1 mjl if (setpriority(PRIO_PROCESS, 0, (int)p) < 0)
521 1.1 mjl syslog(LOG_ERR, "%s: setpriority: %m", lc->lc_class);
522 1.1 mjl }
523 1.1 mjl
524 1.1 mjl if (flags & LOGIN_SETUMASK) {
525 1.1 mjl p = login_getcapnum(lc, "umask", (quad_t) LOGIN_DEFUMASK,
526 1.1 mjl (quad_t) LOGIN_DEFUMASK);
527 1.1 mjl umask((mode_t)p);
528 1.1 mjl }
529 1.1 mjl
530 1.1 mjl if (flags & LOGIN_SETGROUP) {
531 1.1 mjl if (setgid(pwd->pw_gid) < 0) {
532 1.1 mjl syslog(LOG_ERR, "setgid(%d): %m", pwd->pw_gid);
533 1.1 mjl login_close(flc);
534 1.1 mjl return (-1);
535 1.1 mjl }
536 1.1 mjl
537 1.1 mjl if (initgroups(pwd->pw_name, pwd->pw_gid) < 0) {
538 1.1 mjl syslog(LOG_ERR, "initgroups(%s,%d): %m",
539 1.1 mjl pwd->pw_name, pwd->pw_gid);
540 1.1 mjl login_close(flc);
541 1.1 mjl return (-1);
542 1.1 mjl }
543 1.1 mjl }
544 1.1 mjl
545 1.1 mjl if (flags & LOGIN_SETLOGIN)
546 1.1 mjl if (setlogin(pwd->pw_name) < 0) {
547 1.1 mjl syslog(LOG_ERR, "setlogin(%s) failure: %m",
548 1.1 mjl pwd->pw_name);
549 1.1 mjl login_close(flc);
550 1.1 mjl return (-1);
551 1.1 mjl }
552 1.1 mjl
553 1.1 mjl if (flags & LOGIN_SETUSER)
554 1.1 mjl if (setuid(uid) < 0) {
555 1.1 mjl syslog(LOG_ERR, "setuid(%d): %m", uid);
556 1.1 mjl login_close(flc);
557 1.1 mjl return (-1);
558 1.1 mjl }
559 1.4 mjl
560 1.4 mjl if (flags & LOGIN_SETENV)
561 1.4 mjl setuserenv(lc);
562 1.1 mjl
563 1.1 mjl if (flags & LOGIN_SETPATH)
564 1.1 mjl setuserpath(lc, pwd ? pwd->pw_dir : "");
565 1.1 mjl
566 1.1 mjl login_close(flc);
567 1.1 mjl return (0);
568 1.1 mjl }
569 1.1 mjl
570 1.1 mjl static void
571 1.6 ad setuserpath(login_cap_t *lc, char *home)
572 1.1 mjl {
573 1.1 mjl size_t hlen, plen;
574 1.1 mjl int cnt = 0;
575 1.1 mjl char *path;
576 1.1 mjl char *p, *q;
577 1.1 mjl
578 1.1 mjl hlen = strlen(home);
579 1.1 mjl
580 1.1 mjl p = path = login_getcapstr(lc, "path", NULL, NULL);
581 1.1 mjl if (p) {
582 1.1 mjl while (*p)
583 1.1 mjl if (*p++ == '~')
584 1.1 mjl ++cnt;
585 1.1 mjl plen = (p - path) + cnt * (hlen + 1) + 1;
586 1.1 mjl p = path;
587 1.1 mjl q = path = malloc(plen);
588 1.1 mjl if (q) {
589 1.1 mjl while (*p) {
590 1.1 mjl p += strspn(p, " \t");
591 1.1 mjl if (*p == '\0')
592 1.1 mjl break;
593 1.1 mjl plen = strcspn(p, " \t");
594 1.1 mjl if (hlen == 0 && *p == '~') {
595 1.1 mjl p += plen;
596 1.1 mjl continue;
597 1.1 mjl }
598 1.1 mjl if (q != path)
599 1.1 mjl *q++ = ':';
600 1.1 mjl if (*p == '~') {
601 1.1 mjl strcpy(q, home);
602 1.1 mjl q += hlen;
603 1.1 mjl ++p;
604 1.1 mjl --plen;
605 1.1 mjl }
606 1.1 mjl memcpy(q, p, plen);
607 1.1 mjl p += plen;
608 1.1 mjl q += plen;
609 1.1 mjl }
610 1.1 mjl *q = '\0';
611 1.1 mjl } else
612 1.1 mjl path = _PATH_DEFPATH;
613 1.1 mjl } else
614 1.1 mjl path = _PATH_DEFPATH;
615 1.1 mjl if (setenv("PATH", path, 1))
616 1.1 mjl warn("could not set PATH");
617 1.1 mjl }
618 1.1 mjl
619 1.1 mjl /*
620 1.1 mjl * Convert an expression of the following forms
621 1.1 mjl * 1) A number.
622 1.1 mjl * 2) A number followed by a b (mult by 512).
623 1.1 mjl * 3) A number followed by a k (mult by 1024).
624 1.1 mjl * 5) A number followed by a m (mult by 1024 * 1024).
625 1.1 mjl * 6) A number followed by a g (mult by 1024 * 1024 * 1024).
626 1.1 mjl * 7) A number followed by a t (mult by 1024 * 1024 * 1024 * 1024).
627 1.1 mjl * 8) Two or more numbers (with/without k,b,m,g, or t).
628 1.1 mjl * seperated by x (also * for backwards compatibility), specifying
629 1.1 mjl * the product of the indicated values.
630 1.1 mjl */
631 1.6 ad static u_quad_t
632 1.6 ad strtosize(char *str, char **endptr, int radix)
633 1.1 mjl {
634 1.1 mjl u_quad_t num, num2;
635 1.1 mjl char *expr, *expr2;
636 1.1 mjl
637 1.1 mjl errno = 0;
638 1.1 mjl num = strtouq(str, &expr, radix);
639 1.1 mjl if (errno || expr == str) {
640 1.1 mjl if (endptr)
641 1.1 mjl *endptr = expr;
642 1.1 mjl return (num);
643 1.1 mjl }
644 1.1 mjl
645 1.1 mjl switch(*expr) {
646 1.1 mjl case 'b': case 'B':
647 1.1 mjl num = multiply(num, (u_quad_t)512);
648 1.1 mjl ++expr;
649 1.1 mjl break;
650 1.1 mjl case 'k': case 'K':
651 1.1 mjl num = multiply(num, (u_quad_t)1024);
652 1.1 mjl ++expr;
653 1.1 mjl break;
654 1.1 mjl case 'm': case 'M':
655 1.1 mjl num = multiply(num, (u_quad_t)1024 * 1024);
656 1.1 mjl ++expr;
657 1.1 mjl break;
658 1.1 mjl case 'g': case 'G':
659 1.1 mjl num = multiply(num, (u_quad_t)1024 * 1024 * 1024);
660 1.1 mjl ++expr;
661 1.1 mjl break;
662 1.1 mjl case 't': case 'T':
663 1.1 mjl num = multiply(num, (u_quad_t)1024 * 1024);
664 1.1 mjl num = multiply(num, (u_quad_t)1024 * 1024);
665 1.1 mjl ++expr;
666 1.1 mjl break;
667 1.1 mjl }
668 1.1 mjl
669 1.1 mjl if (errno)
670 1.1 mjl goto erange;
671 1.1 mjl
672 1.1 mjl switch(*expr) {
673 1.1 mjl case '*': /* Backward compatible. */
674 1.1 mjl case 'x':
675 1.1 mjl num2 = strtosize(expr+1, &expr2, radix);
676 1.1 mjl if (errno) {
677 1.1 mjl expr = expr2;
678 1.1 mjl goto erange;
679 1.1 mjl }
680 1.1 mjl
681 1.1 mjl if (expr2 == expr + 1) {
682 1.1 mjl if (endptr)
683 1.1 mjl *endptr = expr;
684 1.1 mjl return (num);
685 1.1 mjl }
686 1.1 mjl expr = expr2;
687 1.1 mjl num = multiply(num, num2);
688 1.1 mjl if (errno)
689 1.1 mjl goto erange;
690 1.1 mjl break;
691 1.1 mjl }
692 1.1 mjl if (endptr)
693 1.1 mjl *endptr = expr;
694 1.1 mjl return (num);
695 1.1 mjl erange:
696 1.1 mjl if (endptr)
697 1.1 mjl *endptr = expr;
698 1.1 mjl errno = ERANGE;
699 1.1 mjl return (UQUAD_MAX);
700 1.1 mjl }
701 1.1 mjl
702 1.6 ad static u_quad_t
703 1.6 ad strtolimit(char *str, char **endptr, int radix)
704 1.1 mjl {
705 1.5 mjl if (isinfinite(str)) {
706 1.1 mjl if (endptr)
707 1.1 mjl *endptr = str + strlen(str);
708 1.1 mjl return ((u_quad_t)RLIM_INFINITY);
709 1.1 mjl }
710 1.1 mjl return (strtosize(str, endptr, radix));
711 1.5 mjl }
712 1.5 mjl
713 1.5 mjl static int
714 1.5 mjl isinfinite(const char *s)
715 1.5 mjl {
716 1.5 mjl static const char *infs[] = {
717 1.5 mjl "infinity",
718 1.5 mjl "inf",
719 1.5 mjl "unlimited",
720 1.5 mjl "unlimit",
721 1.5 mjl NULL
722 1.5 mjl };
723 1.5 mjl const char **i;
724 1.5 mjl
725 1.5 mjl for(i = infs; *i; i++) {
726 1.5 mjl if (!strcasecmp(s, *i))
727 1.5 mjl return 1;
728 1.5 mjl }
729 1.5 mjl return 0;
730 1.1 mjl }
731 1.1 mjl
732 1.1 mjl static u_quad_t
733 1.6 ad multiply(u_quad_t n1, u_quad_t n2)
734 1.1 mjl {
735 1.1 mjl static int bpw = 0;
736 1.1 mjl u_quad_t m;
737 1.1 mjl u_quad_t r;
738 1.1 mjl int b1, b2;
739 1.1 mjl
740 1.1 mjl /*
741 1.1 mjl * Get rid of the simple cases
742 1.1 mjl */
743 1.1 mjl if (n1 == 0 || n2 == 0)
744 1.1 mjl return (0);
745 1.1 mjl if (n1 == 1)
746 1.1 mjl return (n2);
747 1.1 mjl if (n2 == 1)
748 1.1 mjl return (n1);
749 1.1 mjl
750 1.1 mjl /*
751 1.1 mjl * sizeof() returns number of bytes needed for storage.
752 1.1 mjl * This may be different from the actual number of useful bits.
753 1.1 mjl */
754 1.1 mjl if (!bpw) {
755 1.1 mjl bpw = sizeof(u_quad_t) * 8;
756 1.1 mjl while (((u_quad_t)1 << (bpw-1)) == 0)
757 1.1 mjl --bpw;
758 1.1 mjl }
759 1.1 mjl
760 1.1 mjl /*
761 1.1 mjl * First check the magnitude of each number. If the sum of the
762 1.1 mjl * magnatude is way to high, reject the number. (If this test
763 1.1 mjl * is not done then the first multiply below may overflow.)
764 1.1 mjl */
765 1.1 mjl for (b1 = bpw; (((u_quad_t)1 << (b1-1)) & n1) == 0; --b1)
766 1.1 mjl ;
767 1.1 mjl for (b2 = bpw; (((u_quad_t)1 << (b2-1)) & n2) == 0; --b2)
768 1.1 mjl ;
769 1.1 mjl if (b1 + b2 - 2 > bpw) {
770 1.1 mjl errno = ERANGE;
771 1.1 mjl return (UQUAD_MAX);
772 1.1 mjl }
773 1.1 mjl
774 1.1 mjl /*
775 1.1 mjl * Decompose the multiplication to be:
776 1.1 mjl * h1 = n1 & ~1
777 1.1 mjl * h2 = n2 & ~1
778 1.1 mjl * l1 = n1 & 1
779 1.1 mjl * l2 = n2 & 1
780 1.1 mjl * (h1 + l1) * (h2 + l2)
781 1.1 mjl * (h1 * h2) + (h1 * l2) + (l1 * h2) + (l1 * l2)
782 1.1 mjl *
783 1.1 mjl * Since h1 && h2 do not have the low bit set, we can then say:
784 1.1 mjl *
785 1.1 mjl * (h1>>1 * h2>>1 * 4) + ...
786 1.1 mjl *
787 1.1 mjl * So if (h1>>1 * h2>>1) > (1<<(bpw - 2)) then the result will
788 1.1 mjl * overflow.
789 1.1 mjl *
790 1.1 mjl * Finally, if MAX - ((h1 * l2) + (l1 * h2) + (l1 * l2)) < (h1*h2)
791 1.1 mjl * then adding in residual amout will cause an overflow.
792 1.1 mjl */
793 1.1 mjl
794 1.1 mjl m = (n1 >> 1) * (n2 >> 1);
795 1.1 mjl
796 1.1 mjl if (m >= ((u_quad_t)1 << (bpw-2))) {
797 1.1 mjl errno = ERANGE;
798 1.1 mjl return (UQUAD_MAX);
799 1.1 mjl }
800 1.1 mjl
801 1.1 mjl m *= 4;
802 1.1 mjl
803 1.1 mjl r = (n1 & n2 & 1)
804 1.1 mjl + (n2 & 1) * (n1 & ~(u_quad_t)1)
805 1.1 mjl + (n1 & 1) * (n2 & ~(u_quad_t)1);
806 1.1 mjl
807 1.1 mjl if ((u_quad_t)(m + r) < m) {
808 1.1 mjl errno = ERANGE;
809 1.1 mjl return (UQUAD_MAX);
810 1.1 mjl }
811 1.1 mjl m += r;
812 1.1 mjl
813 1.1 mjl return (m);
814 1.1 mjl }
815 1.1 mjl
816