1 /* userspec.c -- Parse a user and group string.
2 Copyright (C) 1989, 1990, 1991, 1992 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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
21 #if defined (CONFIG_BROKETS)
22 /* We use <config.h> instead of "config.h" so that a compilation
23 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
24 (which it would do because it found this file in $srcdir). */
32 #include <sys/types.h>
36 #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
55 #ifndef _POSIX_VERSION
56 struct passwd
*getpwnam ();
57 struct group
*getgrnam ();
58 struct group
*getgrgid ();
66 #define isdigit(c) ((c) >= '0' && (c) <= '9')
69 static int isnumber ();
71 /* Extract from NAME, which has the form "[user][:.][group]",
72 a USERNAME, UID U, GROUPNAME, and GID G.
73 Either user or group, or both, must be present.
74 If the group is omitted but the ":" or "." separator is given,
75 use the given user's login group.
77 USERNAME and GROUPNAME will be in newly malloc'd memory.
78 Either one might be NULL instead, indicating that it was not
79 given and the corresponding numeric ID was left unchanged.
80 Might write NULs into NAME.
82 Return NULL if successful, a static error message string if not. */
85 parse_user_spec (name
, uid
, gid
, username
, groupname
)
89 char **username
, **groupname
;
91 static char *tired
= "virtual memory exhausted";
95 int use_login_group
= 0;
97 *username
= *groupname
= NULL
;
99 /* Check whether a group is given. */
100 cp
= index (name
, ':');
102 cp
= index (name
, '.');
109 /* Neither user nor group given, just "." or ":". */
110 return "can not omit both user and group";
117 /* Explicit group. */
118 *groupname
= strdup (cp
);
119 if (*groupname
== NULL
)
125 return "invalid group";
130 endgrent (); /* Save a file descriptor. */
134 /* Parse the user name, now that any group has been removed. */
137 /* No user name was given, just a group. */
140 *username
= strdup (name
);
141 if (*username
== NULL
)
144 pwd
= getpwnam (name
);
147 if (!isnumber (name
))
148 return "invalid user";
150 return "cannot get the login group of a numeric UID";
159 grp
= getgrgid (pwd
->pw_gid
);
162 *groupname
= malloc (15);
163 if (*groupname
== NULL
)
165 sprintf (*groupname
, "%u", pwd
->pw_gid
);
169 *groupname
= strdup (grp
->gr_name
);
170 if (*groupname
== NULL
)
180 /* Return nonzero if STR represents an unsigned decimal integer,
181 otherwise return 0. */