]>
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
38 #include <sys/types.h>
40 #include <sys/resource.h>
41 #include <sys/param.h>
45 #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 char ** internal_array
= NULL
;
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
);
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(char *str
, const char *chars
, int *size
)
110 /* count the sub-strings */
111 for (i
= 0, ptr
= str
; *ptr
; i
++) {
112 int count
= strcspn(ptr
, chars
);
118 /* alloc the array */
119 if ((ptr
= allocstr(str
)) != NULL
) {
120 if ((res
= allocarray(++i
)) == NULL
)
123 /* now split the string */
126 int count
= strcspn(ptr
, chars
);
145 * Frees up all resources relating to a login class
150 login_close(login_cap_t
* lc
)
157 if (--lc_object_count
== 0) {
158 free(internal_string
);
159 free(internal_array
);
160 internal_array
= NULL
;
161 internal_arraysz
= 0;
162 internal_string
= NULL
;
163 internal_stringsz
= 0;
171 * login_getclassbyname() get the login class by its name.
172 * If the name given is NULL or empty, the default class
173 * LOGIN_DEFCLASS (ie. "default") is fetched. If the
174 * 'dir' argument contains a non-NULL non-empty string,
175 * then the file _FILE_LOGIN_CONF is picked up from that
176 * directory instead of the system login database.
177 * Return a filled-out login_cap_t structure, including
178 * class name, and the capability record buffer.
182 login_getclassbyname(char const *name
, const struct passwd
*pwd
)
186 if ((lc
= malloc(sizeof(login_cap_t
))) != NULL
) {
190 const char *msg
= NULL
;
191 const char *dir
= (pwd
== NULL
) ? NULL
: pwd
->pw_dir
;
192 char userpath
[MAXPATHLEN
];
194 static char *login_dbarray
[] = { NULL
, NULL
, NULL
};
197 * Switch to user mode before checking/reading its ~/.login_conf
198 * - some NFSes have root read access disabled.
200 * XXX: This fails to configure additional groups.
205 (void)setegid(pwd
->pw_gid
);
206 (void)seteuid(pwd
->pw_uid
);
209 if (dir
&& snprintf(userpath
, MAXPATHLEN
, "%s/%s", dir
,
210 _FILE_LOGIN_CONF
) < MAXPATHLEN
) {
211 login_dbarray
[i
] = userpath
;
212 if (_secure_path(userpath
, pwd
->pw_uid
, pwd
->pw_gid
) != -1)
213 i
++; /* only use 'secure' data */
215 if (_secure_path(_PATH_LOGIN_CONF
, 0, 0) != -1)
216 login_dbarray
[i
++] = _PATH_LOGIN_CONF
;
217 login_dbarray
[i
] = NULL
;
219 memset(lc
, 0, sizeof(login_cap_t
));
220 lc
->lc_cap
= lc
->lc_class
= lc
->lc_style
= NULL
;
222 if (name
== NULL
|| *name
== '\0')
223 name
= LOGIN_DEFCLASS
;
225 switch (cgetent(&lc
->lc_cap
, login_dbarray
, (char*)name
)) {
226 case -1: /* Failed, entry does not exist */
227 if (strcmp(name
, LOGIN_MECLASS
) == 0)
228 break; /* Don't retry default on 'me' */
231 else if ((r
= open(login_dbarray
[0], O_RDONLY
)) >= 0)
234 * If there's at least one login class database,
235 * and we aren't searching for a default class
236 * then complain about a non-existent class.
238 if (r
>= 0 || strcmp(name
, LOGIN_DEFCLASS
) != 0)
239 syslog(LOG_ERR
, "login_getclass: unknown class '%s'", name
);
240 /* fall-back to default class */
241 name
= LOGIN_DEFCLASS
;
242 msg
= "%s: no default/fallback class '%s'";
243 if (cgetent(&lc
->lc_cap
, login_dbarray
, (char*)name
) != 0 && r
>= 0)
245 /* Fallthru - just return system defaults */
246 case 0: /* success! */
247 if ((lc
->lc_class
= strdup(name
)) != NULL
) {
255 msg
= "%s: strdup: %m";
258 msg
= "%s: retrieving class information: %m";
261 msg
= "%s: 'tc=' reference loop '%s'";
264 msg
= "couldn't resolve 'tc=' reference in '%s'";
267 msg
= "%s: unexpected cgetent() error '%s': %m";
275 syslog(LOG_ERR
, msg
, "login_getclass", name
);
286 * Get the login class for the system (only) login class database.
287 * Return a filled-out login_cap_t structure, including
288 * class name, and the capability record buffer.
292 login_getclass(const char *cls
)
294 return login_getclassbyname(cls
, NULL
);
300 * Get the login class for a given password entry from
301 * the system (only) login class database.
302 * If the password entry's class field is not set, or
303 * the class specified does not exist, then use the
304 * default of LOGIN_DEFCLASS (ie. "default").
305 * Return a filled-out login_cap_t structure, including
306 * class name, and the capability record buffer.
310 login_getpwclass(const struct passwd
*pwd
)
312 const char *cls
= NULL
;
316 if (cls
== NULL
|| *cls
== '\0')
317 cls
= (pwd
->pw_uid
== 0) ? LOGIN_DEFROOTCLASS
: LOGIN_DEFCLASS
;
319 return login_getclassbyname(cls
, pwd
);
324 * login_getuserclass()
325 * Get the login class for a given password entry, allowing user
326 * overrides via ~/.login_conf.
330 login_getuserclass(const struct passwd
*pwd
)
332 return login_getclassbyname(LOGIN_MECLASS
, pwd
);
339 * Given a login_cap entry, and a capability name, return the
340 * value defined for that capability, a defualt if not found, or
341 * an error string on error.
345 login_getcapstr(login_cap_t
*lc
, const char *cap
, char *def
, char *error
)
350 if (lc
== NULL
|| cap
== NULL
|| lc
->lc_cap
== NULL
|| *cap
== '\0')
353 if ((ret
= cgetstr(lc
->lc_cap
, (char *)cap
, &res
)) == -1)
355 return (ret
>= 0) ? res
: error
;
361 * Given a login_cap entry, and a capability name, return the
362 * value defined for that capability split into an array of
367 login_getcaplist(login_cap_t
*lc
, const char *cap
, const char *chars
)
373 if ((lstring
= login_getcapstr(lc
, (char*)cap
, NULL
, NULL
)) != NULL
)
374 return arrayize(lstring
, chars
, NULL
);
381 * From the login_cap_t <lc>, get the capability <cap> which is
382 * formatted as either a space or comma delimited list of paths
383 * and append them all into a string and separate by semicolons.
384 * If there is an error of any kind, return <error>.
388 login_getpath(login_cap_t
*lc
, const char *cap
, char * error
)
392 if ((str
= login_getcapstr(lc
, (char*)cap
, NULL
, NULL
)) == NULL
)
398 int count
= strcspn(ptr
, ", \t");
409 isinfinite(const char *s
)
411 static const char *infs
[] = {
419 const char **i
= &infs
[0];
422 if (strcasecmp(s
, *i
) == 0)
431 rmultiply(u_quad_t n1
, u_quad_t n2
)
438 /* Handle simple cases */
439 if (n1
== 0 || n2
== 0)
447 * sizeof() returns number of bytes needed for storage.
448 * This may be different from the actual number of useful bits.
451 bpw
= sizeof(u_quad_t
) * 8;
452 while (((u_quad_t
)1 << (bpw
-1)) == 0)
457 * First check the magnitude of each number. If the sum of the
458 * magnatude is way to high, reject the number. (If this test
459 * is not done then the first multiply below may overflow.)
461 for (b1
= bpw
; (((u_quad_t
)1 << (b1
-1)) & n1
) == 0; --b1
)
463 for (b2
= bpw
; (((u_quad_t
)1 << (b2
-1)) & n2
) == 0; --b2
)
465 if (b1
+ b2
- 2 > bpw
) {
471 * Decompose the multiplication to be:
476 * (h1 + l1) * (h2 + l2)
477 * (h1 * h2) + (h1 * l2) + (l1 * h2) + (l1 * l2)
479 * Since h1 && h2 do not have the low bit set, we can then say:
481 * (h1>>1 * h2>>1 * 4) + ...
483 * So if (h1>>1 * h2>>1) > (1<<(bpw - 2)) then the result will
486 * Finally, if MAX - ((h1 * l2) + (l1 * h2) + (l1 * l2)) < (h1*h2)
487 * then adding in residual amout will cause an overflow.
490 m
= (n1
>> 1) * (n2
>> 1);
491 if (m
>= ((u_quad_t
)1 << (bpw
-2))) {
498 + (n2
& 1) * (n1
& ~(u_quad_t
)1)
499 + (n1
& 1) * (n2
& ~(u_quad_t
)1);
501 if ((u_quad_t
)(m
+ r
) < m
) {
513 * From the login_cap_t <lc>, get the capability <cap>, which is
514 * formatted as a time (e.g., "<cap>=10h3m2s"). If <cap> is not
515 * present in <lc>, return <def>; if there is an error of some kind,
520 login_getcaptime(login_cap_t
*lc
, const char *cap
, rlim_t def
, rlim_t error
)
522 char *res
, *ep
, *oval
;
527 if (lc
== NULL
|| lc
->lc_cap
== NULL
)
531 * Look for <cap> in lc_cap.
532 * If it's not there (-1), return <def>.
533 * If there's an error, return <error>.
536 if ((r
= cgetstr(lc
->lc_cap
, (char *)cap
, &res
)) == -1)
543 /* "inf" and "infinity" are special cases */
545 return RLIM_INFINITY
;
548 * Now go through the string, turning something like 1h2m3s into
549 * an integral value. Whee.
556 rlim_t tim
= strtoq(res
, &ep
, 0);
559 if (ep
== NULL
|| ep
== res
|| errno
!= 0) {
561 syslog(LOG_WARNING
, "login_getcaptime: class '%s' bad value %s=%s",
562 lc
->lc_class
, cap
, oval
);
566 /* Look for suffixes */
570 break; /* end of string */
571 case 's': case 'S': /* seconds */
573 case 'm': case 'M': /* minutes */
576 case 'h': case 'H': /* hours */
579 case 'd': case 'D': /* days */
580 mult
= 60L * 60L * 24L;
582 case 'w': case 'W': /* weeks */
583 mult
= 60L * 60L * 24L * 7L;
585 case 'y': case 'Y': /* 365-day years */
586 mult
= 60L * 60L * 24L * 365L;
592 tot
+= rmultiply(tim
, mult
);
603 * From the login_cap_t <lc>, extract the numerical value <cap>.
604 * If it is not present, return <def> for a default, and return
605 * <error> if there is an error.
606 * Like login_getcaptime(), only it only converts to a number, not
607 * to a time; "infinity" and "inf" are 'special.'
611 login_getcapnum(login_cap_t
*lc
, const char *cap
, rlim_t def
, rlim_t error
)
617 if (lc
== NULL
|| lc
->lc_cap
== NULL
)
621 * For BSDI compatibility, try for the tag=<val> first
623 if ((r
= cgetstr(lc
->lc_cap
, (char *)cap
, &res
)) == -1) {
625 /* string capability not present, so try for tag#<val> as numeric */
626 if ((r
= cgetnum(lc
->lc_cap
, (char *)cap
, &lval
)) == -1)
627 return def
; /* Not there, so return default */
638 return RLIM_INFINITY
;
641 val
= strtoq(res
, &ep
, 0);
642 if (ep
== NULL
|| ep
== res
|| errno
!= 0) {
643 syslog(LOG_WARNING
, "login_getcapnum: class '%s' bad value %s=%s",
644 lc
->lc_class
, cap
, res
);
656 * From the login_cap_t <lc>, extract the capability <cap>, which is
657 * formatted as a size (e.g., "<cap>=10M"); it can also be "infinity".
658 * If not present, return <def>, or <error> if there is an error of
663 login_getcapsize(login_cap_t
*lc
, const char *cap
, rlim_t def
, rlim_t error
)
665 char *ep
, *res
, *oval
;
669 if (lc
== NULL
|| lc
->lc_cap
== NULL
)
672 if ((r
= cgetstr(lc
->lc_cap
, (char *)cap
, &res
)) == -1)
680 return RLIM_INFINITY
;
686 rlim_t siz
= strtoq(res
, &ep
, 0);
689 if (ep
== NULL
|| ep
== res
|| errno
!= 0) {
691 syslog(LOG_WARNING
, "login_getcapsize: class '%s' bad value %s=%s",
692 lc
->lc_class
, cap
, oval
);
697 case 0: /* end of string */
700 case 'b': case 'B': /* 512-byte blocks */
703 case 'k': case 'K': /* 1024-byte Kilobytes */
706 case 'm': case 'M': /* 1024-k kbytes */
709 case 'g': case 'G': /* 1Gbyte */
710 mult
= 1024 * 1024 * 1024;
712 case 't': case 'T': /* 1TBte */
713 mult
= 1024LL * 1024LL * 1024LL * 1024LL;
719 tot
+= rmultiply(siz
, mult
);
730 * From the login_cap_t <lc>, check for the existance of the capability
731 * of <cap>. Return <def> if <lc>->lc_cap is NULL, otherwise return
732 * the whether or not <cap> exists there.
736 login_getcapbool(login_cap_t
*lc
, const char *cap
, int def
)
738 if (lc
== NULL
|| lc
->lc_cap
== NULL
)
740 return (cgetcap(lc
->lc_cap
, (char *)cap
, ':') != NULL
);
746 * Given a login_cap entry <lc>, and optionally a type of auth <auth>,
747 * and optionally a style <style>, find the style that best suits these
749 * 1. If <auth> is non-null, look for an "auth-<auth>=" string
750 * in the capability; if not present, default to "auth=".
751 * 2. If there is no auth list found from (1), default to
752 * "passwd" as an authorization list.
753 * 3. If <style> is non-null, look for <style> in the list of
754 * authorization methods found from (2); if <style> is NULL, default
755 * to LOGIN_DEFSTYLE ("passwd").
756 * 4. If the chosen style is found in the chosen list of authorization
757 * methods, return that; otherwise, return NULL.
759 * login_getstyle(lc, NULL, "ftp");
760 * login_getstyle(lc, "login", NULL);
761 * login_getstyle(lc, "skey", "network");
765 login_getstyle(login_cap_t
*lc
, char *style
, const char *auth
)
768 char **authtypes
= NULL
;
772 static char *defauthtypes
[] = { LOGIN_DEFSTYLE
, NULL
};
774 if (auth
!= NULL
&& *auth
!= '\0') {
775 if (snprintf(realauth
, sizeof realauth
, "auth-%s", auth
) < sizeof realauth
)
776 authtypes
= login_getcaplist(lc
, realauth
, NULL
);
779 if (authtypes
== NULL
)
780 authtypes
= login_getcaplist(lc
, "auth", NULL
);
782 if (authtypes
== NULL
)
783 authtypes
= defauthtypes
;
786 * We have at least one authtype now; auths is a comma-separated
787 * (or space-separated) list of authentication types. We have to
788 * convert from this to an array of char*'s; authtypes then gets this.
791 if (style
!= NULL
&& *style
!= '\0') {
792 while (authtypes
[i
] != NULL
&& strcmp(style
, authtypes
[i
]) != 0)
797 if (authtypes
[i
] != NULL
&& (auths
= strdup(authtypes
[i
])) != NULL
)
798 lc
->lc_style
= auths
;
800 if (lc
->lc_style
!= NULL
)
801 lc
->lc_style
= strdup(lc
->lc_style
);