pw_utils.c revision 286196
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: head/usr.sbin/pw/pw_utils.c 286196 2015-08-02 12:47:50Z bapt $");
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>
38286196Sbapt
39286196Sbapt#include "pw.h"
40286196Sbapt
41286196Sbaptint
42286196Sbaptpw_checkfd(char *nptr)
43286196Sbapt{
44286196Sbapt	const char *errstr;
45286196Sbapt	int fd = -1;
46286196Sbapt
47286196Sbapt	if (strcmp(nptr, "-") == 0)
48286196Sbapt		return '-';
49286196Sbapt	fd = strtonum(nptr, 0, INT_MAX, &errstr);
50286196Sbapt	if (errstr != NULL)
51286196Sbapt		errx(EX_USAGE, "Bad file descriptor '%s': %s",
52286196Sbapt		    nptr, errstr);
53286196Sbapt	return (fd);
54286196Sbapt}
55286196Sbapt
56286196Sbaptuintmax_t
57286196Sbaptpw_checkid(char *nptr, uintmax_t maxval)
58286196Sbapt{
59286196Sbapt	const char *errstr = NULL;
60286196Sbapt	uintmax_t id;
61286196Sbapt
62286196Sbapt	id = strtounum(nptr, 0, maxval, &errstr);
63286196Sbapt	if (errstr)
64286196Sbapt		errx(EX_USAGE, "Bad id '%s': %s", nptr, errstr);
65286196Sbapt	return (id);
66286196Sbapt}
67286196Sbapt
68286196Sbaptstruct userconf *
69286196Sbaptget_userconfig(const char *config)
70286196Sbapt{
71286196Sbapt	char defaultcfg[MAXPATHLEN];
72286196Sbapt
73286196Sbapt	if (config != NULL)
74286196Sbapt		return (read_userconfig(config));
75286196Sbapt	snprintf(defaultcfg, sizeof(defaultcfg), "%s/pw.conf", conf.etcpath);
76286196Sbapt	return (read_userconfig(defaultcfg));
77286196Sbapt}
78286196Sbapt
79286196Sbaptint
80286196Sbaptnis_update(void) {
81286196Sbapt	pid_t pid;
82286196Sbapt	int i;
83286196Sbapt
84286196Sbapt	fflush(NULL);
85286196Sbapt	if ((pid = fork()) == -1) {
86286196Sbapt		warn("fork()");
87286196Sbapt		return (1);
88286196Sbapt	}
89286196Sbapt	if (pid == 0) {
90286196Sbapt		execlp("/usr/bin/make", "make", "-C", "/var/yp/", (char*) NULL);
91286196Sbapt		_exit(1);
92286196Sbapt	}
93286196Sbapt	waitpid(pid, &i, 0);
94286196Sbapt	if ((i = WEXITSTATUS(i)) != 0)
95286196Sbapt		errx(i, "make exited with status %d", i);
96286196Sbapt	return (i);
97286196Sbapt}
98