14887Schin /***********************************************************************
24887Schin * *
34887Schin * This software is part of the ast package *
4*12068SRoger.Faulkner@Oracle.COM * Copyright (c) 1985-2010 AT&T Intellectual Property *
54887Schin * and is licensed under the *
64887Schin * Common Public License, Version 1.0 *
78462SApril.Chin@Sun.COM * by AT&T Intellectual Property *
84887Schin * *
94887Schin * A copy of the License is available at *
104887Schin * http://www.opensource.org/licenses/cpl1.0.txt *
114887Schin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
124887Schin * *
134887Schin * Information and Software Systems Research *
144887Schin * AT&T Research *
154887Schin * Florham Park NJ *
164887Schin * *
174887Schin * Glenn Fowler <gsf@research.att.com> *
184887Schin * David Korn <dgk@research.att.com> *
194887Schin * Phong Vo <kpv@research.att.com> *
204887Schin * *
214887Schin ***********************************************************************/
224887Schin #pragma prototyped
234887Schin /*
244887Schin * Glenn Fowler
254887Schin * AT&T Bell Laboratories
264887Schin *
274887Schin * uid name -> number
284887Schin */
294887Schin
304887Schin #if defined(__STDPP__directive) && defined(__STDPP__hide)
314887Schin __STDPP__directive pragma pp:hide getpwnam getpwuid
324887Schin #else
334887Schin #define getpwnam ______getpwnam
344887Schin #define getpwuid ______getpwuid
354887Schin #endif
364887Schin
374887Schin #include <ast.h>
384887Schin #include <cdt.h>
394887Schin #include <pwd.h>
404887Schin
414887Schin #if defined(__STDPP__directive) && defined(__STDPP__hide)
424887Schin __STDPP__directive pragma pp:nohide getpwnam getpwuid
434887Schin #else
444887Schin #undef getpwnam
454887Schin #undef getpwuid
464887Schin #endif
474887Schin
484887Schin extern struct passwd* getpwnam(const char*);
494887Schin extern struct passwd* getpwuid(uid_t);
504887Schin
514887Schin typedef struct Id_s
524887Schin {
534887Schin Dtlink_t link;
544887Schin int id;
554887Schin char name[1];
564887Schin } Id_t;
574887Schin
584887Schin /*
594887Schin * return uid number given uid name
604887Schin * -1 on first error for a given name
614887Schin * -2 on subsequent errors for a given name
624887Schin */
634887Schin
644887Schin int
struid(const char * name)654887Schin struid(const char* name)
664887Schin {
674887Schin register Id_t* ip;
684887Schin register struct passwd* pw;
694887Schin int id;
704887Schin char* e;
714887Schin
724887Schin static Dt_t* dict;
734887Schin static Dtdisc_t disc;
744887Schin
754887Schin if (!dict)
764887Schin {
774887Schin disc.key = offsetof(Id_t, name);
784887Schin dict = dtopen(&disc, Dthash);
794887Schin }
804887Schin else if (ip = (Id_t*)dtmatch(dict, name))
814887Schin return ip->id;
824887Schin if (pw = getpwnam(name))
834887Schin id = pw->pw_uid;
844887Schin else
854887Schin {
864887Schin id = strtol(name, &e, 0);
874887Schin #if _WINIX
884887Schin if (!*e)
894887Schin {
904887Schin if (!getpwuid(id))
914887Schin id = -1;
924887Schin }
934887Schin else if (streq(name, "root") && (pw = getpwnam("Administrator")))
944887Schin id = pw->pw_uid;
954887Schin else
964887Schin id = -1;
974887Schin #else
984887Schin if (*e || !getpwuid(id))
994887Schin id = -1;
1004887Schin #endif
1014887Schin }
1024887Schin if (dict && (ip = newof(0, Id_t, 1, strlen(name))))
1034887Schin {
1044887Schin strcpy(ip->name, name);
1054887Schin ip->id = id >= 0 ? id : -2;
1064887Schin dtinsert(dict, ip);
1074887Schin }
1084887Schin return id;
1094887Schin }
110