Mercurial > hg > octave-kai > gnulib-hg
annotate lib/userspec.c @ 7302:8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
longer worry about uses that don't define HAVE_CONFIG_H.
* acl.c, alloca.c, argmatch.c, atexit.c, backupfile.c:
* basename.c, c-stack.c, c-strtod.c, calloc.c, canon-host.c:
* canonicalize.c, chdir-long.c, chdir-safer.c, chown.c:
* cloexec.c, close-stream.c, closeout.c, creat-safer.c:
* cycle-check.c, diacrit.c, dirchownmod.c, dirfd.c, dirname.c:
* dup-safer.c, dup2.c, error.c, euidaccess.c, exclude.c:
* exitfail.c, fchmodat.c, fchown-stub.c, fd-safer.c:
* file-type.c, fileblocks.c, filemode.c, filenamecat.c:
* fnmatch.c, fopen-safer.c, fprintftime.c, free.c, fsusage.c:
* ftruncate.c, fts-cycle.c, fts.c, full-write.c, gai_strerror.c:
* getcwd.c, getdate.y, getdomainname.c, getgroups.c:
* gethostname.c, gethrxtime.c, getloadavg.c, getlogin_r.c:
* getndelim2.c, getnline.c, getopt.c, getopt1.c, getpass.c:
* gettime.c, gettimeofday.c, getugroups.c, getusershell.c:
* glob.c, group-member.c, hard-locale.c, hash-pjw.c, hash.c:
* human.c, idcache.c, inet_ntop.c, inet_pton.c, inttostr.c:
* isdir.c, lchown.c, linebuffer.c, long-options.c, lstat.c:
* malloc.c, md5.c, memcasecmp.c, memchr.c, memcmp.c, memcoll.c:
* memcpy.c, memmove.c, memrchr.c, mkancesdirs.c, mkdir-p.c:
* mkdir.c, mkdirat.c, mkstemp-safer.c, mkstemp.c, modechange.c:
* mountlist.c, nanosleep.c, obstack.c, open-safer.c:
* openat-die.c, openat.c, pagealign_alloc.c, physmem.c:
* pipe-safer.c, posixtm.c, posixver.c, putenv.c, quote.c:
* quotearg.c, raise.c, readtokens.c, readtokens0.c, readutmp.c:
* realloc.c, regex.c, rename.c, rmdir.c, rpmatch.c, safe-read.c:
* same.c, save-cwd.c, savedir.c, setenv.c, settime.c, sha1.c:
* sig2str.c, snprintf.c, strdup.c, strerror.c, strftime.c:
* stripslash.c, strndup.c, strnlen.c, strpbrk.c, strtod.c:
* strtoimax.c, strtol.c, strverscmp.c, tempname.c, time_r.c:
* timegm.c, tmpfile-safer.c, unlinkdir.c, userspec.c, utime.c:
* utimecmp.c, utimens.c, version-etc-fsf.c, version-etc.c:
* xalloc-die.c, xgetcwd.c, xgethostname.c, xmalloc.c:
* xmemcoll.c, xnanosleep.c, xreadlink.c, xstrtod.c:
* xstrtoimax.c, xstrtol.c, xstrtoumax.c, yesno.c:
Likewise.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Wed, 13 Sep 2006 22:38:14 +0000 (2006-09-13) |
parents | bbdf9204a185 |
children | 23cd146c58fd |
rev | line source |
---|---|
5 | 1 /* userspec.c -- Parse a user and group string. |
6969 | 2 Copyright (C) 1989-1992, 1997-1998, 2000, 2002-2006 Free Software |
4844 | 3 Foundation, Inc. |
5 | 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 2, 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 License | |
650
b4ef1c1a0171
update FSF address in copyright
Jim Meyering <jim@meyering.net>
parents:
499
diff
changeset
|
16 along with this program; if not, write to the Free Software Foundation, |
5848
a48fb0e98c8c
*** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents:
5691
diff
changeset
|
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
5 | 18 |
19 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */ | |
966 | 20 |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6969
diff
changeset
|
21 #include <config.h> |
105 | 22 |
4962
6bec4bfbd56e
Include strdup.h. From Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4844
diff
changeset
|
23 /* Specification. */ |
6bec4bfbd56e
Include strdup.h. From Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4844
diff
changeset
|
24 #include "userspec.h" |
6bec4bfbd56e
Include strdup.h. From Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4844
diff
changeset
|
25 |
5159 | 26 #include <stdbool.h> |
5 | 27 #include <stdio.h> |
28 #include <sys/types.h> | |
29 #include <pwd.h> | |
30 #include <grp.h> | |
31 | |
2854 | 32 #if HAVE_SYS_PARAM_H |
33 # include <sys/param.h> | |
34 #endif | |
35 | |
4333 | 36 #include <limits.h> |
4691 | 37 #include <stdlib.h> |
38 #include <string.h> | |
5 | 39 |
6275 | 40 #include <unistd.h> |
5 | 41 |
5691
ec62790f0938
Factor int-properties macros into a single file, except for
Paul Eggert <eggert@cs.ucla.edu>
parents:
5322
diff
changeset
|
42 #include "intprops.h" |
5159 | 43 #include "inttostr.h" |
4962
6bec4bfbd56e
Include strdup.h. From Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4844
diff
changeset
|
44 #include "strdup.h" |
2810
aff553209434
(same_name): Invoke xalloc_die instead of printing our own message.
Jim Meyering <jim@meyering.net>
parents:
2284
diff
changeset
|
45 #include "xalloc.h" |
2854 | 46 #include "xstrtol.h" |
2810
aff553209434
(same_name): Invoke xalloc_die instead of printing our own message.
Jim Meyering <jim@meyering.net>
parents:
2284
diff
changeset
|
47 |
3966
22d3032f0239
Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents:
3700
diff
changeset
|
48 #include "gettext.h" |
22d3032f0239
Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents:
3700
diff
changeset
|
49 #define _(msgid) gettext (msgid) |
22d3032f0239
Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents:
3700
diff
changeset
|
50 #define N_(msgid) msgid |
2273 | 51 |
499
62aa0b766a11
[HAVE_ENDGRENT]: Define away endgrent.
Jim Meyering <jim@meyering.net>
parents:
475
diff
changeset
|
52 #ifndef HAVE_ENDGRENT |
62aa0b766a11
[HAVE_ENDGRENT]: Define away endgrent.
Jim Meyering <jim@meyering.net>
parents:
475
diff
changeset
|
53 # define endgrent() ((void) 0) |
62aa0b766a11
[HAVE_ENDGRENT]: Define away endgrent.
Jim Meyering <jim@meyering.net>
parents:
475
diff
changeset
|
54 #endif |
62aa0b766a11
[HAVE_ENDGRENT]: Define away endgrent.
Jim Meyering <jim@meyering.net>
parents:
475
diff
changeset
|
55 |
62aa0b766a11
[HAVE_ENDGRENT]: Define away endgrent.
Jim Meyering <jim@meyering.net>
parents:
475
diff
changeset
|
56 #ifndef HAVE_ENDPWENT |
62aa0b766a11
[HAVE_ENDGRENT]: Define away endgrent.
Jim Meyering <jim@meyering.net>
parents:
475
diff
changeset
|
57 # define endpwent() ((void) 0) |
5 | 58 #endif |
59 | |
2854 | 60 #ifndef UID_T_MAX |
61 # define UID_T_MAX TYPE_MAXIMUM (uid_t) | |
62 #endif | |
63 | |
64 #ifndef GID_T_MAX | |
65 # define GID_T_MAX TYPE_MAXIMUM (gid_t) | |
66 #endif | |
67 | |
68 /* MAXUID may come from limits.h or sys/params.h. */ | |
69 #ifndef MAXUID | |
70 # define MAXUID UID_T_MAX | |
71 #endif | |
72 #ifndef MAXGID | |
73 # define MAXGID GID_T_MAX | |
74 #endif | |
75 | |
2271 | 76 /* ISDIGIT differs from isdigit, as follows: |
6932
6aeb4d6c28d9
* lib/.cppi-disable: Add wcwidth.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
77 - Its arg may be any int or unsigned int; it need not be an unsigned char |
6aeb4d6c28d9
* lib/.cppi-disable: Add wcwidth.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
78 or EOF. |
2271 | 79 - It's typically faster. |
3699 | 80 POSIX says that only '0' through '9' are digits. Prefer ISDIGIT to |
6932
6aeb4d6c28d9
* lib/.cppi-disable: Add wcwidth.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
81 isdigit unless it's important to use the locale's definition |
3699 | 82 of `digit' even when the host does not conform to POSIX. */ |
5159 | 83 #define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9) |
84 | |
85 #ifdef __DJGPP__ | |
5 | 86 |
5159 | 87 /* Return true if STR represents an unsigned decimal integer. */ |
194 | 88 |
5159 | 89 static bool |
1557 | 90 is_number (const char *str) |
194 | 91 { |
5159 | 92 do |
93 { | |
94 if (!ISDIGIT (*str)) | |
95 return false; | |
96 } | |
97 while (*++str); | |
98 | |
99 return true; | |
194 | 100 } |
5159 | 101 #endif |
5 | 102 |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
103 static char const * |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
104 parse_with_separator (char const *spec, char const *separator, |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
105 uid_t *uid, gid_t *gid, |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
106 char **username, char **groupname) |
5 | 107 { |
2273 | 108 static const char *E_invalid_user = N_("invalid user"); |
109 static const char *E_invalid_group = N_("invalid group"); | |
6969 | 110 static const char *E_bad_spec = N_("invalid spec"); |
2273 | 111 |
194 | 112 const char *error_msg; |
5 | 113 struct passwd *pwd; |
114 struct group *grp; | |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
115 char *u; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
116 char const *g; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
117 char *gname = NULL; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
118 uid_t unum = *uid; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
119 gid_t gnum = *gid; |
5 | 120 |
194 | 121 error_msg = NULL; |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
122 *username = *groupname = NULL; |
194 | 123 |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
124 /* Set U and G to nonzero length strings corresponding to user and |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
125 group specifiers or to NULL. If U is not NULL, it is a newly |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
126 allocated string. */ |
2273 | 127 |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
128 u = NULL; |
4996 | 129 if (separator == NULL) |
2284
ce1bca7b77d5
(parse_user_spec): If there is no `:' but there is a `.',
Jim Meyering <jim@meyering.net>
parents:
2273
diff
changeset
|
130 { |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
131 if (*spec) |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
132 u = xstrdup (spec); |
2284
ce1bca7b77d5
(parse_user_spec): If there is no `:' but there is a `.',
Jim Meyering <jim@meyering.net>
parents:
2273
diff
changeset
|
133 } |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
134 else |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
135 { |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
136 size_t ulen = separator - spec; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
137 if (ulen != 0) |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
138 { |
5322 | 139 u = xmemdup (spec, ulen + 1); |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
140 u[ulen] = '\0'; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
141 } |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
142 } |
194 | 143 |
144 g = (separator == NULL || *(separator + 1) == '\0' | |
145 ? NULL | |
146 : separator + 1); | |
147 | |
1260
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
148 #ifdef __DJGPP__ |
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
149 /* Pretend that we are the user U whose group is G. This makes |
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
150 pwd and grp functions ``know'' about the UID and GID of these. */ |
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
151 if (u && !is_number (u)) |
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
152 setenv ("USER", u, 1); |
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
153 if (g && !is_number (g)) |
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
154 setenv ("GROUP", g, 1); |
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
155 #endif |
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
156 |
194 | 157 if (u != NULL) |
5 | 158 { |
194 | 159 pwd = getpwnam (u); |
160 if (pwd == NULL) | |
5 | 161 { |
5159 | 162 bool use_login_group = (separator != NULL && g == NULL); |
163 if (use_login_group) | |
6969 | 164 { |
165 /* If there is no group, | |
166 then there may not be a trailing ":", either. */ | |
167 error_msg = E_bad_spec; | |
168 } | |
5 | 169 else |
194 | 170 { |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
171 unsigned long int tmp; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
172 if (xstrtoul (u, NULL, 10, &tmp, "") == LONGINT_OK |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
173 && tmp <= MAXUID) |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
174 unum = tmp; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
175 else |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
176 error_msg = E_invalid_user; |
194 | 177 } |
5 | 178 } |
179 else | |
180 { | |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
181 unum = pwd->pw_uid; |
194 | 182 if (g == NULL && separator != NULL) |
5 | 183 { |
194 | 184 /* A separator was given, but a group was not specified, |
185 so get the login group. */ | |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
186 char buf[INT_BUFSIZE_BOUND (uintmax_t)]; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
187 gnum = pwd->pw_gid; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
188 grp = getgrgid (gnum); |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
189 gname = xstrdup (grp ? grp->gr_name : umaxtostr (gnum, buf)); |
194 | 190 endgrent (); |
5 | 191 } |
194 | 192 } |
193 endpwent (); | |
5 | 194 } |
195 | |
195 | 196 if (g != NULL && error_msg == NULL) |
194 | 197 { |
198 /* Explicit group. */ | |
199 grp = getgrnam (g); | |
200 if (grp == NULL) | |
201 { | |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
202 unsigned long int tmp; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
203 if (xstrtoul (g, NULL, 10, &tmp, "") == LONGINT_OK && tmp <= MAXGID) |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
204 gnum = tmp; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
205 else |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
206 error_msg = E_invalid_group; |
194 | 207 } |
208 else | |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
209 gnum = grp->gr_gid; |
194 | 210 endgrent (); /* Save a file descriptor. */ |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
211 gname = xstrdup (g); |
5 | 212 } |
194 | 213 |
230 | 214 if (error_msg == NULL) |
5 | 215 { |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
216 *uid = unum; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
217 *gid = gnum; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
218 *username = u; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
219 *groupname = gname; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
220 u = NULL; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
221 } |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
222 else |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
223 free (gname); |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
224 |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
225 free (u); |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
226 return _(error_msg); |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
227 } |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
228 |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
229 /* Extract from SPEC, which has the form "[user][:.][group]", |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
230 a USERNAME, UID U, GROUPNAME, and GID G. |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
231 Either user or group, or both, must be present. |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
232 If the group is omitted but the separator is given, |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
233 use the given user's login group. |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
234 If SPEC contains a `:', then use that as the separator, ignoring |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
235 any `.'s. If there is no `:', but there is a `.', then first look |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
236 up the entire SPEC as a login name. If that look-up fails, then |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
237 try again interpreting the `.' as a separator. |
230 | 238 |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
239 USERNAME and GROUPNAME will be in newly malloc'd memory. |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
240 Either one might be NULL instead, indicating that it was not |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
241 given and the corresponding numeric ID was left unchanged. |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
242 |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
243 Return NULL if successful, a static error message string if not. */ |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
244 |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
245 char const * |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
246 parse_user_spec (char const *spec, uid_t *uid, gid_t *gid, |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
247 char **username, char **groupname) |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
248 { |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
249 char const *colon = strchr (spec, ':'); |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
250 char const *error_msg = |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
251 parse_with_separator (spec, colon, uid, gid, username, groupname); |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
252 |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
253 if (!colon && error_msg) |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
254 { |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
255 /* If there's no colon but there is a dot, and if looking up the |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
256 whole spec failed (i.e., the spec is not a owner name that |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
257 includes a dot), then try again, but interpret the dot as a |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
258 separator. This is a compatible extension to POSIX, since |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
259 the POSIX-required behavior is always tried first. */ |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
260 |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
261 char const *dot = strchr (spec, '.'); |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
262 if (dot |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
263 && ! parse_with_separator (spec, dot, uid, gid, username, groupname)) |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
264 error_msg = NULL; |
5 | 265 } |
194 | 266 |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
267 return error_msg; |
5 | 268 } |
269 | |
232
0d9395790eb7
(main): Change #ifdef TESTING to TEST.
Jim Meyering <jim@meyering.net>
parents:
231
diff
changeset
|
270 #ifdef TEST |
194 | 271 |
966 | 272 # define NULL_CHECK(s) ((s) == NULL ? "(null)" : (s)) |
194 | 273 |
274 int | |
275 main (int argc, char **argv) | |
276 { | |
277 int i; | |
278 | |
279 for (i = 1; i < argc; i++) | |
280 { | |
281 const char *e; | |
282 char *username, *groupname; | |
283 uid_t uid; | |
284 gid_t gid; | |
285 char *tmp; | |
5 | 286 |
194 | 287 tmp = strdup (argv[i]); |
288 e = parse_user_spec (tmp, &uid, &gid, &username, &groupname); | |
289 free (tmp); | |
5159 | 290 printf ("%s: %lu %lu %s %s %s\n", |
194 | 291 argv[i], |
5159 | 292 (unsigned long int) uid, |
293 (unsigned long int) gid, | |
194 | 294 NULL_CHECK (username), |
295 NULL_CHECK (groupname), | |
296 NULL_CHECK (e)); | |
297 } | |
298 | |
299 exit (0); | |
5 | 300 } |
194 | 301 |
302 #endif |