1 /*-
2 * Copyright (c) 2003-2005 MAEKAWA Masahide <maekawa@cvsync.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 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/types.h>
31 #include <sys/mman.h>
32 #include <sys/stat.h>
33
34 #include <stdio.h>
35 #include <stdlib.h>
36
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <fnmatch.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 #include "compat_stdbool.h"
44
45 #include "distfile.h"
46 #include "logmsg.h"
47 #include "token.h"
48
49 static const struct token_keyword distfile_keywords[] = {
50 { "allow", 5, DISTFILE_ALLOW },
51 { "deny", 4, DISTFILE_DENY },
52 { "nordiff", 7, DISTFILE_NORDIFF },
53 { "omitany", 7, DISTFILE_DENY },
54 { "upgrade", 7, DISTFILE_ALLOW },
55 { NULL, 0, DISTFILE_DENY },
56 };
57
58 struct distfile_args *distfile_parse(FILE *);
59 bool distfile_parse_pattern(FILE *, struct distent *);
60
61 struct distfile_args *
distfile_open(const char * fname)62 distfile_open(const char *fname)
63 {
64 struct distfile_args *da;
65 struct stat st;
66 FILE *fp;
67 int fd;
68
69 if ((fd = open(fname, O_RDONLY, 0)) == -1) {
70 logmsg_err("%s: %s", fname, strerror(errno));
71 return (NULL);
72 }
73 if (fstat(fd, &st) == -1) {
74 logmsg_err("%s: %s", fname, strerror(errno));
75 (void)close(fd);
76 return (NULL);
77 }
78 if (st.st_size > 0) {
79 if ((fp = fdopen(fd, "r")) == NULL) {
80 logmsg_err("%s: %s", fname, strerror(errno));
81 (void)close(fd);
82 return (NULL);
83 }
84 if ((da = distfile_parse(fp)) == NULL) {
85 (void)fclose(fp);
86 return (NULL);
87 }
88 if (fclose(fp) == EOF) {
89 logmsg_err("%s: %s", fname, strerror(errno));
90 distfile_close(da);
91 return (NULL);
92 }
93 } else {
94 if (close(fd) == -1) {
95 logmsg_err("%s: %s", fname, strerror(errno));
96 return (NULL);
97 }
98 if ((da = malloc(sizeof(*da))) == NULL) {
99 logmsg_err("%s", strerror(errno));
100 return (NULL);
101 }
102 (void)memset(da, 0, sizeof(*da));
103 }
104
105 return (da);
106 }
107
108 void
distfile_close(struct distfile_args * da)109 distfile_close(struct distfile_args *da)
110 {
111 size_t i;
112
113 if (da == NULL)
114 return;
115
116 if (da->da_patterns != NULL) {
117 for (i = 0 ; i < da->da_size ; i++)
118 free(da->da_patterns[i].de_pattern);
119 free(da->da_patterns);
120 }
121
122 free(da);
123 }
124
125 struct distfile_args *
distfile_parse(FILE * fp)126 distfile_parse(FILE *fp)
127 {
128 struct distent *de;
129 struct distfile_args *da;
130 size_t max = 0;
131
132 lineno = 1;
133
134 if ((da = malloc(sizeof(*da))) == NULL) {
135 logmsg_err("%s", strerror(errno));
136 return (NULL);
137 }
138 da->da_patterns = NULL;
139 da->da_size = 0;
140
141 for (;;) {
142 if (!token_skip_whitespace(fp)) {
143 if (feof(fp) == 0) {
144 logmsg_err("Distfile Error: premature EOF");
145 distfile_close(da);
146 return (NULL);
147 }
148 break;
149 }
150
151 if (da->da_size == max) {
152 struct distent *newptr;
153 size_t old = max, new = old + 4;
154
155 if ((newptr = malloc(new * sizeof(*newptr))) == NULL) {
156 logmsg_err("%s", strerror(errno));
157 distfile_close(da);
158 return (NULL);
159 }
160
161 if (da->da_patterns != NULL) {
162 (void)memcpy(newptr, da->da_patterns,
163 old * sizeof(*newptr));
164 free(da->da_patterns);
165 }
166 da->da_patterns = newptr;
167 max = new;
168 }
169
170 de = &da->da_patterns[da->da_size];
171
172 if (!distfile_parse_pattern(fp, de)) {
173 distfile_close(da);
174 return (NULL);
175 }
176
177 da->da_size++;
178 }
179
180 if (da->da_size == 0) {
181 if (da->da_patterns != NULL)
182 free(da->da_patterns);
183 da->da_patterns = NULL;
184 }
185
186 return (da);
187 }
188
189 bool
distfile_parse_pattern(FILE * fp,struct distent * de)190 distfile_parse_pattern(FILE *fp, struct distent *de)
191 {
192 const struct token_keyword *key;
193 struct token tk;
194
195 if ((key = token_get_keyword(fp, distfile_keywords)) == NULL)
196 return (false);
197
198 if (!token_get_string(fp, &tk))
199 return (false);
200
201 if ((de->de_pattern = malloc(tk.length + 1)) == NULL) {
202 logmsg_err("%s", strerror(errno));
203 return (false);
204 }
205 (void)memcpy(de->de_pattern, tk.token, tk.length);
206 de->de_pattern[tk.length] = '\0';
207 de->de_status = key->type;
208
209 return (true);
210 }
211
212 int
distfile_access(struct distfile_args * da,const char * filename)213 distfile_access(struct distfile_args *da, const char *filename)
214 {
215 struct distent *de;
216 size_t i;
217
218 if (da->da_size == 0)
219 return (DISTFILE_ALLOW);
220
221 for (i = 0 ; i < da->da_size ; i++) {
222 de = &da->da_patterns[i];
223 if (fnmatch(de->de_pattern, filename, 0) != FNM_NOMATCH)
224 return (de->de_status);
225 }
226
227 return (DISTFILE_ALLOW);
228 }
229