xref: /freebsd/usr.sbin/pw/pw_utils.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1 /*-
2  * Copyright (C) 2015 Baptiste Daroussin <bapt@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/types.h>
31 #include <sys/wait.h>
32 
33 #include <err.h>
34 #include <inttypes.h>
35 #include <sysexits.h>
36 #include <limits.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 #include "pw.h"
42 
43 int
44 pw_checkfd(char *nptr)
45 {
46 	const char *errstr;
47 	int fd = -1;
48 
49 	if (strcmp(nptr, "-") == 0)
50 		return '-';
51 	fd = strtonum(nptr, 0, INT_MAX, &errstr);
52 	if (errstr != NULL)
53 		errx(EX_USAGE, "Bad file descriptor '%s': %s",
54 		    nptr, errstr);
55 	return (fd);
56 }
57 
58 uintmax_t
59 pw_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 
70 struct userconf *
71 get_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 
81 int
82 nis_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