xref: /freebsd/usr.sbin/pw/pw_utils.c (revision 182ed3c0755f1bf161d8be02016b5f6cf9b57556)
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
pw_checkfd(char * nptr)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
pw_checkid(char * nptr,uintmax_t maxval)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 *
get_userconfig(const char * config)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
nis_update(void)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 
96 static void
metalog_emit_record(const char * path,const char * target,mode_t mode,uid_t uid,gid_t gid,int flags)97 metalog_emit_record(const char *path, const char *target, mode_t mode,
98     uid_t uid, gid_t gid, int flags)
99 {
100 	const char *flagstr, *type;
101 	int error;
102 
103 	if (conf.metalog == NULL)
104 		return;
105 
106 	if (target != NULL)
107 		type = "link";
108 	else if (S_ISDIR(mode))
109 		type = "dir";
110 	else if (S_ISREG(mode))
111 		type = "file";
112 	else
113 		errx(1, "metalog_emit: unhandled file type for %s", path);
114 
115 	flagstr = fflagstostr(flags &
116 	    (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND));
117 	if (flagstr == NULL)
118 		errx(1, "metalog_emit: fflagstostr failed");
119 
120 	error = fprintf(conf.metalog,
121 	    "./%s type=%s mode=0%03o uid=%u gid=%u%s%s%s%s\n",
122 	    path, type, mode & ACCESSPERMS, uid, gid,
123 	    target != NULL ? " link=" : "", target != NULL ? target : "",
124 	    *flagstr != '\0' ? " flags=" : "", *flagstr != '\0' ? flagstr : "");
125 	if (error < 0)
126 		errx(1, "metalog_emit: write error");
127 }
128 
129 void
metalog_emit(const char * path,mode_t mode,uid_t uid,gid_t gid,int flags)130 metalog_emit(const char *path, mode_t mode, uid_t uid, gid_t gid, int flags)
131 {
132 	metalog_emit_record(path, NULL, mode, uid, gid, flags);
133 }
134 
135 void
metalog_emit_symlink(const char * path,const char * target,mode_t mode,uid_t uid,gid_t gid)136 metalog_emit_symlink(const char *path, const char *target, mode_t mode,
137     uid_t uid, gid_t gid)
138 {
139 	metalog_emit_record(path, target, mode, uid, gid, 0);
140 }
141