pw_utils.c revision 287084
1139826Simp/*-
278064Sume * Copyright (C) 2015 Baptiste Daroussin <bapt@FreeBSD.org>
378064Sume * All rights reserved.
478064Sume *
578064Sume * Redistribution and use in source and binary forms, with or without
678064Sume * modification, are permitted provided that the following conditions
778064Sume * are met:
878064Sume * 1. Redistributions of source code must retain the above copyright
978064Sume *    notice, this list of conditions and the following disclaimer
1078064Sume *    in this position and unchanged.
1178064Sume * 2. Redistributions in binary form must reproduce the above copyright
1278064Sume *    notice, this list of conditions and the following disclaimer in the
1378064Sume *    documentation and/or other materials provided with the distribution.
1478064Sume *
1578064Sume * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
1678064Sume * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1778064Sume * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1878064Sume * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
1978064Sume * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2078064Sume * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2178064Sume * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2278064Sume * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2378064Sume * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2478064Sume * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2578064Sume */
2678064Sume
2778064Sume#include <sys/cdefs.h>
28174510Sobrien__FBSDID("$FreeBSD: stable/10/usr.sbin/pw/pw_utils.c 287084 2015-08-23 21:42:27Z bapt $");
29174510Sobrien
30174510Sobrien#include <sys/types.h>
3178064Sume#include <sys/wait.h>
3278064Sume
3378064Sume#include <err.h>
3478064Sume#include <inttypes.h>
3578064Sume#include <sysexits.h>
3678064Sume#include <limits.h>
3778064Sume#include <stdlib.h>
3878064Sume#include <string.h>
3978064Sume#include <unistd.h>
40253081Sae
41253081Sae#include "pw.h"
42253081Sae
43253081Saeint
44253081Saepw_checkfd(char *nptr)
45253081Sae{
4678064Sume	const char *errstr;
47253081Sae	int fd = -1;
4878064Sume
4978064Sume	if (strcmp(nptr, "-") == 0)
5078064Sume		return '-';
51253085Sae	fd = strtonum(nptr, 0, INT_MAX, &errstr);
52253085Sae	if (errstr != NULL)
53253085Sae		errx(EX_USAGE, "Bad file descriptor '%s': %s",
54253085Sae		    nptr, errstr);
55253085Sae	return (fd);
56252007Sae}
57253085Sae
5878064Sumeuintmax_t
5978064Sumepw_checkid(char *nptr, uintmax_t maxval)
60{
61	const char *errstr = NULL;
62	uintmax_t id;
63
64	id = strtounum(nptr, 0, maxval, &errstr);
65	if (errstr)
66		errx(EX_USAGE, "Bad id '%s': %s", nptr, errstr);
67	return (id);
68}
69
70struct userconf *
71get_userconfig(const char *config)
72{
73	char defaultcfg[MAXPATHLEN];
74
75	if (config != NULL)
76		return (read_userconfig(config));
77	snprintf(defaultcfg, sizeof(defaultcfg), "%s/pw.conf", conf.etcpath);
78	return (read_userconfig(defaultcfg));
79}
80
81int
82nis_update(void) {
83	pid_t pid;
84	int i;
85
86	fflush(NULL);
87	if ((pid = fork()) == -1) {
88		warn("fork()");
89		return (1);
90	}
91	if (pid == 0) {
92		execlp("/usr/bin/make", "make", "-C", "/var/yp/", (char*) NULL);
93		_exit(1);
94	}
95	waitpid(pid, &i, 0);
96	if ((i = WEXITSTATUS(i)) != 0)
97		errx(i, "make exited with status %d", i);
98	return (i);
99}
100