home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / comm / mail / smail / src / rcs / pw.c,v < prev    next >
Text File  |  1993-12-21  |  7KB  |  425 lines

  1. head    1.4;
  2. access;
  3. symbols
  4.     C_1:1.4;
  5. locks; strict;
  6. comment    @ * @;
  7.  
  8.  
  9. 1.4
  10. date    93.10.31.20.09.32;    author Aussem;    state Exp;
  11. branches;
  12. next    1.3;
  13.  
  14. 1.3
  15. date    93.10.30.21.44.37;    author Aussem;    state Exp;
  16. branches;
  17. next    1.2;
  18.  
  19. 1.2
  20. date    93.09.18.16.47.47;    author Aussem;    state Exp;
  21. branches;
  22. next    1.1;
  23.  
  24. 1.1
  25. date    93.09.08.16.27.13;    author Aussem;    state Exp;
  26. branches;
  27. next    ;
  28.  
  29.  
  30. desc
  31. @passwd routines
  32. @
  33.  
  34.  
  35. 1.4
  36. log
  37. @HAVE_GETPWENT insert for passwd routines without getpwent()
  38. @
  39. text
  40. @/*
  41.  * pw.c
  42.  *
  43.  * Routines for the passwd
  44.  *
  45.  * This program is free software; you can redistribute it and/or
  46.  * modify it under the terms of the GNU General Public License as
  47.  * published by the Free Software Foundation; either version 2 of
  48.  * the License, or (at your option) any later version.
  49.  *
  50.  * This program is distributed in the hope that it will be useful,
  51.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  52.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  53.  * General Public License for more details.
  54.  *
  55.  * You should have received a copy of the GNU General Public License
  56.  * along with this program; if not, write to the Free Software
  57.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  58.  *
  59.  * $Log: pw.c,v $
  60.  * Revision 1.3  1993/10/30  21:44:37  Aussem
  61.  * cosmetics changes
  62.  *
  63.  * Revision 1.2  1993/09/18  16:47:47  Aussem
  64.  * insert GNU license text in the header
  65.  *
  66.  * Revision 1.1  1993/09/08  16:27:13  Aussem
  67.  * Initial revision
  68.  *
  69.  *
  70.  */
  71.  
  72. static char     *rcsid="$Id: pw.c,v 1.3 1993/10/30 21:44:37 Aussem Exp Aussem $";
  73.  
  74. #include <stdio.h>
  75. #include <stdlib.h>
  76. #include <sys/types.h>
  77. #include <sys/stat.h>
  78. #include <ctype.h>
  79. #include "defs.h"
  80.  
  81. #ifdef HAVE_GETPWENT
  82. struct pw_node {
  83.     char *lname;            /* login name */
  84.     char *fname;            /* full name  */
  85.     int  uid;            /* user-id    */
  86.     char *home;            /* login name */
  87.     pwlist *vlink;            /* link to next item */
  88. };
  89.  
  90. pwlist *pwhead;        /* head of linked list */
  91. pwlist *pwparse();    /* head of linked list */
  92.  
  93. #define PNULL    ((pwlist *) 0)
  94.  
  95. char *
  96. pwfnam(user)
  97. char *user;
  98. {
  99.     pwlist *f;
  100.  
  101.     /*
  102.     ** check for previously cached user
  103.     */
  104.  
  105.     for(f=pwhead; f != NULL; f=f->vlink) {
  106.         if(strcmp(user, f->lname) == 0) {
  107.             return(f->fname);
  108.         }
  109.     }
  110.     /*
  111.     ** not found parse the password file
  112.     */
  113.  
  114.     while((f=pwparse()) != PNULL) {
  115.         if(strcmp(user, f->lname) == 0) {
  116.             return(f->fname);
  117.         }
  118.     }
  119.     return(NULL);
  120. }
  121.  
  122. char *
  123. pwuid(uid)
  124. int uid;
  125. {
  126.     pwlist *f;
  127.  
  128.     /*
  129.     ** check for previously cached user
  130.     */
  131.  
  132.     for(f=pwhead; f != NULL; f=f->vlink) {
  133.         if(uid == f->uid) {
  134.             return(f->lname);
  135.         }
  136.     }
  137.     /*
  138.     ** not found parse the password file
  139.     */
  140.  
  141.     while((f=pwparse()) != PNULL) {
  142.         if(uid == f->uid) {
  143.             return(f->lname);
  144.         }
  145.     }
  146.     return(NULL);
  147. }
  148.  
  149. char *
  150. tilde(user)
  151. char *user;
  152. {
  153.     pwlist *f;
  154.  
  155.     /*
  156.     ** check for previously cached user
  157.     */
  158.  
  159.     for(f=pwhead; f != NULL; f=f->vlink) {
  160.         if(strcmp(user, f->lname) == 0) {
  161.             return(f->home);
  162.         }
  163.     }
  164.     /*
  165.     ** not found parse the password file
  166.     */
  167.  
  168.     while((f=pwparse()) != PNULL) {
  169.         if(strcmp(user, f->lname) == 0) {
  170.             return(f->home);
  171.         }
  172.     }
  173.     return(NULL);
  174. }
  175.  
  176. char *
  177. fullname(gecos)
  178. char *gecos;
  179. {
  180.     static char fname[SMLBUF];
  181.     register char *cend;
  182.  
  183.     (void) strcpy(fname, gecos);
  184.     if (cend = index(fname, ','))
  185.         *cend = '\0';
  186.     if (cend = index(fname, '('))
  187.         *cend = '\0';
  188.     /*
  189.     ** Skip USG-style 0000-Name nonsense if necessary.
  190.     */
  191.     if (isdigit(*(cend = fname))) {
  192.         if ((cend = index(fname, '-')) != NULL)
  193.             cend++;
  194.         else
  195.             /*
  196.             ** There was no `-' following digits.
  197.             */
  198.             cend = fname;
  199.     }
  200.     return (cend);
  201. }
  202.  
  203. pwlist *
  204. pwparse()
  205. {
  206.     pwlist *f;
  207.     char *p, *name;
  208.     struct passwd *pwent, *getpwent();
  209.     unsigned int i;
  210.     static int pw_eof = 0;
  211.  
  212.     if((pw_eof == 1)
  213.     || ((pwent = getpwent()) == (struct passwd *) NULL)) {
  214.         pw_eof = 1;
  215.         return(PNULL);
  216.     }
  217.     /*
  218.     ** Get an entry from the password file.
  219.     ** Parse relevant strings.
  220.     */
  221.     f = (pwlist *) malloc(sizeof(pwlist));
  222.     if(f == PNULL) return(PNULL);
  223.  
  224.     f->vlink = pwhead;
  225.     pwhead   = f;
  226.     f->uid   = pwent->pw_uid;
  227.  
  228.     i=strlen(pwent->pw_name)+1;
  229.     p = malloc(i);
  230.     if(p == NULL) return(PNULL);
  231.     f->lname = strcpy(p, pwent->pw_name);
  232.  
  233.     i=strlen(pwent->pw_dir)+1;
  234.     p = malloc(i);
  235.     if(p == NULL) return(PNULL);
  236.     f->home  = strcpy(p, pwent->pw_dir);
  237.  
  238.     name = fullname(pwent->pw_gecos);
  239.     i=strlen(name)+1;
  240.     p = malloc(i);
  241.     if(p == NULL) return(PNULL);
  242.     f->fname = strcpy(p, name);
  243.     return(f);
  244. }
  245. #else /* !HAVE_GETPWENT */
  246.  
  247. struct passwd *getpwuid(int);
  248.  
  249. char *
  250. pwfnam(user)
  251. char *user;
  252. {
  253.    struct passwd *pwd;
  254.  
  255.    pwd=getpwnam(user);
  256.    if(!pwd)
  257.         return(NULL);
  258.     else
  259.         return(pwd->pw_name);
  260. }
  261.  
  262. char *
  263. pwuid(uid)
  264. int uid;
  265. {
  266.    struct passwd *pwd;
  267.  
  268.    pwd=getpwuid(uid);
  269.    if(!pwd)
  270.         return(NULL);
  271.     else
  272.         return(pwd->pw_name);
  273. }
  274.  
  275. char *
  276. tilde(user)
  277. char *user;
  278. {
  279.    struct passwd *pwd;
  280.  
  281.    pwd=getpwnam(user);
  282.    if(!pwd)
  283.         return(NULL);
  284.     else
  285.         return(pwd->pw_dir);
  286. }
  287.  
  288. #endif /* HAVE_GETPWENT */
  289.  
  290. #ifdef FULLNAME
  291. /*
  292. ** Resolve a full name to a login name.
  293. ** Not too much smarts here.
  294. */
  295.  
  296. char *
  297. res_fname(user)
  298. register char *user;
  299. {
  300.     long pos, middle, hi, lo;
  301.     static long pathlength = 0;
  302.     register char *s;
  303.     int c;
  304.     static FILE *file;
  305.     int flag;
  306.     char namebuf[SMLBUF], *path;
  307.     extern enum edebug debug;
  308.     extern char *fnlist;
  309.  
  310. DEBUG("res_fname: looking for '%s'\n", user);
  311.  
  312.     if(pathlength == 0) {    /* open file on first use */
  313.         if((file=fopen(fnlist, "r")) == NULL) {
  314.             DEBUG( "can't access %s.\n", fnlist);
  315.             pathlength = -1;
  316.         } else {
  317.             (void) fseek(file, 0L, 2);     /* find length */
  318.             pathlength = ftell(file);
  319.         }
  320.     }
  321.  
  322.     if(pathlength == -1 ) return(NULL);
  323.  
  324.     lo = 0;
  325.     hi = pathlength;
  326.     path = namebuf;
  327.  
  328.     (void) strcpy( path, user );
  329.     (void) strcat( path, "\t" );
  330.  
  331.     for( ;; ) {
  332.         pos = middle = ( hi+lo+1 )/2;
  333.         (void) fseek( file, pos, 0 );    /* find midpoint */
  334.         if (pos != 0)        /* to beginning of next line */
  335.             while( ( c=getc( file ) ) != EOF && c != '\n' );
  336.         for( flag = 0, s = path; flag == 0; s++ ) { /* match??? */
  337.             if ( *s == '\0' ) {
  338.                 goto solved;
  339.             }
  340.             c = getc( file );
  341.             flag = lower( c ) - lower( *s );
  342.         } 
  343.         if (lo >= middle)        /* failure? */
  344.             return(NULL);
  345.  
  346.         if(c != EOF && flag < 0)    /* close window */
  347.             lo = middle;
  348.         else 
  349.             hi = middle - 1;
  350.     }
  351. /* 
  352. ** Now just copy the result.
  353. */
  354. solved:
  355.     while(((c  = getc(file)) != EOF) && (c != '\t') && (c != '\n')) {
  356.         *path++ = c;
  357.     }
  358.  
  359.     if(path == namebuf) {    /* NULL alias field */
  360.         return(NULL);
  361.     }
  362.  
  363.     *path = '\0';
  364.     if((path = malloc((unsigned) strlen(namebuf)+1)) == NULL) {
  365.         return(NULL);    /* sorry, no memory */
  366.     }
  367.  
  368.     (void) strcpy(path, namebuf);
  369.     return(path);
  370.  
  371. }
  372. #endif    /* FULLNAME */
  373. @
  374.  
  375.  
  376. 1.3
  377. log
  378. @cosmetics changes
  379. @
  380. text
  381. @d21 3
  382. d33 1
  383. a33 1
  384. static char     *rcsid="$Id: pw.c,v 1.2 1993/09/18 16:47:47 Aussem Exp Aussem $";
  385. d42 1
  386. d206 44
  387. @
  388.  
  389.  
  390. 1.2
  391. log
  392. @insert GNU license text in the header
  393. @
  394. text
  395. @d21 3
  396. d30 1
  397. a30 1
  398. static char     *rcsid="$Id: pw.c,v 1.1 1993/09/08 16:27:13 Aussem Exp Aussem $";
  399. a105 1
  400. #ifndef SENDMAIL
  401. a131 1
  402. #endif /* not SENDMAIL */
  403. @
  404.  
  405.  
  406. 1.1
  407. log
  408. @Initial revision
  409. @
  410. text
  411. @d2 1
  412. a2 1
  413.  *  pw.c
  414. d4 1
  415. a4 1
  416.  *  Routines for the passwd
  417. d6 4
  418. a9 1
  419.  * $Log$
  420. d11 14
  421. d27 1
  422. a27 1
  423. static char     *rcsid="$Id$";
  424. @
  425.