xref: /openbsd/usr.bin/cvs/trigger.c (revision b9fc9a72)
1 /*	$OpenBSD: trigger.c,v 1.22 2015/01/16 06:40:07 deraadt Exp $	*/
2 /*
3  * Copyright (c) 2008 Tobias Stoeckmann <tobias@openbsd.org>
4  * Copyright (c) 2008 Jonathan Armani <dbd@asystant.net>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/queue.h>
20 #include <sys/types.h>
21 
22 #include <ctype.h>
23 #include <errno.h>
24 #include <libgen.h>
25 #include <pwd.h>
26 #include <regex.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 
32 #include "config.h"
33 #include "cvs.h"
34 
35 static int	 expand_args(BUF *, struct file_info_list *, const char *,
36     const char *, char *);
37 static int	 expand_var(BUF *, const char *);
38 static char	*parse_cmd(int, char *, const char *, struct file_info_list *);
39 
40 static int
41 expand_args(BUF *buf, struct file_info_list *file_info, const char *repo,
42     const char *allowed_args, char *format)
43 {
44 	int oldstyle, quote;
45 	struct file_info *fi = NULL;
46 	char *p, valbuf[2] = { '\0', '\0' };
47 	const char *val;
48 
49 	if (file_info != NULL && !TAILQ_EMPTY(file_info))
50 		fi = TAILQ_FIRST(file_info);
51 
52 	quote = oldstyle = 0;
53 
54 	/* Why does GNU cvs print something if it encounters %{}? */
55 	if (*format == '\0')
56 		oldstyle = 1;
57 
58 	for (p = format; *p != '\0'; p++) {
59 		if (*p != '%' && strchr(allowed_args, *p) == NULL)
60 			return 1;
61 
62 		switch (*p) {
63 		case 's':
64 		case 'V':
65 		case 'v':
66 			quote = 1;
67 			oldstyle = 1;
68 			break;
69 		default:
70 			break;
71 		}
72 	}
73 	if (quote)
74 		buf_putc(buf, '"');
75 	if (oldstyle) {
76 		buf_puts(buf, repo);
77 		buf_putc(buf, ' ');
78 	}
79 
80 	if (*format == '\0')
81 		return 0;
82 
83 	/*
84 	 * check like this, add only uses loginfo for directories anyway
85 	 */
86 	if (cvs_cmdop == CVS_OP_ADD) {
87 		buf_puts(buf, "- New directory");
88 		if (quote)
89 			buf_putc(buf, '"');
90 		return (0);
91 	}
92 
93 	if (cvs_cmdop == CVS_OP_IMPORT) {
94 		buf_puts(buf, "- Imported sources");
95 		if (quote)
96 			buf_putc(buf, '"');
97 		return (0);
98 	}
99 
100 	for (;;) {
101 		for (p = format; *p != '\0';) {
102 			val = NULL;
103 
104 			switch (*p) {
105 			case '%':
106 				val = "%";
107 				break;
108 			case 'b':
109 				if (fi != NULL) {
110 					valbuf[0] = fi->tag_type;
111 					val = valbuf;
112 				}
113 				break;
114 			case 'o':
115 				if (fi != NULL)
116 					val = fi->tag_op;
117 				break;
118 			case 'p':
119 				val = current_cvsroot->cr_dir;
120 				break;
121 			case 'r':
122 				val = repo;
123 				break;
124 			case 'l':
125 			case 'S':
126 			case 's':
127 				if (fi != NULL)
128 					val = fi->file_path;
129 				break;
130 			case 't':
131 				if (fi != NULL)
132 					val = fi->tag_new;
133 				break;
134 			case 'V':
135 				if (fi != NULL) {
136 					if (fi->crevstr != NULL &&
137 					    !strcmp(fi->crevstr,
138 					    "Non-existent"))
139 						val = "NONE";
140 					else
141 						val = fi->crevstr;
142 				}
143 				break;
144 			case 'v':
145 				if (fi != NULL) {
146 					if (fi->nrevstr != NULL &&
147 					    !strcmp(fi->nrevstr, "Removed"))
148 						val = "NONE";
149 					else
150 						val = fi->nrevstr;
151 				}
152 				break;
153 			default:
154 				return 1;
155 			}
156 
157 			if (val != NULL)
158 				buf_puts(buf, val);
159 
160 			if (*(++p) != '\0')
161 				buf_putc(buf, ',');
162 		}
163 
164 		if (fi != NULL)
165 			fi = TAILQ_NEXT(fi, flist);
166 		if (fi == NULL)
167 			break;
168 
169 		if (strlen(format) == 1 && (*format == '%' || *format == 'o' ||
170 		    *format == 'p' || *format == 'r' || *format == 't'))
171 			break;
172 
173 		buf_putc(buf, ' ');
174 	}
175 
176 	if (quote)
177 		buf_putc(buf, '"');
178 
179 	return 0;
180 }
181 
182 static int
183 expand_var(BUF *buf, const char *var)
184 {
185 	struct passwd *pw;
186 	const char *val;
187 
188 	if (*var == '=') {
189 		if ((val = cvs_var_get(++var)) == NULL) {
190 			cvs_log(LP_ERR, "no such user variable ${=%s}", var);
191 			return (1);
192 		}
193 		buf_puts(buf, val);
194 	} else {
195 		if (strcmp(var, "CVSEDITOR") == 0 ||
196 		    strcmp(var, "EDITOR") == 0 ||
197 		    strcmp(var, "VISUAL") == 0)
198 			buf_puts(buf, cvs_editor);
199 		else if (strcmp(var, "CVSROOT") == 0)
200 			buf_puts(buf, current_cvsroot->cr_dir);
201 		else if (strcmp(var, "USER") == 0) {
202 			pw = getpwuid(geteuid());
203 			if (pw == NULL) {
204 				cvs_log(LP_ERR, "unable to retrieve "
205 				    "caller ID");
206 				return (1);
207 			}
208 			buf_puts(buf, pw->pw_name);
209 		} else if (strcmp(var, "RCSBIN") == 0) {
210 			cvs_log(LP_ERR, "RCSBIN internal variable is no "
211 			    "longer supported");
212 			return (1);
213 		} else {
214 			cvs_log(LP_ERR, "no such internal variable $%s", var);
215 			return (1);
216 		}
217 	}
218 
219 	return (0);
220 }
221 
222 static char *
223 parse_cmd(int type, char *cmd, const char *repo,
224     struct file_info_list *file_info)
225 {
226 	int expanded = 0;
227 	char argbuf[2] = { '\0', '\0' };
228 	char *allowed_args, *default_args, *args, *file, *p, *q = NULL;
229 	size_t pos;
230 	BUF *buf;
231 
232 	switch (type) {
233 	case CVS_TRIGGER_COMMITINFO:
234 		allowed_args = "prsS{}";
235 		default_args = " %p/%r %S";
236 		file = CVS_PATH_COMMITINFO;
237 		break;
238 	case CVS_TRIGGER_LOGINFO:
239 		allowed_args = "prsSvVt{}";
240 		default_args = NULL;
241 		file = CVS_PATH_LOGINFO;
242 		break;
243 	case CVS_TRIGGER_VERIFYMSG:
244 		allowed_args = "l";
245 		default_args = " %l";
246 		file = CVS_PATH_VERIFYMSG;
247 		break;
248 	case CVS_TRIGGER_TAGINFO:
249 		allowed_args = "btoprsSvV{}";
250 		default_args = " %t %o %p/%r %{sv}";
251 		file = CVS_PATH_TAGINFO;
252 		break;
253 	default:
254 		return (NULL);
255 	}
256 
257 	/* before doing any stuff, check if the command starts with % */
258 	for (p = cmd;
259 	     *p != '%' && !isspace((unsigned char)*p) && *p != '\0'; p++)
260 		;
261 	if (*p == '%')
262 		return (NULL);
263 
264 	buf = buf_alloc(1024);
265 
266 	p = cmd;
267 again:
268 	for (; *p != '\0'; p++) {
269 		if ((pos = strcspn(p, "$%")) != 0) {
270 			buf_append(buf, p, pos);
271 			p += pos;
272 		}
273 
274 		q = NULL;
275 		if (*p == '\0')
276 			break;
277 		if (*p++ == '$') {
278 			if (*p == '{') {
279 				pos = strcspn(++p, "}");
280 				if (p[pos] == '\0')
281 					goto bad;
282 			} else {
283 				for (pos = 0; isalpha(p[pos]); pos++)
284 					;
285 				if (pos == 0) {
286 					cvs_log(LP_ERR,
287 					    "unrecognized variable syntax");
288 					goto bad;
289 				}
290 			}
291 			q = xmalloc(pos + 1);
292 			memcpy(q, p, pos);
293 			q[pos] = '\0';
294 			if (expand_var(buf, q))
295 				goto bad;
296 			p += pos - (*(p - 1) == '{' ? 0 : 1);
297 		} else {
298 			switch (*p) {
299 			case '\0':
300 				goto bad;
301 			case '{':
302 				if (strchr(allowed_args, '{') == NULL)
303 					goto bad;
304 				pos = strcspn(++p, "}");
305 				if (p[pos] == '\0')
306 					goto bad;
307 				q = xmalloc(pos + 1);
308 				memcpy(q, p, pos);
309 				q[pos] = '\0';
310 				args = q;
311 				p += pos;
312 				break;
313 			default:
314 				argbuf[0] = *p;
315 				args = argbuf;
316 				break;
317 			}
318 
319 			if (expand_args(buf, file_info, repo, allowed_args,
320 			    args))
321 				goto bad;
322 			expanded = 1;
323 		}
324 
325 		if (q != NULL)
326 			xfree(q);
327 	}
328 
329 	if (!expanded && default_args != NULL) {
330 		p = default_args;
331 		expanded = 1;
332 		goto again;
333 	}
334 
335 	buf_putc(buf, '\0');
336 	return (buf_release(buf));
337 
338 bad:
339 	if (q != NULL)
340 		xfree(q);
341 	cvs_log(LP_NOTICE, "%s contains malformed command '%s'", file, cmd);
342 	buf_free(buf);
343 	return (NULL);
344 }
345 
346 int
347 cvs_trigger_handle(int type, char *repo, char *in, struct trigger_list *list,
348     struct file_info_list *files)
349 {
350 	int r;
351 	char *cmd;
352 	struct trigger_line *line;
353 
354 	TAILQ_FOREACH(line, list, flist) {
355 		if ((cmd = parse_cmd(type, line->line, repo, files)) == NULL)
356 			return (1);
357 		switch(type) {
358 		case CVS_TRIGGER_COMMITINFO:
359 		case CVS_TRIGGER_TAGINFO:
360 		case CVS_TRIGGER_VERIFYMSG:
361 			if ((r = cvs_exec(cmd, NULL, 1)) != 0) {
362 				xfree(cmd);
363 				return (r);
364 			}
365 			break;
366 		default:
367 			(void)cvs_exec(cmd, in, 1);
368 			break;
369 		}
370 		xfree(cmd);
371 	}
372 
373 	return (0);
374 }
375 
376 struct trigger_list *
377 cvs_trigger_getlines(char * file, char * repo)
378 {
379 	FILE *fp;
380 	int allow_all, lineno, match = 0;
381 	size_t len;
382 	regex_t preg;
383 	struct trigger_list *list;
384 	struct trigger_line *tline;
385 	char fpath[PATH_MAX];
386 	char *currentline, *defaultline = NULL, *nline, *p, *q, *regex;
387 
388 	if (strcmp(file, CVS_PATH_EDITINFO) == 0 ||
389 	    strcmp(file, CVS_PATH_VERIFYMSG) == 0)
390 		allow_all = 0;
391 	else
392 		allow_all = 1;
393 
394 	(void)xsnprintf(fpath, PATH_MAX, "%s/%s", current_cvsroot->cr_dir,
395 	    file);
396 
397 	if ((fp = fopen(fpath, "r")) == NULL) {
398 		if (errno != ENOENT)
399 			cvs_log(LP_ERRNO, "cvs_trigger_getlines: %s", file);
400 		return (NULL);
401 	}
402 
403 	list = xmalloc(sizeof(*list));
404 	TAILQ_INIT(list);
405 
406 	lineno = 0;
407 	nline = NULL;
408 	while ((currentline = fgetln(fp, &len)) != NULL) {
409 		if (currentline[len - 1] == '\n') {
410 			currentline[len - 1] = '\0';
411 		} else {
412 			nline = xmalloc(len + 1);
413 			memcpy(nline, currentline, len);
414 			nline[len] = '\0';
415 			currentline = nline;
416 		}
417 
418 		lineno++;
419 
420 		for (p = currentline; isspace((unsigned char)*p); p++)
421 			;
422 
423 		if (*p == '\0' || *p == '#')
424 			continue;
425 
426 		for (q = p; !isspace((unsigned char)*q) && *q != '\0'; q++)
427 			;
428 
429 		if (*q == '\0')
430 			goto bad;
431 
432 		*q++ = '\0';
433 		regex = p;
434 
435 		for (; isspace((unsigned char)*q); q++)
436 			;
437 
438 		if (*q == '\0')
439 			goto bad;
440 
441 		if (strcmp(regex, "ALL") == 0 && allow_all) {
442 			tline = xmalloc(sizeof(*tline));
443 			tline->line = xstrdup(q);
444 			TAILQ_INSERT_TAIL(list, tline, flist);
445 		} else if (defaultline == NULL && !match &&
446 		    strcmp(regex, "DEFAULT") == 0) {
447 			defaultline = xstrdup(q);
448 		} else if (!match) {
449 			if (regcomp(&preg, regex, REG_NOSUB|REG_EXTENDED))
450 				goto bad;
451 
452 			if (regexec(&preg, repo, 0, NULL, 0) != REG_NOMATCH) {
453 				match = 1;
454 
455 				tline = xmalloc(sizeof(*tline));
456 				tline->line = xstrdup(q);
457 				TAILQ_INSERT_HEAD(list, tline, flist);
458 			}
459 			regfree(&preg);
460 		}
461 	}
462 
463 	if (nline != NULL)
464 		xfree(nline);
465 
466 	if (defaultline != NULL) {
467 		if (!match) {
468 			tline = xmalloc(sizeof(*tline));
469 			tline->line = defaultline;
470 			TAILQ_INSERT_HEAD(list, tline, flist);
471 		} else
472 			xfree(defaultline);
473 	}
474 
475 	(void)fclose(fp);
476 
477 	if (TAILQ_EMPTY(list)) {
478 		xfree(list);
479 		list = NULL;
480 	}
481 
482 	return (list);
483 
484 bad:
485 	cvs_log(LP_NOTICE, "%s: malformed line %d", file, lineno);
486 
487 	if (defaultline != NULL)
488 		xfree(defaultline);
489 	cvs_trigger_freelist(list);
490 
491 	(void)fclose(fp);
492 
493 	return (NULL);
494 }
495 
496 void
497 cvs_trigger_freelist(struct trigger_list * list)
498 {
499 	struct trigger_line *line;
500 
501 	while ((line = TAILQ_FIRST(list)) != NULL) {
502 		TAILQ_REMOVE(list, line, flist);
503 		xfree(line->line);
504 		xfree(line);
505 	}
506 
507 	xfree(list);
508 }
509 
510 void
511 cvs_trigger_freeinfo(struct file_info_list * list)
512 {
513 	struct file_info * fi;
514 
515 	while ((fi = TAILQ_FIRST(list)) != NULL) {
516 		TAILQ_REMOVE(list, fi, flist);
517 
518 		if (fi->file_path != NULL)
519 			xfree(fi->file_path);
520 		if (fi->file_wd != NULL)
521 			xfree(fi->file_wd);
522 		if (fi->crevstr != NULL)
523 			xfree(fi->crevstr);
524 		if (fi->nrevstr != NULL)
525 			xfree(fi->nrevstr);
526 		if (fi->tag_new != NULL)
527 			xfree(fi->tag_new);
528 		if (fi->tag_old != NULL)
529 			xfree(fi->tag_old);
530 
531 		xfree(fi);
532 	}
533 }
534 
535 void
536 cvs_trigger_loginfo_header(BUF *buf, char *repo)
537 {
538 	char *dir, pwd[PATH_MAX];
539 	char hostname[HOST_NAME_MAX+1];
540 
541 	if (gethostname(hostname, sizeof(hostname)) == -1) {
542 		fatal("cvs_trigger_loginfo_header: gethostname failed %s",
543 		    strerror(errno));
544 	}
545 
546 	if (getcwd(pwd, sizeof(pwd)) == NULL)
547 		fatal("cvs_trigger_loginfo_header: Cannot get working "
548 		    "directory");
549 
550 	if ((dir = dirname(pwd)) == NULL) {
551 		fatal("cvs_trigger_loginfo_header: dirname failed %s",
552 		    strerror(errno));
553 	}
554 
555 	buf_puts(buf, "Update of ");
556 	buf_puts(buf, current_cvsroot->cr_dir);
557 	buf_putc(buf, '/');
558 	buf_puts(buf, repo);
559 	buf_putc(buf, '\n');
560 
561 	buf_puts(buf, "In directory ");
562 	buf_puts(buf, hostname);
563 	buf_puts(buf, ":");
564 	buf_puts(buf, dir);
565 	buf_putc(buf, '/');
566 	buf_puts(buf, repo);
567 	buf_putc(buf, '\n');
568 	buf_putc(buf, '\n');
569 }
570 
571