]>
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
28 * $Id: login_cap.c,v 1.11 1997/02/27 00:24:05 ache Exp $
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> seperated 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
);
119 /* alloc the array */
120 if ((ptr
= allocstr(str
)) != NULL
) {
121 if ((res
= allocarray(++i
)) == NULL
)
124 /* 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
)
156 if (--lc_object_count
== 0) {
157 free(internal_string
);
158 free(internal_array
);
159 internal_array
= NULL
;
160 internal_arraysz
= 0;
161 internal_string
= NULL
;
162 internal_stringsz
= 0;
170 * login_getclassbyname() get the login class by its name.
171 * If the name given is NULL or empty, the default class
172 * LOGIN_DEFCLASS (ie. "default") is fetched. If the
173 * 'dir' argument contains a non-NULL non-empty string,
174 * then the file _FILE_LOGIN_CONF is picked up from that
175 * directory instead of the system login database.
176 * Return a filled-out login_cap_t structure, including
177 * class name, and the capability record buffer.
181 login_getclassbyname(char const *name
, const struct passwd
*pwd
)
185 if ((lc
= malloc(sizeof(login_cap_t
))) != NULL
) {
187 const char *msg
= NULL
;
188 const char *dir
= (pwd
== NULL
) ? NULL
: pwd
->pw_dir
;
189 char userpath
[MAXPATHLEN
];
191 static char *login_dbarray
[] = { NULL
, NULL
, NULL
};
193 if (dir
&& snprintf(userpath
, MAXPATHLEN
, "%s/%s", dir
,
194 _FILE_LOGIN_CONF
) < MAXPATHLEN
) {
195 login_dbarray
[i
] = userpath
;
196 if (_secure_path(userpath
, pwd
->pw_uid
, pwd
->pw_gid
) != -1)
197 i
++; /* only use 'secure' data */
199 if (_secure_path(_PATH_LOGIN_CONF
, 0, 0) != -1)
200 login_dbarray
[i
++] = _PATH_LOGIN_CONF
;
201 login_dbarray
[i
] = NULL
;
203 memset(lc
, 0, sizeof(login_cap_t
));
204 lc
->lc_cap
= lc
->lc_class
= lc
->lc_style
= NULL
;
206 if (name
== NULL
|| *name
== '\0')
207 name
= LOGIN_DEFCLASS
;
209 switch (cgetent(&lc
->lc_cap
, login_dbarray
, (char*)name
)) {
210 case -1: /* Failed, entry does not exist */
211 if (strcmp(name
, LOGIN_MECLASS
) == 0)
212 break; /* Don't retry default on 'me' */
215 else if ((r
= open(login_dbarray
[0], O_RDONLY
)) >= 0)
218 * If there's at least one login class database,
219 * and we aren't searching for a default class
220 * then complain about a non-existent class.
222 if (r
>= 0 || strcmp(name
, LOGIN_DEFCLASS
) != 0)
223 syslog(LOG_ERR
, "login_getclass: unknown class '%s'", name
);
224 /* fall-back to default class */
225 name
= LOGIN_DEFCLASS
;
226 msg
= "%s: no default/fallback class '%s'";
227 if (cgetent(&lc
->lc_cap
, login_dbarray
, (char*)name
) != 0 && r
>= 0)
229 /* Fallthru - just return system defaults */
230 case 0: /* success! */
231 if ((lc
->lc_class
= strdup(name
)) != NULL
) {
235 msg
= "%s: strdup: %m";
238 msg
= "%s: retrieving class information: %m";
241 msg
= "%s: 'tc=' reference loop '%s'";
244 msg
= "couldn't resolve 'tc=' reference in '%s'";
247 msg
= "%s: unexpected cgetent() error '%s': %m";
251 syslog(LOG_ERR
, msg
, "login_getclass", name
);
262 * Get the login class for the system (only) login class database.
263 * Return a filled-out login_cap_t structure, including
264 * class name, and the capability record buffer.
268 login_getclass(const char *cls
)
270 return login_getclassbyname(cls
, NULL
);
276 * Get the login class for a given password entry from
277 * the system (only) login class database.
278 * If the password entry's class field is not set, or
279 * the class specified does not exist, then use the
280 * default of LOGIN_DEFCLASS (ie. "default").
281 * Return a filled-out login_cap_t structure, including
282 * class name, and the capability record buffer.
286 login_getpwclass(const struct passwd
*pwd
)
288 const char *cls
= NULL
;
292 if (cls
== NULL
|| *cls
== '\0')
293 cls
= (pwd
->pw_uid
== 0) ? LOGIN_DEFROOTCLASS
: LOGIN_DEFCLASS
;
295 return login_getclassbyname(cls
, pwd
);
300 * login_getuserclass()
301 * Get the login class for a given password entry, allowing user
302 * overrides via ~/.login_conf.
306 login_getuserclass(const struct passwd
*pwd
)
308 return login_getclassbyname(LOGIN_MECLASS
, pwd
);
315 * Given a login_cap entry, and a capability name, return the
316 * value defined for that capability, a defualt if not found, or
317 * an error string on error.
321 login_getcapstr(login_cap_t
*lc
, const char *cap
, char *def
, char *error
)
326 if (lc
== NULL
|| cap
== NULL
|| lc
->lc_cap
== NULL
|| *cap
== '\0')
329 if ((ret
= cgetstr(lc
->lc_cap
, (char *)cap
, &res
)) == -1)
331 return (ret
>= 0) ? res
: error
;
337 * Given a login_cap entry, and a capability name, return the
338 * value defined for that capability split into an array of
343 login_getcaplist(login_cap_t
*lc
, const char *cap
, const char *chars
)
349 if ((lstring
= login_getcapstr(lc
, (char*)cap
, NULL
, NULL
)) != NULL
)
350 return arrayize(lstring
, chars
, NULL
);
357 * From the login_cap_t <lc>, get the capability <cap> which is
358 * formatted as either a space or comma delimited list of paths
359 * and append them all into a string and separate by semicolons.
360 * If there is an error of any kind, return <error>.
364 login_getpath(login_cap_t
*lc
, const char *cap
, char * error
)
368 if ((str
= login_getcapstr(lc
, (char*)cap
, NULL
, NULL
)) == NULL
)
374 int count
= strcspn(ptr
, ", \t");
385 isinfinite(const char *s
)
387 static const char *infs
[] = {
395 const char **i
= &infs
[0];
398 if (strcasecmp(s
, *i
) == 0)
407 rmultiply(u_quad_t n1
, u_quad_t n2
)
414 /* Handle simple cases */
415 if (n1
== 0 || n2
== 0)
423 * sizeof() returns number of bytes needed for storage.
424 * This may be different from the actual number of useful bits.
427 bpw
= sizeof(u_quad_t
) * 8;
428 while (((u_quad_t
)1 << (bpw
-1)) == 0)
433 * First check the magnitude of each number. If the sum of the
434 * magnatude is way to high, reject the number. (If this test
435 * is not done then the first multiply below may overflow.)
437 for (b1
= bpw
; (((u_quad_t
)1 << (b1
-1)) & n1
) == 0; --b1
)
439 for (b2
= bpw
; (((u_quad_t
)1 << (b2
-1)) & n2
) == 0; --b2
)
441 if (b1
+ b2
- 2 > bpw
) {
447 * Decompose the multiplication to be:
452 * (h1 + l1) * (h2 + l2)
453 * (h1 * h2) + (h1 * l2) + (l1 * h2) + (l1 * l2)
455 * Since h1 && h2 do not have the low bit set, we can then say:
457 * (h1>>1 * h2>>1 * 4) + ...
459 * So if (h1>>1 * h2>>1) > (1<<(bpw - 2)) then the result will
462 * Finally, if MAX - ((h1 * l2) + (l1 * h2) + (l1 * l2)) < (h1*h2)
463 * then adding in residual amout will cause an overflow.
466 m
= (n1
>> 1) * (n2
>> 1);
467 if (m
>= ((u_quad_t
)1 << (bpw
-2))) {
474 + (n2
& 1) * (n1
& ~(u_quad_t
)1)
475 + (n1
& 1) * (n2
& ~(u_quad_t
)1);
477 if ((u_quad_t
)(m
+ r
) < m
) {
489 * From the login_cap_t <lc>, get the capability <cap>, which is
490 * formatted as a time (e.g., "<cap>=10h3m2s"). If <cap> is not
491 * present in <lc>, return <def>; if there is an error of some kind,
496 login_getcaptime(login_cap_t
*lc
, const char *cap
, rlim_t def
, rlim_t error
)
498 char *res
, *ep
, *oval
;
503 if (lc
== NULL
|| lc
->lc_cap
== NULL
)
507 * Look for <cap> in lc_cap.
508 * If it's not there (-1), return <def>.
509 * If there's an error, return <error>.
512 if ((r
= cgetstr(lc
->lc_cap
, (char *)cap
, &res
)) == -1)
519 /* "inf" and "infinity" are special cases */
521 return RLIM_INFINITY
;
524 * Now go through the string, turning something like 1h2m3s into
525 * an integral value. Whee.
532 rlim_t tim
= strtoq(res
, &ep
, 0);
535 if (ep
== NULL
|| ep
== res
|| errno
!= 0) {
537 syslog(LOG_WARNING
, "login_getcaptime: class '%s' bad value %s=%s",
538 lc
->lc_class
, cap
, oval
);
542 /* Look for suffixes */
546 break; /* end of string */
547 case 's': case 'S': /* seconds */
549 case 'm': case 'M': /* minutes */
552 case 'h': case 'H': /* hours */
555 case 'd': case 'D': /* days */
556 mult
= 60L * 60L * 24L;
558 case 'w': case 'W': /* weeks */
559 mult
= 60L * 60L * 24L * 7L;
560 case 'y': case 'Y': /* 365-day years */
561 mult
= 60L * 60L * 24L * 365L;
566 tot
+= rmultiply(tim
, mult
);
577 * From the login_cap_t <lc>, extract the numerical value <cap>.
578 * If it is not present, return <def> for a default, and return
579 * <error> if there is an error.
580 * Like login_getcaptime(), only it only converts to a number, not
581 * to a time; "infinity" and "inf" are 'special.'
585 login_getcapnum(login_cap_t
*lc
, const char *cap
, rlim_t def
, rlim_t error
)
591 if (lc
== NULL
|| lc
->lc_cap
== NULL
)
595 * For BSDI compatibility, try for the tag=<val> first
597 if ((r
= cgetstr(lc
->lc_cap
, (char *)cap
, &res
)) == -1) {
599 /* string capability not present, so try for tag#<val> as numeric */
600 if ((r
= cgetnum(lc
->lc_cap
, (char *)cap
, &lval
)) == -1)
601 return def
; /* Not there, so return default */
612 return RLIM_INFINITY
;
615 val
= strtoq(res
, &ep
, 0);
616 if (ep
== NULL
|| ep
== res
|| errno
!= 0) {
617 syslog(LOG_WARNING
, "login_getcapnum: class '%s' bad value %s=%s",
618 lc
->lc_class
, cap
, res
);
630 * From the login_cap_t <lc>, extract the capability <cap>, which is
631 * formatted as a size (e.g., "<cap>=10M"); it can also be "infinity".
632 * If not present, return <def>, or <error> if there is an error of
637 login_getcapsize(login_cap_t
*lc
, const char *cap
, rlim_t def
, rlim_t error
)
639 char *ep
, *res
, *oval
;
643 if (lc
== NULL
|| lc
->lc_cap
== NULL
)
646 if ((r
= cgetstr(lc
->lc_cap
, (char *)cap
, &res
)) == -1)
654 return RLIM_INFINITY
;
660 rlim_t siz
= strtoq(res
, &ep
, 0);
663 if (ep
== NULL
|| ep
== res
|| errno
!= 0) {
665 syslog(LOG_WARNING
, "login_getcapsize: class '%s' bad value %s=%s",
666 lc
->lc_class
, cap
, oval
);
671 case 0: /* end of string */
674 case 'b': case 'B': /* 512-byte blocks */
677 case 'k': case 'K': /* 1024-byte Kilobytes */
680 case 'm': case 'M': /* 1024-k kbytes */
683 case 'g': case 'G': /* 1Gbyte */
684 mult
= 1024 * 1024 * 1024;
686 case 't': case 'T': /* 1TBte */
687 mult
= 1024LL * 1024LL * 1024LL * 1024LL;
693 tot
+= rmultiply(siz
, mult
);
704 * From the login_cap_t <lc>, check for the existance of the capability
705 * of <cap>. Return <def> if <lc>->lc_cap is NULL, otherwise return
706 * the whether or not <cap> exists there.
710 login_getcapbool(login_cap_t
*lc
, const char *cap
, int def
)
712 if (lc
== NULL
|| lc
->lc_cap
== NULL
)
714 return (cgetcap(lc
->lc_cap
, (char *)cap
, ':') != NULL
);
720 * Given a login_cap entry <lc>, and optionally a type of auth <auth>,
721 * and optionally a style <style>, find the style that best suits these
723 * 1. If <auth> is non-null, look for an "auth-<auth>=" string
724 * in the capability; if not present, default to "auth=".
725 * 2. If there is no auth list found from (1), default to
726 * "passwd" as an authorization list.
727 * 3. If <style> is non-null, look for <style> in the list of
728 * authorization methods found from (2); if <style> is NULL, default
729 * to LOGIN_DEFSTYLE ("passwd").
730 * 4. If the chosen style is found in the chosen list of authorization
731 * methods, return that; otherwise, return NULL.
733 * login_getstyle(lc, NULL, "ftp");
734 * login_getstyle(lc, "login", NULL);
735 * login_getstyle(lc, "skey", "network");
739 login_getstyle(login_cap_t
*lc
, char *style
, const char *auth
)
742 char **authtypes
= NULL
;
746 static char *defauthtypes
[] = { LOGIN_DEFSTYLE
, NULL
};
748 if (auth
!= NULL
&& *auth
!= '\0') {
749 if (snprintf(realauth
, sizeof realauth
, "auth-%s", auth
) < sizeof realauth
)
750 authtypes
= login_getcaplist(lc
, realauth
, NULL
);
753 if (authtypes
== NULL
)
754 authtypes
= login_getcaplist(lc
, "auth", NULL
);
756 if (authtypes
== NULL
)
757 authtypes
= defauthtypes
;
760 * We have at least one authtype now; auths is a comma-seperated
761 * (or space-separated) list of authentication types. We have to
762 * convert from this to an array of char*'s; authtypes then gets this.
765 if (style
!= NULL
&& *style
!= '\0') {
766 while (authtypes
[i
] != NULL
&& strcmp(style
, authtypes
[i
]) != 0)
771 if (authtypes
[i
] != NULL
&& (auths
= strdup(authtypes
[i
])) != NULL
)
772 lc
->lc_style
= auths
;
774 if (lc
->lc_style
!= NULL
)
775 lc
->lc_style
= strdup(lc
->lc_style
);