home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / npasswd / part01 / checkpasswd / libmain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-19  |  3.4 KB  |  117 lines

  1.  
  2. /* --------------------------------------------------------------------  */
  3. /*                                                                       */
  4. /*                         Author: Clyde Hoover                          */
  5. /*                          Computation Center                           */
  6. /*                   The University of Texas at Austin                   */
  7. /*                          Austin, Texas 78712                          */
  8. /*                         clyde@emx.utexas.edu                          */
  9. /*                   uunet!cs.utexas.edu!ut-emx!clyde                    */
  10. /*                                                                       */
  11. /*This code may be distributed freely, provided this notice is retained. */
  12. /*                                                                       */
  13. /* --------------------------------------------------------------------  */
  14. /*
  15.  *    checkpasswd - Library version main routine
  16.  *
  17.  *    Compilation:    ld -r -o checkpasswd.o checkpasswd.o libmain.o
  18.  *        pwck_dict.o pwck_passwd.o pwck_lexical.o pwck_local.o util.o 
  19.  */
  20. #ifndef lint
  21. static char sccsid[] = "@(#)libmain.c    1.2 11/14/89 (cc.utexas.edu)";
  22. #endif
  23.  
  24. #include "checkpasswd.h"
  25. #include <varargs.h>
  26.  
  27. static char *replies[] = {
  28.     0,                    /* PWCK_OK */
  29.     "Empty password",            /* PWCK_NULL */
  30.     "This password is too easy to guess",    /* PWCK_OBVIOUS */
  31.     "This password is part of your 'finger' information", /* PWCK_FINGER */
  32.     "This password was found in a dictionary",    /* PWCK_INDICT */
  33.     "This password has an illegal character in it",    /* PWCK_ILLCHAR */
  34.     "This password is too short",            /* PWCK_SHORT */
  35.     0
  36. };
  37. #define    NREPLIES    7    /* Number of messages in replies */
  38.  
  39. static char    elucidate[BUFSIZ];    /* Expanded error message */
  40. static char    *configfile = CONFIG_FILE;    /* Configuration file */
  41. static char    configured = 0;        /* Has cf been read? */
  42. static int    silent = 0;        /* Don't print messages */
  43.         returncode = 0;        /* Return PWCK return code */
  44. int    standalone = 0;            /* Not a standalone application */
  45.  
  46. /*
  47.  *    setcheckpasswd - set parameters for checkpasswd
  48.  *
  49.  *    e.g setcheckpasswd("-c", <configfile>, "-e", "-s", 0);
  50.  */
  51. setcheckpasswd(va_alist)
  52. va_dcl        /* List of options */
  53. {
  54.     va_list    optlist;
  55.     char    *optx;
  56.  
  57.     va_start(optlist);
  58.     while (optx = va_arg(optlist, char *)) {
  59.         if (*optx == '-') {
  60.             switch(*++optx) {
  61.             case 's':    /* -s (silent) */
  62.                 silent = 1;
  63.                 break;
  64.             case 'e':    /* -e (return error code) */
  65.                 returncode = 1;
  66.                 break;
  67.             case 'c':    /* -c config-file */
  68.                 if (*++optx)
  69.                     configfile = optx;
  70.                 else {
  71.                     optx = va_arg(optlist, char *);
  72.                     if (optx)
  73.                         configfile = optx;
  74.                 }
  75.                 break;
  76.             }
  77.         }
  78.     }
  79.     va_end(optlist);
  80. }
  81.  
  82. /*
  83.  *    checkpasswd - check password candidate
  84.  *
  85.  *    Returns 1 if <pwd> is ok to use as a password
  86.  *        0 if not & an appropriate error message is issued
  87.  */
  88. checkpasswd(uid, pwd)
  89. int    uid;        /* User who wants this password */
  90. char    *pwd;        /* Password they want */
  91. {
  92.     int    rc;    /* Return code */
  93.  
  94. #ifdef    DEBUG
  95.     printf("checkpasswd %d %s\n", uid, pwd);
  96. #endif
  97.     if (!configured) {
  98.         readconfig(configfile);
  99.         configured++;
  100.     }
  101.     rc = checkpassword(pwd, uid, elucidate);
  102.     if (rc == PWCK_OK)        /* Always silent on success */
  103.         return(returncode ? rc : 1);
  104.     if (silent)
  105.         return(returncode ? rc : 0);
  106.     if (rc <= NREPLIES) {
  107.         if (elucidate[0])
  108.             printf("%s.\n", elucidate);
  109.         else if (replies[rc])
  110.             printf("%s.\n", replies[rc]);
  111.         else
  112.             putchar('\n');
  113.     }
  114.     return(returncode ? rc : 0);
  115. }
  116. /*    End libmain.c */
  117.