home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / passwd+ / verify.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-12  |  4.9 KB  |  261 lines

  1. /*
  2.  * this is the driver for the password tester
  3.  */
  4. #include "passwd.h"
  5.  
  6. /*
  7.  * file variables
  8.  */
  9. int linect = 0;                /* count of lines */
  10.  
  11. /*
  12.  * verify the password is okay
  13.  */
  14. verify(ermsg)
  15. char ermsg[];
  16. {
  17.     char buf[BUFSIZ];        /* input buffer */
  18.     char passbuf[2*BUFSIZ];        /* buffer ater escape translation */
  19.     FILE *fp;            /* test file pointer */
  20.     register char *p;        /* used to append to passbuf[] */
  21.     char *b;            /* used to append to buf[] */
  22.     char *sucmsg;            /* message on failure */
  23.  
  24.     /*
  25.      * set up the password and hostname
  26.      */
  27.     initpw();
  28.  
  29.     /*
  30.      * open the file
  31.      */
  32.     if ((fp = fopen(pwtest, "r")) == NULL){
  33.         if (errno < sys_nerr){
  34.             LOG2(LG_SYSTEM, "%s: %s", pwtest, sys_errlist[errno]);
  35.         }
  36.         else
  37.             LOG2(LG_SYSTEM, "%s: unknown error #%d",
  38.                             pwtest, errno);
  39. #ifdef DEFAULTTEST
  40.         return(strlen(password) > 6 && atoi(findiv('v')->length) > 0);
  41. #else
  42.         return(0);
  43. #endif
  44.     }
  45.  
  46.     /*
  47.      * now grab lines, replacing % and # as required
  48.      */
  49.     while(fgets(buf, BUFSIZ, fp) != NULL){
  50.         /*
  51.          * bump the line count
  52.          */
  53.         linect++;
  54.         /*
  55.          * see if it is a comment line; skip if so
  56.          */
  57.         if (buf[0] == '#')
  58.             continue;
  59.         /*
  60.          * kill the newline if any
  61.          */
  62.         if (*(p = &buf[strlen(buf)-1]) == '\n')
  63.             *p = '\0';
  64.         /*
  65.          * look for various special things
  66.          */
  67.         if (strncmp(buf, "GECOS:", 6) == 0 ||
  68.             strncmp(buf, "SETGECOS:", 9) == 0){    /* gecos description */
  69.             (void) loadgecos(&buf[6], CH_NULL, CH_NULL);
  70.             continue;
  71.         }
  72.         if (strncmp(buf, "SIGCHARS:", 9) == 0){    /* significant chars */
  73.             loadsig(&buf[9]);
  74.             continue;
  75.         }
  76.         if (strncmp(buf, "SETVAR:", 7) == 0){    /* set variable */
  77.             escape(&buf[7], passbuf);
  78.             setvar(passbuf);
  79.             continue;
  80.         }
  81.         if (strncmp(buf, "LOGLEVEL:", 9) == 0){    /* logging */
  82.             loadlevel(&buf[9]);
  83.             continue;
  84.         }
  85.         if (strncmp(buf, "FORCEGECOS:", 11) == 0)  /* force gecos */
  86.             continue;
  87.         /*
  88.          * an unescaped tab ends the test
  89.          * and begins the error message
  90.          */
  91.         sucmsg = NULL;
  92.         for(b = buf; (p = index(b, '\t')) != NULL &&
  93.                     (p != buf && p[-1] == '\\'); b = p);
  94.         if (p != NULL){
  95.             /*
  96.              * move to the end of the white spaces;
  97.              * the error message begins there
  98.              */
  99.             *p++ = '\0';
  100.             while(isspace(*p))
  101.                 p++;
  102.             if (*p != '\0')
  103.                 sucmsg = p;
  104.         }
  105.         /*
  106.          * now for the escapes
  107.          * go down the tests and interpolate all the escapes
  108.          */
  109.         escape(buf, passbuf);
  110.  
  111.         /*
  112.          * pass the buffer to the tester
  113.          */
  114.         LOG2(LG_DEBUG, "testing buffer \"%s\" on line %d",
  115.                             passbuf, linect);
  116.         if (passtest(passbuf) == 1){
  117.             /*
  118.              * it passed; return any error message
  119.              * and close the file
  120.              */
  121.             if (sucmsg == NULL)
  122.                 ermsg[0] = '\0';
  123.             else
  124.                 (void) strcpy(ermsg, sucmsg);
  125.             (void) fclose(fp);
  126.             LOG2(LG_ITEM, "failed (%s) on line %d",
  127.                                sucmsg, linect);
  128.             return(0);
  129.         }
  130.     }
  131.     /*
  132.      * it failed; close the file
  133.      * and return
  134.      */
  135.     (void) fclose(fp);
  136.     return(1);
  137. }
  138.  
  139. /*
  140.  * this expands all escapes
  141.  */
  142. escape(b, p)
  143. register char *b;            /* input buffer */
  144. register char *p;            /* expanded buffer */
  145. {
  146.     register int n1, n2;        /* begin, end position numbers */
  147.     register int sgn;        /* sign of escape */
  148.     register int num;        /* format field */
  149.     register struct intvar *ip;    /* points to variable storage */
  150.     char *tb;            /* passes address of reg variable */
  151.  
  152.     /*
  153.      * do the required interpolations
  154.      */
  155.     for( ; *b; b++){
  156.         /*
  157.          * string escapes
  158.          */
  159.         if (*b == '%'){
  160.             /*
  161.              * get the sign if any
  162.              */
  163.             if (b[1] == '+' || b[1] == '-'){
  164.                 sgn = (b[1] == '-');
  165.                 b++;
  166.             }
  167.             else
  168.                 sgn = 0;
  169.             /*
  170.              * get the first number, if any
  171.              */
  172.             if (isdigit(b[1])){
  173.                 tb = b + 1;
  174.                 n1 = matoi(&tb)-1;
  175.                 b = tb - 1;
  176.                 /*
  177.                  * if a decimal point, a second
  178.                  * number may follow
  179.                  */
  180.                 if (b[1] == '.'){
  181.                     b++;
  182.                     if (isdigit(b[1])){
  183.                         tb = b + 1;
  184.                         n2 = matoi(&tb)-1;
  185.                         b = tb - 1;
  186.                     }
  187.                     else
  188.                         n2 = BUFSIZ;
  189.                 }
  190.                 else{
  191.                     /*
  192.                      * just a number -- number
  193.                      * of initial chars
  194.                      */
  195.                     n2 = n1;
  196.                     n1 = 0;
  197.                 }
  198.             }
  199.             else{
  200.                 /*
  201.                  * no numbers, just make limits BIG
  202.                  */
  203.                 n1 = 0;
  204.                 n2 = BUFSIZ;
  205.             }
  206.             /*
  207.              * now look for number sign
  208.              */
  209.             switch(b[1]){
  210.             case '^':        /* all upper case */
  211.                 num = F_UPPER;
  212.                 b++;
  213.                 break;
  214.             case '|':        /* first letter cap */
  215.                 num = F_FIRST;
  216.                 b++;
  217.                 break;
  218.             case '*':        /* all lower case */
  219.                 num = F_LOWER;
  220.                 b++;
  221.                 break;
  222.             case '#':        /* associated number */
  223.                 num = F_NUMBER;
  224.                 b++;
  225.                 break;
  226.             default:        /* as is */
  227.                 num = F_ASIS;
  228.                 break;
  229.             }
  230.             ip = findiv(*++b);
  231.             switch(ip->nstype){
  232.             case TY_STR:        /* string variable */
  233.                 p = sfmt(p, sgn, n1, n2, num, 
  234.                     ip->string, ip->length);
  235.                 break;
  236.             case TY_NUM:        /* numeric variable */
  237.                 p = nfmt(p, sgn, ip->string, ip->length);
  238.                 break;
  239.             }
  240.         }
  241.         /*
  242.          * escaped char; just stuff the next one
  243.          */
  244.         else if (*b == '\\' &&
  245.               (b[1] == '%' || b[1] == '#' || b[1] == '\\')){
  246.             *++p = *++b;
  247.             p++;
  248.         }
  249.         /*
  250.          * something else; copy it
  251.          */
  252.         else
  253.             *p++ = *b;
  254.     }
  255.  
  256.     /*
  257.      * close the buffer
  258.      */
  259.     *p = '\0';
  260. }
  261.