Annotation of src/usr.bin/cvs/trigger.c, Revision 1.20
1.20 ! nicm 1: /* $OpenBSD: trigger.c,v 1.19 2010/10/29 17:49:37 nicm Exp $ */
1.1 joris 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>
1.3 joris 24: #include <libgen.h>
1.16 tobias 25: #include <pwd.h>
1.1 joris 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 *);
1.16 tobias 37: static int expand_var(BUF *, const char *);
1.1 joris 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)
1.18 ray 74: buf_putc(buf, '"');
1.1 joris 75: if (oldstyle) {
1.18 ray 76: buf_puts(buf, repo);
77: buf_putc(buf, ' ');
1.1 joris 78: }
79:
80: if (*format == '\0')
81: return 0;
82:
1.7 joris 83: /*
84: * check like this, add only uses loginfo for directories anyway
85: */
86: if (cvs_cmdop == CVS_OP_ADD) {
1.18 ray 87: buf_puts(buf, "- New directory");
1.8 joris 88: if (quote)
1.18 ray 89: buf_putc(buf, '"');
1.8 joris 90: return (0);
91: }
92:
93: if (cvs_cmdop == CVS_OP_IMPORT) {
1.18 ray 94: buf_puts(buf, "- Imported sources");
1.7 joris 95: if (quote)
1.18 ray 96: buf_putc(buf, '"');
1.7 joris 97: return (0);
98: }
99:
1.1 joris 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':
1.12 joris 119: val = current_cvsroot->cr_dir;
1.1 joris 120: break;
121: case 'r':
122: val = repo;
123: break;
124: case 'l':
125: case 'S':
126: case 's':
1.11 joris 127: if (fi != NULL)
128: val = fi->file_path;
1.1 joris 129: break;
130: case 't':
131: if (fi != NULL)
132: val = fi->tag_new;
133: break;
134: case 'V':
1.5 joris 135: if (fi != NULL) {
1.7 joris 136: if (fi->crevstr != NULL &&
137: !strcmp(fi->crevstr,
1.5 joris 138: "Non-existent"))
139: val = "NONE";
140: else
141: val = fi->crevstr;
142: }
1.1 joris 143: break;
144: case 'v':
1.11 joris 145: if (fi != NULL) {
146: if (fi->nrevstr != NULL &&
147: !strcmp(fi->nrevstr, "Removed"))
148: val = "NONE";
149: else
150: val = fi->nrevstr;
151: }
1.1 joris 152: break;
153: default:
154: return 1;
155: }
156:
157: if (val != NULL)
1.18 ray 158: buf_puts(buf, val);
1.1 joris 159:
160: if (*(++p) != '\0')
1.18 ray 161: buf_putc(buf, ',');
1.1 joris 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:
1.18 ray 173: buf_putc(buf, ' ');
1.1 joris 174: }
175:
176: if (quote)
1.18 ray 177: buf_putc(buf, '"');
1.1 joris 178:
179: return 0;
180: }
181:
1.16 tobias 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: }
1.18 ray 193: buf_puts(buf, val);
1.16 tobias 194: } else {
195: if (strcmp(var, "CVSEDITOR") == 0 ||
196: strcmp(var, "EDITOR") == 0 ||
197: strcmp(var, "VISUAL") == 0)
1.18 ray 198: buf_puts(buf, cvs_editor);
1.16 tobias 199: else if (strcmp(var, "CVSROOT") == 0)
1.18 ray 200: buf_puts(buf, current_cvsroot->cr_dir);
1.16 tobias 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: }
1.18 ray 208: buf_puts(buf, pw->pw_name);
1.16 tobias 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:
1.1 joris 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' };
1.9 tobias 228: char *allowed_args, *default_args, *args, *file, *p, *q = NULL;
1.1 joris 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";
1.9 tobias 236: file = CVS_PATH_COMMITINFO;
1.1 joris 237: break;
238: case CVS_TRIGGER_LOGINFO:
1.4 joris 239: allowed_args = "prsSvVt{}";
1.1 joris 240: default_args = NULL;
1.9 tobias 241: file = CVS_PATH_LOGINFO;
1.1 joris 242: break;
243: case CVS_TRIGGER_VERIFYMSG:
244: allowed_args = "l";
245: default_args = " %l";
1.9 tobias 246: file = CVS_PATH_VERIFYMSG;
1.1 joris 247: break;
248: case CVS_TRIGGER_TAGINFO:
249: allowed_args = "btoprsSvV{}";
250: default_args = " %t %o %p/%r %{sv}";
1.9 tobias 251: file = CVS_PATH_TAGINFO;
1.1 joris 252: break;
253: default:
254: return (NULL);
255: }
256:
257: /* before doing any stuff, check if the command starts with % */
258: for (p = cmd; *p != '%' && !isspace(*p) && *p != '\0'; p++)
259: ;
260: if (*p == '%')
261: return (NULL);
262:
1.18 ray 263: buf = buf_alloc(1024);
1.1 joris 264:
265: p = cmd;
266: again:
267: for (; *p != '\0'; p++) {
1.16 tobias 268: if ((pos = strcspn(p, "$%")) != 0) {
1.18 ray 269: buf_append(buf, p, pos);
1.1 joris 270: p += pos;
271: }
272:
1.16 tobias 273: q = NULL;
274: if (*p == '\0')
1.1 joris 275: break;
1.16 tobias 276: if (*p++ == '$') {
277: if (*p == '{') {
278: pos = strcspn(++p, "}");
279: if (p[pos] == '\0')
280: goto bad;
281: } else {
1.17 joris 282: for (pos = 0; isalpha(p[pos]); pos++)
283: ;
284: if (pos == 0) {
1.16 tobias 285: cvs_log(LP_ERR,
286: "unrecognized variable syntax");
287: goto bad;
288: }
289: }
1.1 joris 290: q = xmalloc(pos + 1);
291: memcpy(q, p, pos);
292: q[pos] = '\0';
1.16 tobias 293: if (expand_var(buf, q))
294: goto bad;
1.17 joris 295: p += pos - (*(p - 1) == '{' ? 0 : 1);
1.16 tobias 296: } else {
297: switch (*p) {
298: case '\0':
299: goto bad;
300: case '{':
301: if (strchr(allowed_args, '{') == NULL)
302: goto bad;
303: pos = strcspn(++p, "}");
304: if (p[pos] == '\0')
305: goto bad;
306: q = xmalloc(pos + 1);
307: memcpy(q, p, pos);
308: q[pos] = '\0';
309: args = q;
310: p += pos;
311: break;
312: default:
313: argbuf[0] = *p;
314: args = argbuf;
315: break;
316: }
317:
318: if (expand_args(buf, file_info, repo, allowed_args,
319: args))
320: goto bad;
321: expanded = 1;
1.1 joris 322: }
323:
324: if (q != NULL)
325: xfree(q);
326: }
327:
328: if (!expanded && default_args != NULL) {
329: p = default_args;
330: expanded = 1;
331: goto again;
332: }
333:
1.18 ray 334: buf_putc(buf, '\0');
335: return (buf_release(buf));
1.1 joris 336:
337: bad:
338: if (q != NULL)
339: xfree(q);
1.9 tobias 340: cvs_log(LP_NOTICE, "%s contains malformed command '%s'", file, cmd);
1.18 ray 341: buf_free(buf);
1.1 joris 342: return (NULL);
343: }
344:
345: int
346: cvs_trigger_handle(int type, char *repo, char *in, struct trigger_list *list,
347: struct file_info_list *files)
348: {
349: int r;
350: char *cmd;
351: struct trigger_line *line;
352:
353: TAILQ_FOREACH(line, list, flist) {
1.15 tobias 354: if ((cmd = parse_cmd(type, line->line, repo, files)) == NULL)
355: return (1);
356: switch(type) {
357: case CVS_TRIGGER_COMMITINFO:
358: case CVS_TRIGGER_TAGINFO:
359: case CVS_TRIGGER_VERIFYMSG:
360: if ((r = cvs_exec(cmd, NULL, 1)) != 0) {
361: xfree(cmd);
362: return (r);
1.1 joris 363: }
1.15 tobias 364: break;
365: default:
366: (void)cvs_exec(cmd, in, 1);
367: break;
1.1 joris 368: }
1.15 tobias 369: xfree(cmd);
1.1 joris 370: }
371:
1.15 tobias 372: return (0);
1.1 joris 373: }
374:
375: struct trigger_list *
376: cvs_trigger_getlines(char * file, char * repo)
377: {
378: FILE *fp;
379: int allow_all, lineno, match = 0;
380: size_t len;
381: regex_t preg;
382: struct trigger_list *list;
383: struct trigger_line *tline;
384: char fpath[MAXPATHLEN];
385: char *currentline, *defaultline = NULL, *nline, *p, *q, *regex;
386:
387: if (strcmp(file, CVS_PATH_EDITINFO) == 0 ||
388: strcmp(file, CVS_PATH_VERIFYMSG) == 0)
389: allow_all = 0;
390: else
391: allow_all = 1;
392:
393: (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s", current_cvsroot->cr_dir,
394: file);
395:
396: if ((fp = fopen(fpath, "r")) == NULL) {
397: if (errno != ENOENT)
398: cvs_log(LP_ERRNO, "cvs_trigger_getlines: %s", file);
399: return (NULL);
400: }
401:
1.14 tobias 402: list = xmalloc(sizeof(*list));
403: TAILQ_INIT(list);
404:
1.1 joris 405: lineno = 0;
406: nline = NULL;
1.2 joris 407: while ((currentline = fgetln(fp, &len)) != NULL) {
1.1 joris 408: if (currentline[len - 1] == '\n') {
409: currentline[len - 1] = '\0';
410: } else {
411: nline = xmalloc(len + 1);
412: memcpy(nline, currentline, len);
413: nline[len] = '\0';
414: currentline = nline;
415: }
416:
417: lineno++;
418:
419: for (p = currentline; isspace(*p); p++)
420: ;
421:
422: if (*p == '\0' || *p == '#')
423: continue;
424:
425: for (q = p; !isspace(*q) && *q != '\0'; q++)
426: ;
427:
428: if (*q == '\0')
429: goto bad;
430:
431: *q++ = '\0';
432: regex = p;
433:
434: for (; isspace(*q); q++)
435: ;
436:
437: if (*q == '\0')
438: goto bad;
439:
440: if (strcmp(regex, "ALL") == 0 && allow_all) {
441: tline = xmalloc(sizeof(*tline));
442: tline->line = xstrdup(q);
443: TAILQ_INSERT_TAIL(list, tline, flist);
444: } else if (defaultline == NULL && !match &&
445: strcmp(regex, "DEFAULT") == 0) {
446: defaultline = xstrdup(q);
447: } else if (!match) {
448: if (regcomp(&preg, regex, REG_NOSUB|REG_EXTENDED))
449: goto bad;
450:
451: if (regexec(&preg, repo, 0, NULL, 0) != REG_NOMATCH) {
452: match = 1;
453:
454: tline = xmalloc(sizeof(*tline));
455: tline->line = xstrdup(q);
456: TAILQ_INSERT_HEAD(list, tline, flist);
457: }
458: regfree(&preg);
459: }
460: }
1.2 joris 461:
1.1 joris 462: if (nline != NULL)
463: xfree(nline);
464:
465: if (defaultline != NULL) {
466: if (!match) {
467: tline = xmalloc(sizeof(*tline));
468: tline->line = defaultline;
469: TAILQ_INSERT_HEAD(list, tline, flist);
470: } else
471: xfree(defaultline);
472: }
473:
1.14 tobias 474: (void)fclose(fp);
475:
1.1 joris 476: if (TAILQ_EMPTY(list)) {
477: xfree(list);
478: list = NULL;
479: }
480:
481: return (list);
482:
483: bad:
484: cvs_log(LP_NOTICE, "%s: malformed line %d", file, lineno);
485:
486: if (defaultline != NULL)
487: xfree(defaultline);
488: cvs_trigger_freelist(list);
489:
1.14 tobias 490: (void)fclose(fp);
491:
1.1 joris 492: return (NULL);
493: }
494:
495: void
496: cvs_trigger_freelist(struct trigger_list * list)
497: {
498: struct trigger_line *line;
499:
500: while ((line = TAILQ_FIRST(list)) != NULL) {
501: TAILQ_REMOVE(list, line, flist);
502: xfree(line->line);
503: xfree(line);
504: }
505:
506: xfree(list);
507: }
508:
509: void
510: cvs_trigger_freeinfo(struct file_info_list * list)
511: {
512: struct file_info * fi;
513:
514: while ((fi = TAILQ_FIRST(list)) != NULL) {
515: TAILQ_REMOVE(list, fi, flist);
516:
517: if (fi->file_path != NULL)
518: xfree(fi->file_path);
519: if (fi->file_wd != NULL)
520: xfree(fi->file_wd);
521: if (fi->crevstr != NULL)
522: xfree(fi->crevstr);
523: if (fi->nrevstr != NULL)
524: xfree(fi->nrevstr);
525: if (fi->tag_new != NULL)
526: xfree(fi->tag_new);
527: if (fi->tag_old != NULL)
528: xfree(fi->tag_old);
529:
530: xfree(fi);
531: }
532: }
533:
534: void
535: cvs_trigger_loginfo_header(BUF *buf, char *repo)
536: {
537: char *dir, pwd[MAXPATHLEN];
538: char hostname[MAXHOSTNAMELEN];
539:
540: if (gethostname(hostname, sizeof(hostname)) == -1) {
541: fatal("cvs_trigger_loginfo_header: gethostname failed %s",
542: strerror(errno));
543: }
544:
545: if (getcwd(pwd, sizeof(pwd)) == NULL)
546: fatal("cvs_trigger_loginfo_header: Cannot get working "
547: "directory");
548:
549: if ((dir = dirname(pwd)) == NULL) {
550: fatal("cvs_trigger_loginfo_header: dirname failed %s",
551: strerror(errno));
552: }
553:
1.18 ray 554: buf_puts(buf, "Update of ");
555: buf_puts(buf, current_cvsroot->cr_dir);
556: buf_putc(buf, '/');
557: buf_puts(buf, repo);
558: buf_putc(buf, '\n');
559:
560: buf_puts(buf, "In directory ");
561: buf_puts(buf, hostname);
562: buf_puts(buf, ":");
1.19 nicm 563: buf_puts(buf, dir);
1.18 ray 564: buf_putc(buf, '/');
565: buf_puts(buf, repo);
566: buf_putc(buf, '\n');
567: buf_putc(buf, '\n');
1.1 joris 568: }
569: