xref: /freebsd/usr.sbin/pw/pw_utils.c (revision 732a02b4e77866604a120a275c082bb6221bd2ff)
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/wait.h>
31 
32 #include <err.h>
33 #include <sysexits.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 
38 #include "pw.h"
39 
40 int
41 pw_checkfd(char *nptr)
42 {
43 	const char *errstr;
44 	int fd = -1;
45 
46 	if (strcmp(nptr, "-") == 0)
47 		return '-';
48 	fd = strtonum(nptr, 0, INT_MAX, &errstr);
49 	if (errstr != NULL)
50 		errx(EX_USAGE, "Bad file descriptor '%s': %s",
51 		    nptr, errstr);
52 	return (fd);
53 }
54 
55 uintmax_t
56 pw_checkid(char *nptr, uintmax_t maxval)
57 {
58 	const char *errstr = NULL;
59 	uintmax_t id;
60 
61 	id = strtounum(nptr, 0, maxval, &errstr);
62 	if (errstr)
63 		errx(EX_USAGE, "Bad id '%s': %s", nptr, errstr);
64 	return (id);
65 }
66 
67 struct userconf *
68 get_userconfig(const char *config)
69 {
70 	char defaultcfg[MAXPATHLEN];
71 
72 	if (config != NULL)
73 		return (read_userconfig(config));
74 	snprintf(defaultcfg, sizeof(defaultcfg), "%s/" _PW_CONF, conf.etcpath);
75 	return (read_userconfig(defaultcfg));
76 }
77 
78 int
79 nis_update(void) {
80 	pid_t pid;
81 	int i;
82 
83 	fflush(NULL);
84 	if ((pid = fork()) == -1) {
85 		warn("fork()");
86 		return (1);
87 	}
88 	if (pid == 0) {
89 		execlp("/usr/bin/make", "make", "-C", "/var/yp/", (char*) NULL);
90 		_exit(1);
91 	}
92 	waitpid(pid, &i, 0);
93 	if ((i = WEXITSTATUS(i)) != 0)
94 		errx(i, "make exited with status %d", i);
95 	return (i);
96 }
97