xref: /dflybsd-src/contrib/libarchive/cpio/cmdline.c (revision afd311f52496a4b5c3df02ea6d4bdab591886c60)
160b4ad09SPeter Avalos /*-
260b4ad09SPeter Avalos  * Copyright (c) 2003-2007 Tim Kientzle
360b4ad09SPeter Avalos  * All rights reserved.
460b4ad09SPeter Avalos  *
560b4ad09SPeter Avalos  * Redistribution and use in source and binary forms, with or without
660b4ad09SPeter Avalos  * modification, are permitted provided that the following conditions
760b4ad09SPeter Avalos  * are met:
860b4ad09SPeter Avalos  * 1. Redistributions of source code must retain the above copyright
960b4ad09SPeter Avalos  *    notice, this list of conditions and the following disclaimer
1060b4ad09SPeter Avalos  *    in this position and unchanged.
1160b4ad09SPeter Avalos  * 2. Redistributions in binary form must reproduce the above copyright
1260b4ad09SPeter Avalos  *    notice, this list of conditions and the following disclaimer in the
1360b4ad09SPeter Avalos  *    documentation and/or other materials provided with the distribution.
1460b4ad09SPeter Avalos  *
1560b4ad09SPeter Avalos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
1660b4ad09SPeter Avalos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1760b4ad09SPeter Avalos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1860b4ad09SPeter Avalos  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
1960b4ad09SPeter Avalos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2060b4ad09SPeter Avalos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2160b4ad09SPeter Avalos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2260b4ad09SPeter Avalos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2360b4ad09SPeter Avalos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2460b4ad09SPeter Avalos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2560b4ad09SPeter Avalos  */
2660b4ad09SPeter Avalos 
2760b4ad09SPeter Avalos 
2860b4ad09SPeter Avalos #include "cpio_platform.h"
298029ab02SPeter Avalos __FBSDID("$FreeBSD: src/usr.bin/cpio/cmdline.c,v 1.5 2008/12/06 07:30:40 kientzle Exp $");
3060b4ad09SPeter Avalos 
3160b4ad09SPeter Avalos #ifdef HAVE_ERRNO_H
3260b4ad09SPeter Avalos #include <errno.h>
3360b4ad09SPeter Avalos #endif
3460b4ad09SPeter Avalos #ifdef HAVE_GRP_H
3560b4ad09SPeter Avalos #include <grp.h>
3660b4ad09SPeter Avalos #endif
3760b4ad09SPeter Avalos #ifdef HAVE_PWD_H
3860b4ad09SPeter Avalos #include <pwd.h>
3960b4ad09SPeter Avalos #endif
4060b4ad09SPeter Avalos #include <stdio.h>
4160b4ad09SPeter Avalos #ifdef HAVE_STDLIB_H
4260b4ad09SPeter Avalos #include <stdlib.h>
4360b4ad09SPeter Avalos #endif
4460b4ad09SPeter Avalos #ifdef HAVE_STRING_H
4560b4ad09SPeter Avalos #include <string.h>
4660b4ad09SPeter Avalos #endif
4760b4ad09SPeter Avalos 
4860b4ad09SPeter Avalos #include "cpio.h"
499c82a63eSPeter Avalos #include "err.h"
5060b4ad09SPeter Avalos 
5160b4ad09SPeter Avalos /*
528029ab02SPeter Avalos  * Short options for cpio.  Please keep this sorted.
5360b4ad09SPeter Avalos  */
54*50f8aa9cSAntonio Huete Jimenez static const char *short_options = "067AaBC:cdE:F:f:H:hI:iJjLlmnO:opR:rtuVvW:yZz";
5560b4ad09SPeter Avalos 
5660b4ad09SPeter Avalos /*
578029ab02SPeter Avalos  * Long options for cpio.  Please keep this sorted.
5860b4ad09SPeter Avalos  */
598029ab02SPeter Avalos static const struct option {
608029ab02SPeter Avalos 	const char *name;
618029ab02SPeter Avalos 	int required;	/* 1 if this option requires an argument */
628029ab02SPeter Avalos 	int equivalent;	/* Equivalent short option. */
638029ab02SPeter Avalos } cpio_longopts[] = {
64d4d8193eSPeter Avalos 	{ "b64encode",			0, OPTION_B64ENCODE },
65*50f8aa9cSAntonio Huete Jimenez 	{ "binary",			0, '7' },
668029ab02SPeter Avalos 	{ "create",			0, 'o' },
67e95abc47Szrj 	{ "dereference",		0, 'L' },
68c09f92d2SPeter Avalos 	{ "dot",			0, 'V' },
698029ab02SPeter Avalos 	{ "extract",			0, 'i' },
708029ab02SPeter Avalos 	{ "file",			1, 'F' },
718029ab02SPeter Avalos 	{ "format",             	1, 'H' },
72d4d8193eSPeter Avalos 	{ "grzip",			0, OPTION_GRZIP },
738029ab02SPeter Avalos 	{ "help",			0, 'h' },
748029ab02SPeter Avalos 	{ "insecure",			0, OPTION_INSECURE },
758029ab02SPeter Avalos 	{ "link",			0, 'l' },
768029ab02SPeter Avalos 	{ "list",			0, 't' },
77d4d8193eSPeter Avalos 	{ "lrzip",			0, OPTION_LRZIP },
786b384f39SPeter Avalos 	{ "lz4",			0, OPTION_LZ4 },
799c82a63eSPeter Avalos 	{ "lzma",			0, OPTION_LZMA },
80d4d8193eSPeter Avalos 	{ "lzop",			0, OPTION_LZOP },
818029ab02SPeter Avalos 	{ "make-directories",		0, 'd' },
828029ab02SPeter Avalos 	{ "no-preserve-owner",		0, OPTION_NO_PRESERVE_OWNER },
838029ab02SPeter Avalos 	{ "null",			0, '0' },
848029ab02SPeter Avalos 	{ "numeric-uid-gid",		0, 'n' },
858029ab02SPeter Avalos 	{ "owner",			1, 'R' },
866b384f39SPeter Avalos 	{ "passphrase",			1, OPTION_PASSPHRASE },
878029ab02SPeter Avalos 	{ "pass-through",		0, 'p' },
888029ab02SPeter Avalos 	{ "preserve-modification-time", 0, 'm' },
899c82a63eSPeter Avalos 	{ "preserve-owner",		0, OPTION_PRESERVE_OWNER },
90*50f8aa9cSAntonio Huete Jimenez 	{ "pwb",			0, '6' },
918029ab02SPeter Avalos 	{ "quiet",			0, OPTION_QUIET },
928029ab02SPeter Avalos 	{ "unconditional",		0, 'u' },
93d4d8193eSPeter Avalos 	{ "uuencode",			0, OPTION_UUENCODE },
948029ab02SPeter Avalos 	{ "verbose",			0, 'v' },
958029ab02SPeter Avalos 	{ "version",			0, OPTION_VERSION },
969c82a63eSPeter Avalos 	{ "xz",				0, 'J' },
97e95abc47Szrj 	{ "zstd",			0, OPTION_ZSTD },
988029ab02SPeter Avalos 	{ NULL, 0, 0 }
9960b4ad09SPeter Avalos };
10060b4ad09SPeter Avalos 
10160b4ad09SPeter Avalos /*
1028029ab02SPeter Avalos  * I used to try to select platform-provided getopt() or
1038029ab02SPeter Avalos  * getopt_long(), but that caused a lot of headaches.  In particular,
1048029ab02SPeter Avalos  * I couldn't consistently use long options in the test harness
1058029ab02SPeter Avalos  * because not all platforms have getopt_long().  That in turn led to
1068029ab02SPeter Avalos  * overuse of the -W hack in the test harness, which made it rough to
1078029ab02SPeter Avalos  * run the test harness against GNU cpio.  (I periodically run the
1088029ab02SPeter Avalos  * test harness here against GNU cpio as a sanity-check.  Yes,
1098029ab02SPeter Avalos  * I've found a couple of bugs in GNU cpio that way.)
11060b4ad09SPeter Avalos  */
11160b4ad09SPeter Avalos int
cpio_getopt(struct cpio * cpio)11260b4ad09SPeter Avalos cpio_getopt(struct cpio *cpio)
11360b4ad09SPeter Avalos {
1148029ab02SPeter Avalos 	enum { state_start = 0, state_next_word, state_short, state_long };
1158029ab02SPeter Avalos 	static int state = state_start;
1168029ab02SPeter Avalos 	static char *opt_word;
11760b4ad09SPeter Avalos 
1188029ab02SPeter Avalos 	const struct option *popt, *match = NULL, *match2 = NULL;
1198029ab02SPeter Avalos 	const char *p, *long_prefix = "--";
1208029ab02SPeter Avalos 	size_t optlength;
1218029ab02SPeter Avalos 	int opt = '?';
1228029ab02SPeter Avalos 	int required = 0;
12360b4ad09SPeter Avalos 
124c09f92d2SPeter Avalos 	cpio->argument = NULL;
12560b4ad09SPeter Avalos 
1268029ab02SPeter Avalos 	/* First time through, initialize everything. */
1278029ab02SPeter Avalos 	if (state == state_start) {
1288029ab02SPeter Avalos 		/* Skip program name. */
1298029ab02SPeter Avalos 		++cpio->argv;
1308029ab02SPeter Avalos 		--cpio->argc;
1318029ab02SPeter Avalos 		state = state_next_word;
1328029ab02SPeter Avalos 	}
1338029ab02SPeter Avalos 
1348029ab02SPeter Avalos 	/*
1358029ab02SPeter Avalos 	 * We're ready to look at the next word in argv.
1368029ab02SPeter Avalos 	 */
1378029ab02SPeter Avalos 	if (state == state_next_word) {
1388029ab02SPeter Avalos 		/* No more arguments, so no more options. */
1398029ab02SPeter Avalos 		if (cpio->argv[0] == NULL)
1408029ab02SPeter Avalos 			return (-1);
1418029ab02SPeter Avalos 		/* Doesn't start with '-', so no more options. */
1428029ab02SPeter Avalos 		if (cpio->argv[0][0] != '-')
1438029ab02SPeter Avalos 			return (-1);
1448029ab02SPeter Avalos 		/* "--" marks end of options; consume it and return. */
1458029ab02SPeter Avalos 		if (strcmp(cpio->argv[0], "--") == 0) {
1468029ab02SPeter Avalos 			++cpio->argv;
1478029ab02SPeter Avalos 			--cpio->argc;
1488029ab02SPeter Avalos 			return (-1);
1498029ab02SPeter Avalos 		}
1508029ab02SPeter Avalos 		/* Get next word for parsing. */
1518029ab02SPeter Avalos 		opt_word = *cpio->argv++;
1528029ab02SPeter Avalos 		--cpio->argc;
1538029ab02SPeter Avalos 		if (opt_word[1] == '-') {
1548029ab02SPeter Avalos 			/* Set up long option parser. */
1558029ab02SPeter Avalos 			state = state_long;
1568029ab02SPeter Avalos 			opt_word += 2; /* Skip leading '--' */
1578029ab02SPeter Avalos 		} else {
1588029ab02SPeter Avalos 			/* Set up short option parser. */
1598029ab02SPeter Avalos 			state = state_short;
1608029ab02SPeter Avalos 			++opt_word;  /* Skip leading '-' */
1618029ab02SPeter Avalos 		}
1628029ab02SPeter Avalos 	}
1638029ab02SPeter Avalos 
1648029ab02SPeter Avalos 	/*
1658029ab02SPeter Avalos 	 * We're parsing a group of POSIX-style single-character options.
1668029ab02SPeter Avalos 	 */
1678029ab02SPeter Avalos 	if (state == state_short) {
1688029ab02SPeter Avalos 		/* Peel next option off of a group of short options. */
1698029ab02SPeter Avalos 		opt = *opt_word++;
1708029ab02SPeter Avalos 		if (opt == '\0') {
1718029ab02SPeter Avalos 			/* End of this group; recurse to get next option. */
1728029ab02SPeter Avalos 			state = state_next_word;
1738029ab02SPeter Avalos 			return cpio_getopt(cpio);
1748029ab02SPeter Avalos 		}
1758029ab02SPeter Avalos 
1768029ab02SPeter Avalos 		/* Does this option take an argument? */
1778029ab02SPeter Avalos 		p = strchr(short_options, opt);
1788029ab02SPeter Avalos 		if (p == NULL)
1798029ab02SPeter Avalos 			return ('?');
1808029ab02SPeter Avalos 		if (p[1] == ':')
1818029ab02SPeter Avalos 			required = 1;
1828029ab02SPeter Avalos 
1838029ab02SPeter Avalos 		/* If it takes an argument, parse that. */
1848029ab02SPeter Avalos 		if (required) {
1858029ab02SPeter Avalos 			/* If arg is run-in, opt_word already points to it. */
1868029ab02SPeter Avalos 			if (opt_word[0] == '\0') {
1878029ab02SPeter Avalos 				/* Otherwise, pick up the next word. */
1888029ab02SPeter Avalos 				opt_word = *cpio->argv;
1898029ab02SPeter Avalos 				if (opt_word == NULL) {
1909c82a63eSPeter Avalos 					lafe_warnc(0,
1918029ab02SPeter Avalos 					    "Option -%c requires an argument",
1928029ab02SPeter Avalos 					    opt);
1938029ab02SPeter Avalos 					return ('?');
1948029ab02SPeter Avalos 				}
1958029ab02SPeter Avalos 				++cpio->argv;
1968029ab02SPeter Avalos 				--cpio->argc;
1978029ab02SPeter Avalos 			}
19860b4ad09SPeter Avalos 			if (opt == 'W') {
1998029ab02SPeter Avalos 				state = state_long;
2008029ab02SPeter Avalos 				long_prefix = "-W "; /* For clearer errors. */
20160b4ad09SPeter Avalos 			} else {
2028029ab02SPeter Avalos 				state = state_next_word;
203c09f92d2SPeter Avalos 				cpio->argument = opt_word;
20460b4ad09SPeter Avalos 			}
2058029ab02SPeter Avalos 		}
20660b4ad09SPeter Avalos 	}
20760b4ad09SPeter Avalos 
2088029ab02SPeter Avalos 	/* We're reading a long option, including -W long=arg convention. */
2098029ab02SPeter Avalos 	if (state == state_long) {
2108029ab02SPeter Avalos 		/* After this long option, we'll be starting a new word. */
2118029ab02SPeter Avalos 		state = state_next_word;
21260b4ad09SPeter Avalos 
2138029ab02SPeter Avalos 		/* Option name ends at '=' if there is one. */
2148029ab02SPeter Avalos 		p = strchr(opt_word, '=');
2158029ab02SPeter Avalos 		if (p != NULL) {
2168029ab02SPeter Avalos 			optlength = (size_t)(p - opt_word);
217c09f92d2SPeter Avalos 			cpio->argument = (char *)(uintptr_t)(p + 1);
21860b4ad09SPeter Avalos 		} else {
2198029ab02SPeter Avalos 			optlength = strlen(opt_word);
22060b4ad09SPeter Avalos 		}
22160b4ad09SPeter Avalos 
2228029ab02SPeter Avalos 		/* Search the table for an unambiguous match. */
2238029ab02SPeter Avalos 		for (popt = cpio_longopts; popt->name != NULL; popt++) {
2248029ab02SPeter Avalos 			/* Short-circuit if first chars don't match. */
2258029ab02SPeter Avalos 			if (popt->name[0] != opt_word[0])
2268029ab02SPeter Avalos 				continue;
2278029ab02SPeter Avalos 			/* If option is a prefix of name in table, record it.*/
2288029ab02SPeter Avalos 			if (strncmp(opt_word, popt->name, optlength) == 0) {
2298029ab02SPeter Avalos 				match2 = match; /* Record up to two matches. */
2308029ab02SPeter Avalos 				match = popt;
2318029ab02SPeter Avalos 				/* If it's an exact match, we're done. */
2328029ab02SPeter Avalos 				if (strlen(popt->name) == optlength) {
2338029ab02SPeter Avalos 					match2 = NULL; /* Forget the others. */
2348029ab02SPeter Avalos 					break;
23560b4ad09SPeter Avalos 				}
23660b4ad09SPeter Avalos 			}
2378029ab02SPeter Avalos 		}
2388029ab02SPeter Avalos 
2398029ab02SPeter Avalos 		/* Fail if there wasn't a unique match. */
2408029ab02SPeter Avalos 		if (match == NULL) {
2419c82a63eSPeter Avalos 			lafe_warnc(0,
2428029ab02SPeter Avalos 			    "Option %s%s is not supported",
2438029ab02SPeter Avalos 			    long_prefix, opt_word);
2448029ab02SPeter Avalos 			return ('?');
2458029ab02SPeter Avalos 		}
2468029ab02SPeter Avalos 		if (match2 != NULL) {
2479c82a63eSPeter Avalos 			lafe_warnc(0,
2488029ab02SPeter Avalos 			    "Ambiguous option %s%s (matches --%s and --%s)",
2498029ab02SPeter Avalos 			    long_prefix, opt_word, match->name, match2->name);
2508029ab02SPeter Avalos 			return ('?');
2518029ab02SPeter Avalos 		}
2528029ab02SPeter Avalos 
2538029ab02SPeter Avalos 		/* We've found a unique match; does it need an argument? */
2548029ab02SPeter Avalos 		if (match->required) {
2558029ab02SPeter Avalos 			/* Argument required: get next word if necessary. */
256c09f92d2SPeter Avalos 			if (cpio->argument == NULL) {
257c09f92d2SPeter Avalos 				cpio->argument = *cpio->argv;
258c09f92d2SPeter Avalos 				if (cpio->argument == NULL) {
2599c82a63eSPeter Avalos 					lafe_warnc(0,
2608029ab02SPeter Avalos 					    "Option %s%s requires an argument",
2618029ab02SPeter Avalos 					    long_prefix, match->name);
2628029ab02SPeter Avalos 					return ('?');
2638029ab02SPeter Avalos 				}
2648029ab02SPeter Avalos 				++cpio->argv;
2658029ab02SPeter Avalos 				--cpio->argc;
2668029ab02SPeter Avalos 			}
2678029ab02SPeter Avalos 		} else {
2688029ab02SPeter Avalos 			/* Argument forbidden: fail if there is one. */
269c09f92d2SPeter Avalos 			if (cpio->argument != NULL) {
2709c82a63eSPeter Avalos 				lafe_warnc(0,
2718029ab02SPeter Avalos 				    "Option %s%s does not allow an argument",
2728029ab02SPeter Avalos 				    long_prefix, match->name);
2738029ab02SPeter Avalos 				return ('?');
2748029ab02SPeter Avalos 			}
2758029ab02SPeter Avalos 		}
2768029ab02SPeter Avalos 		return (match->equivalent);
2778029ab02SPeter Avalos 	}
27860b4ad09SPeter Avalos 
27960b4ad09SPeter Avalos 	return (opt);
28060b4ad09SPeter Avalos }
28160b4ad09SPeter Avalos 
28260b4ad09SPeter Avalos 
28360b4ad09SPeter Avalos /*
28460b4ad09SPeter Avalos  * Parse the argument to the -R or --owner flag.
28560b4ad09SPeter Avalos  *
28660b4ad09SPeter Avalos  * The format is one of the following:
2879c82a63eSPeter Avalos  *   <username|uid>    - Override user but not group
2889c82a63eSPeter Avalos  *   <username>:   - Override both, group is user's default group
2899c82a63eSPeter Avalos  *   <uid>:    - Override user but not group
2909c82a63eSPeter Avalos  *   <username|uid>:<groupname|gid> - Override both
2919c82a63eSPeter Avalos  *   :<groupname|gid>  - Override group but not user
2929c82a63eSPeter Avalos  *
2939c82a63eSPeter Avalos  * Where uid/gid are decimal representations and groupname/username
2949c82a63eSPeter Avalos  * are names to be looked up in system database.  Note that we try
2959c82a63eSPeter Avalos  * to look up an argument as a name first, then try numeric parsing.
29660b4ad09SPeter Avalos  *
29760b4ad09SPeter Avalos  * A period can be used instead of the colon.
29860b4ad09SPeter Avalos  *
2999c82a63eSPeter Avalos  * Sets uid/gid return as appropriate, -1 indicates uid/gid not specified.
300c09f92d2SPeter Avalos  * TODO: If the spec uses uname/gname, then return those to the caller
301c09f92d2SPeter Avalos  * as well.  If the spec provides uid/gid, just return names as NULL.
3029c82a63eSPeter Avalos  *
3039c82a63eSPeter Avalos  * Returns NULL if no error, otherwise returns error string for display.
30460b4ad09SPeter Avalos  *
30560b4ad09SPeter Avalos  */
3069c82a63eSPeter Avalos const char *
owner_parse(const char * spec,int * uid,int * gid)30760b4ad09SPeter Avalos owner_parse(const char *spec, int *uid, int *gid)
30860b4ad09SPeter Avalos {
3099c82a63eSPeter Avalos 	static char errbuff[128];
31060b4ad09SPeter Avalos 	const char *u, *ue, *g;
31160b4ad09SPeter Avalos 
31260b4ad09SPeter Avalos 	*uid = -1;
31360b4ad09SPeter Avalos 	*gid = -1;
31460b4ad09SPeter Avalos 
3159c82a63eSPeter Avalos 	if (spec[0] == '\0')
3169c82a63eSPeter Avalos 		return ("Invalid empty user/group spec");
3179c82a63eSPeter Avalos 
31860b4ad09SPeter Avalos 	/*
31960b4ad09SPeter Avalos 	 * Split spec into [user][:.][group]
32060b4ad09SPeter Avalos 	 *  u -> first char of username, NULL if no username
32160b4ad09SPeter Avalos 	 *  ue -> first char after username (colon, period, or \0)
32260b4ad09SPeter Avalos 	 *  g -> first char of group name
32360b4ad09SPeter Avalos 	 */
32460b4ad09SPeter Avalos 	if (*spec == ':' || *spec == '.') {
32560b4ad09SPeter Avalos 		/* If spec starts with ':' or '.', then just group. */
32660b4ad09SPeter Avalos 		ue = u = NULL;
32760b4ad09SPeter Avalos 		g = spec + 1;
32860b4ad09SPeter Avalos 	} else {
32960b4ad09SPeter Avalos 		/* Otherwise, [user] or [user][:] or [user][:][group] */
33060b4ad09SPeter Avalos 		ue = u = spec;
33160b4ad09SPeter Avalos 		while (*ue != ':' && *ue != '.' && *ue != '\0')
33260b4ad09SPeter Avalos 			++ue;
33360b4ad09SPeter Avalos 		g = ue;
33460b4ad09SPeter Avalos 		if (*g != '\0') /* Skip : or . to find first char of group. */
33560b4ad09SPeter Avalos 			++g;
33660b4ad09SPeter Avalos 	}
33760b4ad09SPeter Avalos 
33860b4ad09SPeter Avalos 	if (u != NULL) {
33960b4ad09SPeter Avalos 		/* Look up user: ue is first char after end of user. */
34060b4ad09SPeter Avalos 		char *user;
34160b4ad09SPeter Avalos 		struct passwd *pwent;
34260b4ad09SPeter Avalos 
34360b4ad09SPeter Avalos 		user = (char *)malloc(ue - u + 1);
3449c82a63eSPeter Avalos 		if (user == NULL)
3459c82a63eSPeter Avalos 			return ("Couldn't allocate memory");
34660b4ad09SPeter Avalos 		memcpy(user, u, ue - u);
34760b4ad09SPeter Avalos 		user[ue - u] = '\0';
3489c82a63eSPeter Avalos 		if ((pwent = getpwnam(user)) != NULL) {
3499c82a63eSPeter Avalos 			*uid = pwent->pw_uid;
3509c82a63eSPeter Avalos 			if (*ue != '\0')
3519c82a63eSPeter Avalos 				*gid = pwent->pw_gid;
3529c82a63eSPeter Avalos 		} else {
3539c82a63eSPeter Avalos 			char *end;
3549c82a63eSPeter Avalos 			errno = 0;
355c09f92d2SPeter Avalos 			*uid = (int)strtoul(user, &end, 10);
3569c82a63eSPeter Avalos 			if (errno || *end != '\0') {
3579c82a63eSPeter Avalos 				snprintf(errbuff, sizeof(errbuff),
3589c82a63eSPeter Avalos 				    "Couldn't lookup user ``%s''", user);
3599c82a63eSPeter Avalos 				errbuff[sizeof(errbuff) - 1] = '\0';
36059bf7050SPeter Avalos 				free(user);
3619c82a63eSPeter Avalos 				return (errbuff);
3629c82a63eSPeter Avalos 			}
36360b4ad09SPeter Avalos 		}
36460b4ad09SPeter Avalos 		free(user);
36560b4ad09SPeter Avalos 	}
3669c82a63eSPeter Avalos 
36760b4ad09SPeter Avalos 	if (*g != '\0') {
36860b4ad09SPeter Avalos 		struct group *grp;
3699c82a63eSPeter Avalos 		if ((grp = getgrnam(g)) != NULL) {
37060b4ad09SPeter Avalos 			*gid = grp->gr_gid;
3719c82a63eSPeter Avalos 		} else {
3729c82a63eSPeter Avalos 			char *end;
3739c82a63eSPeter Avalos 			errno = 0;
374c09f92d2SPeter Avalos 			*gid = (int)strtoul(g, &end, 10);
3759c82a63eSPeter Avalos 			if (errno || *end != '\0') {
3769c82a63eSPeter Avalos 				snprintf(errbuff, sizeof(errbuff),
3779c82a63eSPeter Avalos 				    "Couldn't lookup group ``%s''", g);
3789c82a63eSPeter Avalos 				errbuff[sizeof(errbuff) - 1] = '\0';
3799c82a63eSPeter Avalos 				return (errbuff);
38060b4ad09SPeter Avalos 			}
38160b4ad09SPeter Avalos 		}
3829c82a63eSPeter Avalos 	}
3839c82a63eSPeter Avalos 	return (NULL);
38460b4ad09SPeter Avalos }
385