1*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gate /*
4*0Sstevel@tonic-gate * Replacement for getpwnam - we need it to handle files other than
5*0Sstevel@tonic-gate * /etc/passwd so we can permit different passwd files for each different
6*0Sstevel@tonic-gate * host
7*0Sstevel@tonic-gate * (c) 1998-2000 by Bernhard Rosenkr�nzer <bero@redhat.com>
8*0Sstevel@tonic-gate * 19980930 Initial version
9*0Sstevel@tonic-gate * 20000211 Various fixes
10*0Sstevel@tonic-gate */
11*0Sstevel@tonic-gate
12*0Sstevel@tonic-gate #include "config.h"
13*0Sstevel@tonic-gate #include <pwd.h>
14*0Sstevel@tonic-gate #include <sys/types.h>
15*0Sstevel@tonic-gate #include <stdio.h>
16*0Sstevel@tonic-gate #ifdef SHADOW_PASSWORD
17*0Sstevel@tonic-gate # ifdef HAVE_SHADOW_H
18*0Sstevel@tonic-gate # include <shadow.h>
19*0Sstevel@tonic-gate # endif
20*0Sstevel@tonic-gate #endif
21*0Sstevel@tonic-gate
22*0Sstevel@tonic-gate #ifndef HAVE_FGETPWENT /* Some systems (*BSD) don't have fgetpwent... */
23*0Sstevel@tonic-gate #ifdef HAVE_STRINGS_H
24*0Sstevel@tonic-gate #include <strings.h>
25*0Sstevel@tonic-gate #else
26*0Sstevel@tonic-gate #include <string.h>
27*0Sstevel@tonic-gate #endif
fgetpwent(FILE * stream)28*0Sstevel@tonic-gate struct passwd *fgetpwent(FILE *stream)
29*0Sstevel@tonic-gate {
30*0Sstevel@tonic-gate char *entry=(char *) malloc(1024);
31*0Sstevel@tonic-gate struct passwd *p=(struct passwd *) malloc(sizeof(struct passwd));
32*0Sstevel@tonic-gate char *tmp,*tmp2;
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gate if(!fgets(entry,1024,stream)) {
35*0Sstevel@tonic-gate free(entry);
36*0Sstevel@tonic-gate free(p);
37*0Sstevel@tonic-gate return NULL;
38*0Sstevel@tonic-gate }
39*0Sstevel@tonic-gate tmp=strdup(entry);
40*0Sstevel@tonic-gate if(strchr(tmp,':')) {
41*0Sstevel@tonic-gate *strchr(tmp,':')=0;
42*0Sstevel@tonic-gate p->pw_name=tmp;
43*0Sstevel@tonic-gate } else {
44*0Sstevel@tonic-gate free(tmp); free(entry); free(p); return NULL;
45*0Sstevel@tonic-gate }
46*0Sstevel@tonic-gate tmp2=strchr(entry,':')+1;
47*0Sstevel@tonic-gate tmp=strdup(tmp2);
48*0Sstevel@tonic-gate if(strchr(tmp,':')) {
49*0Sstevel@tonic-gate *strchr(tmp,':')=0;
50*0Sstevel@tonic-gate p->pw_passwd=tmp;
51*0Sstevel@tonic-gate } else {
52*0Sstevel@tonic-gate free(tmp); free(entry); free(p->pw_name); free(p); return NULL;
53*0Sstevel@tonic-gate }
54*0Sstevel@tonic-gate tmp2=strchr(tmp2,':')+1;
55*0Sstevel@tonic-gate tmp=strdup(tmp2);
56*0Sstevel@tonic-gate if(strchr(tmp,':')) {
57*0Sstevel@tonic-gate *strchr(tmp,':')=0;
58*0Sstevel@tonic-gate p->pw_uid=(uid_t) atoi(tmp);
59*0Sstevel@tonic-gate } else {
60*0Sstevel@tonic-gate free(tmp); free(entry); free(p->pw_passwd); free(p->pw_name); free(p); return NULL;
61*0Sstevel@tonic-gate }
62*0Sstevel@tonic-gate free(tmp);
63*0Sstevel@tonic-gate tmp2=strchr(tmp2,':')+1;
64*0Sstevel@tonic-gate tmp=strdup(tmp2);
65*0Sstevel@tonic-gate if(strchr(tmp,':')) {
66*0Sstevel@tonic-gate *strchr(tmp,':')=0;
67*0Sstevel@tonic-gate p->pw_gid=(gid_t) atoi(tmp);
68*0Sstevel@tonic-gate } else {
69*0Sstevel@tonic-gate free(tmp); free(entry); free(p->pw_passwd); free(p->pw_name); free(p); return NULL;
70*0Sstevel@tonic-gate }
71*0Sstevel@tonic-gate free(tmp);
72*0Sstevel@tonic-gate tmp2=strchr(tmp2,':')+1;
73*0Sstevel@tonic-gate tmp=strdup(tmp2);
74*0Sstevel@tonic-gate if(strchr(tmp,':')) {
75*0Sstevel@tonic-gate *strchr(tmp,':')=0;
76*0Sstevel@tonic-gate p->pw_gecos=tmp;
77*0Sstevel@tonic-gate } else {
78*0Sstevel@tonic-gate free(tmp); free(entry); free(p->pw_passwd); free(p->pw_name); free(p); return NULL;
79*0Sstevel@tonic-gate }
80*0Sstevel@tonic-gate tmp2=strchr(tmp2,':')+1;
81*0Sstevel@tonic-gate tmp=strdup(tmp2);
82*0Sstevel@tonic-gate if(strchr(tmp,':')) {
83*0Sstevel@tonic-gate *strchr(tmp,':')=0;
84*0Sstevel@tonic-gate p->pw_dir=tmp;
85*0Sstevel@tonic-gate } else {
86*0Sstevel@tonic-gate free(tmp); free(entry); free(p->pw_gecos); free(p->pw_passwd); free(p->pw_name); free(p); return NULL;
87*0Sstevel@tonic-gate }
88*0Sstevel@tonic-gate tmp2=strchr(tmp2,':')+1;
89*0Sstevel@tonic-gate if(strchr(tmp2,':')) {
90*0Sstevel@tonic-gate free(entry); free(p->pw_dir); free(p->pw_gecos); free(p->pw_passwd); free(p->pw_name); free(p); return NULL;
91*0Sstevel@tonic-gate }
92*0Sstevel@tonic-gate while(strlen(tmp2) && isspace(tmp2[strlen(tmp2)-1]))
93*0Sstevel@tonic-gate tmp2[strlen(tmp2)-1]=0;
94*0Sstevel@tonic-gate p->pw_shell=strdup(tmp2);
95*0Sstevel@tonic-gate free(entry);
96*0Sstevel@tonic-gate return p;
97*0Sstevel@tonic-gate }
98*0Sstevel@tonic-gate #endif
99*0Sstevel@tonic-gate
100*0Sstevel@tonic-gate
bero_getpwnam(const char * name,const char * file)101*0Sstevel@tonic-gate struct passwd *bero_getpwnam(const char * name, const char * file)
102*0Sstevel@tonic-gate {
103*0Sstevel@tonic-gate FILE *f;
104*0Sstevel@tonic-gate struct passwd *p;
105*0Sstevel@tonic-gate struct passwd *r;
106*0Sstevel@tonic-gate
107*0Sstevel@tonic-gate if (!strcmp(file,"/etc/passwd"))
108*0Sstevel@tonic-gate return (getpwnam(name));
109*0Sstevel@tonic-gate f=fopen(file,"r");
110*0Sstevel@tonic-gate if(f==NULL)
111*0Sstevel@tonic-gate return NULL;
112*0Sstevel@tonic-gate p=NULL;
113*0Sstevel@tonic-gate r=NULL;
114*0Sstevel@tonic-gate while((r==NULL) && (p=fgetpwent(f)))
115*0Sstevel@tonic-gate if(!strcasecmp(p->pw_name,name))
116*0Sstevel@tonic-gate r=p;
117*0Sstevel@tonic-gate fclose(f);
118*0Sstevel@tonic-gate return r;
119*0Sstevel@tonic-gate }
120*0Sstevel@tonic-gate
bero_getpwuid(uid_t uid,const char * file)121*0Sstevel@tonic-gate struct passwd *bero_getpwuid(uid_t uid, const char * file)
122*0Sstevel@tonic-gate {
123*0Sstevel@tonic-gate FILE *f;
124*0Sstevel@tonic-gate struct passwd *p;
125*0Sstevel@tonic-gate struct passwd *r;
126*0Sstevel@tonic-gate
127*0Sstevel@tonic-gate if (!strcmp(file,"/etc/passwd"))
128*0Sstevel@tonic-gate return getpwuid(uid);
129*0Sstevel@tonic-gate f=fopen(file,"r");
130*0Sstevel@tonic-gate if(f==NULL)
131*0Sstevel@tonic-gate return NULL;
132*0Sstevel@tonic-gate p=NULL;
133*0Sstevel@tonic-gate r=NULL;
134*0Sstevel@tonic-gate while((r==NULL) && (p=fgetpwent(f)))
135*0Sstevel@tonic-gate if(p->pw_uid==uid)
136*0Sstevel@tonic-gate r=p;
137*0Sstevel@tonic-gate fclose(f);
138*0Sstevel@tonic-gate return r;
139*0Sstevel@tonic-gate }
140*0Sstevel@tonic-gate
141*0Sstevel@tonic-gate #ifdef SHADOW_PASSWORD
bero_getspnam(const char * name,const char * file)142*0Sstevel@tonic-gate struct spwd *bero_getspnam(const char * name, const char * file)
143*0Sstevel@tonic-gate {
144*0Sstevel@tonic-gate FILE *f;
145*0Sstevel@tonic-gate struct spwd *s;
146*0Sstevel@tonic-gate struct spwd *r;
147*0Sstevel@tonic-gate f=fopen(file,"r");
148*0Sstevel@tonic-gate if(f==NULL)
149*0Sstevel@tonic-gate return NULL;
150*0Sstevel@tonic-gate s=NULL;
151*0Sstevel@tonic-gate r=NULL;
152*0Sstevel@tonic-gate while((r==NULL) && (s=fgetspent(f)))
153*0Sstevel@tonic-gate if(!strcasecmp(s->sp_namp,name))
154*0Sstevel@tonic-gate r=s;
155*0Sstevel@tonic-gate fclose(f);
156*0Sstevel@tonic-gate return r;
157*0Sstevel@tonic-gate }
158*0Sstevel@tonic-gate #endif
159