home *** CD-ROM | disk | FTP | other *** search
/ Beijing Paradise BBS Backup / PARADISE.ISO / software / BBSDOORW / SMAILSRC.ZIP / SMAIL.ZIP / PW.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-05  |  4.8 KB  |  268 lines

  1. #ifndef lint
  2. static char *sccsid = "@(#)pw.c    2.5 (smail) 9/15/87";
  3. #endif
  4.  
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <pwd.h>
  9. #include "defs.h"
  10. #include <ctype.h>
  11.  
  12. char *malloc();
  13. void free();
  14.  
  15. typedef struct pw_node pwlist;
  16.  
  17. struct pw_node {
  18.     char *lname;            /* login name */
  19.     char *fname;            /* full name  */
  20.     int  uid;            /* user-id    */
  21.     char *home;            /* login name */
  22.     pwlist *vlink;            /* link to next item */
  23. };
  24.  
  25. pwlist *pwhead;        /* head of linked list */
  26. pwlist *pwparse();    /* head of linked list */
  27.  
  28. #define PNULL    ((pwlist *) 0)
  29.  
  30. char *
  31. pwfnam(user)
  32. char *user;
  33. {
  34.     pwlist *f;
  35.  
  36.     /*
  37.     ** check for previously cached user
  38.     */
  39.  
  40.     for(f=pwhead; f != NULL; f=f->vlink) {
  41.         if(strcmp(user, f->lname) == 0) {
  42.             return(f->fname);
  43.         }
  44.     }
  45.     /*
  46.     ** not found parse the password file
  47.     */
  48.  
  49.     while((f=pwparse()) != PNULL) {
  50.         if(strcmp(user, f->lname) == 0) {
  51.             return(f->fname);
  52.         }
  53.     }
  54.     return(NULL);
  55. }
  56.  
  57. char *
  58. pwuid(uid)
  59. int uid;
  60. {
  61.     pwlist *f;
  62.  
  63.     /*
  64.     ** check for previously cached user
  65.     */
  66.  
  67.     for(f=pwhead; f != NULL; f=f->vlink) {
  68.         if(uid == f->uid) {
  69.             return(f->lname);
  70.         }
  71.     }
  72.     /*
  73.     ** not found parse the password file
  74.     */
  75.  
  76.     while((f=pwparse()) != PNULL) {
  77.         if(uid == f->uid) {
  78.             return(f->lname);
  79.         }
  80.     }
  81.     return(NULL);
  82. }
  83.  
  84. #ifndef SENDMAIL
  85. char *
  86. tilde(user)
  87. char *user;
  88. {
  89.     pwlist *f;
  90.  
  91.     /*
  92.     ** check for previously cached user
  93.     */
  94.  
  95.     for(f=pwhead; f != NULL; f=f->vlink) {
  96.         if(strcmp(user, f->lname) == 0) {
  97.             return(f->home);
  98.         }
  99.     }
  100.     /*
  101.     ** not found parse the password file
  102.     */
  103.  
  104.     while((f=pwparse()) != PNULL) {
  105.         if(strcmp(user, f->lname) == 0) {
  106.             return(f->home);
  107.         }
  108.     }
  109.     return(NULL);
  110. }
  111. #endif /* not SENDMAIL */
  112.  
  113. char *
  114. fullname(gecos)
  115. char *gecos;
  116. {
  117.     static char fname[SMLBUF];
  118.     register char *cend;
  119.  
  120.     (void) strcpy(fname, gecos);
  121.     if (cend = index(fname, ','))
  122.         *cend = '\0';
  123.     if (cend = index(fname, '('))
  124.         *cend = '\0';
  125.     /*
  126.     ** Skip USG-style 0000-Name nonsense if necessary.
  127.     */
  128.     if (isdigit(*(cend = fname))) {
  129.         if ((cend = index(fname, '-')) != NULL)
  130.             cend++;
  131.         else
  132.             /*
  133.             ** There was no `-' following digits.
  134.             */
  135.             cend = fname;
  136.     }
  137.     return (cend);
  138. }
  139.  
  140. pwlist *
  141. pwparse()
  142. {
  143.     pwlist *f;
  144.     char *p, *name;
  145.     struct passwd *pwent, *getpwent();
  146.     unsigned int i;
  147.     static int pw_eof = 0;
  148.  
  149.     if((pw_eof == 1)
  150.     || ((pwent = getpwent()) == (struct passwd *) NULL)) {
  151.         pw_eof = 1;
  152.         return(PNULL);
  153.     }
  154.     /*
  155.     ** Get an entry from the password file.
  156.     ** Parse relevant strings.
  157.     */
  158.     f = (pwlist *) malloc(sizeof(pwlist));
  159.     if(f == PNULL) return(PNULL);
  160.  
  161.     f->vlink = pwhead;
  162.     pwhead   = f;
  163.     f->uid   = pwent->pw_uid;
  164.  
  165.     i=strlen(pwent->pw_name)+1;
  166.     p = malloc(i);
  167.     if(p == NULL) return(PNULL);
  168.     f->lname = strcpy(p, pwent->pw_name);
  169.  
  170.     i=strlen(pwent->pw_dir)+1;
  171.     p = malloc(i);
  172.     if(p == NULL) return(PNULL);
  173.     f->home  = strcpy(p, pwent->pw_dir);
  174.  
  175.     name = fullname(pwent->pw_gecos);
  176.     i=strlen(name)+1;
  177.     p = malloc(i);
  178.     if(p == NULL) return(PNULL);
  179.     f->fname = strcpy(p, name);
  180.     return(f);
  181. }
  182.  
  183. #ifdef FULLNAME
  184. /*
  185. ** Resolve a full name to a login name.
  186. ** Not too much smarts here.
  187. */
  188.  
  189. char *
  190. res_fname(user)
  191. register char *user;
  192. {
  193.     long pos, middle, hi, lo;
  194.     static long pathlength = 0;
  195.     register char *s;
  196.     int c;
  197.     static FILE *file;
  198.     int flag;
  199.     char namebuf[SMLBUF], *path;
  200.     extern enum edebug debug;
  201.     extern char *fnlist;
  202.  
  203.  
  204.  
  205. DEBUG("res_fname: looking for '%s'\n", user);
  206.  
  207.     if(pathlength == 0) {    /* open file on first use */
  208.         if((file=fopen(fnlist, "r")) == NULL) {
  209.             DEBUG( "can't access %s.\n", fnlist);
  210.             pathlength = -1;
  211.         } else {
  212.             (void) fseek(file, 0L, 2);     /* find length */
  213.             pathlength = ftell(file);
  214.         }
  215.     }
  216.  
  217.     if(pathlength == -1 ) return(NULL);
  218.  
  219.     lo = 0;
  220.     hi = pathlength;
  221.     path = namebuf;
  222.  
  223.     (void) strcpy( path, user );
  224.     (void) strcat( path, "\t" );
  225.  
  226.     for( ;; ) {
  227.         pos = middle = ( hi+lo+1 )/2;
  228.         (void) fseek( file, pos, 0 );    /* find midpoint */
  229.         if (pos != 0)        /* to beginning of next line */
  230.             while( ( c=getc( file ) ) != EOF && c != '\n' );
  231.         for( flag = 0, s = path; flag == 0; s++ ) { /* match??? */
  232.             if ( *s == '\0' ) {
  233.                 goto solved;
  234.             }
  235.             c = getc( file );
  236.             flag = lower( c ) - lower( *s );
  237.         } 
  238.         if (lo >= middle)        /* failure? */
  239.             return(NULL);
  240.  
  241.         if(c != EOF && flag < 0)    /* close window */
  242.             lo = middle;
  243.         else 
  244.             hi = middle - 1;
  245.     }
  246. /* 
  247. ** Now just copy the result.
  248. */
  249. solved:
  250.     while(((c  = getc(file)) != EOF) && (c != '\t') && (c != '\n')) {
  251.         *path++ = c;
  252.     }
  253.  
  254.     if(path == namebuf) {    /* NULL alias field */
  255.         return(NULL);
  256.     }
  257.  
  258.     *path = '\0';
  259.     if((path = malloc((unsigned) strlen(namebuf)+1)) == NULL) {
  260.         return(NULL);    /* sorry, no memory */
  261.     }
  262.  
  263.     (void) strcpy(path, namebuf);
  264.     return(path);
  265.  
  266. }
  267. #endif    /* FULLNAME */
  268.