[BACK]Return to trigger.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cvs

Annotation of src/usr.bin/cvs/trigger.c, Revision 1.22

1.22    ! deraadt     1: /*     $OpenBSD: trigger.c,v 1.21 2014/01/08 13:23:55 okan 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 % */
1.21      okan      258:        for (p = cmd;
                    259:             *p != '%' && !isspace((unsigned char)*p) && *p != '\0'; p++)
1.1       joris     260:                ;
                    261:        if (*p == '%')
                    262:                return (NULL);
                    263:
1.18      ray       264:        buf = buf_alloc(1024);
1.1       joris     265:
                    266:        p = cmd;
                    267: again:
                    268:        for (; *p != '\0'; p++) {
1.16      tobias    269:                if ((pos = strcspn(p, "$%")) != 0) {
1.18      ray       270:                        buf_append(buf, p, pos);
1.1       joris     271:                        p += pos;
                    272:                }
                    273:
1.16      tobias    274:                q = NULL;
                    275:                if (*p == '\0')
1.1       joris     276:                        break;
1.16      tobias    277:                if (*p++ == '$') {
                    278:                        if (*p == '{') {
                    279:                                pos = strcspn(++p, "}");
                    280:                                if (p[pos] == '\0')
                    281:                                        goto bad;
                    282:                        } else {
1.17      joris     283:                                for (pos = 0; isalpha(p[pos]); pos++)
                    284:                                        ;
                    285:                                if (pos == 0) {
1.16      tobias    286:                                        cvs_log(LP_ERR,
                    287:                                            "unrecognized variable syntax");
                    288:                                        goto bad;
                    289:                                }
                    290:                        }
1.1       joris     291:                        q = xmalloc(pos + 1);
                    292:                        memcpy(q, p, pos);
                    293:                        q[pos] = '\0';
1.16      tobias    294:                        if (expand_var(buf, q))
                    295:                                goto bad;
1.17      joris     296:                        p += pos - (*(p - 1) == '{' ? 0 : 1);
1.16      tobias    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;
1.1       joris     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:
1.18      ray       335:        buf_putc(buf, '\0');
                    336:        return (buf_release(buf));
1.1       joris     337:
                    338: bad:
                    339:        if (q != NULL)
                    340:                xfree(q);
1.9       tobias    341:        cvs_log(LP_NOTICE, "%s contains malformed command '%s'", file, cmd);
1.18      ray       342:        buf_free(buf);
1.1       joris     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) {
1.15      tobias    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);
1.1       joris     364:                        }
1.15      tobias    365:                        break;
                    366:                default:
                    367:                        (void)cvs_exec(cmd, in, 1);
                    368:                        break;
1.1       joris     369:                }
1.15      tobias    370:                xfree(cmd);
1.1       joris     371:        }
                    372:
1.15      tobias    373:        return (0);
1.1       joris     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;
1.22    ! deraadt   385:        char fpath[PATH_MAX];
1.1       joris     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:
1.22    ! deraadt   394:        (void)xsnprintf(fpath, PATH_MAX, "%s/%s", current_cvsroot->cr_dir,
1.1       joris     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:
1.14      tobias    403:        list = xmalloc(sizeof(*list));
                    404:        TAILQ_INIT(list);
                    405:
1.1       joris     406:        lineno = 0;
                    407:        nline = NULL;
1.2       joris     408:        while ((currentline = fgetln(fp, &len)) != NULL) {
1.1       joris     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:
1.21      okan      420:                for (p = currentline; isspace((unsigned char)*p); p++)
1.1       joris     421:                        ;
                    422:
                    423:                if (*p == '\0' || *p == '#')
                    424:                        continue;
                    425:
1.21      okan      426:                for (q = p; !isspace((unsigned char)*q) && *q != '\0'; q++)
1.1       joris     427:                        ;
                    428:
                    429:                if (*q == '\0')
                    430:                        goto bad;
                    431:
                    432:                *q++ = '\0';
                    433:                regex = p;
                    434:
1.21      okan      435:                for (; isspace((unsigned char)*q); q++)
1.1       joris     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:        }
1.2       joris     462:
1.1       joris     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:
1.14      tobias    475:        (void)fclose(fp);
                    476:
1.1       joris     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:
1.14      tobias    491:        (void)fclose(fp);
                    492:
1.1       joris     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: {
1.22    ! deraadt   538:        char *dir, pwd[PATH_MAX];
        !           539:        char hostname[HOST_NAME_MAX+1];
1.1       joris     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:
1.18      ray       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, ":");
1.19      nicm      564:        buf_puts(buf, dir);
1.18      ray       565:        buf_putc(buf, '/');
                    566:        buf_puts(buf, repo);
                    567:        buf_putc(buf, '\n');
                    568:        buf_putc(buf, '\n');
1.1       joris     569: }
                    570: