Home | History | Annotate | Line # | Download | only in libutil
login_cap.c revision 1.25
      1  1.25  christos /*	$NetBSD: login_cap.c,v 1.25 2006/10/15 19:33:03 christos 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.25  christos __RCSID("$NetBSD: login_cap.c,v 1.25 2006/10/15 19:33:03 christos 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.10     lukem #include <assert.h>
     48   1.4       mjl #include <ctype.h>
     49   1.1       mjl #include <err.h>
     50   1.1       mjl #include <errno.h>
     51   1.1       mjl #include <fcntl.h>
     52   1.1       mjl #include <limits.h>
     53   1.1       mjl #include <login_cap.h>
     54   1.1       mjl #include <paths.h>
     55   1.1       mjl #include <pwd.h>
     56   1.1       mjl #include <stdio.h>
     57   1.1       mjl #include <stdlib.h>
     58   1.1       mjl #include <string.h>
     59   1.1       mjl #include <syslog.h>
     60   1.1       mjl #include <unistd.h>
     61   1.7        ad #include <util.h>
     62   1.1       mjl 
     63   1.6        ad static u_quad_t	multiply(u_quad_t, u_quad_t);
     64  1.19  christos static u_quad_t	strtolimit(const char *, char **, int);
     65  1.19  christos static u_quad_t	strtosize(const char *, char **, int);
     66  1.19  christos static int	gsetrl(login_cap_t *, int, const char *, int type);
     67   1.6        ad static int	isinfinite(const char *);
     68  1.23  christos static int	envset(void *, const char *, const char *, int);
     69   1.1       mjl 
     70   1.1       mjl login_cap_t *
     71  1.19  christos login_getclass(const char *class)
     72   1.1       mjl {
     73  1.17  christos 	const char *classfiles[2];
     74   1.1       mjl 	login_cap_t *lc;
     75   1.1       mjl 	int res;
     76   1.1       mjl 
     77  1.10     lukem 	/* class may be NULL */
     78  1.10     lukem 
     79   1.9    itojun 	if (secure_path(_PATH_LOGIN_CONF) == 0) {
     80   1.9    itojun 		classfiles[0] = _PATH_LOGIN_CONF;
     81   1.9    itojun 		classfiles[1] = NULL;
     82   1.9    itojun 	} else {
     83   1.9    itojun 		classfiles[0] = NULL;
     84   1.9    itojun 	}
     85   1.1       mjl 
     86   1.1       mjl 	if ((lc = malloc(sizeof(login_cap_t))) == NULL) {
     87   1.1       mjl 		syslog(LOG_ERR, "%s:%d malloc: %m", __FILE__, __LINE__);
     88   1.1       mjl 		return (0);
     89   1.1       mjl 	}
     90   1.1       mjl 
     91   1.1       mjl 	lc->lc_cap = 0;
     92   1.1       mjl 	lc->lc_style = 0;
     93   1.1       mjl 
     94   1.1       mjl 	if (class == NULL || class[0] == '\0')
     95   1.1       mjl 		class = LOGIN_DEFCLASS;
     96   1.1       mjl 
     97   1.1       mjl     	if ((lc->lc_class = strdup(class)) == NULL) {
     98   1.1       mjl 		syslog(LOG_ERR, "%s:%d strdup: %m", __FILE__, __LINE__);
     99   1.1       mjl 		free(lc);
    100   1.1       mjl 		return (0);
    101   1.1       mjl 	}
    102   1.9    itojun 
    103   1.9    itojun 	/*
    104   1.9    itojun 	 * Not having a login.conf file is not an error condition.
    105   1.9    itojun 	 * The individual routines deal reasonably with missing
    106   1.9    itojun 	 * capabilities and use default values.
    107   1.9    itojun 	 */
    108   1.9    itojun 	if (classfiles[0] == NULL)
    109   1.9    itojun 		return(lc);
    110   1.1       mjl 
    111   1.8    itojun 	if ((res = cgetent(&lc->lc_cap, classfiles, lc->lc_class)) != 0) {
    112   1.1       mjl 		lc->lc_cap = 0;
    113   1.1       mjl 		switch (res) {
    114   1.1       mjl 		case 1:
    115   1.1       mjl 			syslog(LOG_ERR, "%s: couldn't resolve 'tc'",
    116   1.1       mjl 				lc->lc_class);
    117   1.1       mjl 			break;
    118   1.1       mjl 		case -1:
    119  1.21  drochner 			if (strcmp(lc->lc_class, LOGIN_DEFCLASS) == 0)
    120   1.1       mjl 				return (lc);
    121   1.1       mjl 			syslog(LOG_ERR, "%s: unknown class", lc->lc_class);
    122   1.1       mjl 			break;
    123   1.1       mjl 		case -2:
    124   1.1       mjl 			syslog(LOG_ERR, "%s: getting class information: %m",
    125   1.1       mjl 				lc->lc_class);
    126   1.1       mjl 			break;
    127   1.1       mjl 		case -3:
    128   1.1       mjl 			syslog(LOG_ERR, "%s: 'tc' reference loop",
    129   1.1       mjl 				lc->lc_class);
    130   1.1       mjl 			break;
    131   1.1       mjl 		default:
    132   1.1       mjl 			syslog(LOG_ERR, "%s: unexpected cgetent error",
    133   1.1       mjl 				lc->lc_class);
    134   1.1       mjl 			break;
    135   1.1       mjl 		}
    136   1.1       mjl 		free(lc->lc_class);
    137   1.1       mjl 		free(lc);
    138   1.1       mjl 		return (0);
    139   1.1       mjl 	}
    140   1.1       mjl 	return (lc);
    141   1.1       mjl }
    142   1.1       mjl 
    143   1.4       mjl login_cap_t *
    144   1.6        ad login_getpwclass(const struct passwd *pwd)
    145   1.4       mjl {
    146  1.10     lukem 
    147  1.10     lukem 	/* pwd may be NULL */
    148  1.10     lukem 
    149   1.4       mjl 	return login_getclass(pwd ? pwd->pw_class : NULL);
    150   1.4       mjl }
    151   1.4       mjl 
    152   1.1       mjl char *
    153  1.19  christos login_getcapstr(login_cap_t *lc, const char *cap, char *def, char *e)
    154   1.1       mjl {
    155  1.13    itojun 	char *res = NULL;
    156   1.1       mjl 	int status;
    157   1.1       mjl 
    158   1.1       mjl 	errno = 0;
    159   1.1       mjl 
    160  1.10     lukem 	_DIAGASSERT(cap != NULL);
    161  1.10     lukem 
    162   1.2       mjl 	if (!lc || !lc->lc_cap)
    163   1.1       mjl 		return (def);
    164   1.1       mjl 
    165   1.1       mjl 	switch (status = cgetstr(lc->lc_cap, cap, &res)) {
    166   1.1       mjl 	case -1:
    167  1.12    itojun 		if (res)
    168  1.12    itojun 			free(res);
    169   1.1       mjl 		return (def);
    170   1.1       mjl 	case -2:
    171   1.1       mjl 		syslog(LOG_ERR, "%s: getting capability %s: %m",
    172   1.1       mjl 		    lc->lc_class, cap);
    173  1.12    itojun 		if (res)
    174  1.12    itojun 			free(res);
    175   1.1       mjl 		return (e);
    176   1.1       mjl 	default:
    177   1.1       mjl 		if (status >= 0)
    178   1.1       mjl 			return (res);
    179   1.1       mjl 		syslog(LOG_ERR, "%s: unexpected error with capability %s",
    180   1.1       mjl 		    lc->lc_class, cap);
    181  1.12    itojun 		if (res)
    182  1.12    itojun 			free(res);
    183   1.1       mjl 		return (e);
    184   1.1       mjl 	}
    185   1.1       mjl }
    186   1.1       mjl 
    187   1.1       mjl quad_t
    188  1.19  christos login_getcaptime(login_cap_t *lc, const char *cap, quad_t def, quad_t e)
    189   1.1       mjl {
    190   1.1       mjl 	char *ep;
    191  1.13    itojun 	char *res = NULL, *sres;
    192   1.1       mjl 	int status;
    193   1.1       mjl 	quad_t q, r;
    194   1.1       mjl 
    195  1.10     lukem 	_DIAGASSERT(cap != NULL);
    196  1.10     lukem 
    197   1.1       mjl 	errno = 0;
    198   1.2       mjl 	if (!lc || !lc->lc_cap)
    199   1.1       mjl 		return (def);
    200   1.1       mjl 
    201   1.1       mjl 	switch (status = cgetstr(lc->lc_cap, cap, &res)) {
    202   1.1       mjl 	case -1:
    203  1.12    itojun 		if (res)
    204  1.12    itojun 			free(res);
    205   1.1       mjl 		return (def);
    206   1.1       mjl 	case -2:
    207   1.1       mjl 		syslog(LOG_ERR, "%s: getting capability %s: %m",
    208   1.1       mjl 		    lc->lc_class, cap);
    209   1.1       mjl 		errno = ERANGE;
    210  1.12    itojun 		if (res)
    211  1.12    itojun 			free(res);
    212   1.1       mjl 		return (e);
    213   1.1       mjl 	default:
    214   1.1       mjl 		if (status >= 0)
    215   1.1       mjl 			break;
    216   1.1       mjl 		syslog(LOG_ERR, "%s: unexpected error with capability %s",
    217   1.1       mjl 		    lc->lc_class, cap);
    218   1.1       mjl 		errno = ERANGE;
    219  1.12    itojun 		if (res)
    220  1.12    itojun 			free(res);
    221   1.1       mjl 		return (e);
    222   1.1       mjl 	}
    223   1.1       mjl 
    224   1.5       mjl 	if (isinfinite(res))
    225   1.1       mjl 		return (RLIM_INFINITY);
    226   1.1       mjl 
    227   1.1       mjl 	errno = 0;
    228   1.1       mjl 
    229   1.1       mjl 	q = 0;
    230   1.1       mjl 	sres = res;
    231   1.1       mjl 	while (*res) {
    232   1.1       mjl 		r = strtoq(res, &ep, 0);
    233   1.1       mjl 		if (!ep || ep == res ||
    234   1.1       mjl 		    ((r == QUAD_MIN || r == QUAD_MAX) && errno == ERANGE)) {
    235   1.1       mjl invalid:
    236   1.1       mjl 			syslog(LOG_ERR, "%s:%s=%s: invalid time",
    237   1.1       mjl 			    lc->lc_class, cap, sres);
    238   1.1       mjl 			errno = ERANGE;
    239  1.12    itojun 			free(sres);
    240   1.1       mjl 			return (e);
    241   1.1       mjl 		}
    242   1.1       mjl 		switch (*ep++) {
    243   1.1       mjl 		case '\0':
    244   1.1       mjl 			--ep;
    245   1.1       mjl 			break;
    246   1.1       mjl 		case 's': case 'S':
    247   1.1       mjl 			break;
    248   1.1       mjl 		case 'm': case 'M':
    249   1.1       mjl 			r *= 60;
    250   1.1       mjl 			break;
    251   1.1       mjl 		case 'h': case 'H':
    252   1.1       mjl 			r *= 60 * 60;
    253   1.1       mjl 			break;
    254   1.1       mjl 		case 'd': case 'D':
    255   1.1       mjl 			r *= 60 * 60 * 24;
    256   1.1       mjl 			break;
    257   1.1       mjl 		case 'w': case 'W':
    258   1.1       mjl 			r *= 60 * 60 * 24 * 7;
    259   1.1       mjl 			break;
    260   1.1       mjl 		case 'y': case 'Y':	/* Pretty absurd */
    261   1.1       mjl 			r *= 60 * 60 * 24 * 365;
    262   1.1       mjl 			break;
    263   1.1       mjl 		default:
    264   1.1       mjl 			goto invalid;
    265   1.1       mjl 		}
    266   1.1       mjl 		res = ep;
    267   1.1       mjl 		q += r;
    268   1.1       mjl 	}
    269  1.12    itojun 	free(sres);
    270   1.1       mjl 	return (q);
    271   1.1       mjl }
    272   1.1       mjl 
    273   1.1       mjl quad_t
    274  1.19  christos login_getcapnum(login_cap_t *lc, const char *cap, quad_t def, quad_t e)
    275   1.1       mjl {
    276   1.1       mjl 	char *ep;
    277  1.13    itojun 	char *res = NULL;
    278   1.1       mjl 	int status;
    279   1.1       mjl 	quad_t q;
    280   1.1       mjl 
    281  1.10     lukem 	_DIAGASSERT(cap != NULL);
    282  1.10     lukem 
    283   1.1       mjl 	errno = 0;
    284   1.2       mjl 	if (!lc || !lc->lc_cap)
    285   1.1       mjl 		return (def);
    286   1.1       mjl 
    287   1.1       mjl 	switch (status = cgetstr(lc->lc_cap, cap, &res)) {
    288   1.1       mjl 	case -1:
    289  1.12    itojun 		if (res)
    290  1.12    itojun 			free(res);
    291   1.1       mjl 		return (def);
    292   1.1       mjl 	case -2:
    293   1.1       mjl 		syslog(LOG_ERR, "%s: getting capability %s: %m",
    294   1.1       mjl 		    lc->lc_class, cap);
    295   1.1       mjl 		errno = ERANGE;
    296  1.12    itojun 		if (res)
    297  1.12    itojun 			free(res);
    298   1.1       mjl 		return (e);
    299   1.1       mjl 	default:
    300   1.1       mjl 		if (status >= 0)
    301   1.1       mjl 			break;
    302   1.1       mjl 		syslog(LOG_ERR, "%s: unexpected error with capability %s",
    303   1.1       mjl 		    lc->lc_class, cap);
    304   1.1       mjl 		errno = ERANGE;
    305  1.12    itojun 		if (res)
    306  1.12    itojun 			free(res);
    307   1.1       mjl 		return (e);
    308   1.1       mjl 	}
    309   1.1       mjl 
    310   1.5       mjl 	if (isinfinite(res))
    311   1.1       mjl 		return (RLIM_INFINITY);
    312   1.1       mjl 
    313   1.1       mjl 	errno = 0;
    314   1.1       mjl     	q = strtoq(res, &ep, 0);
    315   1.1       mjl 	if (!ep || ep == res || ep[0] ||
    316   1.1       mjl 	    ((q == QUAD_MIN || q == QUAD_MAX) && errno == ERANGE)) {
    317   1.1       mjl 		syslog(LOG_ERR, "%s:%s=%s: invalid number",
    318   1.1       mjl 		    lc->lc_class, cap, res);
    319   1.1       mjl 		errno = ERANGE;
    320  1.12    itojun 		free(res);
    321   1.1       mjl 		return (e);
    322   1.1       mjl 	}
    323  1.12    itojun 	free(res);
    324   1.1       mjl 	return (q);
    325   1.1       mjl }
    326   1.1       mjl 
    327   1.1       mjl quad_t
    328  1.19  christos login_getcapsize(login_cap_t *lc, const char *cap, quad_t def, quad_t e)
    329   1.1       mjl {
    330   1.1       mjl 	char *ep;
    331  1.13    itojun 	char *res = NULL;
    332   1.1       mjl 	int status;
    333   1.1       mjl 	quad_t q;
    334   1.1       mjl 
    335  1.10     lukem 	_DIAGASSERT(cap != NULL);
    336  1.10     lukem 
    337   1.1       mjl 	errno = 0;
    338   1.1       mjl 
    339   1.2       mjl 	if (!lc || !lc->lc_cap)
    340   1.1       mjl 		return (def);
    341   1.1       mjl 
    342   1.1       mjl 	switch (status = cgetstr(lc->lc_cap, cap, &res)) {
    343   1.1       mjl 	case -1:
    344  1.12    itojun 		if (res)
    345  1.12    itojun 			free(res);
    346   1.1       mjl 		return (def);
    347   1.1       mjl 	case -2:
    348   1.1       mjl 		syslog(LOG_ERR, "%s: getting capability %s: %m",
    349   1.1       mjl 		    lc->lc_class, cap);
    350   1.1       mjl 		errno = ERANGE;
    351  1.12    itojun 		if (res)
    352  1.12    itojun 			free(res);
    353   1.1       mjl 		return (e);
    354   1.1       mjl 	default:
    355   1.1       mjl 		if (status >= 0)
    356   1.1       mjl 			break;
    357   1.1       mjl 		syslog(LOG_ERR, "%s: unexpected error with capability %s",
    358   1.1       mjl 		    lc->lc_class, cap);
    359   1.1       mjl 		errno = ERANGE;
    360  1.12    itojun 		if (res)
    361  1.12    itojun 			free(res);
    362   1.1       mjl 		return (e);
    363   1.1       mjl 	}
    364   1.1       mjl 
    365   1.1       mjl 	errno = 0;
    366   1.1       mjl 	q = strtolimit(res, &ep, 0);
    367   1.1       mjl 	if (!ep || ep == res || (ep[0] && ep[1]) ||
    368   1.1       mjl 	    ((q == QUAD_MIN || q == QUAD_MAX) && errno == ERANGE)) {
    369   1.1       mjl 		syslog(LOG_ERR, "%s:%s=%s: invalid size",
    370   1.1       mjl 		    lc->lc_class, cap, res);
    371   1.1       mjl 		errno = ERANGE;
    372  1.12    itojun 		free(res);
    373   1.1       mjl 		return (e);
    374   1.1       mjl 	}
    375  1.12    itojun 	free(res);
    376   1.1       mjl 	return (q);
    377   1.1       mjl }
    378   1.1       mjl 
    379   1.1       mjl int
    380  1.19  christos login_getcapbool(login_cap_t *lc, const char *cap, u_int def)
    381   1.1       mjl {
    382  1.10     lukem 
    383  1.10     lukem 	_DIAGASSERT(cap != NULL);
    384  1.10     lukem 
    385   1.2       mjl 	if (!lc || !lc->lc_cap)
    386   1.1       mjl 		return (def);
    387   1.1       mjl 
    388   1.1       mjl 	return (cgetcap(lc->lc_cap, cap, ':') != NULL);
    389   1.1       mjl }
    390   1.1       mjl 
    391   1.1       mjl void
    392   1.6        ad login_close(login_cap_t *lc)
    393   1.1       mjl {
    394  1.10     lukem 
    395   1.1       mjl 	if (lc) {
    396   1.1       mjl 		if (lc->lc_class)
    397   1.1       mjl 			free(lc->lc_class);
    398   1.1       mjl 		if (lc->lc_cap)
    399   1.1       mjl 			free(lc->lc_cap);
    400   1.1       mjl 		if (lc->lc_style)
    401   1.1       mjl 			free(lc->lc_style);
    402   1.1       mjl 		free(lc);
    403   1.1       mjl 	}
    404   1.1       mjl }
    405   1.1       mjl 
    406   1.7        ad #define	R_CTIME	1
    407   1.7        ad #define	R_CSIZE	2
    408   1.7        ad #define	R_CNUMB	3
    409   1.1       mjl 
    410   1.1       mjl static struct {
    411   1.1       mjl 	int	what;
    412   1.1       mjl 	int	type;
    413  1.19  christos 	const char *name;
    414   1.1       mjl } r_list[] = {
    415   1.7        ad 	{ RLIMIT_CPU,		R_CTIME, "cputime", },
    416   1.7        ad 	{ RLIMIT_FSIZE,		R_CSIZE, "filesize", },
    417   1.7        ad 	{ RLIMIT_DATA,		R_CSIZE, "datasize", },
    418   1.7        ad 	{ RLIMIT_STACK,		R_CSIZE, "stacksize", },
    419   1.7        ad 	{ RLIMIT_RSS,		R_CSIZE, "memoryuse", },
    420   1.7        ad 	{ RLIMIT_MEMLOCK,	R_CSIZE, "memorylocked", },
    421   1.7        ad 	{ RLIMIT_NPROC,		R_CNUMB, "maxproc", },
    422   1.7        ad 	{ RLIMIT_NOFILE,	R_CNUMB, "openfiles", },
    423   1.7        ad 	{ RLIMIT_CORE,		R_CSIZE, "coredumpsize", },
    424  1.18     lukem 	{ RLIMIT_SBSIZE,	R_CSIZE, "sbsize", },
    425   1.1       mjl 	{ -1, 0, 0 }
    426   1.1       mjl };
    427   1.1       mjl 
    428   1.1       mjl static int
    429  1.19  christos gsetrl(login_cap_t *lc, int what, const char *name, int type)
    430   1.1       mjl {
    431   1.1       mjl 	struct rlimit rl;
    432   1.1       mjl 	struct rlimit r;
    433   1.1       mjl 	char name_cur[32];
    434   1.1       mjl 	char name_max[32];
    435   1.1       mjl 
    436  1.10     lukem 	_DIAGASSERT(name != NULL);
    437  1.10     lukem 
    438  1.25  christos 	(void)snprintf(name_cur, sizeof(name_cur), "%s-cur", name);
    439  1.25  christos 	(void)snprintf(name_max, sizeof(name_max), "%s-max", name);
    440   1.1       mjl 
    441   1.1       mjl 	if (getrlimit(what, &r)) {
    442   1.1       mjl 		syslog(LOG_ERR, "getting resource limit: %m");
    443   1.1       mjl 		return (-1);
    444   1.1       mjl 	}
    445   1.1       mjl 
    446   1.1       mjl #define	RCUR	r.rlim_cur
    447   1.1       mjl #define	RMAX	r.rlim_max
    448   1.1       mjl 
    449   1.1       mjl 	switch (type) {
    450   1.7        ad 	case R_CTIME:
    451   1.1       mjl 		RCUR = login_getcaptime(lc, name, RCUR, RCUR);
    452   1.1       mjl 		RMAX = login_getcaptime(lc, name, RMAX, RMAX);
    453   1.1       mjl 		rl.rlim_cur = login_getcaptime(lc, name_cur, RCUR, RCUR);
    454   1.1       mjl 		rl.rlim_max = login_getcaptime(lc, name_max, RMAX, RMAX);
    455   1.1       mjl 		break;
    456   1.7        ad 	case R_CSIZE:
    457   1.1       mjl 		RCUR = login_getcapsize(lc, name, RCUR, RCUR);
    458   1.1       mjl 		RMAX = login_getcapsize(lc, name, RMAX, RMAX);
    459   1.1       mjl 		rl.rlim_cur = login_getcapsize(lc, name_cur, RCUR, RCUR);
    460   1.1       mjl 		rl.rlim_max = login_getcapsize(lc, name_max, RMAX, RMAX);
    461   1.1       mjl 		break;
    462   1.7        ad 	case R_CNUMB:
    463   1.1       mjl 		RCUR = login_getcapnum(lc, name, RCUR, RCUR);
    464   1.1       mjl 		RMAX = login_getcapnum(lc, name, RMAX, RMAX);
    465   1.1       mjl 		rl.rlim_cur = login_getcapnum(lc, name_cur, RCUR, RCUR);
    466   1.1       mjl 		rl.rlim_max = login_getcapnum(lc, name_max, RMAX, RMAX);
    467   1.1       mjl 		break;
    468   1.1       mjl 	default:
    469  1.25  christos 		syslog(LOG_ERR, "%s: invalid type %d setting resource limit %s",
    470  1.25  christos 		    lc->lc_class, type, name);
    471   1.1       mjl 		return (-1);
    472   1.1       mjl 	}
    473   1.1       mjl 
    474   1.1       mjl 	if (setrlimit(what, &rl)) {
    475   1.1       mjl 		syslog(LOG_ERR, "%s: setting resource limit %s: %m",
    476   1.1       mjl 		    lc->lc_class, name);
    477   1.1       mjl 		return (-1);
    478   1.1       mjl 	}
    479   1.1       mjl #undef	RCUR
    480   1.1       mjl #undef	RMAX
    481   1.1       mjl 	return (0);
    482   1.1       mjl }
    483   1.1       mjl 
    484   1.4       mjl static int
    485  1.23  christos /*ARGSUSED*/
    486  1.23  christos envset(void *envp, const char *name, const char *value, int overwrite)
    487  1.23  christos {
    488  1.23  christos 	return setenv(name, value, overwrite);
    489  1.23  christos }
    490  1.23  christos 
    491  1.23  christos int
    492  1.23  christos setuserenv(login_cap_t *lc, envfunc_t senv, void *envp)
    493   1.4       mjl {
    494  1.19  christos 	const char *stop = ", \t";
    495  1.24  christos 	size_t i, count;
    496   1.4       mjl 	char *ptr;
    497   1.4       mjl 	char **res;
    498   1.4       mjl 	char *str = login_getcapstr(lc, "setenv", NULL, NULL);
    499   1.4       mjl 
    500   1.8    itojun 	if (str == NULL || *str == '\0')
    501   1.4       mjl 		return 0;
    502   1.4       mjl 
    503  1.24  christos 	/*
    504  1.24  christos 	 * count the sub-strings, this may over-count since we don't
    505  1.24  christos 	 * account for escaped delimiters.
    506  1.24  christos 	 */
    507   1.4       mjl 	for (i = 1, ptr = str; *ptr; i++) {
    508   1.4       mjl 		ptr += strcspn(ptr, stop);
    509   1.4       mjl 		if (*ptr)
    510   1.4       mjl 			ptr++;
    511   1.8    itojun 	}
    512   1.4       mjl 
    513   1.4       mjl 	/* allocate ptr array and string */
    514   1.4       mjl 	count = i;
    515   1.8    itojun 	res = malloc(count * sizeof(char *) + strlen(str) + 1);
    516   1.4       mjl 
    517   1.8    itojun 	if (!res)
    518   1.4       mjl 		return -1;
    519   1.4       mjl 
    520  1.24  christos 	ptr = (char *)(void *)&res[count];
    521  1.24  christos 	(void)strcpy(ptr, str);
    522   1.4       mjl 
    523   1.4       mjl 	/* split string */
    524  1.24  christos 	for (i = 0; (res[i] = stresep(&ptr, stop, '\\')) != NULL; )
    525  1.24  christos 		if (*res[i])
    526  1.24  christos 			i++;
    527   1.4       mjl 
    528  1.24  christos 	count = i;
    529   1.4       mjl 
    530  1.24  christos 	for (i = 0; i < count; i++) {
    531  1.24  christos 		if ((ptr = strchr(res[i], '=')) != NULL)
    532  1.24  christos 			*ptr++ = '\0';
    533  1.24  christos 		else
    534  1.24  christos 			ptr = NULL;
    535  1.24  christos 		(void)(*senv)(envp, res[i], ptr ? ptr : "", 1);
    536   1.4       mjl 	}
    537   1.4       mjl 
    538   1.5       mjl 	free(res);
    539   1.4       mjl 	return 0;
    540   1.4       mjl }
    541   1.4       mjl 
    542   1.1       mjl int
    543  1.19  christos setclasscontext(const char *class, u_int flags)
    544   1.1       mjl {
    545   1.1       mjl 	int ret;
    546   1.1       mjl 	login_cap_t *lc;
    547   1.1       mjl 
    548   1.1       mjl 	flags &= LOGIN_SETRESOURCES | LOGIN_SETPRIORITY | LOGIN_SETUMASK |
    549   1.1       mjl 	    LOGIN_SETPATH;
    550   1.1       mjl 
    551   1.1       mjl 	lc = login_getclass(class);
    552   1.1       mjl 	ret = lc ? setusercontext(lc, NULL, 0, flags) : -1;
    553   1.1       mjl 	login_close(lc);
    554   1.1       mjl 	return (ret);
    555   1.1       mjl }
    556   1.1       mjl 
    557   1.1       mjl int
    558   1.6        ad setusercontext(login_cap_t *lc, struct passwd *pwd, uid_t uid, u_int flags)
    559   1.1       mjl {
    560   1.1       mjl 	login_cap_t *flc;
    561   1.1       mjl 	quad_t p;
    562   1.1       mjl 	int i;
    563   1.1       mjl 
    564   1.1       mjl 	flc = NULL;
    565   1.1       mjl 
    566   1.3       mjl 	if (!lc)
    567   1.3       mjl 		flc = lc = login_getclass(pwd ? pwd->pw_class : NULL);
    568   1.1       mjl 
    569   1.1       mjl 	/*
    570   1.1       mjl 	 * Without the pwd entry being passed we cannot set either
    571   1.1       mjl 	 * the group or the login.  We could complain about it.
    572   1.1       mjl 	 */
    573   1.1       mjl 	if (pwd == NULL)
    574   1.1       mjl 		flags &= ~(LOGIN_SETGROUP|LOGIN_SETLOGIN);
    575   1.1       mjl 
    576   1.1       mjl 	if (flags & LOGIN_SETRESOURCES)
    577   1.1       mjl 		for (i = 0; r_list[i].name; ++i)
    578  1.25  christos 			(void)gsetrl(lc, r_list[i].what, r_list[i].name,
    579  1.25  christos 			    r_list[i].type);
    580   1.1       mjl 
    581   1.1       mjl 	if (flags & LOGIN_SETPRIORITY) {
    582  1.22      elad 		p = login_getcapnum(lc, "priority", (quad_t)0, (quad_t)0);
    583   1.1       mjl 
    584   1.1       mjl 		if (setpriority(PRIO_PROCESS, 0, (int)p) < 0)
    585   1.1       mjl 			syslog(LOG_ERR, "%s: setpriority: %m", lc->lc_class);
    586   1.1       mjl 	}
    587   1.1       mjl 
    588   1.1       mjl 	if (flags & LOGIN_SETUMASK) {
    589   1.1       mjl 		p = login_getcapnum(lc, "umask", (quad_t) LOGIN_DEFUMASK,
    590   1.1       mjl 												   (quad_t) LOGIN_DEFUMASK);
    591   1.1       mjl 		umask((mode_t)p);
    592   1.1       mjl 	}
    593   1.1       mjl 
    594   1.1       mjl 	if (flags & LOGIN_SETGROUP) {
    595   1.1       mjl 		if (setgid(pwd->pw_gid) < 0) {
    596   1.1       mjl 			syslog(LOG_ERR, "setgid(%d): %m", pwd->pw_gid);
    597   1.1       mjl 			login_close(flc);
    598   1.1       mjl 			return (-1);
    599   1.1       mjl 		}
    600   1.1       mjl 
    601   1.1       mjl 		if (initgroups(pwd->pw_name, pwd->pw_gid) < 0) {
    602   1.1       mjl 			syslog(LOG_ERR, "initgroups(%s,%d): %m",
    603   1.1       mjl 			    pwd->pw_name, pwd->pw_gid);
    604   1.1       mjl 			login_close(flc);
    605   1.1       mjl 			return (-1);
    606   1.1       mjl 		}
    607   1.1       mjl 	}
    608   1.1       mjl 
    609   1.1       mjl 	if (flags & LOGIN_SETLOGIN)
    610   1.1       mjl 		if (setlogin(pwd->pw_name) < 0) {
    611   1.1       mjl 			syslog(LOG_ERR, "setlogin(%s) failure: %m",
    612   1.1       mjl 			    pwd->pw_name);
    613   1.1       mjl 			login_close(flc);
    614   1.1       mjl 			return (-1);
    615   1.1       mjl 		}
    616   1.1       mjl 
    617   1.1       mjl 	if (flags & LOGIN_SETUSER)
    618   1.1       mjl 		if (setuid(uid) < 0) {
    619   1.1       mjl 			syslog(LOG_ERR, "setuid(%d): %m", uid);
    620   1.1       mjl 			login_close(flc);
    621   1.1       mjl 			return (-1);
    622   1.1       mjl 		}
    623   1.4       mjl 
    624   1.4       mjl 	if (flags & LOGIN_SETENV)
    625  1.23  christos 		setuserenv(lc, envset, NULL);
    626   1.1       mjl 
    627   1.1       mjl 	if (flags & LOGIN_SETPATH)
    628  1.23  christos 		setuserpath(lc, pwd ? pwd->pw_dir : "", envset, NULL);
    629   1.1       mjl 
    630   1.1       mjl 	login_close(flc);
    631   1.1       mjl 	return (0);
    632   1.1       mjl }
    633   1.1       mjl 
    634  1.23  christos void
    635  1.23  christos setuserpath(login_cap_t *lc, const char *home, envfunc_t senv, void *envp)
    636   1.1       mjl {
    637   1.1       mjl 	size_t hlen, plen;
    638   1.1       mjl 	int cnt = 0;
    639   1.1       mjl 	char *path;
    640  1.19  christos 	const char *cpath;
    641   1.1       mjl 	char *p, *q;
    642   1.1       mjl 
    643  1.10     lukem 	_DIAGASSERT(home != NULL);
    644  1.10     lukem 
    645   1.1       mjl 	hlen = strlen(home);
    646   1.1       mjl 
    647  1.20  christos 	p = path = login_getcapstr(lc, "path", NULL, NULL);
    648   1.1       mjl 	if (p) {
    649   1.1       mjl 		while (*p)
    650   1.1       mjl 			if (*p++ == '~')
    651   1.1       mjl 				++cnt;
    652   1.1       mjl 		plen = (p - path) + cnt * (hlen + 1) + 1;
    653   1.1       mjl 		p = path;
    654   1.1       mjl 		q = path = malloc(plen);
    655   1.1       mjl 		if (q) {
    656   1.1       mjl 			while (*p) {
    657   1.1       mjl 				p += strspn(p, " \t");
    658   1.1       mjl 				if (*p == '\0')
    659   1.1       mjl 					break;
    660   1.1       mjl 				plen = strcspn(p, " \t");
    661   1.1       mjl 				if (hlen == 0 && *p == '~') {
    662   1.1       mjl 					p += plen;
    663   1.1       mjl 					continue;
    664   1.1       mjl 				}
    665   1.1       mjl 				if (q != path)
    666   1.1       mjl 					*q++ = ':';
    667   1.1       mjl 				if (*p == '~') {
    668   1.1       mjl 					strcpy(q, home);
    669   1.1       mjl 					q += hlen;
    670   1.1       mjl 					++p;
    671   1.1       mjl 					--plen;
    672   1.1       mjl 				}
    673   1.1       mjl 				memcpy(q, p, plen);
    674   1.1       mjl 				p += plen;
    675   1.1       mjl 				q += plen;
    676   1.1       mjl 			}
    677   1.1       mjl 			*q = '\0';
    678  1.20  christos 			cpath = path;
    679   1.1       mjl 		} else
    680  1.19  christos 			cpath = _PATH_DEFPATH;
    681   1.1       mjl 	} else
    682  1.19  christos 		cpath = _PATH_DEFPATH;
    683  1.23  christos 	if ((*senv)(envp, "PATH", cpath, 1))
    684   1.1       mjl 		warn("could not set PATH");
    685   1.1       mjl }
    686   1.1       mjl 
    687   1.1       mjl /*
    688   1.1       mjl  * Convert an expression of the following forms
    689   1.1       mjl  * 	1) A number.
    690   1.1       mjl  *	2) A number followed by a b (mult by 512).
    691   1.1       mjl  *	3) A number followed by a k (mult by 1024).
    692   1.1       mjl  *	5) A number followed by a m (mult by 1024 * 1024).
    693   1.1       mjl  *	6) A number followed by a g (mult by 1024 * 1024 * 1024).
    694   1.1       mjl  *	7) A number followed by a t (mult by 1024 * 1024 * 1024 * 1024).
    695   1.1       mjl  *	8) Two or more numbers (with/without k,b,m,g, or t).
    696  1.11       wiz  *	   separated by x (also * for backwards compatibility), specifying
    697   1.1       mjl  *	   the product of the indicated values.
    698   1.1       mjl  */
    699   1.6        ad static u_quad_t
    700  1.19  christos strtosize(const char *str, char **endptr, int radix)
    701   1.1       mjl {
    702   1.1       mjl 	u_quad_t num, num2;
    703   1.1       mjl 	char *expr, *expr2;
    704   1.1       mjl 
    705  1.10     lukem 	_DIAGASSERT(str != NULL);
    706  1.10     lukem 	/* endptr may be NULL */
    707  1.10     lukem 
    708   1.1       mjl 	errno = 0;
    709   1.1       mjl 	num = strtouq(str, &expr, radix);
    710   1.1       mjl 	if (errno || expr == str) {
    711   1.1       mjl 		if (endptr)
    712   1.1       mjl 			*endptr = expr;
    713   1.1       mjl 		return (num);
    714   1.1       mjl 	}
    715   1.1       mjl 
    716   1.1       mjl 	switch(*expr) {
    717   1.1       mjl 	case 'b': case 'B':
    718   1.1       mjl 		num = multiply(num, (u_quad_t)512);
    719   1.1       mjl 		++expr;
    720   1.1       mjl 		break;
    721   1.1       mjl 	case 'k': case 'K':
    722   1.1       mjl 		num = multiply(num, (u_quad_t)1024);
    723   1.1       mjl 		++expr;
    724   1.1       mjl 		break;
    725   1.1       mjl 	case 'm': case 'M':
    726   1.1       mjl 		num = multiply(num, (u_quad_t)1024 * 1024);
    727   1.1       mjl 		++expr;
    728   1.1       mjl 		break;
    729   1.1       mjl 	case 'g': case 'G':
    730   1.1       mjl 		num = multiply(num, (u_quad_t)1024 * 1024 * 1024);
    731   1.1       mjl 		++expr;
    732   1.1       mjl 		break;
    733   1.1       mjl 	case 't': case 'T':
    734   1.1       mjl 		num = multiply(num, (u_quad_t)1024 * 1024);
    735   1.1       mjl 		num = multiply(num, (u_quad_t)1024 * 1024);
    736   1.1       mjl 		++expr;
    737   1.1       mjl 		break;
    738   1.1       mjl 	}
    739   1.1       mjl 
    740   1.1       mjl 	if (errno)
    741   1.1       mjl 		goto erange;
    742   1.1       mjl 
    743   1.1       mjl 	switch(*expr) {
    744   1.1       mjl 	case '*':			/* Backward compatible. */
    745   1.1       mjl 	case 'x':
    746   1.1       mjl 		num2 = strtosize(expr+1, &expr2, radix);
    747   1.1       mjl 		if (errno) {
    748   1.1       mjl 			expr = expr2;
    749   1.1       mjl 			goto erange;
    750   1.1       mjl 		}
    751   1.1       mjl 
    752   1.1       mjl 		if (expr2 == expr + 1) {
    753   1.1       mjl 			if (endptr)
    754   1.1       mjl 				*endptr = expr;
    755   1.1       mjl 			return (num);
    756   1.1       mjl 		}
    757   1.1       mjl 		expr = expr2;
    758   1.1       mjl 		num = multiply(num, num2);
    759   1.1       mjl 		if (errno)
    760   1.1       mjl 			goto erange;
    761   1.1       mjl 		break;
    762   1.1       mjl 	}
    763   1.1       mjl 	if (endptr)
    764   1.1       mjl 		*endptr = expr;
    765   1.1       mjl 	return (num);
    766   1.1       mjl erange:
    767   1.1       mjl 	if (endptr)
    768   1.1       mjl 		*endptr = expr;
    769   1.1       mjl 	errno = ERANGE;
    770   1.1       mjl 	return (UQUAD_MAX);
    771   1.1       mjl }
    772   1.1       mjl 
    773   1.6        ad static u_quad_t
    774  1.19  christos strtolimit(const char *str, char **endptr, int radix)
    775   1.1       mjl {
    776  1.10     lukem 
    777  1.10     lukem 	_DIAGASSERT(str != NULL);
    778  1.10     lukem 	/* endptr may be NULL */
    779  1.10     lukem 
    780   1.5       mjl 	if (isinfinite(str)) {
    781   1.1       mjl 		if (endptr)
    782  1.19  christos 			*endptr = (char *)__UNCONST(str) + strlen(str);
    783   1.1       mjl 		return ((u_quad_t)RLIM_INFINITY);
    784   1.1       mjl 	}
    785   1.1       mjl 	return (strtosize(str, endptr, radix));
    786   1.5       mjl }
    787   1.5       mjl 
    788   1.5       mjl static int
    789   1.5       mjl isinfinite(const char *s)
    790   1.5       mjl {
    791   1.5       mjl 	static const char *infs[] = {
    792   1.5       mjl 		"infinity",
    793   1.5       mjl 		"inf",
    794   1.5       mjl 		"unlimited",
    795   1.5       mjl 		"unlimit",
    796   1.5       mjl 		NULL
    797   1.5       mjl 	};
    798   1.5       mjl 	const char **i;
    799  1.10     lukem 
    800  1.10     lukem 	_DIAGASSERT(s != NULL);
    801   1.5       mjl 
    802   1.8    itojun 	for (i = infs; *i; i++) {
    803   1.5       mjl 		if (!strcasecmp(s, *i))
    804   1.5       mjl 			return 1;
    805   1.5       mjl 	}
    806   1.5       mjl 	return 0;
    807   1.1       mjl }
    808   1.1       mjl 
    809   1.1       mjl static u_quad_t
    810   1.6        ad multiply(u_quad_t n1, u_quad_t n2)
    811   1.1       mjl {
    812   1.1       mjl 	static int bpw = 0;
    813   1.1       mjl 	u_quad_t m;
    814   1.1       mjl 	u_quad_t r;
    815   1.1       mjl 	int b1, b2;
    816   1.1       mjl 
    817   1.1       mjl 	/*
    818   1.1       mjl 	 * Get rid of the simple cases
    819   1.1       mjl 	 */
    820   1.1       mjl 	if (n1 == 0 || n2 == 0)
    821   1.1       mjl 		return (0);
    822   1.1       mjl 	if (n1 == 1)
    823   1.1       mjl 		return (n2);
    824   1.1       mjl 	if (n2 == 1)
    825   1.1       mjl 		return (n1);
    826   1.1       mjl 
    827   1.1       mjl 	/*
    828   1.1       mjl 	 * sizeof() returns number of bytes needed for storage.
    829   1.1       mjl 	 * This may be different from the actual number of useful bits.
    830   1.1       mjl 	 */
    831   1.1       mjl 	if (!bpw) {
    832   1.1       mjl 		bpw = sizeof(u_quad_t) * 8;
    833   1.1       mjl 		while (((u_quad_t)1 << (bpw-1)) == 0)
    834   1.1       mjl 			--bpw;
    835   1.1       mjl 	}
    836   1.1       mjl 
    837   1.1       mjl 	/*
    838   1.1       mjl 	 * First check the magnitude of each number.  If the sum of the
    839   1.1       mjl 	 * magnatude is way to high, reject the number.  (If this test
    840   1.1       mjl 	 * is not done then the first multiply below may overflow.)
    841   1.1       mjl 	 */
    842   1.1       mjl 	for (b1 = bpw; (((u_quad_t)1 << (b1-1)) & n1) == 0; --b1)
    843   1.1       mjl 		;
    844   1.1       mjl 	for (b2 = bpw; (((u_quad_t)1 << (b2-1)) & n2) == 0; --b2)
    845   1.1       mjl 		;
    846   1.1       mjl 	if (b1 + b2 - 2 > bpw) {
    847   1.1       mjl 		errno = ERANGE;
    848   1.1       mjl 		return (UQUAD_MAX);
    849   1.1       mjl 	}
    850   1.1       mjl 
    851   1.1       mjl 	/*
    852   1.1       mjl 	 * Decompose the multiplication to be:
    853   1.1       mjl 	 * h1 = n1 & ~1
    854   1.1       mjl 	 * h2 = n2 & ~1
    855   1.1       mjl 	 * l1 = n1 & 1
    856   1.1       mjl 	 * l2 = n2 & 1
    857   1.1       mjl 	 * (h1 + l1) * (h2 + l2)
    858   1.1       mjl 	 * (h1 * h2) + (h1 * l2) + (l1 * h2) + (l1 * l2)
    859   1.1       mjl 	 *
    860   1.1       mjl 	 * Since h1 && h2 do not have the low bit set, we can then say:
    861   1.1       mjl 	 *
    862   1.1       mjl 	 * (h1>>1 * h2>>1 * 4) + ...
    863   1.1       mjl 	 *
    864   1.1       mjl 	 * So if (h1>>1 * h2>>1) > (1<<(bpw - 2)) then the result will
    865   1.1       mjl 	 * overflow.
    866   1.1       mjl 	 *
    867   1.1       mjl 	 * Finally, if MAX - ((h1 * l2) + (l1 * h2) + (l1 * l2)) < (h1*h2)
    868   1.1       mjl 	 * then adding in residual amout will cause an overflow.
    869   1.1       mjl 	 */
    870   1.1       mjl 
    871   1.1       mjl 	m = (n1 >> 1) * (n2 >> 1);
    872   1.1       mjl 
    873   1.1       mjl 	if (m >= ((u_quad_t)1 << (bpw-2))) {
    874   1.1       mjl 		errno = ERANGE;
    875   1.1       mjl 		return (UQUAD_MAX);
    876   1.1       mjl 	}
    877   1.1       mjl 
    878   1.1       mjl 	m *= 4;
    879   1.1       mjl 
    880   1.1       mjl 	r = (n1 & n2 & 1)
    881   1.1       mjl 	  + (n2 & 1) * (n1 & ~(u_quad_t)1)
    882   1.1       mjl 	  + (n1 & 1) * (n2 & ~(u_quad_t)1);
    883   1.1       mjl 
    884   1.1       mjl 	if ((u_quad_t)(m + r) < m) {
    885   1.1       mjl 		errno = ERANGE;
    886   1.1       mjl 		return (UQUAD_MAX);
    887   1.1       mjl 	}
    888   1.1       mjl 	m += r;
    889   1.1       mjl 
    890   1.1       mjl 	return (m);
    891   1.1       mjl }
    892