home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / shadow-3.1.4 / getdef.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-05  |  6.8 KB  |  363 lines

  1. /*
  2.  * Copyright 1991, 1992, John F. Haugh II and Chip Rosenthal
  3.  * All rights reserved.
  4.  *
  5.  * Permission is granted to copy and create derivative works for any
  6.  * non-commercial purpose, provided this copyright notice is preserved
  7.  * in all copies of source code, or included in human readable form
  8.  * and conspicuously displayed on all copies of object code or
  9.  * distribution media.
  10.  */
  11.  
  12. #ifndef lint
  13. static    char    sccsid[] = "@(#)getdef.c    3.5    21:21:14    3/7/92";
  14. #endif
  15.  
  16. #include <stdio.h>
  17. #include <ctype.h>
  18. #ifndef BSD
  19. # include <string.h>
  20. #else
  21. # include <strings.h>
  22. #endif
  23. #include "config.h"
  24.  
  25. #ifdef    USE_SYSLOG
  26. #include <syslog.h>
  27.  
  28. #ifndef    LOG_WARN
  29. #define    LOG_WARN    LOG_WARNING
  30. #endif
  31. #endif
  32.  
  33. /*
  34.  * A configuration item definition.
  35.  */
  36.  
  37. struct itemdef {
  38.     char *name;        /* name of the item            */
  39.     char *value;        /* value given, or NULL if no value    */
  40. };
  41.  
  42. /*
  43.  * This list *must* be sorted by the "name" member.
  44.  */
  45.  
  46. #define NUMDEFS    (sizeof(def_table)/sizeof(def_table[0]))
  47. struct itemdef def_table[] = {
  48.     { "CONSOLE",            NULL },
  49.     { "DIALUPS_CHECK_ENAB",        NULL },
  50.     { "ENV_HZ",            NULL },
  51.     { "ENV_PATH" ,            NULL },
  52.     { "ENV_SUPATH",            NULL },
  53.     { "ENV_TZ",            NULL },
  54.     { "ERASECHAR",            NULL },
  55.     { "FAILLOG_ENAB",        NULL },
  56.     { "FTMP_FILE",            NULL },
  57.     { "HUSHLOGIN_FILE",        NULL },
  58.     { "ISSUE_FILE_ENAB",        NULL },
  59.     { "KILLCHAR",            NULL },
  60.     { "LASTLOG_ENAB",        NULL },
  61.     { "LOG_UNKFAIL_ENAB",        NULL },
  62.     { "MAIL_CHECK_ENAB",        NULL },
  63.     { "MAIL_DIR",            NULL },
  64.     { "MAIL_FILE",            NULL },
  65.     { "MOTD_FILE",            NULL },
  66.     { "NOLOGINS_FILE",        NULL },
  67.     { "NOLOGIN_STR",        NULL },
  68.     { "OBSCURE_CHECKS_ENAB",    NULL },
  69.     { "PASS_MAX_DAYS",        NULL },
  70.     { "PASS_MIN_DAYS",        NULL },
  71.     { "PASS_MIN_LEN",        NULL },
  72.     { "PASS_WARN_AGE",        NULL },
  73.     { "PORTTIME_CHECKS_ENAB",    NULL },
  74.     { "QUOTAS_ENAB",        NULL },
  75.     { "SULOG_FILE",            NULL },
  76.     { "SU_NAME",            NULL },
  77.     { "SYSLOG_SU_ENAB",        NULL },
  78.     { "TTYGROUP",            NULL },
  79.     { "TTYPERM",            NULL },
  80.     { "TTYTYPE_FILE",        NULL },
  81.     { "ULIMIT",            NULL },
  82.     { "UMASK",            NULL },
  83. };
  84.  
  85. static char def_fname[] = LOGINDEFS;    /* login config defs file    */
  86. static int def_loaded = 0;        /* are defs already loaded?    */
  87.  
  88. extern long strtol();
  89.  
  90. static struct itemdef *def_find();
  91. static void def_load();
  92.  
  93.  
  94. /*
  95.  * getdef_str - get string value from table of definitions.
  96.  *
  97.  * Return point to static data for specified item, or NULL if item is not
  98.  * defined.  First time invoked, will load definitions from the file.
  99.  */
  100.  
  101. char *
  102. getdef_str(item)
  103. char *item;
  104. {
  105.     struct itemdef *d;
  106.  
  107.     if (!def_loaded)
  108.         def_load();
  109.  
  110.     return ((d = def_find(item)) == NULL ? (char *)NULL : d->value);
  111. }
  112.  
  113.  
  114. /*
  115.  * getdef_bool - get boolean value from table of definitions.
  116.  *
  117.  * Return TRUE if specified item is defined as "yes", else FALSE.
  118.  */
  119.  
  120. int
  121. getdef_bool(item)
  122. char *item;
  123. {
  124.     struct itemdef *d;
  125.  
  126.     if (!def_loaded)
  127.         def_load();
  128.  
  129.     if ((d = def_find(item)) == NULL || d->value == NULL)
  130.         return 0;
  131.  
  132.     return (strcmp(d->value, "yes") == 0);
  133. }
  134.  
  135.  
  136. /*
  137.  * getdef_num - get numerical value from table of definitions
  138.  *
  139.  * Returns numeric value of specified item, else the "dflt" value if
  140.  * the item is not defined.  Octal (leading "0") and hex (leading "0x")
  141.  * values are handled.
  142.  */
  143.  
  144. int
  145. getdef_num(item, dflt)
  146. char *item;
  147. int dflt;
  148. {
  149.     struct itemdef *d;
  150.  
  151.     if (!def_loaded)
  152.         def_load();
  153.  
  154.     if ((d = def_find(item)) == NULL || d->value == NULL)
  155.         return dflt;
  156.  
  157.     return (int) strtol(d->value, (char **)NULL, 0);
  158. }
  159.  
  160.  
  161. /*
  162.  * getdef_long - get long integer value from table of definitions
  163.  *
  164.  * Returns numeric value of specified item, else the "dflt" value if
  165.  * the item is not defined.  Octal (leading "0") and hex (leading "0x")
  166.  * values are handled.
  167.  */
  168.  
  169. long
  170. getdef_long(item, dflt)
  171. char *item;
  172. long dflt;
  173. {
  174.     struct itemdef *d;
  175.  
  176.     if (!def_loaded)
  177.         def_load();
  178.  
  179.     if ((d = def_find(item)) == NULL || d->value == NULL)
  180.         return dflt;
  181.  
  182.     return strtol(d->value, (char **)NULL, 0);
  183. }
  184.  
  185. /*
  186.  * def_find - locate named item in table
  187.  *
  188.  * Search through a sorted table of configurable items to locate the
  189.  * specified configuration option.
  190.  */
  191.  
  192. static struct itemdef *
  193. def_find(name)
  194. char *name;
  195. {
  196.     int min, max, curr, n;
  197.  
  198.     /*
  199.      * Invariant - desired item in range [min:max].
  200.      */
  201.  
  202.     min = 0;
  203.     max = NUMDEFS-1;
  204.  
  205.     /*
  206.      * Binary search into the table.  Relies on the items being
  207.      * sorted by name.
  208.      */
  209.  
  210.     while (min <= max) {
  211.         curr = (min+max)/2;
  212.  
  213.         if (! (n = strcmp(def_table[curr].name, name)))
  214.             return &def_table[curr];
  215.  
  216.         if (n < 0)
  217.             min = curr+1;
  218.         else
  219.             max = curr-1;
  220.     }
  221.  
  222.     /*
  223.      * Item was never found.
  224.      */
  225.  
  226.     fprintf(stderr, "configuration error - unknown item '%s' (notify administrator)\r\n", name);
  227. #ifdef USE_SYSLOG
  228.     syslog(LOG_CRIT, "unknown configuration item `%s'", name);
  229. #endif
  230.     return (struct itemdef *) NULL;
  231. }
  232.  
  233. /*
  234.  * def_load - load configuration table
  235.  *
  236.  * Loads the user-configured options from the default configuration file
  237.  */
  238.  
  239. static void
  240. def_load()
  241. {
  242.     int i;
  243.     FILE *fp;
  244.     struct itemdef *d;
  245.     char buf[BUFSIZ], *name, *value, *s;
  246.  
  247. #ifdef CKDEFS
  248.  
  249.     /*
  250.      * Set this flag early so the errors will be reported only
  251.      * during testing.
  252.      */
  253.  
  254.     ++def_loaded;
  255. #endif
  256.  
  257.     /*
  258.      * Open the configuration definitions file.
  259.      */
  260.  
  261.     if ((fp = fopen(def_fname, "r")) == NULL) {
  262. #ifdef USE_SYSLOG
  263.         extern int errno;
  264.         extern char *sys_errlist[];
  265.  
  266.         syslog(LOG_CRIT, "cannot open login definitions %s [%s]",
  267.             def_fname, sys_errlist[errno]);
  268. #endif
  269.         return;
  270.     }
  271.  
  272.     /*
  273.      * Go through all of the lines in the file.
  274.      */
  275.  
  276.     while (fgets(buf, sizeof(buf), fp) != NULL) {
  277.  
  278.         /*
  279.          * Trim trailing whitespace.
  280.          */
  281.  
  282.         for (i = strlen(buf)-1 ; i >= 0 ; --i) {
  283.             if (!isspace(buf[i]))
  284.                 break;
  285.         }
  286.         buf[++i] = '\0';
  287.  
  288.         /*
  289.          * Break the line into two fields.
  290.          */
  291.  
  292.         name = buf + strspn(buf, " \t");    /* first nonwhite */
  293.         if (*name == '\0' || *name == '#')
  294.             continue;            /* comment or empty */
  295.  
  296.         s = name + strcspn(name, " \t");    /* end of field */
  297.         if (*s == '\0')
  298.             continue;            /* only 1 field?? */
  299.  
  300.         *s++ = '\0';
  301.         value = s + strspn(s, " \t");        /* next nonwhite */
  302.  
  303.         /*
  304.          * Locate the slot to save the value.  If this parameter
  305.          * is unknown then "def_find" will print an err message.
  306.          */
  307.  
  308.         if ((d = def_find(name)) == NULL)
  309.             continue;
  310.  
  311.         /*
  312.          * Save off the value.
  313.          */
  314.  
  315.         if ((d->value = strdup(value)) == NULL) {
  316.             if (! def_loaded)
  317.                 break;
  318.  
  319.             fputs("Could not allocate space for config info.\r\n", stderr);
  320. #ifndef CKDEFS
  321. #ifdef USE_SYSLOG
  322.             syslog(LOG_ERR, "could not allocate space for config info");
  323. #endif
  324. #endif
  325.             break;
  326.         }
  327.     }
  328.     (void) fclose(fp);
  329.  
  330.     /*
  331.      * Set the initialized flag.
  332.      */
  333.  
  334.     ++def_loaded;
  335. }
  336.  
  337. #ifdef CKDEFS
  338. main(argc, argv)
  339. int    argc;
  340. char    **argv;
  341. {
  342.     int i;
  343.     char *cp;
  344.     struct itemdef *d;
  345.  
  346.     def_load ();
  347.  
  348.     for (i = 0 ; i < NUMDEFS ; ++i) {
  349.         if ((d = def_find(def_table[i].name)) == NULL)
  350.             printf("error - lookup '%s' failed\n", def_table[i].name);
  351.         else
  352.             printf("%4d %-24s %s\n", i+1, d->name, d->value);
  353.     }
  354.     for (i = 1;i < argc;i++) {
  355.         if (cp = getdef_str (argv[1]))
  356.             printf ("%s `%s'\n", argv[1], cp);
  357.         else
  358.             printf ("%s not found\n", argv[1]);
  359.     }
  360.     exit(0);
  361. }
  362. #endif
  363.