1286196Sbapt/*-
2286196Sbapt * Copyright (C) 2015 Baptiste Daroussin <bapt@FreeBSD.org>
3286196Sbapt * All rights reserved.
4286196Sbapt *
5286196Sbapt * Redistribution and use in source and binary forms, with or without
6286196Sbapt * modification, are permitted provided that the following conditions
7286196Sbapt * are met:
8286196Sbapt * 1. Redistributions of source code must retain the above copyright
9286196Sbapt *    notice, this list of conditions and the following disclaimer
10286196Sbapt *    in this position and unchanged.
11286196Sbapt * 2. Redistributions in binary form must reproduce the above copyright
12286196Sbapt *    notice, this list of conditions and the following disclaimer in the
13286196Sbapt *    documentation and/or other materials provided with the distribution.
14286196Sbapt *
15286196Sbapt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16286196Sbapt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17286196Sbapt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18286196Sbapt * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19286196Sbapt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20286196Sbapt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21286196Sbapt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22286196Sbapt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23286196Sbapt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24286196Sbapt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25286196Sbapt */
26286196Sbapt
27286196Sbapt#include <sys/cdefs.h>
28286196Sbapt__FBSDID("$FreeBSD$");
29286196Sbapt
30286196Sbapt#include <sys/wait.h>
31286196Sbapt
32286196Sbapt#include <err.h>
33286196Sbapt#include <sysexits.h>
34286196Sbapt#include <stdlib.h>
35286201Sbapt#include <string.h>
36286201Sbapt#include <unistd.h>
37286196Sbapt
38286196Sbapt#include "pw.h"
39286196Sbapt
40286196Sbaptint
41286196Sbaptpw_checkfd(char *nptr)
42286196Sbapt{
43286196Sbapt	const char *errstr;
44286196Sbapt	int fd = -1;
45286196Sbapt
46286196Sbapt	if (strcmp(nptr, "-") == 0)
47286196Sbapt		return '-';
48286196Sbapt	fd = strtonum(nptr, 0, INT_MAX, &errstr);
49286196Sbapt	if (errstr != NULL)
50286196Sbapt		errx(EX_USAGE, "Bad file descriptor '%s': %s",
51286196Sbapt		    nptr, errstr);
52286196Sbapt	return (fd);
53286196Sbapt}
54286196Sbapt
55286196Sbaptuintmax_t
56286196Sbaptpw_checkid(char *nptr, uintmax_t maxval)
57286196Sbapt{
58286196Sbapt	const char *errstr = NULL;
59286196Sbapt	uintmax_t id;
60286196Sbapt
61286196Sbapt	id = strtounum(nptr, 0, maxval, &errstr);
62286196Sbapt	if (errstr)
63286196Sbapt		errx(EX_USAGE, "Bad id '%s': %s", nptr, errstr);
64286196Sbapt	return (id);
65286196Sbapt}
66286196Sbapt
67286196Sbaptstruct userconf *
68286196Sbaptget_userconfig(const char *config)
69286196Sbapt{
70286196Sbapt	char defaultcfg[MAXPATHLEN];
71286196Sbapt
72286196Sbapt	if (config != NULL)
73286196Sbapt		return (read_userconfig(config));
74286196Sbapt	snprintf(defaultcfg, sizeof(defaultcfg), "%s/pw.conf", conf.etcpath);
75286196Sbapt	return (read_userconfig(defaultcfg));
76286196Sbapt}
77286196Sbapt
78286196Sbaptint
79286196Sbaptnis_update(void) {
80286196Sbapt	pid_t pid;
81286196Sbapt	int i;
82286196Sbapt
83286196Sbapt	fflush(NULL);
84286196Sbapt	if ((pid = fork()) == -1) {
85286196Sbapt		warn("fork()");
86286196Sbapt		return (1);
87286196Sbapt	}
88286196Sbapt	if (pid == 0) {
89286196Sbapt		execlp("/usr/bin/make", "make", "-C", "/var/yp/", (char*) NULL);
90286196Sbapt		_exit(1);
91286196Sbapt	}
92286196Sbapt	waitpid(pid, &i, 0);
93286196Sbapt	if ((i = WEXITSTATUS(i)) != 0)
94286196Sbapt		errx(i, "make exited with status %d", i);
95286196Sbapt	return (i);
96286196Sbapt}
97