14f6bdcfbapt/*-
24f6bdcfbapt * Copyright (C) 2015 Baptiste Daroussin <bapt@FreeBSD.org>
34f6bdcfbapt * All rights reserved.
44f6bdcfbapt *
54f6bdcfbapt * Redistribution and use in source and binary forms, with or without
64f6bdcfbapt * modification, are permitted provided that the following conditions
74f6bdcfbapt * are met:
84f6bdcfbapt * 1. Redistributions of source code must retain the above copyright
94f6bdcfbapt *    notice, this list of conditions and the following disclaimer
104f6bdcfbapt *    in this position and unchanged.
114f6bdcfbapt * 2. Redistributions in binary form must reproduce the above copyright
124f6bdcfbapt *    notice, this list of conditions and the following disclaimer in the
134f6bdcfbapt *    documentation and/or other materials provided with the distribution.
144f6bdcfbapt *
154f6bdcfbapt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
164f6bdcfbapt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
174f6bdcfbapt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
184f6bdcfbapt * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
194f6bdcfbapt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
204f6bdcfbapt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
214f6bdcfbapt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
224f6bdcfbapt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
234f6bdcfbapt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
244f6bdcfbapt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
254f6bdcfbapt */
264f6bdcfbapt
274f6bdcfbapt#include <sys/cdefs.h>
284f6bdcfbapt__FBSDID("$FreeBSD$");
294f6bdcfbapt
304f6bdcfbapt#include <sys/wait.h>
314f6bdcfbapt
324f6bdcfbapt#include <err.h>
334f6bdcfbapt#include <sysexits.h>
344f6bdcfbapt#include <stdlib.h>
354469579bapt#include <string.h>
364469579bapt#include <unistd.h>
374f6bdcfbapt
384f6bdcfbapt#include "pw.h"
394f6bdcfbapt
404f6bdcfbaptint
414f6bdcfbaptpw_checkfd(char *nptr)
424f6bdcfbapt{
434f6bdcfbapt	const char *errstr;
444f6bdcfbapt	int fd = -1;
454f6bdcfbapt
464f6bdcfbapt	if (strcmp(nptr, "-") == 0)
474f6bdcfbapt		return '-';
484f6bdcfbapt	fd = strtonum(nptr, 0, INT_MAX, &errstr);
494f6bdcfbapt	if (errstr != NULL)
504f6bdcfbapt		errx(EX_USAGE, "Bad file descriptor '%s': %s",
514f6bdcfbapt		    nptr, errstr);
524f6bdcfbapt	return (fd);
534f6bdcfbapt}
544f6bdcfbapt
554f6bdcfbaptuintmax_t
564f6bdcfbaptpw_checkid(char *nptr, uintmax_t maxval)
574f6bdcfbapt{
584f6bdcfbapt	const char *errstr = NULL;
594f6bdcfbapt	uintmax_t id;
604f6bdcfbapt
614f6bdcfbapt	id = strtounum(nptr, 0, maxval, &errstr);
624f6bdcfbapt	if (errstr)
634f6bdcfbapt		errx(EX_USAGE, "Bad id '%s': %s", nptr, errstr);
644f6bdcfbapt	return (id);
654f6bdcfbapt}
664f6bdcfbapt
674f6bdcfbaptstruct userconf *
684f6bdcfbaptget_userconfig(const char *config)
694f6bdcfbapt{
704f6bdcfbapt	char defaultcfg[MAXPATHLEN];
714f6bdcfbapt
724f6bdcfbapt	if (config != NULL)
734f6bdcfbapt		return (read_userconfig(config));
740f94e2cyuripv	snprintf(defaultcfg, sizeof(defaultcfg), "%s/" _PW_CONF, conf.etcpath);
754f6bdcfbapt	return (read_userconfig(defaultcfg));
764f6bdcfbapt}
774f6bdcfbapt
784f6bdcfbaptint
794f6bdcfbaptnis_update(void) {
804f6bdcfbapt	pid_t pid;
814f6bdcfbapt	int i;
824f6bdcfbapt
834f6bdcfbapt	fflush(NULL);
844f6bdcfbapt	if ((pid = fork()) == -1) {
854f6bdcfbapt		warn("fork()");
864f6bdcfbapt		return (1);
874f6bdcfbapt	}
884f6bdcfbapt	if (pid == 0) {
894f6bdcfbapt		execlp("/usr/bin/make", "make", "-C", "/var/yp/", (char*) NULL);
904f6bdcfbapt		_exit(1);
914f6bdcfbapt	}
924f6bdcfbapt	waitpid(pid, &i, 0);
934f6bdcfbapt	if ((i = WEXITSTATUS(i)) != 0)
944f6bdcfbapt		errx(i, "make exited with status %d", i);
954f6bdcfbapt	return (i);
964f6bdcfbapt}
97