2 * Copyright (c) 1996 by
3 * Sean Eric Fagan <sef@kithrup.com>
4 * David Nugent <davidn@blaze.net.au>
7 * Portions copyright (c) 1995,1997
8 * Berkeley Software Design, Inc.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, is permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice immediately at the beginning of the file, without modification,
16 * this list of conditions, and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. This work was done expressly for inclusion into FreeBSD. Other use
21 * is permitted provided this notation is included.
22 * 4. Absolutely no warranty of function or purpose is made by the authors.
23 * 5. Modifications may be freely made to this file providing the above
26 * Low-level routines relating to the user capabilities database
28 * $FreeBSD: head/lib/libutil/login_cap.c 255007 2013-08-28 21:10:37Z jilles $
31 #include <sys/types.h>
33 #include <sys/resource.h>
34 #include <sys/param.h>
38 #include <login_cap.h>
48 * Manage a single static pointer for handling a local char* buffer,
49 * resizing as necessary to contain the string.
52 * Manage a static array for handling a group of strings, resizing
56 static int lc_object_count
= 0;
58 static size_t internal_stringsz
= 0;
59 static char * internal_string
= NULL
;
60 static size_t internal_arraysz
= 0;
61 static const char ** internal_array
= NULL
;
63 static char path_login_conf
[] = _PATH_LOGIN_CONF
;
66 allocstr(const char *str
)
70 size_t sz
= strlen(str
) + 1; /* realloc() only if necessary */
71 if (sz
<= internal_stringsz
)
72 p
= strcpy(internal_string
, str
);
73 else if ((p
= realloc(internal_string
, sz
)) != NULL
) {
74 internal_stringsz
= sz
;
75 internal_string
= strcpy(p
, str
);
84 static const char **p
;
86 if (sz
<= internal_arraysz
)
88 else if ((p
= realloc(internal_array
, sz
* sizeof(char*))) != NULL
) {
89 internal_arraysz
= sz
;
98 * Turn a simple string <str> separated by any of
99 * the set of <chars> into an array. The last element
100 * of the array will be NULL, as is proper.
101 * Free using freearraystr()
105 arrayize(const char *str
, const char *chars
, int *size
)
110 const char **res
= NULL
;
112 /* count the sub-strings */
113 for (i
= 0, cptr
= str
; *cptr
; i
++) {
114 int count
= strcspn(cptr
, chars
);
120 /* alloc the array */
121 if ((ptr
= allocstr(str
)) != NULL
) {
122 if ((res
= allocarray(++i
)) == NULL
)
123 free((void *)(uintptr_t)(const void *)str
);
125 /* now split the string */
128 int count
= strcspn(ptr
, chars
);
147 * Frees up all resources relating to a login class
152 login_close(login_cap_t
* lc
)
159 if (--lc_object_count
== 0) {
160 free(internal_string
);
161 free(internal_array
);
162 internal_array
= NULL
;
163 internal_arraysz
= 0;
164 internal_string
= NULL
;
165 internal_stringsz
= 0;
173 * login_getclassbyname()
174 * Get the login class by its name.
175 * If the name given is NULL or empty, the default class
176 * LOGIN_DEFCLASS (i.e., "default") is fetched.
177 * If the name given is LOGIN_MECLASS and
178 * 'pwd' argument is non-NULL and contains an non-NULL
179 * dir entry, then the file _FILE_LOGIN_CONF is picked
180 * up from that directory and used before the system
181 * login database. In that case the system login database
182 * is looked up using LOGIN_MECLASS, too, which is a bug.
183 * Return a filled-out login_cap_t structure, including
184 * class name, and the capability record buffer.
188 login_getclassbyname(char const *name
, const struct passwd
*pwd
)
192 if ((lc
= malloc(sizeof(login_cap_t
))) != NULL
) {
196 const char *msg
= NULL
;
198 char userpath
[MAXPATHLEN
];
200 static char *login_dbarray
[] = { NULL
, NULL
, NULL
};
202 me
= (name
!= NULL
&& strcmp(name
, LOGIN_MECLASS
) == 0);
203 dir
= (!me
|| pwd
== NULL
) ? NULL
: pwd
->pw_dir
;
205 * Switch to user mode before checking/reading its ~/.login_conf
206 * - some NFSes have root read access disabled.
208 * XXX: This fails to configure additional groups.
213 (void)setegid(pwd
->pw_gid
);
214 (void)seteuid(pwd
->pw_uid
);
217 if (dir
&& snprintf(userpath
, MAXPATHLEN
, "%s/%s", dir
,
218 _FILE_LOGIN_CONF
) < MAXPATHLEN
) {
219 if (_secure_path(userpath
, pwd
->pw_uid
, pwd
->pw_gid
) != -1)
220 login_dbarray
[i
++] = userpath
;
223 * XXX: Why to add the system database if the class is `me'?
225 if (_secure_path(path_login_conf
, 0, 0) != -1)
226 login_dbarray
[i
++] = path_login_conf
;
227 login_dbarray
[i
] = NULL
;
229 memset(lc
, 0, sizeof(login_cap_t
));
230 lc
->lc_cap
= lc
->lc_class
= lc
->lc_style
= NULL
;
232 if (name
== NULL
|| *name
== '\0')
233 name
= LOGIN_DEFCLASS
;
235 switch (cgetent(&lc
->lc_cap
, login_dbarray
, name
)) {
236 case -1: /* Failed, entry does not exist */
238 break; /* Don't retry default on 'me' */
241 else if ((r
= open(login_dbarray
[0], O_RDONLY
| O_CLOEXEC
)) >= 0)
244 * If there's at least one login class database,
245 * and we aren't searching for a default class
246 * then complain about a non-existent class.
248 if (r
>= 0 || strcmp(name
, LOGIN_DEFCLASS
) != 0)
249 syslog(LOG_ERR
, "login_getclass: unknown class '%s'", name
);
250 /* fall-back to default class */
251 name
= LOGIN_DEFCLASS
;
252 msg
= "%s: no default/fallback class '%s'";
253 if (cgetent(&lc
->lc_cap
, login_dbarray
, name
) != 0 && r
>= 0)
255 /* FALLTHROUGH - just return system defaults */
256 case 0: /* success! */
257 if ((lc
->lc_class
= strdup(name
)) != NULL
) {
265 msg
= "%s: strdup: %m";
268 msg
= "%s: retrieving class information: %m";
271 msg
= "%s: 'tc=' reference loop '%s'";
274 msg
= "couldn't resolve 'tc=' reference in '%s'";
277 msg
= "%s: unexpected cgetent() error '%s': %m";
285 syslog(LOG_ERR
, msg
, "login_getclass", name
);
296 * Get the login class for the system (only) login class database.
297 * Return a filled-out login_cap_t structure, including
298 * class name, and the capability record buffer.
302 login_getclass(const char *cls
)
304 return login_getclassbyname(cls
, NULL
);
310 * Get the login class for a given password entry from
311 * the system (only) login class database.
312 * If the password entry's class field is not set, or
313 * the class specified does not exist, then use the
314 * default of LOGIN_DEFCLASS (i.e., "default") for an unprivileged
315 * user or that of LOGIN_DEFROOTCLASS (i.e., "root") for a super-user.
316 * Return a filled-out login_cap_t structure, including
317 * class name, and the capability record buffer.
321 login_getpwclass(const struct passwd
*pwd
)
323 const char *cls
= NULL
;
327 if (cls
== NULL
|| *cls
== '\0')
328 cls
= (pwd
->pw_uid
== 0) ? LOGIN_DEFROOTCLASS
: LOGIN_DEFCLASS
;
331 * XXX: pwd should be unused by login_getclassbyname() unless cls is `me',
332 * so NULL can be passed instead of pwd for more safety.
334 return login_getclassbyname(cls
, pwd
);
339 * login_getuserclass()
340 * Get the `me' login class, allowing user overrides via ~/.login_conf.
341 * Note that user overrides are allowed only in the `me' class.
345 login_getuserclass(const struct passwd
*pwd
)
347 return login_getclassbyname(LOGIN_MECLASS
, pwd
);
353 * Given a login_cap entry, and a capability name, return the
354 * value defined for that capability, a default if not found, or
355 * an error string on error.
359 login_getcapstr(login_cap_t
*lc
, const char *cap
, const char *def
, const char *error
)
364 if (lc
== NULL
|| cap
== NULL
|| lc
->lc_cap
== NULL
|| *cap
== '\0')
367 if ((ret
= cgetstr(lc
->lc_cap
, cap
, &res
)) == -1)
369 return (ret
>= 0) ? res
: error
;
375 * Given a login_cap entry, and a capability name, return the
376 * value defined for that capability split into an array of
381 login_getcaplist(login_cap_t
*lc
, const char *cap
, const char *chars
)
387 if ((lstring
= login_getcapstr(lc
, cap
, NULL
, NULL
)) != NULL
)
388 return arrayize(lstring
, chars
, NULL
);
395 * From the login_cap_t <lc>, get the capability <cap> which is
396 * formatted as either a space or comma delimited list of paths
397 * and append them all into a string and separate by semicolons.
398 * If there is an error of any kind, return <error>.
402 login_getpath(login_cap_t
*lc
, const char *cap
, const char *error
)
408 str
= login_getcapstr(lc
, cap
, NULL
, NULL
);
411 ptr
= __DECONST(char *, str
); /* XXXX Yes, very dodgy */
413 count
= strcspn(ptr
, ", \t");
423 isinfinite(const char *s
)
425 static const char *infs
[] = {
433 const char **i
= &infs
[0];
436 if (strcasecmp(s
, *i
) == 0)
445 rmultiply(u_quad_t n1
, u_quad_t n2
)
452 /* Handle simple cases */
453 if (n1
== 0 || n2
== 0)
461 * sizeof() returns number of bytes needed for storage.
462 * This may be different from the actual number of useful bits.
465 bpw
= sizeof(u_quad_t
) * 8;
466 while (((u_quad_t
)1 << (bpw
-1)) == 0)
471 * First check the magnitude of each number. If the sum of the
472 * magnatude is way to high, reject the number. (If this test
473 * is not done then the first multiply below may overflow.)
475 for (b1
= bpw
; (((u_quad_t
)1 << (b1
-1)) & n1
) == 0; --b1
)
477 for (b2
= bpw
; (((u_quad_t
)1 << (b2
-1)) & n2
) == 0; --b2
)
479 if (b1
+ b2
- 2 > bpw
) {
485 * Decompose the multiplication to be:
490 * (h1 + l1) * (h2 + l2)
491 * (h1 * h2) + (h1 * l2) + (l1 * h2) + (l1 * l2)
493 * Since h1 && h2 do not have the low bit set, we can then say:
495 * (h1>>1 * h2>>1 * 4) + ...
497 * So if (h1>>1 * h2>>1) > (1<<(bpw - 2)) then the result will
500 * Finally, if MAX - ((h1 * l2) + (l1 * h2) + (l1 * l2)) < (h1*h2)
501 * then adding in residual amout will cause an overflow.
504 m
= (n1
>> 1) * (n2
>> 1);
505 if (m
>= ((u_quad_t
)1 << (bpw
-2))) {
512 + (n2
& 1) * (n1
& ~(u_quad_t
)1)
513 + (n1
& 1) * (n2
& ~(u_quad_t
)1);
515 if ((u_quad_t
)(m
+ r
) < m
) {
527 * From the login_cap_t <lc>, get the capability <cap>, which is
528 * formatted as a time (e.g., "<cap>=10h3m2s"). If <cap> is not
529 * present in <lc>, return <def>; if there is an error of some kind,
534 login_getcaptime(login_cap_t
*lc
, const char *cap
, rlim_t def
, rlim_t error
)
536 char *res
, *ep
, *oval
;
541 if (lc
== NULL
|| lc
->lc_cap
== NULL
)
545 * Look for <cap> in lc_cap.
546 * If it's not there (-1), return <def>.
547 * If there's an error, return <error>.
550 if ((r
= cgetstr(lc
->lc_cap
, cap
, &res
)) == -1)
557 /* "inf" and "infinity" are special cases */
559 return RLIM_INFINITY
;
562 * Now go through the string, turning something like 1h2m3s into
563 * an integral value. Whee.
570 rlim_t tim
= strtoq(res
, &ep
, 0);
573 if (ep
== NULL
|| ep
== res
|| errno
!= 0) {
575 syslog(LOG_WARNING
, "login_getcaptime: class '%s' bad value %s=%s",
576 lc
->lc_class
, cap
, oval
);
580 /* Look for suffixes */
584 break; /* end of string */
585 case 's': case 'S': /* seconds */
587 case 'm': case 'M': /* minutes */
590 case 'h': case 'H': /* hours */
593 case 'd': case 'D': /* days */
594 mult
= 60L * 60L * 24L;
596 case 'w': case 'W': /* weeks */
597 mult
= 60L * 60L * 24L * 7L;
599 case 'y': case 'Y': /* 365-day years */
600 mult
= 60L * 60L * 24L * 365L;
606 tot
+= rmultiply(tim
, mult
);
617 * From the login_cap_t <lc>, extract the numerical value <cap>.
618 * If it is not present, return <def> for a default, and return
619 * <error> if there is an error.
620 * Like login_getcaptime(), only it only converts to a number, not
621 * to a time; "infinity" and "inf" are 'special.'
625 login_getcapnum(login_cap_t
*lc
, const char *cap
, rlim_t def
, rlim_t error
)
631 if (lc
== NULL
|| lc
->lc_cap
== NULL
)
635 * For BSDI compatibility, try for the tag=<val> first
637 if ((r
= cgetstr(lc
->lc_cap
, cap
, &res
)) == -1) {
639 /* string capability not present, so try for tag#<val> as numeric */
640 if ((r
= cgetnum(lc
->lc_cap
, cap
, &lval
)) == -1)
641 return def
; /* Not there, so return default */
652 return RLIM_INFINITY
;
655 val
= strtoq(res
, &ep
, 0);
656 if (ep
== NULL
|| ep
== res
|| errno
!= 0) {
657 syslog(LOG_WARNING
, "login_getcapnum: class '%s' bad value %s=%s",
658 lc
->lc_class
, cap
, res
);
670 * From the login_cap_t <lc>, extract the capability <cap>, which is
671 * formatted as a size (e.g., "<cap>=10M"); it can also be "infinity".
672 * If not present, return <def>, or <error> if there is an error of
677 login_getcapsize(login_cap_t
*lc
, const char *cap
, rlim_t def
, rlim_t error
)
679 char *ep
, *res
, *oval
;
683 if (lc
== NULL
|| lc
->lc_cap
== NULL
)
686 if ((r
= cgetstr(lc
->lc_cap
, cap
, &res
)) == -1)
694 return RLIM_INFINITY
;
700 rlim_t siz
= strtoq(res
, &ep
, 0);
703 if (ep
== NULL
|| ep
== res
|| errno
!= 0) {
705 syslog(LOG_WARNING
, "login_getcapsize: class '%s' bad value %s=%s",
706 lc
->lc_class
, cap
, oval
);
711 case 0: /* end of string */
714 case 'b': case 'B': /* 512-byte blocks */
717 case 'k': case 'K': /* 1024-byte Kilobytes */
720 case 'm': case 'M': /* 1024-k kbytes */
723 case 'g': case 'G': /* 1Gbyte */
724 mult
= 1024 * 1024 * 1024;
726 case 't': case 'T': /* 1TBte */
727 mult
= 1024LL * 1024LL * 1024LL * 1024LL;
733 tot
+= rmultiply(siz
, mult
);
744 * From the login_cap_t <lc>, check for the existance of the capability
745 * of <cap>. Return <def> if <lc>->lc_cap is NULL, otherwise return
746 * the whether or not <cap> exists there.
750 login_getcapbool(login_cap_t
*lc
, const char *cap
, int def
)
752 if (lc
== NULL
|| lc
->lc_cap
== NULL
)
754 return (cgetcap(lc
->lc_cap
, cap
, ':') != NULL
);
760 * Given a login_cap entry <lc>, and optionally a type of auth <auth>,
761 * and optionally a style <style>, find the style that best suits these
763 * 1. If <auth> is non-null, look for an "auth-<auth>=" string
764 * in the capability; if not present, default to "auth=".
765 * 2. If there is no auth list found from (1), default to
766 * "passwd" as an authorization list.
767 * 3. If <style> is non-null, look for <style> in the list of
768 * authorization methods found from (2); if <style> is NULL, default
769 * to LOGIN_DEFSTYLE ("passwd").
770 * 4. If the chosen style is found in the chosen list of authorization
771 * methods, return that; otherwise, return NULL.
773 * login_getstyle(lc, NULL, "ftp");
774 * login_getstyle(lc, "login", NULL);
775 * login_getstyle(lc, "skey", "network");
779 login_getstyle(login_cap_t
*lc
, const char *style
, const char *auth
)
782 const char **authtypes
= NULL
;
786 static const char *defauthtypes
[] = { LOGIN_DEFSTYLE
, NULL
};
788 if (auth
!= NULL
&& *auth
!= '\0') {
789 if (snprintf(realauth
, sizeof realauth
, "auth-%s", auth
) < (int)sizeof(realauth
))
790 authtypes
= login_getcaplist(lc
, realauth
, NULL
);
793 if (authtypes
== NULL
)
794 authtypes
= login_getcaplist(lc
, "auth", NULL
);
796 if (authtypes
== NULL
)
797 authtypes
= defauthtypes
;
800 * We have at least one authtype now; auths is a comma-separated
801 * (or space-separated) list of authentication types. We have to
802 * convert from this to an array of char*'s; authtypes then gets this.
805 if (style
!= NULL
&& *style
!= '\0') {
806 while (authtypes
[i
] != NULL
&& strcmp(style
, authtypes
[i
]) != 0)
811 if (authtypes
[i
] != NULL
&& (auths
= strdup(authtypes
[i
])) != NULL
)
812 lc
->lc_style
= auths
;
814 if (lc
->lc_style
!= NULL
)
815 lc
->lc_style
= strdup(lc
->lc_style
);