1 /* userspec.c -- Parse a user and group string.
2 Copyright (C) 1989, 1990, 1991, 1992, 1997, 1998 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
25 # define alloca __builtin_alloca
39 #include <sys/types.h>
60 #ifndef _POSIX_VERSION
61 struct passwd *getpwnam ();
62 struct group *getgrnam ();
63 struct group *getgrgid ();
67 # define endgrent() ((void) 0)
71 # define endpwent() ((void) 0)
74 /* Perform the equivalent of the statement `dest = strdup (src);',
75 but obtaining storage via alloca instead of from the heap. */
77 #define V_STRDUP(dest, src) \
80 int _len = strlen ((src)); \
81 (dest) = (char *) alloca (_len + 1); \
86 #define isdigit(c) ((c) >= '0' && (c) <= '9')
92 /* Return nonzero if STR represents an unsigned decimal integer,
93 otherwise return 0. */
96 is_number (const char *str)
104 /* Extract from NAME, which has the form "[user][:.][group]",
105 a USERNAME, UID U, GROUPNAME, and GID G.
106 Either user or group, or both, must be present.
107 If the group is omitted but the ":" or "." separator is given,
108 use the given user's login group.
110 USERNAME and GROUPNAME will be in newly malloc'd memory.
111 Either one might be NULL instead, indicating that it was not
112 given and the corresponding numeric ID was left unchanged.
114 Return NULL if successful, a static error message string if not. */
117 parse_user_spec (const char *spec_arg, uid_t *uid, gid_t *gid, char **username_arg, char **groupname_arg)
119 static const char *tired = "virtual memory exhausted";
120 const char *error_msg;
121 char *spec; /* A copy we can write on. */
124 char *g, *u, *separator;
128 *username_arg = *groupname_arg = NULL;
131 V_STRDUP (spec, spec_arg);
133 /* Find the separator if there is one. */
134 separator = strchr (spec, ':');
135 if (separator == NULL)
136 separator = strchr (spec, '.');
138 /* Replace separator with a NUL. */
139 if (separator != NULL)
142 /* Set U and G to non-zero length strings corresponding to user and
143 group specifiers or to NULL. */
144 u = (*spec == '\0' ? NULL : spec);
146 g = (separator == NULL || *(separator + 1) == '\0'
150 if (u == NULL && g == NULL)
151 return "can not omit both user and group";
154 /* Pretend that we are the user U whose group is G. This makes
155 pwd and grp functions ``know'' about the UID and GID of these. */
156 if (u && !is_number (u))
157 setenv ("USER", u, 1);
158 if (g && !is_number (g))
159 setenv ("GROUP", g, 1);
169 error_msg = "invalid user";
173 use_login_group = (separator != NULL && g == NULL);
175 error_msg = "cannot get the login group of a numeric UID";
183 if (g == NULL && separator != NULL)
185 /* A separator was given, but a group was not specified,
186 so get the login group. */
188 grp = getgrgid (pwd->pw_gid);
191 /* This is enough room to hold the unsigned decimal
192 representation of any 32-bit quantity and the trailing
195 sprintf (uint_buf, "%u", (unsigned) (pwd->pw_gid));
196 V_STRDUP (groupname, uint_buf);
200 V_STRDUP (groupname, grp->gr_name);
208 if (g != NULL && error_msg == NULL)
210 /* Explicit group. */
215 error_msg = "invalid group";
221 endgrent (); /* Save a file descriptor. */
223 if (error_msg == NULL)
224 V_STRDUP (groupname, g);
227 if (error_msg == NULL)
231 *username_arg = strdup (u);
232 if (*username_arg == NULL)
236 if (groupname != NULL && error_msg == NULL)
238 *groupname_arg = strdup (groupname);
239 if (*groupname_arg == NULL)
241 if (*username_arg != NULL)
243 free (*username_arg);
244 *username_arg = NULL;
256 # define NULL_CHECK(s) ((s) == NULL ? "(null)" : (s))
259 main (int argc, char **argv)
263 for (i = 1; i < argc; i++)
266 char *username, *groupname;
271 tmp = strdup (argv[i]);
272 e = parse_user_spec (tmp, &uid, &gid, &username, &groupname);
274 printf ("%s: %u %u %s %s %s\n",
278 NULL_CHECK (username),
279 NULL_CHECK (groupname),