2 * Copyright (c) 1991 - 1994, Julianne Frances Haugh
3 * Copyright (c) 1996 - 2000, Marek Michałkiewicz
4 * Copyright (c) 2002 - 2006, Tomasz Kłoczko
5 * Copyright (c) 2007 - 2008, Nicolas François
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the copyright holders or contributors may not be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #ident "$Id: getdef.c 2772 2009-04-23 11:46:46Z nekral-guest $"
37 #include "prototypes.h"
45 * A configuration item definition.
48 /*@null@*/const char *name; /* name of the item */
49 /*@null@*/char *value; /* value given, or NULL if no value */
52 #define NUMDEFS (sizeof(def_table)/sizeof(def_table[0]))
53 static struct itemdef def_table[] = {
54 {"CHFN_RESTRICT", NULL},
55 {"CONSOLE_GROUPS", NULL},
57 {"CREATE_HOME", NULL},
58 {"DEFAULT_HOME", NULL},
59 {"ENCRYPT_METHOD", NULL},
67 {"HUSHLOGIN_FILE", NULL},
69 {"LOGIN_RETRIES", NULL},
70 {"LOGIN_TIMEOUT", NULL},
71 {"LOG_OK_LOGINS", NULL},
72 {"LOG_UNKFAIL_ENAB", NULL},
75 {"MAX_MEMBERS_PER_GROUP", NULL},
76 {"MD5_CRYPT_ENAB", NULL},
77 {"PASS_MAX_DAYS", NULL},
78 {"PASS_MIN_DAYS", NULL},
79 {"PASS_WARN_AGE", NULL},
81 {"SHA_CRYPT_MAX_ROUNDS", NULL},
82 {"SHA_CRYPT_MIN_ROUNDS", NULL},
86 {"SYS_GID_MAX", NULL},
87 {"SYS_GID_MIN", NULL},
88 {"SYS_UID_MAX", NULL},
89 {"SYS_UID_MIN", NULL},
92 {"TTYTYPE_FILE", NULL},
96 {"USERDEL_CMD", NULL},
97 {"USERGROUPS_ENAB", NULL},
101 {"CRACKLIB_DICTPATH", NULL},
103 {"ENVIRON_FILE", NULL},
105 {"FAILLOG_ENAB", NULL},
107 {"ISSUE_FILE", NULL},
108 {"LASTLOG_ENAB", NULL},
109 {"LOGIN_STRING", NULL},
110 {"MAIL_CHECK_ENAB", NULL},
112 {"NOLOGINS_FILE", NULL},
113 {"OBSCURE_CHECKS_ENAB", NULL},
114 {"PASS_ALWAYS_WARN", NULL},
115 {"PASS_CHANGE_TRIES", NULL},
116 {"PASS_MAX_LEN", NULL},
117 {"PASS_MIN_LEN", NULL},
118 {"PORTTIME_CHECKS_ENAB", NULL},
119 {"QUOTAS_ENAB", NULL},
120 {"SU_WHEEL_ONLY", NULL},
124 {"SYSLOG_SG_ENAB", NULL},
125 {"SYSLOG_SU_ENAB", NULL},
131 #define LOGINDEFS "/etc/login.defs"
134 static char def_fname[] = LOGINDEFS; /* login config defs file */
135 static bool def_loaded = false; /* are defs already loaded? */
137 /* local function prototypes */
138 static /*@observer@*/ /*@null@*/struct itemdef *def_find (const char *);
139 static void def_load (void);
143 * getdef_str - get string value from table of definitions.
145 * Return point to static data for specified item, or NULL if item is not
146 * defined. First time invoked, will load definitions from the file.
149 /*@observer@*/ /*@null@*/const char *getdef_str (const char *item)
158 return ((NULL == d)? (const char *) NULL : d->value);
163 * getdef_bool - get boolean value from table of definitions.
165 * Return TRUE if specified item is defined as "yes", else FALSE.
168 bool getdef_bool (const char *item)
177 if ((NULL == d) || (NULL == d->value)) {
181 return (strcasecmp (d->value, "yes") == 0);
186 * getdef_num - get numerical value from table of definitions
188 * Returns numeric value of specified item, else the "dflt" value if
189 * the item is not defined. Octal (leading "0") and hex (leading "0x")
190 * values are handled.
193 int getdef_num (const char *item, int dflt)
203 if ((NULL == d) || (NULL == d->value)) {
207 if ( (getlong (d->value, &val) == 0)
209 || (val < INT_MIN)) {
211 _("configuration error - cannot parse %s value: '%s'"),
221 * getdef_unum - get unsigned numerical value from table of definitions
223 * Returns numeric value of specified item, else the "dflt" value if
224 * the item is not defined. Octal (leading "0") and hex (leading "0x")
225 * values are handled.
228 unsigned int getdef_unum (const char *item, unsigned int dflt)
238 if ((NULL == d) || (NULL == d->value)) {
242 if ( (getlong (d->value, &val) == 0)
244 || (val > INT_MAX)) {
246 _("configuration error - cannot parse %s value: '%s'"),
251 return (unsigned int) val;
256 * getdef_long - get long integer value from table of definitions
258 * Returns numeric value of specified item, else the "dflt" value if
259 * the item is not defined. Octal (leading "0") and hex (leading "0x")
260 * values are handled.
263 long getdef_long (const char *item, long dflt)
273 if ((NULL == d) || (NULL == d->value)) {
277 if (getlong (d->value, &val) == 0) {
279 _("configuration error - cannot parse %s value: '%s'"),
288 * getdef_ulong - get unsigned long numerical value from table of definitions
290 * Returns numeric value of specified item, else the "dflt" value if
291 * the item is not defined. Octal (leading "0") and hex (leading "0x")
292 * values are handled.
295 unsigned long getdef_ulong (const char *item, unsigned long dflt)
305 if ((NULL == d) || (NULL == d->value)) {
309 if (getulong (d->value, &val) == 0) {
310 /* FIXME: we should have a getulong */
312 _("configuration error - cannot parse %s value: '%s'"),
321 * putdef_str - override the value read from /etc/login.defs
322 * (also used when loading the initial defaults)
325 int putdef_str (const char *name, const char *value)
335 * Locate the slot to save the value. If this parameter
336 * is unknown then "def_find" will print an err message.
344 * Save off the value.
348 (void) fputs (_("Could not allocate space for config info.\n"),
350 SYSLOG ((LOG_ERR, "could not allocate space for config info"));
354 if (NULL != d->value) {
364 * def_find - locate named item in table
366 * Search through a table of configurable items to locate the
367 * specified configuration option.
370 static /*@observer@*/ /*@null@*/struct itemdef *def_find (const char *name)
376 * Search into the table.
379 for (ptr = def_table; NULL != ptr->name; ptr++) {
380 if (strcmp (ptr->name, name) == 0) {
386 * Item was never found.
390 _("configuration error - unknown item '%s' (notify administrator)\n"),
392 SYSLOG ((LOG_CRIT, "unknown configuration item `%s'", name));
393 return (struct itemdef *) NULL;
397 * def_load - load configuration table
399 * Loads the user-configured options from the default configuration file
402 static void def_load (void)
406 char buf[1024], *name, *value, *s;
409 * Open the configuration definitions file.
411 fp = fopen (def_fname, "r");
414 SYSLOG ((LOG_CRIT, "cannot open login definitions %s [%s]",
415 def_fname, strerror (err)));
420 * Set the initialized flag.
421 * (do it early to prevent recursion in putdef_str())
426 * Go through all of the lines in the file.
428 while (fgets (buf, (int) sizeof (buf), fp) != NULL) {
431 * Trim trailing whitespace.
433 for (i = (int) strlen (buf) - 1; i >= 0; --i) {
434 if (!isspace (buf[i])) {
442 * Break the line into two fields.
444 name = buf + strspn (buf, " \t"); /* first nonwhite */
445 if (*name == '\0' || *name == '#')
446 continue; /* comment or empty */
448 s = name + strcspn (name, " \t"); /* end of field */
450 continue; /* only 1 field?? */
453 value = s + strspn (s, " \"\t"); /* next nonwhite */
454 *(value + strcspn (value, "\"")) = '\0';
457 * Store the value in def_table.
459 * Ignore failures to load the login.defs file.
460 * The error was already reported to the user and to
461 * syslog. The tools will just use their default values.
463 (void)putdef_str (name, value);
466 if (ferror (fp) != 0) {
468 SYSLOG ((LOG_CRIT, "cannot read login definitions %s [%s]",
469 def_fname, strerror (err)));
478 int main (int argc, char **argv)
486 for (i = 0; i < NUMDEFS; ++i) {
487 d = def_find (def_table[i].name);
489 printf ("error - lookup '%s' failed\n",
492 printf ("%4d %-24s %s\n", i + 1, d->name, d->value);
495 for (i = 1; i < argc; i++) {
496 cp = getdef_str (argv[1]);
498 printf ("%s `%s'\n", argv[1], cp);
500 printf ("%s not found\n", argv[1]);