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