3 /* userspec.c -- Parse a user and group string.
4 Copyright (C) 1989, 1990, 1991, 1992, 2001,
5 2004, 2005 Free Software Foundation, Inc.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public
18 License along with this program; if not, write to the Free
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301 USA. */
22 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
27 #define alloca __builtin_alloca
42 #include <sys/types.h>
46 #if !HAVE_DECL_GETPWNAM
47 extern struct passwd *getpwnam (const char *name);
49 #if !HAVE_DECL_GETGRNAM
50 extern struct group *getgrnam (const char *name);
52 #if !HAVE_DECL_GETGRGID
53 extern struct group *getgrgid (gid_t gid);
63 /* Perform the equivalent of the statement `dest = strdup (src);',
64 but obtaining storage via alloca instead of from the heap. */
66 #define V_STRDUP(dest, src) \
69 int _len = strlen ((src)); \
70 (dest) = (char *) alloca (_len + 1); \
75 /* Return nonzero if STR represents an unsigned decimal integer,
76 otherwise return 0. */
79 isnumber_p (const char *str)
87 /* Extract from NAME, which has the form "[user][:.][group]",
88 a USERNAME, UID U, GROUPNAME, and GID G.
89 Either user or group, or both, must be present.
90 If the group is omitted but the ":" or "." separator is given,
91 use the given user's login group.
93 USERNAME and GROUPNAME will be in newly malloc'd memory.
94 Either one might be NULL instead, indicating that it was not
95 given and the corresponding numeric ID was left unchanged.
97 Return NULL if successful, a static error message string if not. */
100 parse_user_spec (const char *spec_arg, uid_t *uid, gid_t *gid,
101 char **username_arg, char **groupname_arg)
103 static const char *tired = "virtual memory exhausted";
104 const char *error_msg;
105 char *spec; /* A copy we can write on. */
108 char *g, *u, *separator;
112 *username_arg = *groupname_arg = NULL;
115 V_STRDUP (spec, spec_arg);
117 /* Find the separator if there is one. */
118 separator = strchr (spec, ':');
119 if (separator == NULL)
120 separator = strchr (spec, '.');
122 /* Replace separator with a NUL. */
123 if (separator != NULL)
126 /* Set U and G to non-zero length strings corresponding to user and
127 group specifiers or to NULL. */
128 u = (*spec == '\0' ? NULL : spec);
130 g = (separator == NULL || *(separator + 1) == '\0'
134 if (u == NULL && g == NULL)
135 return "can not omit both user and group";
144 error_msg = _("invalid user");
148 use_login_group = (separator != NULL && g == NULL);
150 error_msg = _("cannot get the login group of a numeric UID");
158 if (g == NULL && separator != NULL)
160 /* A separator was given, but a group was not specified,
161 so get the login group. */
163 grp = getgrgid (pwd->pw_gid);
166 /* This is enough room to hold the unsigned decimal
167 representation of any 32-bit quantity and the trailing
170 sprintf (uint_buf, "%u", (unsigned) (pwd->pw_gid));
171 V_STRDUP (groupname, uint_buf);
175 V_STRDUP (groupname, grp->gr_name);
183 if (g != NULL && error_msg == NULL)
185 /* Explicit group. */
190 error_msg = _("invalid group");
196 endgrent (); /* Save a file descriptor. */
198 if (error_msg == NULL)
199 V_STRDUP (groupname, g);
202 if (error_msg == NULL)
206 *username_arg = strdup (u);
207 if (*username_arg == NULL)
211 if (groupname != NULL && error_msg == NULL)
213 *groupname_arg = strdup (groupname);
214 if (*groupname_arg == NULL)
216 if (*username_arg != NULL)
218 free (*username_arg);
219 *username_arg = NULL;
231 #define NULL_CHECK(s) ((s) == NULL ? "(null)" : (s))
234 main (int argc, char **argv)
238 for (i = 1; i < argc; i++)
241 char *username, *groupname;
246 tmp = strdup (argv[i]);
247 e = parse_user_spec (tmp, &uid, &gid, &username, &groupname);
249 printf ("%s: %u %u %s %s %s\n",
253 NULL_CHECK (username),
254 NULL_CHECK (groupname),