]>
git.cameronkatri.com Git - pw-darwin.git/blob - libutil/login_cap.c
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
34 #include <sys/resource.h>
35 #include <sys/param.h>
39 #include <login_cap.h>
49 * Manage a single static pointer for handling a local char* buffer,
50 * resizing as necessary to contain the string.
53 * Manage a static array for handling a group of strings, resizing
57 static int lc_object_count
= 0;
59 static size_t internal_stringsz
= 0;
60 static char * internal_string
= NULL
;
61 static size_t internal_arraysz
= 0;
62 static const char ** internal_array
= NULL
;
65 allocstr(const char *str
)
69 size_t sz
= strlen(str
) + 1; /* realloc() only if necessary */
70 if (sz
<= internal_stringsz
)
71 p
= strcpy(internal_string
, str
);
72 else if ((p
= realloc(internal_string
, sz
)) != NULL
) {
73 internal_stringsz
= sz
;
74 internal_string
= strcpy(p
, str
);
83 static const char **p
;
85 if (sz
<= internal_arraysz
)
87 else if ((p
= realloc(internal_array
, sz
* sizeof(char*))) != NULL
) {
88 internal_arraysz
= sz
;
97 * Turn a simple string <str> separated by any of
98 * the set of <chars> into an array. The last element
99 * of the array will be NULL, as is proper.
100 * Free using freearraystr()
104 arrayize(const char *str
, const char *chars
, int *size
)
109 const char **res
= NULL
;
111 /* count the sub-strings */
112 for (i
= 0, cptr
= str
; *cptr
; i
++) {
113 int count
= strcspn(cptr
, chars
);
119 /* alloc the array */
120 if ((ptr
= allocstr(str
)) != NULL
) {
121 if ((res
= allocarray(++i
)) == NULL
)
122 free((void *)(uintptr_t)(const void *)str
);
124 /* now split the string */
127 int count
= strcspn(ptr
, chars
);
146 * Frees up all resources relating to a login class
151 login_close(login_cap_t
* lc
)
158 if (--lc_object_count
== 0) {
159 free(internal_string
);
160 free(internal_array
);
161 internal_array
= NULL
;
162 internal_arraysz
= 0;
163 internal_string
= NULL
;
164 internal_stringsz
= 0;
172 * login_getclassbyname() get the login class by its name.
173 * If the name given is NULL or empty, the default class
174 * LOGIN_DEFCLASS (ie. "default") is fetched. If the
175 * 'dir' argument contains a non-NULL non-empty string,
176 * then the file _FILE_LOGIN_CONF is picked up from that
177 * directory instead of the system login database.
178 * Return a filled-out login_cap_t structure, including
179 * class name, and the capability record buffer.
183 login_getclassbyname(char const *name
, const struct passwd
*pwd
)
187 if ((lc
= malloc(sizeof(login_cap_t
))) != NULL
) {
191 const char *msg
= NULL
;
193 char userpath
[MAXPATHLEN
];
195 static char *login_dbarray
[] = { NULL
, NULL
, NULL
};
197 me
= (name
!= NULL
&& strcmp(name
, LOGIN_MECLASS
) == 0);
198 dir
= (!me
|| pwd
== NULL
) ? NULL
: pwd
->pw_dir
;
200 * Switch to user mode before checking/reading its ~/.login_conf
201 * - some NFSes have root read access disabled.
203 * XXX: This fails to configure additional groups.
208 (void)setegid(pwd
->pw_gid
);
209 (void)seteuid(pwd
->pw_uid
);
212 if (dir
&& snprintf(userpath
, MAXPATHLEN
, "%s/%s", dir
,
213 _FILE_LOGIN_CONF
) < MAXPATHLEN
) {
214 login_dbarray
[i
] = userpath
;
215 if (_secure_path(userpath
, pwd
->pw_uid
, pwd
->pw_gid
) != -1)
216 i
++; /* only use 'secure' data */
218 if (_secure_path(_PATH_LOGIN_CONF
, 0, 0) != -1)
219 login_dbarray
[i
++] = _PATH_LOGIN_CONF
;
220 login_dbarray
[i
] = NULL
;
222 memset(lc
, 0, sizeof(login_cap_t
));
223 lc
->lc_cap
= lc
->lc_class
= lc
->lc_style
= NULL
;
225 if (name
== NULL
|| *name
== '\0')
226 name
= LOGIN_DEFCLASS
;
228 switch (cgetent(&lc
->lc_cap
, login_dbarray
, name
)) {
229 case -1: /* Failed, entry does not exist */
231 break; /* Don't retry default on 'me' */
234 else if ((r
= open(login_dbarray
[0], O_RDONLY
)) >= 0)
237 * If there's at least one login class database,
238 * and we aren't searching for a default class
239 * then complain about a non-existent class.
241 if (r
>= 0 || strcmp(name
, LOGIN_DEFCLASS
) != 0)
242 syslog(LOG_ERR
, "login_getclass: unknown class '%s'", name
);
243 /* fall-back to default class */
244 name
= LOGIN_DEFCLASS
;
245 msg
= "%s: no default/fallback class '%s'";
246 if (cgetent(&lc
->lc_cap
, login_dbarray
, name
) != 0 && r
>= 0)
248 /* FALLTHROUGH - just return system defaults */
249 case 0: /* success! */
250 if ((lc
->lc_class
= strdup(name
)) != NULL
) {
258 msg
= "%s: strdup: %m";
261 msg
= "%s: retrieving class information: %m";
264 msg
= "%s: 'tc=' reference loop '%s'";
267 msg
= "couldn't resolve 'tc=' reference in '%s'";
270 msg
= "%s: unexpected cgetent() error '%s': %m";
278 syslog(LOG_ERR
, msg
, "login_getclass", name
);
289 * Get the login class for the system (only) login class database.
290 * Return a filled-out login_cap_t structure, including
291 * class name, and the capability record buffer.
295 login_getclass(const char *cls
)
297 return login_getclassbyname(cls
, NULL
);
303 * Get the login class for a given password entry from
304 * the system (only) login class database.
305 * If the password entry's class field is not set, or
306 * the class specified does not exist, then use the
307 * default of LOGIN_DEFCLASS (ie. "default").
308 * Return a filled-out login_cap_t structure, including
309 * class name, and the capability record buffer.
313 login_getpwclass(const struct passwd
*pwd
)
315 const char *cls
= NULL
;
319 if (cls
== NULL
|| *cls
== '\0')
320 cls
= (pwd
->pw_uid
== 0) ? LOGIN_DEFROOTCLASS
: LOGIN_DEFCLASS
;
322 return login_getclassbyname(cls
, pwd
);
327 * login_getuserclass()
328 * Get the login class for a given password entry, allowing user
329 * overrides via ~/.login_conf.
333 login_getuserclass(const struct passwd
*pwd
)
335 return login_getclassbyname(LOGIN_MECLASS
, pwd
);
342 * Given a login_cap entry, and a capability name, return the
343 * value defined for that capability, a defualt if not found, or
344 * an error string on error.
348 login_getcapstr(login_cap_t
*lc
, const char *cap
, const char *def
, const char *error
)
353 if (lc
== NULL
|| cap
== NULL
|| lc
->lc_cap
== NULL
|| *cap
== '\0')
356 if ((ret
= cgetstr(lc
->lc_cap
, cap
, &res
)) == -1)
358 return (ret
>= 0) ? res
: error
;
364 * Given a login_cap entry, and a capability name, return the
365 * value defined for that capability split into an array of
370 login_getcaplist(login_cap_t
*lc
, const char *cap
, const char *chars
)
376 if ((lstring
= login_getcapstr(lc
, cap
, NULL
, NULL
)) != NULL
)
377 return arrayize(lstring
, chars
, NULL
);
384 * From the login_cap_t <lc>, get the capability <cap> which is
385 * formatted as either a space or comma delimited list of paths
386 * and append them all into a string and separate by semicolons.
387 * If there is an error of any kind, return <error>.
391 login_getpath(login_cap_t
*lc
, const char *cap
, const char *error
)
397 str
= login_getcapstr(lc
, cap
, NULL
, NULL
);
400 ptr
= __DECONST(char *, str
); /* XXXX Yes, very dodgy */
402 count
= strcspn(ptr
, ", \t");
412 isinfinite(const char *s
)
414 static const char *infs
[] = {
422 const char **i
= &infs
[0];
425 if (strcasecmp(s
, *i
) == 0)
434 rmultiply(u_quad_t n1
, u_quad_t n2
)
441 /* Handle simple cases */
442 if (n1
== 0 || n2
== 0)
450 * sizeof() returns number of bytes needed for storage.
451 * This may be different from the actual number of useful bits.
454 bpw
= sizeof(u_quad_t
) * 8;
455 while (((u_quad_t
)1 << (bpw
-1)) == 0)
460 * First check the magnitude of each number. If the sum of the
461 * magnatude is way to high, reject the number. (If this test
462 * is not done then the first multiply below may overflow.)
464 for (b1
= bpw
; (((u_quad_t
)1 << (b1
-1)) & n1
) == 0; --b1
)
466 for (b2
= bpw
; (((u_quad_t
)1 << (b2
-1)) & n2
) == 0; --b2
)
468 if (b1
+ b2
- 2 > bpw
) {
474 * Decompose the multiplication to be:
479 * (h1 + l1) * (h2 + l2)
480 * (h1 * h2) + (h1 * l2) + (l1 * h2) + (l1 * l2)
482 * Since h1 && h2 do not have the low bit set, we can then say:
484 * (h1>>1 * h2>>1 * 4) + ...
486 * So if (h1>>1 * h2>>1) > (1<<(bpw - 2)) then the result will
489 * Finally, if MAX - ((h1 * l2) + (l1 * h2) + (l1 * l2)) < (h1*h2)
490 * then adding in residual amout will cause an overflow.
493 m
= (n1
>> 1) * (n2
>> 1);
494 if (m
>= ((u_quad_t
)1 << (bpw
-2))) {
501 + (n2
& 1) * (n1
& ~(u_quad_t
)1)
502 + (n1
& 1) * (n2
& ~(u_quad_t
)1);
504 if ((u_quad_t
)(m
+ r
) < m
) {
516 * From the login_cap_t <lc>, get the capability <cap>, which is
517 * formatted as a time (e.g., "<cap>=10h3m2s"). If <cap> is not
518 * present in <lc>, return <def>; if there is an error of some kind,
523 login_getcaptime(login_cap_t
*lc
, const char *cap
, rlim_t def
, rlim_t error
)
525 char *res
, *ep
, *oval
;
530 if (lc
== NULL
|| lc
->lc_cap
== NULL
)
534 * Look for <cap> in lc_cap.
535 * If it's not there (-1), return <def>.
536 * If there's an error, return <error>.
539 if ((r
= cgetstr(lc
->lc_cap
, cap
, &res
)) == -1)
546 /* "inf" and "infinity" are special cases */
548 return RLIM_INFINITY
;
551 * Now go through the string, turning something like 1h2m3s into
552 * an integral value. Whee.
559 rlim_t tim
= strtoq(res
, &ep
, 0);
562 if (ep
== NULL
|| ep
== res
|| errno
!= 0) {
564 syslog(LOG_WARNING
, "login_getcaptime: class '%s' bad value %s=%s",
565 lc
->lc_class
, cap
, oval
);
569 /* Look for suffixes */
573 break; /* end of string */
574 case 's': case 'S': /* seconds */
576 case 'm': case 'M': /* minutes */
579 case 'h': case 'H': /* hours */
582 case 'd': case 'D': /* days */
583 mult
= 60L * 60L * 24L;
585 case 'w': case 'W': /* weeks */
586 mult
= 60L * 60L * 24L * 7L;
588 case 'y': case 'Y': /* 365-day years */
589 mult
= 60L * 60L * 24L * 365L;
595 tot
+= rmultiply(tim
, mult
);
606 * From the login_cap_t <lc>, extract the numerical value <cap>.
607 * If it is not present, return <def> for a default, and return
608 * <error> if there is an error.
609 * Like login_getcaptime(), only it only converts to a number, not
610 * to a time; "infinity" and "inf" are 'special.'
614 login_getcapnum(login_cap_t
*lc
, const char *cap
, rlim_t def
, rlim_t error
)
620 if (lc
== NULL
|| lc
->lc_cap
== NULL
)
624 * For BSDI compatibility, try for the tag=<val> first
626 if ((r
= cgetstr(lc
->lc_cap
, cap
, &res
)) == -1) {
628 /* string capability not present, so try for tag#<val> as numeric */
629 if ((r
= cgetnum(lc
->lc_cap
, cap
, &lval
)) == -1)
630 return def
; /* Not there, so return default */
641 return RLIM_INFINITY
;
644 val
= strtoq(res
, &ep
, 0);
645 if (ep
== NULL
|| ep
== res
|| errno
!= 0) {
646 syslog(LOG_WARNING
, "login_getcapnum: class '%s' bad value %s=%s",
647 lc
->lc_class
, cap
, res
);
659 * From the login_cap_t <lc>, extract the capability <cap>, which is
660 * formatted as a size (e.g., "<cap>=10M"); it can also be "infinity".
661 * If not present, return <def>, or <error> if there is an error of
666 login_getcapsize(login_cap_t
*lc
, const char *cap
, rlim_t def
, rlim_t error
)
668 char *ep
, *res
, *oval
;
672 if (lc
== NULL
|| lc
->lc_cap
== NULL
)
675 if ((r
= cgetstr(lc
->lc_cap
, cap
, &res
)) == -1)
683 return RLIM_INFINITY
;
689 rlim_t siz
= strtoq(res
, &ep
, 0);
692 if (ep
== NULL
|| ep
== res
|| errno
!= 0) {
694 syslog(LOG_WARNING
, "login_getcapsize: class '%s' bad value %s=%s",
695 lc
->lc_class
, cap
, oval
);
700 case 0: /* end of string */
703 case 'b': case 'B': /* 512-byte blocks */
706 case 'k': case 'K': /* 1024-byte Kilobytes */
709 case 'm': case 'M': /* 1024-k kbytes */
712 case 'g': case 'G': /* 1Gbyte */
713 mult
= 1024 * 1024 * 1024;
715 case 't': case 'T': /* 1TBte */
716 mult
= 1024LL * 1024LL * 1024LL * 1024LL;
722 tot
+= rmultiply(siz
, mult
);
733 * From the login_cap_t <lc>, check for the existance of the capability
734 * of <cap>. Return <def> if <lc>->lc_cap is NULL, otherwise return
735 * the whether or not <cap> exists there.
739 login_getcapbool(login_cap_t
*lc
, const char *cap
, int def
)
741 if (lc
== NULL
|| lc
->lc_cap
== NULL
)
743 return (cgetcap(lc
->lc_cap
, cap
, ':') != NULL
);
749 * Given a login_cap entry <lc>, and optionally a type of auth <auth>,
750 * and optionally a style <style>, find the style that best suits these
752 * 1. If <auth> is non-null, look for an "auth-<auth>=" string
753 * in the capability; if not present, default to "auth=".
754 * 2. If there is no auth list found from (1), default to
755 * "passwd" as an authorization list.
756 * 3. If <style> is non-null, look for <style> in the list of
757 * authorization methods found from (2); if <style> is NULL, default
758 * to LOGIN_DEFSTYLE ("passwd").
759 * 4. If the chosen style is found in the chosen list of authorization
760 * methods, return that; otherwise, return NULL.
762 * login_getstyle(lc, NULL, "ftp");
763 * login_getstyle(lc, "login", NULL);
764 * login_getstyle(lc, "skey", "network");
768 login_getstyle(login_cap_t
*lc
, const char *style
, const char *auth
)
771 const char **authtypes
= NULL
;
775 static const char *defauthtypes
[] = { LOGIN_DEFSTYLE
, NULL
};
777 if (auth
!= NULL
&& *auth
!= '\0') {
778 if (snprintf(realauth
, sizeof realauth
, "auth-%s", auth
) < (int)sizeof(realauth
))
779 authtypes
= login_getcaplist(lc
, realauth
, NULL
);
782 if (authtypes
== NULL
)
783 authtypes
= login_getcaplist(lc
, "auth", NULL
);
785 if (authtypes
== NULL
)
786 authtypes
= defauthtypes
;
789 * We have at least one authtype now; auths is a comma-separated
790 * (or space-separated) list of authentication types. We have to
791 * convert from this to an array of char*'s; authtypes then gets this.
794 if (style
!= NULL
&& *style
!= '\0') {
795 while (authtypes
[i
] != NULL
&& strcmp(style
, authtypes
[i
]) != 0)
800 if (authtypes
[i
] != NULL
&& (auths
= strdup(authtypes
[i
])) != NULL
)
801 lc
->lc_style
= auths
;
803 if (lc
->lc_style
!= NULL
)
804 lc
->lc_style
= strdup(lc
->lc_style
);