Home | History | Annotate | Line # | Download | only in newsyslog
newsyslog.c revision 1.30
      1 /*	$NetBSD: newsyslog.c,v 1.30 2000/07/11 12:06:32 ad Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1999, 2000 Andrew Doran <ad (at) NetBSD.org>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  *
     28  */
     29 
     30 /*
     31  * This file contains changes from the Open Software Foundation.
     32  */
     33 
     34 /*
     35  * Copyright 1988, 1989 by the Massachusetts Institute of Technology
     36  *
     37  * Permission to use, copy, modify, and distribute this software
     38  * and its documentation for any purpose and without fee is
     39  * hereby granted, provided that the above copyright notice
     40  * appear in all copies and that both that copyright notice and
     41  * this permission notice appear in supporting documentation,
     42  * and that the names of M.I.T. and the M.I.T. S.I.P.B. not be
     43  * used in advertising or publicity pertaining to distribution
     44  * of the software without specific, written prior permission.
     45  * M.I.T. and the M.I.T. S.I.P.B. make no representations about
     46  * the suitability of this software for any purpose.  It is
     47  * provided "as is" without express or implied warranty.
     48  *
     49  */
     50 
     51 /*
     52  * newsyslog(1) - a program to roll over log files provided that specified
     53  * critera are met, optionally preserving a number of historical log files.
     54  */
     55 
     56 #include <sys/cdefs.h>
     57 #ifndef lint
     58 __RCSID("$NetBSD: newsyslog.c,v 1.30 2000/07/11 12:06:32 ad Exp $");
     59 #endif /* not lint */
     60 
     61 #include <sys/types.h>
     62 #include <sys/time.h>
     63 #include <sys/stat.h>
     64 #include <sys/param.h>
     65 
     66 #include <ctype.h>
     67 #include <fcntl.h>
     68 #include <grp.h>
     69 #include <pwd.h>
     70 #include <signal.h>
     71 #include <stdio.h>
     72 #include <stdlib.h>
     73 #include <stdarg.h>
     74 #include <string.h>
     75 #include <time.h>
     76 #include <unistd.h>
     77 #include <errno.h>
     78 #include <err.h>
     79 #include <util.h>
     80 
     81 #include "pathnames.h"
     82 
     83 #define	PRHDRINFO(x)	((void)(verbose ? printf x : 0))
     84 #define	PRINFO(x)	((void)(verbose ? printf("  ") + printf x : 0))
     85 
     86 #define	CE_COMPRESS	0x01	/* Compress the achived log files */
     87 #define	CE_BINARY	0x02	/* Logfile is a binary file/non-syslog */
     88 #define	CE_NOSIGNAL	0x04	/* Don't send a signal when trimmed */
     89 #define CE_CREATE	0x08	/* Create log file if none exists */
     90 #define CE_PLAIN0	0x10	/* Do not compress zero'th history file */
     91 
     92 struct conf_entry {
     93 	uid_t	uid;			/* Owner of log */
     94 	gid_t	gid;			/* Group of log */
     95 	mode_t	mode;			/* File permissions */
     96 	int	numhist;		/* Number of historical logs to keep */
     97 	size_t	maxsize;		/* Maximum log size */
     98 	int	maxage;			/* Hours between log trimming */
     99 	int	flags;			/* Flags (CE_*) */
    100 	int	signum;			/* Signal to send */
    101 	char	pidfile[MAXPATHLEN];	/* File containing PID to signal */
    102 	char	logfile[MAXPATHLEN];	/* Path to log file */
    103 };
    104 
    105 int     verbose = 0;			/* Be verbose */
    106 int	noaction = 0;			/* Take no action */
    107 char    hostname[MAXHOSTNAMELEN + 1];	/* Hostname, stripped of domain */
    108 
    109 int	main(int, char **);
    110 int	parse(struct conf_entry *, FILE *, size_t *);
    111 
    112 void	log_compress(struct conf_entry *, const char *);
    113 void	log_create(struct conf_entry *);
    114 void	log_examine(struct conf_entry *, int);
    115 void	log_trim(struct conf_entry *);
    116 void	log_trimmed(struct conf_entry *);
    117 
    118 int	getsig(const char *);
    119 int	isnumber(const char *);
    120 int	parseuserspec(const char *, struct passwd **, struct group **);
    121 pid_t	readpidfile(const char *);
    122 void	usage(void);
    123 
    124 /*
    125  * Program entry point.
    126  */
    127 int
    128 main(int argc, char **argv)
    129 {
    130 	struct conf_entry log;
    131 	FILE *fd;
    132 	char *p, *cfile;
    133 	int c, force, needroot;
    134 	size_t lineno;
    135 
    136 	force = 0;
    137 	needroot = 1;
    138 	cfile = _PATH_NEWSYSLOGCONF;
    139 
    140 	gethostname(hostname, sizeof(hostname));
    141 	hostname[sizeof(hostname) - 1] = '\0';
    142 
    143 	/* Truncate domain */
    144 	if ((p = strchr(hostname, '.')) != NULL)
    145 		*p = '\0';
    146 
    147 	/* Parse command line options */
    148 	while ((c = getopt(argc, argv, "f:nrvF")) != -1) {
    149 		switch (c) {
    150 		case 'f':
    151 			cfile = optarg;
    152 			break;
    153 		case 'n':
    154 			noaction = 1;
    155 			verbose = 1;
    156 			break;
    157 		case 'r':
    158 			needroot = 0;
    159 			break;
    160 		case 'v':
    161 			verbose = 1;
    162 			break;
    163 		case 'F':
    164 			force = 1;
    165 			break;
    166 		default:
    167 			usage();
    168 			/* NOTREACHED */
    169 		}
    170 	}
    171 
    172 	if (needroot && geteuid() != 0)
    173 		errx(EXIT_FAILURE, "must be run as root");
    174 
    175 	if (strcmp(cfile, "-") == 0)
    176 		fd = stdin;
    177 	else if ((fd = fopen(cfile, "rt")) == NULL)
    178 		err(EXIT_FAILURE, "%s", cfile);
    179 
    180 	for (lineno = 0; !parse(&log, fd, &lineno);)
    181 		log_examine(&log, force);
    182 
    183 	if (fd != stdin)
    184 		fclose(fd);
    185 
    186 	exit(EXIT_SUCCESS);
    187 	/* NOTREACHED */
    188 }
    189 
    190 /*
    191  * Parse a single line from the configuration file.
    192  */
    193 int
    194 parse(struct conf_entry *log, FILE *fd, size_t *_lineno)
    195 {
    196 	char *line, *q, **ap, *argv[10];
    197 	struct passwd *pw;
    198 	struct group *gr;
    199 	int nf, lineno, i;
    200 
    201 	memset(log, 0, sizeof(*log));
    202 
    203 	if ((line = fparseln(fd, NULL, _lineno, NULL, 0)) == NULL)
    204 		return (-1);
    205 	lineno = (int)*_lineno;
    206 
    207 	for (ap = argv, nf = 0; (*ap = strsep(&line, " \t")) != NULL;)
    208 		if (**ap != '\0') {
    209 			if (++nf == sizeof(argv) / sizeof(argv[0])) {
    210 				warnx("config line %d: too many fields",
    211 				    lineno);
    212 				return (-1);
    213 			}
    214 			ap++;
    215 		}
    216 
    217 	if (nf == 0)
    218 		return (0);
    219 
    220 	if (nf < 6)
    221 		errx(EXIT_FAILURE, "config line %d: too few fields", lineno);
    222 
    223 	ap = argv;
    224 	strlcpy(log->logfile, *ap++, sizeof(log->logfile));
    225 
    226 	if (strchr(*ap, ':') != NULL) {
    227 		if (parseuserspec(*ap++, &pw, &gr)) {
    228 			warnx("config line %d: unknown user/group", lineno);
    229 			return (-1);
    230 		}
    231 		log->uid = pw->pw_uid;
    232 		log->gid = gr->gr_gid;
    233 		if (nf < 7)
    234 			errx(EXIT_FAILURE, "config line %d: too few fields",
    235 			    lineno);
    236 	}
    237 
    238 	if (sscanf(*ap++, "%o", &i) != 1) {
    239 		warnx("config line %d: bad permissions", lineno);
    240 		return (-1);
    241 	}
    242 	log->mode = (mode_t)i;
    243 
    244 	if (sscanf(*ap++, "%d", &log->numhist) != 1) {
    245 		warnx("config line %d: bad log count", lineno);
    246 		return (-1);
    247 	}
    248 
    249 	if (isdigit(**ap))
    250 		log->maxsize = atoi(*ap);
    251 	else if (**ap == '*')
    252 		log->maxsize = (size_t)-1;
    253 	else {
    254 		warnx("config line %d: bad log size", lineno);
    255 		return (-1);
    256 	}
    257 	ap++;
    258 
    259 	if (isdigit(**ap))
    260 		log->maxage = atoi(*ap);
    261 	else if (**ap == '*')
    262 		log->maxage = -1;
    263 	else {
    264 		warnx("config line %d: bad log age", lineno);
    265 		return (-1);
    266 	}
    267 	ap++;
    268 
    269 	log->flags = 0;
    270 	for (q = *ap++; q != NULL && *q != '\0'; q++) {
    271 		switch (tolower(*q)) {
    272 		case 'b':
    273 			log->flags |= CE_BINARY;
    274 			break;
    275 		case 'c':
    276 			log->flags |= CE_CREATE;
    277 			break;
    278 		case 'n':
    279 			log->flags |= CE_NOSIGNAL;
    280 			break;
    281 		case 'p':
    282 			log->flags |= CE_PLAIN0;
    283 			break;
    284 		case 'z':
    285 			log->flags |= CE_COMPRESS;
    286 			break;
    287 		case '-':
    288 			break;
    289 		default:
    290 			warnx("config line %d: bad flags", lineno);
    291 			return (-1);
    292 		}
    293 	}
    294 
    295 	if (*ap != NULL && **ap == '/')
    296 		strlcpy(log->pidfile, *ap++, sizeof(log->pidfile));
    297 	else
    298 		log->pidfile[0] = '\0';
    299 
    300 	if (*ap != NULL && (log->signum = getsig(*ap++)) < 0) {
    301 		warnx("config line %d: bad signal type", lineno);
    302 		return (-1);
    303 	} else
    304 		log->signum = SIGHUP;
    305 
    306 	return (0);
    307 }
    308 
    309 /*
    310  * Examine a log file.  If the trim conditions are met, call log_trim() to
    311  * trim the log file.
    312  */
    313 void
    314 log_examine(struct conf_entry *log, int force)
    315 {
    316 	struct stat sb;
    317 	size_t size;
    318 	int age;
    319 	char tmp[MAXPATHLEN];
    320 	time_t now;
    321 
    322 	if (log->logfile[0] == '\0')
    323 		return;
    324 
    325 	PRHDRINFO(("\n%s <%d%s>: ", log->logfile, log->numhist,
    326 	    (log->flags & CE_COMPRESS) != 0 ? "Z" : ""));
    327 
    328 	if (stat(log->logfile, &sb) < 0) {
    329 		if (errno == ENOENT && (log->flags & CE_CREATE) != 0) {
    330 			PRHDRINFO(("creating; "));
    331 			if (!noaction)
    332 				log_create(log);
    333 			else {
    334 				PRHDRINFO(("can't proceed with `-n'\n"));
    335 				return;
    336 			}
    337 			if (stat(log->logfile, &sb))
    338 				err(EXIT_FAILURE, "%s", log->logfile);
    339 		} else if (errno == ENOENT) {
    340 			PRHDRINFO(("does not exist --> skip log\n"));
    341 			return;
    342 		} else if (errno != 0)
    343 			err(EXIT_FAILURE, "%s", log->logfile);
    344 	}
    345 
    346 
    347 	size = ((size_t)sb.st_blocks * S_BLKSIZE) >> 10;
    348 
    349 	now = time(NULL);
    350 	strlcpy(tmp, log->logfile, sizeof(tmp));
    351 	strlcat(tmp, ".0", sizeof(tmp));
    352 	if (stat(tmp, &sb) < 0) {
    353 		strlcat(tmp, ".gz", sizeof(tmp));
    354 		if (stat(tmp, &sb) < 0)
    355 			age = -1;
    356 		else
    357 			age = (int)(now - sb.st_mtime + 1800) / 3600;
    358 	} else
    359 		age = (int)(now - sb.st_mtime + 1800) / 3600;
    360 
    361 	if (verbose) {
    362 		if (log->maxsize != (size_t)-1)
    363 			PRHDRINFO(("size (Kb): %lu [%lu] ",
    364 				(u_long)size,
    365 				(u_long)log->maxsize));
    366 		if (log->maxage > 0)
    367 			PRHDRINFO(("age (hr): %d [%d] ", age, log->maxage));
    368 	}
    369 
    370 	/*
    371 	 * Note: if maxage is used as a trim condition, we need at least one
    372 	 * historical log file to determine the `age' of the active log file.
    373 	 */
    374 	if ((log->maxage > 0 && (age >= log->maxage || age < 0)) ||
    375 	    size >= log->maxsize || force) {
    376 		PRHDRINFO(("--> trim log\n"));
    377 		log_trim(log);
    378 	} else
    379 		PRHDRINFO(("--> skip log\n"));
    380 }
    381 
    382 /*
    383  * Trim the specified log file.
    384  */
    385 void
    386 log_trim(struct conf_entry *log)
    387 {
    388 	char file1[MAXPATHLEN], file2[MAXPATHLEN];
    389 	int i;
    390 	struct stat st;
    391 	pid_t pid;
    392 
    393 	/* Remove oldest historical log */
    394 	snprintf(file1, sizeof(file1), "%s.%d", log->logfile, log->numhist - 1);
    395 
    396 	PRINFO(("rm -f %s\n", file1));
    397 	if (!noaction)
    398 		unlink(file1);
    399 	strlcat(file1, ".gz", sizeof(file1));
    400 	PRINFO(("rm -f %s\n", file1));
    401 	if (!noaction)
    402 		unlink(file1);
    403 
    404 	/* Move down log files */
    405 	for (i = log->numhist - 1; i != 0; i--) {
    406 		snprintf(file1, sizeof(file1), "%s.%d", log->logfile, i - 1);
    407 		snprintf(file2, sizeof(file2), "%s.%d", log->logfile, i);
    408 
    409 		if (lstat(file1, &st) != 0) {
    410 			strlcat(file1, ".gz", sizeof(file1));
    411 			strlcat(file2, ".gz", sizeof(file2));
    412 			if (lstat(file1, &st) != 0)
    413 				continue;
    414 		}
    415 
    416 		PRINFO(("mv %s %s\n", file1, file2));
    417 		if (!noaction)
    418 			if (rename(file1, file2))
    419 				err(EXIT_FAILURE, "%s", file1);
    420 		PRINFO(("chmod %o %s\n", log->mode, file2));
    421 		if (!noaction)
    422 			if (chmod(file2, log->mode))
    423 				err(EXIT_FAILURE, "%s", file2);
    424 		PRINFO(("chown %d:%d %s\n", log->uid, log->gid, file2));
    425 		if (!noaction)
    426 			if (chown(file2, log->uid, log->gid))
    427 				err(EXIT_FAILURE, "%s", file2);
    428 
    429 	}
    430 
    431 	/*
    432 	 * If a historical log file isn't compressed, and 'z' has been
    433 	 * specified, compress it.  (This is convenient, but is also needed
    434 	 * if 'p' has been specified.)  It should be noted that gzip(1)
    435 	 * preserves file ownership and file mode.
    436 	 */
    437 	for (i = (log->flags & CE_PLAIN0) != 0; i < log->numhist; i++) {
    438 		snprintf(file1, sizeof(file1), "%s.%d", log->logfile, i);
    439 		if (lstat(file1, &st) != 0)
    440 			continue;
    441 		snprintf(file2, sizeof(file2), "%s.gz", file1);
    442 		if (lstat(file2, &st) == 0)
    443 			continue;
    444 		log_compress(log, file1);
    445 	}
    446 
    447 	log_trimmed(log);
    448 
    449 	if (log->numhist == 0) {
    450 		PRINFO(("rm -f %s\n", log->logfile));
    451 		if (!noaction)
    452 			if (unlink(log->logfile))
    453 				err(EXIT_FAILURE, "%s", log->logfile);
    454 	} else {
    455 		snprintf(file1, sizeof(file1), "%s.0", log->logfile);
    456 		PRINFO(("mv %s %s\n", log->logfile, file1));
    457 		if (!noaction)
    458 			if (rename(log->logfile, file1))
    459 				err(EXIT_FAILURE, "%s", log->logfile);
    460 	}
    461 
    462 	PRINFO(("(create new log)\n"));
    463 	log_create(log);
    464 	log_trimmed(log);
    465 
    466 	PRINFO(("chmod %o %s\n", log->mode, log->logfile));
    467 	if (!noaction)
    468 		if (chmod(log->logfile, log->mode))
    469 			err(EXIT_FAILURE, "%s", log->logfile);
    470 
    471 	if ((log->flags & CE_NOSIGNAL) == 0) {
    472 		if (log->pidfile[0] != '\0')
    473 			pid = readpidfile(log->pidfile);
    474 		else
    475 			pid = readpidfile(_PATH_SYSLOGDPID);
    476 
    477 		if (pid != (pid_t)-1) {
    478 			PRINFO(("kill -%s %lu\n", sys_signame[log->signum],
    479 			    (u_long)pid));
    480 			if (!noaction)
    481 				if (kill(pid, log->signum))
    482 					warn("kill");
    483 		}
    484 	}
    485 
    486 	if ((log->flags & (CE_PLAIN0 | CE_COMPRESS)) == CE_COMPRESS) {
    487 		snprintf(file1, sizeof(file1), "%s.0", log->logfile);
    488 		log_compress(log, file1);
    489 	}
    490 }
    491 
    492 /*
    493  * Write an entry to the log file recording the fact that it was trimmed.
    494  */
    495 void
    496 log_trimmed(struct conf_entry *log)
    497 {
    498 	FILE *fd;
    499 	time_t now;
    500 	char *daytime;
    501 
    502 	if ((log->flags & CE_BINARY) != 0)
    503 		return;
    504 	PRINFO(("(append rotation notice to %s)\n", log->logfile));
    505 	if (noaction)
    506 		return;
    507 
    508 	if ((fd = fopen(log->logfile, "at")) == NULL)
    509 		err(EXIT_FAILURE, "%s", log->logfile);
    510 
    511 	now = time(NULL);
    512 	daytime = ctime(&now) + 4;
    513 	daytime[15] = '\0';
    514 
    515 	fprintf(fd, "%s %s newsyslog[%lu]: log file turned over\n", daytime,
    516 	    hostname, (u_long)getpid());
    517 	fclose(fd);
    518 }
    519 
    520 /*
    521  * Create a new log file.
    522  */
    523 void
    524 log_create(struct conf_entry *log)
    525 {
    526 	int fd;
    527 
    528 	if (noaction)
    529 		return;
    530 
    531 	if ((fd = creat(log->logfile, log->mode)) < 0)
    532 		err(EXIT_FAILURE, "%s", log->logfile);
    533 	if (fchown(fd, log->uid, log->gid) < 0)
    534 		err(EXIT_FAILURE, "%s", log->logfile);
    535 	close(fd);
    536 }
    537 
    538 /*
    539  * Compress a log file.  This routine takes an additional string argument:
    540  * it is also used to compress historical log files.
    541  */
    542 void
    543 log_compress(struct conf_entry *log, const char *fn)
    544 {
    545 	pid_t pid;
    546 
    547 	PRINFO(("gzip %s\n", fn));
    548 	if (!noaction) {
    549 		if ((pid = fork()) < 0)
    550 			err(EXIT_FAILURE, "fork");
    551 		else if (pid == 0) {
    552 			execl(_PATH_GZIP, "gzip", "-f", fn, NULL);
    553 			err(EXIT_FAILURE, _PATH_GZIP);
    554 		}
    555 	}
    556 }
    557 
    558 /*
    559  * Display program usage information.
    560  */
    561 void
    562 usage(void)
    563 {
    564 
    565 	fprintf(stderr, "usage: newsyslog [-Frv] [-f config-file]\n");
    566 	exit(EXIT_FAILURE);
    567 }
    568 
    569 /*
    570  * Return non-zero if a string represents a decimal value.
    571  */
    572 int
    573 isnumber(const char *string)
    574 {
    575 
    576 	while (isdigit(*string))
    577 		string++;
    578 
    579 	return (*string == '\0');
    580 }
    581 
    582 /*
    583  * Given a signal name, attempt to find the corresponding signal number.
    584  */
    585 int
    586 getsig(const char *sig)
    587 {
    588 	char *p;
    589 	int n;
    590 
    591 	if (isnumber(sig)) {
    592 		n = (int)strtol(sig, &p, 0);
    593 		if (p != '\0' || n < 0 || n >= NSIG)
    594 			return (-1);
    595 		return (n);
    596 	}
    597 
    598 	if (strncasecmp(sig, "SIG", 3) == 0)
    599 		sig += 3;
    600 	for (n = 1; n < NSIG; n++)
    601 		if (strcasecmp(sys_signame[n], sig) == 0)
    602 			return (n);
    603 	return (-1);
    604 }
    605 
    606 /*
    607  * Given a path to a PID file, return the PID contained within.
    608  */
    609 pid_t
    610 readpidfile(const char *file)
    611 {
    612 	FILE *fd;
    613 	char line[BUFSIZ];
    614 	pid_t pid;
    615 
    616 #ifdef notyet
    617 	if (file[0] != '/')
    618 		snprintf(tmp, sizeof(tmp), "%s%s", _PATH_VARRUN, file);
    619 	else
    620 		strlcpy(tmp, file, sizeof(tmp));
    621 #endif
    622 
    623 	if ((fd = fopen(file, "rt")) == NULL) {
    624 		warn("%s", file);
    625 		return (-1);
    626 	}
    627 
    628 	if (fgets(line, sizeof(line) - 1, fd) != NULL) {
    629 		line[sizeof(line) - 1] = '\0';
    630 		pid = (pid_t)strtol(line, NULL, 0);
    631 	} else {
    632 		warnx("unable to read %s", file);
    633 		pid = (pid_t)-1;
    634 	}
    635 
    636 	fclose(fd);
    637 	return (pid);
    638 }
    639 
    640 /*
    641  * Parse a user:group specification.
    642  *
    643  * XXX This is over the top for newsyslog(1).  It should be moved to libutil.
    644  */
    645 int
    646 parseuserspec(const char *name, struct passwd **pw, struct group **gr)
    647 {
    648 	char buf[MAXLOGNAME * 2 + 2], *group;
    649 
    650 	strlcpy(buf, name, sizeof(buf));
    651 	*gr = NULL;
    652 
    653 	/*
    654 	 * Before attempting to use '.' as a separator, see if the whole
    655 	 * string resolves as a user name.
    656 	 */
    657 	if ((*pw = getpwnam(buf)) != NULL) {
    658 		*gr = getgrgid((*pw)->pw_gid);
    659 		return (0);
    660 	}
    661 
    662 	/* Split the user and group name. */
    663 	if ((group = strchr(buf, ':')) != NULL ||
    664 	    (group = strchr(buf, '.')) != NULL)
    665 		*group++ = '\0';
    666 
    667 	if (isnumber(buf))
    668 		*pw = getpwuid((uid_t)atoi(buf));
    669 	else
    670 		*pw = getpwnam(buf);
    671 
    672 	/*
    673 	 * Find the group.  If a group wasn't specified, use the user's
    674 	 * `natural' group.  We get to this point even if no user was found.
    675 	 * This is to allow the caller to get a better idea of what went
    676 	 * wrong, if anything.
    677 	 */
    678 	if (group == NULL || *group == '\0') {
    679 		if (*pw == NULL)
    680 			return (-1);
    681 		*gr = getgrgid((*pw)->pw_gid);
    682 	} else if (isnumber(group))
    683 		*gr = getgrgid((gid_t)atoi(group));
    684 	else
    685 		*gr = getgrnam(group);
    686 
    687 	return (*pw != NULL && *gr != NULL ? 0 : -1);
    688 }
    689