home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / stg_v4.lzh / pwd.c < prev    next >
C/C++ Source or Header  |  1994-11-11  |  3KB  |  209 lines

  1. /*
  2.  * password file routines
  3.  *
  4.  * 90/04/18 StG initial version on OSK v4.0 (taken from v3 getpw())
  5.  * 92/10/19 StG rewritten for semi unix compatible
  6. */
  7.  
  8. #include "stglib.h"
  9.  
  10. /* unix already has these functions */
  11. #ifndef _UNIX
  12.  
  13. #include "pwd.h"
  14. #include "stglib.h"
  15.  
  16. #define PWDBUF 256
  17.  
  18. int hPWD=0;   /* file number */
  19. struct passwd _pwd;
  20. char pwd_buf[PWDBUF];
  21.  
  22. /* 
  23.  * microware has a bug in login - if no . in uid, takes lsw from priority
  24.  *
  25.  * this function is designed to duplicate the bug
  26. */
  27. unsigned long
  28. cvt_uid(s)
  29. char *s;
  30. {
  31.     unsigned long n;
  32.  
  33.     n=atoi(s);
  34.     n<<=16;
  35.  
  36.     while (*s && *s!='.' && *s!=',') s++;
  37.     n|=atoi(++s);
  38.  
  39.     return(n);
  40. }
  41.  
  42. void
  43. unpad(s)
  44. char *s;
  45. {
  46.     if (!*s)
  47.         return;
  48.  
  49.     while (*s) s++;
  50.     s--;
  51.  
  52.     while (*s && *s==' ') *s--=0;
  53. }    
  54.  
  55. setpwent()
  56. {
  57.     int uid;
  58.  
  59.     if (hPWD)
  60.     {
  61.         lseek(hPWD,0L,0);
  62.         return(0);
  63.     }
  64.  
  65.     uid=getuid();
  66.     setuid(0);
  67.     
  68.     hPWD=open(PWD_FILE,O_RDONLY);
  69.     if (hPWD==ERR)
  70.     {
  71.         syslog(LOG_EMERG,"setpwent: cant access %s %m",PWD_FILE);
  72.         hPWD=0;
  73.         setuid(uid);
  74.         return(ERR);
  75.     }
  76.     setuid(uid);
  77.     return(0);
  78. }
  79.  
  80. endpwent()
  81. {
  82.     if (hPWD)
  83.     {
  84.         close(hPWD);
  85.         hPWD=0;
  86.     }
  87.     return(0);
  88. }
  89.  
  90. struct passwd *
  91. getpwent()
  92. {
  93.     char *p;
  94.     int n;
  95.  
  96.     if (!hPWD)
  97.         if (setpwent()==ERR)
  98.             return(0);
  99.  
  100. nextline:
  101.     n=readln(hPWD,pwd_buf,PWDBUF);
  102.  
  103.     if (n<=0)
  104.         return(0);
  105.  
  106.     pwd_buf[n-1]=0;
  107.  
  108.     if (!*pwd_buf || *pwd_buf=='*')
  109.         goto nextline;
  110.  
  111.     _pwd.pw_passwd=_pwd.pw_xdir=_pwd.pw_dir=_pwd.pw_shell=_pwd.pw_gecos=0;
  112.     _pwd.pw_uid=0;
  113.  
  114.     _pwd.pw_name=pwd_buf;
  115.     p=strcut(pwd_buf,',');
  116.     if (!p)
  117.         goto nextline;
  118.  
  119.     _pwd.pw_passwd=p;
  120.     p=strcut(p,',');
  121.     if (!p)
  122.         goto invalid;
  123.     
  124.     _pwd.pw_uid=cvt_uid(p);
  125.     p=strcut(p,',');
  126.     if (!p)
  127.         goto invalid;
  128.  
  129.     _pwd.pw_pri=atoi(p);
  130.     p=strcut(p,',');
  131.     if (!p)
  132.         goto invalid;
  133.  
  134.     _pwd.pw_xdir=p;
  135.     p=strcut(p,',');
  136.     if (!p)
  137.         goto invalid;
  138.  
  139.     _pwd.pw_dir=p;
  140.     p=strcut(p,',');
  141.     if (!p)
  142.         goto invalid;
  143.  
  144.     _pwd.pw_shell=p;
  145.     p=strcut(p,',');
  146.     if (p)
  147.         _pwd.pw_gecos=p;
  148.  
  149.     unpad(_pwd.pw_name);
  150.     unpad(_pwd.pw_passwd);
  151.     return(&_pwd);
  152.  
  153. invalid:
  154.     syslog(LOG_ERR,"getpwent: invalid line in password file");
  155.     goto nextline;
  156. }
  157.  
  158. struct passwd *
  159. getpwnam(name)
  160. char *name;
  161. {
  162.     struct passwd *pw;
  163.  
  164.     if (_pwd.pw_name && !stricmp(_pwd.pw_name,name))
  165.         return(&_pwd);
  166.  
  167.     setpwent();
  168.     do
  169.     {
  170.         pw=getpwent();
  171.         if (!pw)
  172.         {
  173. /*            endpwent(); */
  174.             return(0);
  175.         }
  176.     }
  177.     while (stricmp(pw->pw_name,name));
  178.  
  179. /*    endpwent(); */
  180.     return(pw);
  181. }
  182.  
  183. struct passwd *
  184. getpwuid(uid)
  185. long uid;
  186. {
  187.     struct passwd *pw;
  188.  
  189.     if (_pwd.pw_name && _pwd.pw_uid==uid)
  190.         return(&_pwd);
  191.  
  192.     setpwent();
  193.     do
  194.     {
  195.         pw=getpwent();
  196.         if (!pw)
  197.         {
  198. /*            endpwent(); */
  199.             return(0);
  200.         }
  201.     }
  202.     while (pw->pw_uid!=uid);
  203.  
  204. /*    endpwent(); */
  205.     return(pw);
  206. }
  207.  
  208. #endif
  209.