1 | /* userspec.c -- Parse a user and group string.
|
---|
2 | Copyright (C) 1989-1992, 2001, 2004-2005, 2007, 2010, 2014-2015 Free
|
---|
3 | Software Foundation, Inc.
|
---|
4 |
|
---|
5 | This program is free software; you can redistribute it and/or modify
|
---|
6 | it under the terms of the GNU General Public License as published by
|
---|
7 | the Free Software Foundation; either version 3, or (at your option)
|
---|
8 | any later version.
|
---|
9 |
|
---|
10 | This program is distributed in the hope that it will be useful,
|
---|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | GNU General Public License for more details.
|
---|
14 |
|
---|
15 | You should have received a copy of the GNU General Public
|
---|
16 | License along with this program; if not, write to the Free
|
---|
17 | Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
---|
18 | Boston, MA 02110-1301 USA. */
|
---|
19 |
|
---|
20 | /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
|
---|
21 | |
---|
22 |
|
---|
23 | #include <system.h>
|
---|
24 | #include <alloca.h>
|
---|
25 | #include <stdio.h>
|
---|
26 | #include <ctype.h>
|
---|
27 | #include <sys/types.h>
|
---|
28 |
|
---|
29 | #ifndef HAVE_ENDPWENT
|
---|
30 | # define endpwent()
|
---|
31 | #endif
|
---|
32 | #ifndef HAVE_ENDGRENT
|
---|
33 | # define endgrent()
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | /* Perform the equivalent of the statement `dest = strdup (src);',
|
---|
37 | but obtaining storage via alloca instead of from the heap. */
|
---|
38 |
|
---|
39 | #define V_STRDUP(dest, src) \
|
---|
40 | do \
|
---|
41 | { \
|
---|
42 | int _len = strlen ((src)); \
|
---|
43 | (dest) = (char *) alloca (_len + 1); \
|
---|
44 | strcpy (dest, src); \
|
---|
45 | } \
|
---|
46 | while (0)
|
---|
47 |
|
---|
48 | /* Return nonzero if STR represents an unsigned decimal integer,
|
---|
49 | otherwise return 0. */
|
---|
50 |
|
---|
51 | static int
|
---|
52 | isnumber_p (const char *str)
|
---|
53 | {
|
---|
54 | for (; *str; str++)
|
---|
55 | if (!isdigit (*str))
|
---|
56 | return 0;
|
---|
57 | return 1;
|
---|
58 | }
|
---|
59 |
|
---|
60 | /* Extract from NAME, which has the form "[user][:.][group]",
|
---|
61 | a USERNAME, UID U, GROUPNAME, and GID G.
|
---|
62 | Either user or group, or both, must be present.
|
---|
63 | If the group is omitted but the ":" or "." separator is given,
|
---|
64 | use the given user's login group.
|
---|
65 |
|
---|
66 | USERNAME and GROUPNAME will be in newly malloc'd memory.
|
---|
67 | Either one might be NULL instead, indicating that it was not
|
---|
68 | given and the corresponding numeric ID was left unchanged.
|
---|
69 |
|
---|
70 | Return NULL if successful, a static error message string if not. */
|
---|
71 |
|
---|
72 | const char *
|
---|
73 | parse_user_spec (const char *spec_arg, uid_t *uid, gid_t *gid,
|
---|
74 | char **username_arg, char **groupname_arg)
|
---|
75 | {
|
---|
76 | static const char *tired = "virtual memory exhausted";
|
---|
77 | const char *error_msg;
|
---|
78 | char *spec; /* A copy we can write on. */
|
---|
79 | struct passwd *pwd;
|
---|
80 | struct group *grp;
|
---|
81 | char *g, *u, *separator;
|
---|
82 | char *groupname;
|
---|
83 |
|
---|
84 | error_msg = NULL;
|
---|
85 | *username_arg = *groupname_arg = NULL;
|
---|
86 | groupname = NULL;
|
---|
87 |
|
---|
88 | V_STRDUP (spec, spec_arg);
|
---|
89 |
|
---|
90 | /* Find the separator if there is one. */
|
---|
91 | separator = strchr (spec, ':');
|
---|
92 | if (separator == NULL)
|
---|
93 | separator = strchr (spec, '.');
|
---|
94 |
|
---|
95 | /* Replace separator with a NUL. */
|
---|
96 | if (separator != NULL)
|
---|
97 | *separator = '\0';
|
---|
98 |
|
---|
99 | /* Set U and G to non-zero length strings corresponding to user and
|
---|
100 | group specifiers or to NULL. */
|
---|
101 | u = (*spec == '\0' ? NULL : spec);
|
---|
102 |
|
---|
103 | g = (separator == NULL || *(separator + 1) == '\0'
|
---|
104 | ? NULL
|
---|
105 | : separator + 1);
|
---|
106 |
|
---|
107 | if (u == NULL && g == NULL)
|
---|
108 | return "can not omit both user and group";
|
---|
109 |
|
---|
110 | if (u != NULL)
|
---|
111 | {
|
---|
112 | if (*u == '+')
|
---|
113 | {
|
---|
114 | pwd = NULL;
|
---|
115 | ++u;
|
---|
116 | }
|
---|
117 | else
|
---|
118 | pwd = getpwnam (u);
|
---|
119 |
|
---|
120 | if (pwd == NULL)
|
---|
121 | {
|
---|
122 | if (!isnumber_p (u))
|
---|
123 | error_msg = _("invalid user");
|
---|
124 | else
|
---|
125 | {
|
---|
126 | int use_login_group;
|
---|
127 | use_login_group = (separator != NULL && g == NULL);
|
---|
128 | if (use_login_group)
|
---|
129 | error_msg = _("cannot get the login group of a numeric UID");
|
---|
130 | else
|
---|
131 | *uid = atoi (u);
|
---|
132 | }
|
---|
133 | }
|
---|
134 | else
|
---|
135 | {
|
---|
136 | *uid = pwd->pw_uid;
|
---|
137 | if (g == NULL && separator != NULL)
|
---|
138 | {
|
---|
139 | /* A separator was given, but a group was not specified,
|
---|
140 | so get the login group. */
|
---|
141 | *gid = pwd->pw_gid;
|
---|
142 | grp = getgrgid (pwd->pw_gid);
|
---|
143 | if (grp == NULL)
|
---|
144 | {
|
---|
145 | /* This is enough room to hold the unsigned decimal
|
---|
146 | representation of any 32-bit quantity and the trailing
|
---|
147 | zero byte. */
|
---|
148 | char uint_buf[21];
|
---|
149 | sprintf (uint_buf, "%u", (unsigned) (pwd->pw_gid));
|
---|
150 | V_STRDUP (groupname, uint_buf);
|
---|
151 | }
|
---|
152 | else
|
---|
153 | {
|
---|
154 | V_STRDUP (groupname, grp->gr_name);
|
---|
155 | }
|
---|
156 | endgrent ();
|
---|
157 | }
|
---|
158 | }
|
---|
159 | endpwent ();
|
---|
160 | }
|
---|
161 |
|
---|
162 | if (g != NULL && error_msg == NULL)
|
---|
163 | {
|
---|
164 | /* Explicit group. */
|
---|
165 | if (*g == '+')
|
---|
166 | {
|
---|
167 | grp = NULL;
|
---|
168 | ++g;
|
---|
169 | }
|
---|
170 | else
|
---|
171 | grp = getgrnam (g);
|
---|
172 |
|
---|
173 | if (grp == NULL)
|
---|
174 | {
|
---|
175 | if (!isnumber_p (g))
|
---|
176 | error_msg = _("invalid group");
|
---|
177 | else
|
---|
178 | *gid = atoi (g);
|
---|
179 | }
|
---|
180 | else
|
---|
181 | *gid = grp->gr_gid;
|
---|
182 | endgrent (); /* Save a file descriptor. */
|
---|
183 |
|
---|
184 | if (error_msg == NULL)
|
---|
185 | V_STRDUP (groupname, g);
|
---|
186 | }
|
---|
187 |
|
---|
188 | if (error_msg == NULL)
|
---|
189 | {
|
---|
190 | if (u != NULL)
|
---|
191 | {
|
---|
192 | *username_arg = strdup (u);
|
---|
193 | if (*username_arg == NULL)
|
---|
194 | error_msg = tired;
|
---|
195 | }
|
---|
196 |
|
---|
197 | if (groupname != NULL && error_msg == NULL)
|
---|
198 | {
|
---|
199 | *groupname_arg = strdup (groupname);
|
---|
200 | if (*groupname_arg == NULL)
|
---|
201 | {
|
---|
202 | if (*username_arg != NULL)
|
---|
203 | {
|
---|
204 | free (*username_arg);
|
---|
205 | *username_arg = NULL;
|
---|
206 | }
|
---|
207 | error_msg = tired;
|
---|
208 | }
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | return error_msg;
|
---|
213 | }
|
---|
214 |
|
---|
215 | #ifdef TEST
|
---|
216 |
|
---|
217 | #define NULL_CHECK(s) ((s) == NULL ? "(null)" : (s))
|
---|
218 |
|
---|
219 | int
|
---|
220 | main (int argc, char **argv)
|
---|
221 | {
|
---|
222 | int i;
|
---|
223 |
|
---|
224 | for (i = 1; i < argc; i++)
|
---|
225 | {
|
---|
226 | const char *e;
|
---|
227 | char *username, *groupname;
|
---|
228 | uid_t uid;
|
---|
229 | gid_t gid;
|
---|
230 | char *tmp;
|
---|
231 |
|
---|
232 | tmp = strdup (argv[i]);
|
---|
233 | e = parse_user_spec (tmp, &uid, &gid, &username, &groupname);
|
---|
234 | free (tmp);
|
---|
235 | printf ("%s: %u %u %s %s %s\n",
|
---|
236 | argv[i],
|
---|
237 | (unsigned int) uid,
|
---|
238 | (unsigned int) gid,
|
---|
239 | NULL_CHECK (username),
|
---|
240 | NULL_CHECK (groupname),
|
---|
241 | NULL_CHECK (e));
|
---|
242 | }
|
---|
243 |
|
---|
244 | exit (0);
|
---|
245 | }
|
---|
246 |
|
---|
247 | #endif
|
---|