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/types.h>
31286196Sbapt#include <sys/wait.h>
32286196Sbapt
33286196Sbapt#include <err.h>
34286196Sbapt#include <inttypes.h>
35286196Sbapt#include <sysexits.h>
36286196Sbapt#include <limits.h>
37286196Sbapt#include <stdlib.h>
38287084Sbapt#include <string.h>
39287084Sbapt#include <unistd.h>
40286196Sbapt
41286196Sbapt#include "pw.h"
42286196Sbapt
43286196Sbaptint
44286196Sbaptpw_checkfd(char *nptr)
45286196Sbapt{
46286196Sbapt	const char *errstr;
47286196Sbapt	int fd = -1;
48286196Sbapt
49286196Sbapt	if (strcmp(nptr, "-") == 0)
50286196Sbapt		return '-';
51286196Sbapt	fd = strtonum(nptr, 0, INT_MAX, &errstr);
52286196Sbapt	if (errstr != NULL)
53286196Sbapt		errx(EX_USAGE, "Bad file descriptor '%s': %s",
54286196Sbapt		    nptr, errstr);
55286196Sbapt	return (fd);
56286196Sbapt}
57286196Sbapt
58286196Sbaptuintmax_t
59286196Sbaptpw_checkid(char *nptr, uintmax_t maxval)
60286196Sbapt{
61286196Sbapt	const char *errstr = NULL;
62286196Sbapt	uintmax_t id;
63286196Sbapt
64286196Sbapt	id = strtounum(nptr, 0, maxval, &errstr);
65286196Sbapt	if (errstr)
66286196Sbapt		errx(EX_USAGE, "Bad id '%s': %s", nptr, errstr);
67286196Sbapt	return (id);
68286196Sbapt}
69286196Sbapt
70286196Sbaptstruct userconf *
71286196Sbaptget_userconfig(const char *config)
72286196Sbapt{
73286196Sbapt	char defaultcfg[MAXPATHLEN];
74286196Sbapt
75286196Sbapt	if (config != NULL)
76286196Sbapt		return (read_userconfig(config));
77286196Sbapt	snprintf(defaultcfg, sizeof(defaultcfg), "%s/pw.conf", conf.etcpath);
78286196Sbapt	return (read_userconfig(defaultcfg));
79286196Sbapt}
80286196Sbapt
81286196Sbaptint
82286196Sbaptnis_update(void) {
83286196Sbapt	pid_t pid;
84286196Sbapt	int i;
85286196Sbapt
86286196Sbapt	fflush(NULL);
87286196Sbapt	if ((pid = fork()) == -1) {
88286196Sbapt		warn("fork()");
89286196Sbapt		return (1);
90286196Sbapt	}
91286196Sbapt	if (pid == 0) {
92286196Sbapt		execlp("/usr/bin/make", "make", "-C", "/var/yp/", (char*) NULL);
93286196Sbapt		_exit(1);
94286196Sbapt	}
95286196Sbapt	waitpid(pid, &i, 0);
96286196Sbapt	if ((i = WEXITSTATUS(i)) != 0)
97286196Sbapt		errx(i, "make exited with status %d", i);
98286196Sbapt	return (i);
99286196Sbapt}
100