xref: /titanic_41/usr/src/lib/libast/common/string/struid.c (revision fbe82215144da71ed02c3a920667472cc567fafd)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 /*
24  * Glenn Fowler
25  * AT&T Bell Laboratories
26  *
27  * uid name -> number
28  */
29 
30 #if defined(__STDPP__directive) && defined(__STDPP__hide)
31 __STDPP__directive pragma pp:hide getpwnam getpwuid
32 #else
33 #define getpwnam	______getpwnam
34 #define getpwuid	______getpwuid
35 #endif
36 
37 #include <ast.h>
38 #include <cdt.h>
39 #include <pwd.h>
40 
41 #if defined(__STDPP__directive) && defined(__STDPP__hide)
42 __STDPP__directive pragma pp:nohide getpwnam getpwuid
43 #else
44 #undef	getpwnam
45 #undef	getpwuid
46 #endif
47 
48 extern struct passwd*	getpwnam(const char*);
49 extern struct passwd*	getpwuid(uid_t);
50 
51 typedef struct Id_s
52 {
53 	Dtlink_t	link;
54 	int		id;
55 	char		name[1];
56 } Id_t;
57 
58 /*
59  * return uid number given uid name
60  * -1 on first error for a given name
61  * -2 on subsequent errors for a given name
62  */
63 
64 int
65 struid(const char* name)
66 {
67 	register Id_t*		ip;
68 	register struct passwd*	pw;
69 	int			id;
70 	char*			e;
71 
72 	static Dt_t*		dict;
73 	static Dtdisc_t		disc;
74 
75 	if (!dict)
76 	{
77 		disc.key = offsetof(Id_t, name);
78 		dict = dtopen(&disc, Dthash);
79 	}
80 	else if (ip = (Id_t*)dtmatch(dict, name))
81 		return ip->id;
82 	if (pw = getpwnam(name))
83 		id = pw->pw_uid;
84 	else
85 	{
86 		id = strtol(name, &e, 0);
87 #if _WINIX
88 		if (!*e)
89 		{
90 			if (!getpwuid(id))
91 				id = -1;
92 		}
93 		else if (streq(name, "root") && (pw = getpwnam("Administrator")))
94 			id = pw->pw_uid;
95 		else
96 			id = -1;
97 #else
98 		if (*e || !getpwuid(id))
99 			id = -1;
100 #endif
101 	}
102 	if (dict && (ip = newof(0, Id_t, 1, strlen(name))))
103 	{
104 		strcpy(ip->name, name);
105 		ip->id = id >= 0 ? id : -2;
106 		dtinsert(dict, ip);
107 	}
108 	return id;
109 }
110