Index: stable/4/lib/libutil/login_cap.3 =================================================================== --- stable/4/lib/libutil/login_cap.3 (revision 130524) +++ stable/4/lib/libutil/login_cap.3 (revision 130525) @@ -1,421 +1,441 @@ .\" Copyright (c) 1995 David Nugent .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, is permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice immediately at the beginning of the file, without modification, .\" this list of conditions, and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" 3. This work was done expressly for inclusion into FreeBSD. Other use .\" is permitted provided this notation is included. .\" 4. Absolutely no warranty of function or purpose is made by the author .\" David Nugent. .\" 5. Modifications may be freely made to this file providing the above .\" conditions are met. .\" .\" $FreeBSD$ .\" .Dd December 27, 1996 .Os .Dt LOGIN_CAP 3 .Sh NAME .Nm login_close , .Nm login_getcapbool , .Nm login_getcaplist , .Nm login_getcapnum , .Nm login_getcapstr , .Nm login_getcapsize , .Nm login_getcaptime , .Nm login_getclass , .Nm login_getclassbyname , .Nm login_getpwclass , .Nm login_getstyle , .Nm login_getuserclass , .Nm login_setcryptfmt .Nd "functions for accessing the login class capabilities database" .Sh LIBRARY .Lb libutil .Sh SYNOPSIS .In sys/types.h .In login_cap.h .Ft void .Fn login_close "login_cap_t *lc" .Ft login_cap_t * .Fn login_getclassbyname "const char *nam" "const struct passwd *pwd" .Ft login_cap_t * .Fn login_getclass "const char *nam" .Ft login_cap_t * .Fn login_getpwclass "const struct passwd *pwd" .Ft login_cap_t * .Fn login_getuserclass "const struct passwd *pwd" .Ft char * .Fn login_getcapstr "login_cap_t *lc" "const char *cap" "char *def" "char *error" .Ft char ** .Fn login_getcaplist "login_cap_t *lc" "const char *cap" "const char *chars" .Ft char * .Fn login_getpath "login_cap_t *lc" "const char *cap" "char *error" .Ft rlim_t .Fn login_getcaptime "login_cap_t *lc" "const char *cap" "rlim_t def" "rlim_t error" .Ft rlim_t .Fn login_getcapnum "login_cap_t *lc" "const char *cap" "rlim_t def" "rlim_t error" .Ft rlim_t .Fn login_getcapsize "login_cap_t *lc" "const char *cap" "rlim_t def" "rlim_t error" .Ft int .Fn login_getcapbool "login_cap_t *lc" "const char *cap" "int def" .Ft char * .Fn login_getstyle "login_cap_t *lc" "char *style" "const char *auth" .Ft const char * .Fn login_setcryptfmt "login_cap_t *lc" "const char *def" "const char *error" .Sh DESCRIPTION These functions represent a programming interface to the login classes database provided in .Xr login.conf 5 . This database contains capabilities, attributes and default environment and accounting settings for users and programs running as specific users, as determined by the login class field within entries in .Pa /etc/master.passwd . .Pp Entries in .Xr login.conf 5 consist of colon .Ql \&: separated fields, the first field in each record being one or more identifiers for the record (which must be unique for the entire database), each separated by a '|', and may optionally include a description as the last 'name'. Remaining fields in the record consist of keyword/data pairs. Long lines may be continued with a backslash within empty entries, with the second and subsequent lines optionally indented for readability. This is similar to the format used in .Xr termcap 5 , except that keywords are not limited to two significant characters, and are usually longer for improved readability. As with termcap entries, multiple records can be linked together (one record including another) using a field containing tc=. The result is that the entire record referenced by replaces the tc= field at the point at which it occurs. See .Xr getcap 3 for further details on the format and use of a capabilities database. .Pp The .Nm login_cap interface provides a convenient means of retrieving login class records with all tc= references expanded. A program will typically call one of .Fn login_getclass , .Fn login_getpwclass , .Fn login_getuserclass or .Fn login_getclassbyname according to its requirements. Each of these functions returns a login capabilities structure, .Ft login_cap_t , which may subsequently be used to interrogate the database for specific values using the rest of the API. Once the login_cap_t is of no further use, the .Fn login_close function should be called to free all resources used. .Pp The structure of login_cap_t is defined in login_cap.h, as: .Bd -literal -offset indent typedef struct { char *lc_class; char *lc_cap; char *lc_style; } login_cap_t; .Ed .Pp The .Ar lc_class member contains a pointer to the name of the login class retrieved. This may not necessarily be the same as the one requested, either directly via .Fn login_getclassbyname , indirectly via a user's login record using .Fn login_getpwclass , by class name using .Fn login_getclass , or .Fn login_getuserclass . If the referenced user has no login class specified in .Pa /etc/master.passwd , the class name is NULL or an empty string. If the class specified does not exist in the database, each of these functions will search for a record with an id of "default", with that name returned in the .Ar lc_class field. In addition, if the referenced user has a UID of 0 (normally, "root", although the user name is not considered) then .Fn login_getpwclass will search for a record with an id of "root" before it searches for the record with the id of "default". .Pp The .Ar lc_cap field is used internally by the library to contain the expanded login capabilities record. Programs with unusual requirements may wish to use this with the lower-level .Fn getcap style functions to access the record directly. .Pp The .Ar lc_style field is set by the .Fn login_getstyle function to the authorisation style, according to the requirements of the program handling a login itself. .Pp As noted above, the -.Fn get*class +.Fn login_get*class functions return a login_cap_t object which is used to access the matching or default record in the capabilities database. -.Fn getclassbyname -accepts two arguments: the first one is the record identifier of the -record to be retrieved, the second is an optional directory name. +The +.Fn login_getclassbyname +function accepts two arguments: the first one is the record identifier of the +record to be retrieved, the second is an optional pointer to a +.Li passwd +structure. If the first .Ar name argument is NULL, an empty string, or a class that does not exist in the supplemental or system login class database, then the system .Em default record is returned instead. If the second -.Ar dir +.Ar pwd parameter is NULL, then only the system login class database is -used, but when not NULL, the named directory is searched for +used. +However, +if the +.Ar pwd +parameter and the value of +.Ar pwd->pw_dir +are both not NULL, then the directory contained in +.Ar pwd->pw_dir +is searched for a login database file called ".login_conf", and capability records contained within it may override the system defaults. This scheme allows users to override some login settings from those in the system login class database by creating class records for their own private class with a record id of `me'. In the context of a .Em login , it should be noted that some options cannot by overridden by users for two reasons; many options, such as resource settings and default process priorities, require root privileges in order to take effect, and other fields in the user's file are not be consulted at all during the early phases of login for security or administrative reasons. See .Xr login.conf 5 for more information on which settings a user is able to override. Typically, these are limited purely to the user's default login environment which might otherwise have been overridden in shell startup scripts in any case. The user's .Pa .login_conf merely provides a convenient way for a user to set up their preferred login environment before the shell is invoked on login. +Note that access to the +.Pa /etc/login.conf +and +.Pa .login_conf +files will only be performed subject to the security checks documented in +.Xr _secure_path 3 +for the uids 0 and +.Ar pwd->pw_uid +respectively. .Pp If the specified record is NULL, empty or does not exist, and the system has no "default" record available to fall back to, there is a memory allocation error or for some reason .Xr cgetent 3 is unable to access the login capabilities database, this function returns NULL. .Pp The functions .Fn login_getpwclass , .Fn login_getclass and .Fn login_getuserclass retrieve the applicable login class record for the user's passwd entry or class name by calling .Fn login_getclassbyname . On failure, NULL is returned. The difference between these functions is that .Fn login_getuserclass includes the user's overriding .Pa .login_conf that exists in the user's home directory, and .Fn login_getpwclass and .Fn login_getclass restrict lookup only to the system login class database in .Pa /etc/login.conf . As explained earlier, .Fn login_getpwclass only differs from .Fn login_getclass in that it allows the default class for user 'root' as "root" if none has been specified in the password database. Otherwise, if the passwd pointer is NULL, or the user record has no login class, then the system "default" entry is retrieved. .Pp Once a program no longer wishes to use a login_cap_t object, .Fn login_close may be called to free all resources used by the login class. .Fn login_close may be passed a NULL pointer with no harmful side-effects. .Pp The remaining functions may be used to retrieve individual capability records. Each function takes a login_cap_t object as its first parameter, a capability tag as the second, and remaining parameters being default and error values that are returned if the capability is not found. The type of the additional parameters passed and returned depend on the .Em type of capability each deals with, be it a simple string, a list, a time value, a file or memory size value, a path (consisting of a colon-separated list of directories) or a boolean flag. The manpage for .Xr login.conf 5 deals in specific tags and their type. .Pp Note that with all functions in this group, you should not call .Xr free 3 on any pointers returned. Memory allocated during retrieval or processing of capability tags is automatically reused by subsequent calls to functions in this group, or deallocated on calling .Fn login_close . .Bl -tag -width "login_getcaplist()" .It Fn login_getcapstr This function returns a simple string capability. If the string is not found, then the value in .Ar def is returned as the default value, or if an error occurs, the value in the .Ar error parameter is returned. .It Fn login_getcaplist This function returns the value corresponding to the named capability tag as a list of values in a NULL terminated array. Within the login class database, some tags are of type .Em list , which consist of one or more comma- or space separated values. Usually, this function is not called directly from an application, but is used indirectly via .Fn login_getstyle . .It Fn login_getpath This function returns a list of directories separated by colons .Ql &: . Capability tags for which this function is called consist of a list of directories separated by spaces. .It Fn login_getcaptime This function returns a .Em time value associated with a particular capability tag with the value expressed in seconds (the default), minutes, hours, days, weeks or (365 day) years or any combination of these. A suffix determines the units used: S for seconds, M for minutes, H for hours, D for days, W for weeks and Y for 365 day years. Case of the units suffix is ignored. .Pp Time values are normally used for setting resource, accounting and session limits. If supported by the operating system and compiler (which is true of .Fx ) , the value returned is a quad (long long), of type .Em rlim_t . A value "inf" or "infinity" may be used to express an infinite value, in which case RLIM_INFINITY is returned. .It Fn login_getcapnum This function returns a numeric value for a tag, expressed either as tag= or the standard .Fn cgetnum format tag#. The first format should be used in preference to the second, the second format is provided for compatibility and consistency with the .Xr getcap 3 database format where numeric types use the .Ql \&# as the delimiter for numeric values. If in the first format, then the value given may be "inf" or "infinity" which results in a return value of RLIM_INFINITY. If the given capability tag cannot be found, the .Ar def parameter is returned, and if an error occurs, the .Ar error parameter is returned. .It Fn login_getcapsize .Fn login_getcapsize returns a value representing a size (typically, file or memory) which may be expressed as bytes (the default), 512 byte blocks, kilobytes, megabytes, gigabytes, and on systems that support the .Ar long long type, terabytes. The suffix used determines the units, and multiple values and units may be used in combination (e.g. 1m500k = 1.5 megabytes). A value with no suffix is interpreted as bytes, B as 512-byte blocks, K as kilobytes, M as megabytes, G as gigabytes and T as terabytes. Case is ignored. The error value is returned if there is a login capabilities database error, if an invalid suffix is used, or if a numeric value cannot be interpreted. .It Fn login_getcapbool This function returns a boolean value tied to a particular flag. It returns 0 if the given capability tag is not present or is negated by the presence of a "tag@" (See .Xr getcap 3 for more information on boolean flags), and returns 1 if the tag is found. .It Fn login_getstyle This function is used by the login authorisation system to determine the style of login available in a particular case. The function accepts three parameters, the login_cap entry itself and two optional parameters, and authorisation type 'auth' and 'style', and applies these to determine the authorisation style that best suites these rules. .Bl -bullet .It If 'auth' is neither NULL nor an empty string, look for a tag of type "auth-" in the capability record. If not present, then look for the default tag "auth=". .It If no valid authorisation list was found from the previous step, then default to "passwd" as the authorisation list. .It If 'style' is not NULL or empty, look for it in the list of authorisation methods found from the pprevious step. If 'style' is NULL or an empty string, then default to "passwd" authorisation. .It If 'style' is found in the chosen list of authorisation methods, then return that, otherwise return NULL. .El .Pp This scheme allows the administrator to determine the types of authorisation methods accepted by the system, depending on the means by which the access occurs. For example, the administrator may require skey or kerberos as the authentication method used for access to the system via the network, and standard methods via direct dialup or console logins, significantly reducing the risk of password discovery by "snooping" network packets. .It Fn login_setcryptfmt The .Fn login_setcryptfmt function is used to set the .Xr crypt 3 format using the .Ql passwd_format configuration entry. If no entry is found, .Fa def is taken to be used as the fallback. If calling .Xr crypt_set_format 3 on the specifier fails, .Fa error is returned to indicate this. .El .Sh SEE ALSO .Xr crypt 3 , .Xr getcap 3 , .Xr login_class 3 , .Xr login.conf 5 , .Xr termcap 5 Index: stable/4/lib/libutil/login_cap.c =================================================================== --- stable/4/lib/libutil/login_cap.c (revision 130524) +++ stable/4/lib/libutil/login_cap.c (revision 130525) @@ -1,806 +1,807 @@ /*- * Copyright (c) 1996 by * Sean Eric Fagan * David Nugent * All rights reserved. * * Portions copyright (c) 1995,1997 * Berkeley Software Design, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, is permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice immediately at the beginning of the file, without modification, * this list of conditions, and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. This work was done expressly for inclusion into FreeBSD. Other use * is permitted provided this notation is included. * 4. Absolutely no warranty of function or purpose is made by the authors. * 5. Modifications may be freely made to this file providing the above * conditions are met. * * Low-level routines relating to the user capabilities database * * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* * allocstr() * Manage a single static pointer for handling a local char* buffer, * resizing as necessary to contain the string. * * allocarray() * Manage a static array for handling a group of strings, resizing * when necessary. */ static int lc_object_count = 0; static size_t internal_stringsz = 0; static char * internal_string = NULL; static size_t internal_arraysz = 0; static char ** internal_array = NULL; static char * allocstr(char *str) { char *p; size_t sz = strlen(str) + 1; /* realloc() only if necessary */ if (sz <= internal_stringsz) p = strcpy(internal_string, str); else if ((p = realloc(internal_string, sz)) != NULL) { internal_stringsz = sz; internal_string = strcpy(p, str); } return p; } static char ** allocarray(size_t sz) { char **p; if (sz <= internal_arraysz) p = internal_array; else if ((p = realloc(internal_array, sz * sizeof(char*))) != NULL) { internal_arraysz = sz; internal_array = p; } return p; } /* * arrayize() * Turn a simple string separated by any of * the set of into an array. The last element * of the array will be NULL, as is proper. * Free using freearraystr() */ static char ** arrayize(char *str, const char *chars, int *size) { int i; char *ptr; char **res = NULL; /* count the sub-strings */ for (i = 0, ptr = str; *ptr; i++) { int count = strcspn(ptr, chars); ptr += count; if (*ptr) ++ptr; } /* alloc the array */ if ((ptr = allocstr(str)) != NULL) { if ((res = allocarray(++i)) == NULL) free(str); else { /* now split the string */ i = 0; while (*ptr) { int count = strcspn(ptr, chars); res[i++] = ptr; ptr += count; if (*ptr) *ptr++ = '\0'; } res[i] = NULL; } } if (size) *size = i; return res; } /* * login_close() * Frees up all resources relating to a login class * */ void login_close(login_cap_t * lc) { if (lc) { free(lc->lc_style); free(lc->lc_class); free(lc->lc_cap); free(lc); if (--lc_object_count == 0) { free(internal_string); free(internal_array); internal_array = NULL; internal_arraysz = 0; internal_string = NULL; internal_stringsz = 0; cgetclose(); } } } /* * login_getclassbyname() get the login class by its name. * If the name given is NULL or empty, the default class * LOGIN_DEFCLASS (ie. "default") is fetched. If the - * 'dir' argument contains a non-NULL non-empty string, - * then the file _FILE_LOGIN_CONF is picked up from that - * directory instead of the system login database. + * 'pwd' argument is non-NULL and contains an non-NULL + * dir entry, then the file _FILE_LOGIN_CONF is picked + * up from that directory and used before the system + * login database. * Return a filled-out login_cap_t structure, including * class name, and the capability record buffer. */ login_cap_t * login_getclassbyname(char const *name, const struct passwd *pwd) { login_cap_t *lc; if ((lc = malloc(sizeof(login_cap_t))) != NULL) { int r, me, i = 0; uid_t euid = 0; gid_t egid = 0; const char *msg = NULL; const char *dir; char userpath[MAXPATHLEN]; static char *login_dbarray[] = { NULL, NULL, NULL }; me = (name != NULL && strcmp(name, LOGIN_MECLASS) == 0); dir = (!me || pwd == NULL) ? NULL : pwd->pw_dir; /* * Switch to user mode before checking/reading its ~/.login_conf * - some NFSes have root read access disabled. * * XXX: This fails to configure additional groups. */ if (dir) { euid = geteuid(); egid = getegid(); (void)setegid(pwd->pw_gid); (void)seteuid(pwd->pw_uid); } if (dir && snprintf(userpath, MAXPATHLEN, "%s/%s", dir, _FILE_LOGIN_CONF) < MAXPATHLEN) { login_dbarray[i] = userpath; if (_secure_path(userpath, pwd->pw_uid, pwd->pw_gid) != -1) i++; /* only use 'secure' data */ } if (_secure_path(_PATH_LOGIN_CONF, 0, 0) != -1) login_dbarray[i++] = _PATH_LOGIN_CONF; login_dbarray[i] = NULL; memset(lc, 0, sizeof(login_cap_t)); lc->lc_cap = lc->lc_class = lc->lc_style = NULL; if (name == NULL || *name == '\0') name = LOGIN_DEFCLASS; switch (cgetent(&lc->lc_cap, login_dbarray, (char*)name)) { case -1: /* Failed, entry does not exist */ if (me) break; /* Don't retry default on 'me' */ if (i == 0) r = -1; else if ((r = open(login_dbarray[0], O_RDONLY)) >= 0) close(r); /* * If there's at least one login class database, * and we aren't searching for a default class * then complain about a non-existent class. */ if (r >= 0 || strcmp(name, LOGIN_DEFCLASS) != 0) syslog(LOG_ERR, "login_getclass: unknown class '%s'", name); /* fall-back to default class */ name = LOGIN_DEFCLASS; msg = "%s: no default/fallback class '%s'"; if (cgetent(&lc->lc_cap, login_dbarray, (char*)name) != 0 && r >= 0) break; /* Fallthru - just return system defaults */ case 0: /* success! */ if ((lc->lc_class = strdup(name)) != NULL) { if (dir) { (void)seteuid(euid); (void)setegid(egid); } ++lc_object_count; return lc; } msg = "%s: strdup: %m"; break; case -2: msg = "%s: retrieving class information: %m"; break; case -3: msg = "%s: 'tc=' reference loop '%s'"; break; case 1: msg = "couldn't resolve 'tc=' reference in '%s'"; break; default: msg = "%s: unexpected cgetent() error '%s': %m"; break; } if (dir) { (void)seteuid(euid); (void)setegid(egid); } if (msg != NULL) syslog(LOG_ERR, msg, "login_getclass", name); free(lc); } return NULL; } /* * login_getclass() * Get the login class for the system (only) login class database. * Return a filled-out login_cap_t structure, including * class name, and the capability record buffer. */ login_cap_t * login_getclass(const char *cls) { return login_getclassbyname(cls, NULL); } /* * login_getclass() * Get the login class for a given password entry from * the system (only) login class database. * If the password entry's class field is not set, or * the class specified does not exist, then use the * default of LOGIN_DEFCLASS (ie. "default"). * Return a filled-out login_cap_t structure, including * class name, and the capability record buffer. */ login_cap_t * login_getpwclass(const struct passwd *pwd) { const char *cls = NULL; if (pwd != NULL) { cls = pwd->pw_class; if (cls == NULL || *cls == '\0') cls = (pwd->pw_uid == 0) ? LOGIN_DEFROOTCLASS : LOGIN_DEFCLASS; } return login_getclassbyname(cls, pwd); } /* * login_getuserclass() * Get the login class for a given password entry, allowing user * overrides via ~/.login_conf. */ login_cap_t * login_getuserclass(const struct passwd *pwd) { return login_getclassbyname(LOGIN_MECLASS, pwd); } /* * login_getcapstr() * Given a login_cap entry, and a capability name, return the * value defined for that capability, a defualt if not found, or * an error string on error. */ char * login_getcapstr(login_cap_t *lc, const char *cap, char *def, char *error) { char *res; int ret; if (lc == NULL || cap == NULL || lc->lc_cap == NULL || *cap == '\0') return def; if ((ret = cgetstr(lc->lc_cap, (char *)cap, &res)) == -1) return def; return (ret >= 0) ? res : error; } /* * login_getcaplist() * Given a login_cap entry, and a capability name, return the * value defined for that capability split into an array of * strings. */ char ** login_getcaplist(login_cap_t *lc, const char *cap, const char *chars) { char *lstring; if (chars == NULL) chars = ", \t"; if ((lstring = login_getcapstr(lc, (char*)cap, NULL, NULL)) != NULL) return arrayize(lstring, chars, NULL); return NULL; } /* * login_getpath() * From the login_cap_t , get the capability which is * formatted as either a space or comma delimited list of paths * and append them all into a string and separate by semicolons. * If there is an error of any kind, return . */ char * login_getpath(login_cap_t *lc, const char *cap, char * error) { char *str; if ((str = login_getcapstr(lc, (char*)cap, NULL, NULL)) == NULL) str = error; else { char *ptr = str; while (*ptr) { int count = strcspn(ptr, ", \t"); ptr += count; if (*ptr) *ptr++ = ':'; } } return str; } static int isinfinite(const char *s) { static const char *infs[] = { "infinity", "inf", "unlimited", "unlimit", "-1", NULL }; const char **i = &infs[0]; while (*i != NULL) { if (strcasecmp(s, *i) == 0) return 1; ++i; } return 0; } static u_quad_t rmultiply(u_quad_t n1, u_quad_t n2) { u_quad_t m, r; int b1, b2; static int bpw = 0; /* Handle simple cases */ if (n1 == 0 || n2 == 0) return 0; if (n1 == 1) return n2; if (n2 == 1) return n1; /* * sizeof() returns number of bytes needed for storage. * This may be different from the actual number of useful bits. */ if (!bpw) { bpw = sizeof(u_quad_t) * 8; while (((u_quad_t)1 << (bpw-1)) == 0) --bpw; } /* * First check the magnitude of each number. If the sum of the * magnatude is way to high, reject the number. (If this test * is not done then the first multiply below may overflow.) */ for (b1 = bpw; (((u_quad_t)1 << (b1-1)) & n1) == 0; --b1) ; for (b2 = bpw; (((u_quad_t)1 << (b2-1)) & n2) == 0; --b2) ; if (b1 + b2 - 2 > bpw) { errno = ERANGE; return (UQUAD_MAX); } /* * Decompose the multiplication to be: * h1 = n1 & ~1 * h2 = n2 & ~1 * l1 = n1 & 1 * l2 = n2 & 1 * (h1 + l1) * (h2 + l2) * (h1 * h2) + (h1 * l2) + (l1 * h2) + (l1 * l2) * * Since h1 && h2 do not have the low bit set, we can then say: * * (h1>>1 * h2>>1 * 4) + ... * * So if (h1>>1 * h2>>1) > (1<<(bpw - 2)) then the result will * overflow. * * Finally, if MAX - ((h1 * l2) + (l1 * h2) + (l1 * l2)) < (h1*h2) * then adding in residual amout will cause an overflow. */ m = (n1 >> 1) * (n2 >> 1); if (m >= ((u_quad_t)1 << (bpw-2))) { errno = ERANGE; return (UQUAD_MAX); } m *= 4; r = (n1 & n2 & 1) + (n2 & 1) * (n1 & ~(u_quad_t)1) + (n1 & 1) * (n2 & ~(u_quad_t)1); if ((u_quad_t)(m + r) < m) { errno = ERANGE; return (UQUAD_MAX); } m += r; return (m); } /* * login_getcaptime() * From the login_cap_t , get the capability , which is * formatted as a time (e.g., "=10h3m2s"). If is not * present in , return ; if there is an error of some kind, * return . */ rlim_t login_getcaptime(login_cap_t *lc, const char *cap, rlim_t def, rlim_t error) { char *res, *ep, *oval; int r; rlim_t tot; errno = 0; if (lc == NULL || lc->lc_cap == NULL) return def; /* * Look for in lc_cap. * If it's not there (-1), return . * If there's an error, return . */ if ((r = cgetstr(lc->lc_cap, (char *)cap, &res)) == -1) return def; else if (r < 0) { errno = ERANGE; return error; } /* "inf" and "infinity" are special cases */ if (isinfinite(res)) return RLIM_INFINITY; /* * Now go through the string, turning something like 1h2m3s into * an integral value. Whee. */ errno = 0; tot = 0; oval = res; while (*res) { rlim_t tim = strtoq(res, &ep, 0); rlim_t mult = 1; if (ep == NULL || ep == res || errno != 0) { invalid: syslog(LOG_WARNING, "login_getcaptime: class '%s' bad value %s=%s", lc->lc_class, cap, oval); errno = ERANGE; return error; } /* Look for suffixes */ switch (*ep++) { case 0: ep--; break; /* end of string */ case 's': case 'S': /* seconds */ break; case 'm': case 'M': /* minutes */ mult = 60; break; case 'h': case 'H': /* hours */ mult = 60L * 60L; break; case 'd': case 'D': /* days */ mult = 60L * 60L * 24L; break; case 'w': case 'W': /* weeks */ mult = 60L * 60L * 24L * 7L; break; case 'y': case 'Y': /* 365-day years */ mult = 60L * 60L * 24L * 365L; break; default: goto invalid; } res = ep; tot += rmultiply(tim, mult); if (errno) goto invalid; } return tot; } /* * login_getcapnum() * From the login_cap_t , extract the numerical value . * If it is not present, return for a default, and return * if there is an error. * Like login_getcaptime(), only it only converts to a number, not * to a time; "infinity" and "inf" are 'special.' */ rlim_t login_getcapnum(login_cap_t *lc, const char *cap, rlim_t def, rlim_t error) { char *ep, *res; int r; rlim_t val; if (lc == NULL || lc->lc_cap == NULL) return def; /* * For BSDI compatibility, try for the tag= first */ if ((r = cgetstr(lc->lc_cap, (char *)cap, &res)) == -1) { long lval; /* string capability not present, so try for tag# as numeric */ if ((r = cgetnum(lc->lc_cap, (char *)cap, &lval)) == -1) return def; /* Not there, so return default */ else if (r >= 0) return (rlim_t)lval; } if (r < 0) { errno = ERANGE; return error; } if (isinfinite(res)) return RLIM_INFINITY; errno = 0; val = strtoq(res, &ep, 0); if (ep == NULL || ep == res || errno != 0) { syslog(LOG_WARNING, "login_getcapnum: class '%s' bad value %s=%s", lc->lc_class, cap, res); errno = ERANGE; return error; } return val; } /* * login_getcapsize() * From the login_cap_t , extract the capability , which is * formatted as a size (e.g., "=10M"); it can also be "infinity". * If not present, return , or if there is an error of * some sort. */ rlim_t login_getcapsize(login_cap_t *lc, const char *cap, rlim_t def, rlim_t error) { char *ep, *res, *oval; int r; rlim_t tot; if (lc == NULL || lc->lc_cap == NULL) return def; if ((r = cgetstr(lc->lc_cap, (char *)cap, &res)) == -1) return def; else if (r < 0) { errno = ERANGE; return error; } if (isinfinite(res)) return RLIM_INFINITY; errno = 0; tot = 0; oval = res; while (*res) { rlim_t siz = strtoq(res, &ep, 0); rlim_t mult = 1; if (ep == NULL || ep == res || errno != 0) { invalid: syslog(LOG_WARNING, "login_getcapsize: class '%s' bad value %s=%s", lc->lc_class, cap, oval); errno = ERANGE; return error; } switch (*ep++) { case 0: /* end of string */ ep--; break; case 'b': case 'B': /* 512-byte blocks */ mult = 512; break; case 'k': case 'K': /* 1024-byte Kilobytes */ mult = 1024; break; case 'm': case 'M': /* 1024-k kbytes */ mult = 1024 * 1024; break; case 'g': case 'G': /* 1Gbyte */ mult = 1024 * 1024 * 1024; break; case 't': case 'T': /* 1TBte */ mult = 1024LL * 1024LL * 1024LL * 1024LL; break; default: goto invalid; } res = ep; tot += rmultiply(siz, mult); if (errno) goto invalid; } return tot; } /* * login_getcapbool() * From the login_cap_t , check for the existance of the capability * of . Return if ->lc_cap is NULL, otherwise return * the whether or not exists there. */ int login_getcapbool(login_cap_t *lc, const char *cap, int def) { if (lc == NULL || lc->lc_cap == NULL) return def; return (cgetcap(lc->lc_cap, (char *)cap, ':') != NULL); } /* * login_getstyle() * Given a login_cap entry , and optionally a type of auth , * and optionally a style