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

  1. /*
  2.  * various utilities, like stream readers, error handlers, etc.
  3.  */
  4. #include "passwd.h"
  5. #include <signal.h>
  6.  
  7. /*
  8.  * determine if password is in the contents of file "name"
  9.  */
  10. int strfp(neg, str, name, fopn, fclo)
  11. int neg;        /* 1 if success on finding line, 0 if not */
  12. char *str;        /* string to be scanned for */
  13. char *name;        /* file or program name */
  14. FILE *(*fopn)();    /* opens stream */
  15. int (*fclo)();        /* closes stream */
  16. {
  17.     char buf[BUFSIZ];        /* buffer for read (output) word */
  18.     FILE *fp;            /* points to (output) stream */
  19.     void (*child)();
  20.  
  21.     child = signal(SIGCHLD, SIG_IGN);
  22.     /*
  23.      * open the file or output stream
  24.      */
  25.     if ((fp = (*fopn)(name, "r")) == FI_NULL){
  26.         sysyyerror(name);
  27.         (void) signal(SIGCHLD, child);
  28.         return(!neg);
  29.     }
  30.  
  31.     /*
  32.      * read each line and compare
  33.      */
  34.     while(mgets(buf, BUFSIZ, fp)){
  35.         if (strncmp(str, buf, pwsig) == 0){
  36.             /*
  37.              * a match; report success
  38.              */
  39.             (void) (*fclo)(fp);
  40.             (void) signal(SIGCHLD, child);
  41.             return(neg);
  42.         }
  43.     }
  44.  
  45.     /*
  46.      * close the stream; report failure
  47.      */
  48.     (void) (*fclo)(fp);
  49.     (void) signal(SIGCHLD, child);
  50.     return(!neg);
  51. }
  52.  
  53. /*
  54.  * determine if password is matched by a string in the contents of file "name"
  55.  */
  56. int patfp(neg, pat, name, fopn, fclo)
  57. int neg;        /* 1 if success on finding line, 0 if not */
  58. char *pat;        /* pattern to be scanned for */
  59. char *name;        /* file or program name */
  60. FILE *(*fopn)();    /* opens stream */
  61. int (*fclo)();        /* closes stream */
  62. {
  63.     char buf[BUFSIZ];        /* buffer for read (output) word */
  64.     FILE *fp;            /* points to (output) stream */
  65.     void (*child)();
  66.  
  67.     /*
  68.      * if matching, set up the right routine
  69.      */
  70.     if (smatch(pat))
  71.         return(neg);
  72.  
  73.     child = signal(SIGCHLD, SIG_IGN);
  74.     /*
  75.      * open the file or output stream
  76.      */
  77.     if ((fp = (*fopn)(name, "r")) == FI_NULL){
  78.         sysyyerror(name);
  79.         (void) signal(SIGCHLD, child);
  80.         return(!neg);
  81.     }
  82.  
  83.     /*
  84.      * read each line and compare
  85.      */
  86.     while(mgets(buf, BUFSIZ, fp)){
  87.         if (match(buf)){
  88.             /*
  89.              * a match; report success
  90.              */
  91.             (void) (*fclo)(fp);
  92.             (void) signal(SIGCHLD, child);
  93.             return(neg);
  94.         }
  95.     }
  96.  
  97.     /*
  98.      * close the stream; report failure
  99.      */
  100.     (void) (*fclo)(fp);
  101.     (void) signal(SIGCHLD, child);
  102.     return(!neg);
  103. }
  104.  
  105. /*
  106.  * determine if password matches a pattern in the contents of file "name"
  107.  */
  108. int patinfp(neg, str, name, fopn, fclo)
  109. int neg;        /* 1 if success on finding line, 0 if not */
  110. char *str;        /* string to be scanned for */
  111. char *name;        /* file or program name */
  112. FILE *(*fopn)();    /* opens stream */
  113. int (*fclo)();        /* closes stream */
  114. {
  115.     char buf[BUFSIZ];        /* buffer for read (output) word */
  116.     FILE *fp;            /* points to (output) stream */
  117.     void (*child)();
  118.  
  119.     child = signal(SIGCHLD, SIG_IGN);
  120.     /*
  121.      * open the file or output stream
  122.      */
  123.     if ((fp = (*fopn)(name, "r")) == FI_NULL){
  124.         sysyyerror(name);
  125.         (void) signal(SIGCHLD, child);
  126.         return(!neg);
  127.     }
  128.  
  129.     /*
  130.      * read each line and compare
  131.      */
  132.     while(mgets(buf, BUFSIZ, fp)){
  133.         if (smatch(buf) == 0 && match(str)){
  134.             /*
  135.              * a match; report success
  136.              */
  137.             (void) (*fclo)(fp);
  138.             (void) signal(SIGCHLD, child);
  139.             return(neg);
  140.         }
  141.     }
  142.  
  143.     /*
  144.      * close the stream; report failure
  145.      */
  146.     (void) (*fclo)(fp);
  147.     (void) signal(SIGCHLD, child);
  148.     return(!neg);
  149. }
  150.  
  151. /*
  152.  * like fgets, but it clobbers the newline and returns 0 on EOF,
  153.  * nonzero on nonEOF; only loads pwsig chars
  154.  */
  155. mgets(buf, n, fp)
  156. char buf[];        /* input goes here */
  157. int n;            /* size of buf[] */
  158. FILE *fp;        /* points to stream being read */
  159. {
  160.     register int c;        /* input character */
  161.     register char *b = buf;    /* walks buffer storing characters */
  162.  
  163.     /*
  164.      * load characters until: bufferis full; or EOF is found;
  165.      * or line ends
  166.      */
  167.     while(n-- > 1 && (c = getc(fp)) != EOF && c != '\n')
  168.         *b++ = c;
  169.     /*
  170.      * terminate the input buffer
  171.      * if ended by EOF, return 0
  172.      */
  173.     if (n <= 1 || c == '\n')
  174.         *b = '\0';
  175.     else if (c == EOF && b == buf)
  176.         return(0);
  177.     /*
  178.      * not EOF -- return nonzero
  179.      */
  180.     return(1);
  181. }
  182.  
  183. /*
  184.  * get the first line of output from a file or program
  185.  */
  186. firstline(name, buf, nbuf, fopn, fclo)
  187. char *name;            /* file name or program */
  188. char *buf;            /* return buffer */
  189. int nbuf;            /* size of buffer */
  190. FILE *(*fopn)();        /* opens stream */
  191. int (*fclo)();            /* closes stream */
  192. {
  193.     int rval = 1;            /* return value */
  194.     FILE *fp;            /* points to (output) stream */
  195.     void (*child)();
  196.  
  197.     child = signal(SIGCHLD, SIG_IGN);
  198.     /*
  199.      * open the file or output stream
  200.      */
  201.     if ((fp = (*fopn)(name, "r")) == FI_NULL){
  202.         sysyyerror(name);
  203.         (void) signal(SIGCHLD, child);
  204.         return(0);
  205.     }
  206.  
  207.     /*
  208.      * read and save first line; error on EOF
  209.      * otherwise just toss the rest of the lines
  210.      */
  211.     rval = mgets(buf, nbuf, fp);
  212.  
  213.     /*
  214.      * close the stream; report failure
  215.      */
  216.     (void) (*fclo)(fp);
  217.     (void) signal(SIGCHLD, child);
  218.     return(rval);
  219. }
  220.