xref: /titanic_51/usr/src/lib/libast/common/string/strgid.c (revision 24fe0b3bf671e123467ce1df0b67cadd3614c8e4)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2008 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  * gid name -> number
28  */
29 
30 #if defined(__STDPP__directive) && defined(__STDPP__hide)
31 __STDPP__directive pragma pp:hide getgrgid getgrnam getpwnam
32 #else
33 #define getgrgid	______getgrgid
34 #define getgrnam	______getgrnam
35 #define getpwnam	______getpwnam
36 #endif
37 
38 #include <ast.h>
39 #include <cdt.h>
40 #include <pwd.h>
41 #include <grp.h>
42 
43 #if defined(__STDPP__directive) && defined(__STDPP__hide)
44 __STDPP__directive pragma pp:nohide getgrgid getgrnam getpwnam
45 #else
46 #undef	getgrgid
47 #undef	getgrnam
48 #undef	getpwnam
49 #endif
50 
51 extern struct group*	getgrgid(gid_t);
52 extern struct group*	getgrnam(const char*);
53 extern struct passwd*	getpwnam(const char*);
54 
55 typedef struct Id_s
56 {
57 	Dtlink_t	link;
58 	int		id;
59 	char		name[1];
60 } Id_t;
61 
62 /*
63  * return gid number given gid/uid name
64  * gid attempted first, then uid->pw_gid
65  * -1 on first error for a given name
66  * -2 on subsequent errors for a given name
67  */
68 
69 int
70 strgid(const char* name)
71 {
72 	register Id_t*		ip;
73 	register struct group*	gr;
74 	register struct passwd*	pw;
75 	int			id;
76 	char*			e;
77 
78 	static Dt_t*		dict;
79 	static Dtdisc_t		disc;
80 
81 	if (!dict)
82 	{
83 		disc.key = offsetof(Id_t, name);
84 		dict = dtopen(&disc, Dthash);
85 	}
86 	else if (ip = (Id_t*)dtmatch(dict, name))
87 		return ip->id;
88 	if (gr = getgrnam(name))
89 		id = gr->gr_gid;
90 	else if (pw = getpwnam(name))
91 		id = pw->pw_gid;
92 	else
93 	{
94 		id = strtol(name, &e, 0);
95 #if _WINIX
96 		if (!*e)
97 		{
98 			if (!getgrgid(id))
99 				id = -1;
100 		}
101 		else if (!streq(name, "sys"))
102 			id = -1;
103 		else if (gr = getgrnam("Administrators"))
104 			id = gr->gr_gid;
105 		else if (pw = getpwnam("Administrator"))
106 			id = pw->pw_gid;
107 		else
108 			id = -1;
109 #else
110 		if (*e || !getgrgid(id))
111 			id = -1;
112 #endif
113 	}
114 	if (dict && (ip = newof(0, Id_t, 1, strlen(name))))
115 	{
116 		strcpy(ip->name, name);
117 		ip->id = id >= 0 ? id : -2;
118 		dtinsert(dict, ip);
119 	}
120 	return id;
121 }
122