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 >
Wrap
C/C++ Source or Header
|
1994-11-11
|
3KB
|
209 lines
/*
* password file routines
*
* 90/04/18 StG initial version on OSK v4.0 (taken from v3 getpw())
* 92/10/19 StG rewritten for semi unix compatible
*/
#include "stglib.h"
/* unix already has these functions */
#ifndef _UNIX
#include "pwd.h"
#include "stglib.h"
#define PWDBUF 256
int hPWD=0; /* file number */
struct passwd _pwd;
char pwd_buf[PWDBUF];
/*
* microware has a bug in login - if no . in uid, takes lsw from priority
*
* this function is designed to duplicate the bug
*/
unsigned long
cvt_uid(s)
char *s;
{
unsigned long n;
n=atoi(s);
n<<=16;
while (*s && *s!='.' && *s!=',') s++;
n|=atoi(++s);
return(n);
}
void
unpad(s)
char *s;
{
if (!*s)
return;
while (*s) s++;
s--;
while (*s && *s==' ') *s--=0;
}
setpwent()
{
int uid;
if (hPWD)
{
lseek(hPWD,0L,0);
return(0);
}
uid=getuid();
setuid(0);
hPWD=open(PWD_FILE,O_RDONLY);
if (hPWD==ERR)
{
syslog(LOG_EMERG,"setpwent: cant access %s %m",PWD_FILE);
hPWD=0;
setuid(uid);
return(ERR);
}
setuid(uid);
return(0);
}
endpwent()
{
if (hPWD)
{
close(hPWD);
hPWD=0;
}
return(0);
}
struct passwd *
getpwent()
{
char *p;
int n;
if (!hPWD)
if (setpwent()==ERR)
return(0);
nextline:
n=readln(hPWD,pwd_buf,PWDBUF);
if (n<=0)
return(0);
pwd_buf[n-1]=0;
if (!*pwd_buf || *pwd_buf=='*')
goto nextline;
_pwd.pw_passwd=_pwd.pw_xdir=_pwd.pw_dir=_pwd.pw_shell=_pwd.pw_gecos=0;
_pwd.pw_uid=0;
_pwd.pw_name=pwd_buf;
p=strcut(pwd_buf,',');
if (!p)
goto nextline;
_pwd.pw_passwd=p;
p=strcut(p,',');
if (!p)
goto invalid;
_pwd.pw_uid=cvt_uid(p);
p=strcut(p,',');
if (!p)
goto invalid;
_pwd.pw_pri=atoi(p);
p=strcut(p,',');
if (!p)
goto invalid;
_pwd.pw_xdir=p;
p=strcut(p,',');
if (!p)
goto invalid;
_pwd.pw_dir=p;
p=strcut(p,',');
if (!p)
goto invalid;
_pwd.pw_shell=p;
p=strcut(p,',');
if (p)
_pwd.pw_gecos=p;
unpad(_pwd.pw_name);
unpad(_pwd.pw_passwd);
return(&_pwd);
invalid:
syslog(LOG_ERR,"getpwent: invalid line in password file");
goto nextline;
}
struct passwd *
getpwnam(name)
char *name;
{
struct passwd *pw;
if (_pwd.pw_name && !stricmp(_pwd.pw_name,name))
return(&_pwd);
setpwent();
do
{
pw=getpwent();
if (!pw)
{
/* endpwent(); */
return(0);
}
}
while (stricmp(pw->pw_name,name));
/* endpwent(); */
return(pw);
}
struct passwd *
getpwuid(uid)
long uid;
{
struct passwd *pw;
if (_pwd.pw_name && _pwd.pw_uid==uid)
return(&_pwd);
setpwent();
do
{
pw=getpwent();
if (!pw)
{
/* endpwent(); */
return(0);
}
}
while (pw->pw_uid!=uid);
/* endpwent(); */
return(pw);
}
#endif