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

Annotation of src/usr.bin/cvs/server.c, Revision 1.34

1.34    ! xsa         1: /*     $OpenBSD: server.c,v 1.33 2006/11/09 10:08:33 xsa Exp $ */
1.1       jfb         2: /*
1.29      joris       3:  * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
1.1       jfb         4:  *
1.29      joris       5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
1.1       jfb         8:  *
1.29      joris       9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       jfb        16:  */
                     17:
1.27      xsa        18: #include "includes.h"
1.1       jfb        19:
                     20: #include "cvs.h"
                     21: #include "log.h"
1.29      joris      22: #include "diff.h"
                     23: #include "remote.h"
                     24:
                     25: struct cvs_resp cvs_responses[] = {
                     26:        /* this is what our server uses, the client should support it */
                     27:        { "Valid-requests",     1,      cvs_client_validreq, RESP_NEEDED },
                     28:        { "ok",                 0,      cvs_client_ok, RESP_NEEDED},
                     29:        { "error",              0,      cvs_client_error, RESP_NEEDED },
                     30:        { "E",                  0,      cvs_client_e, RESP_NEEDED },
                     31:        { "M",                  0,      cvs_client_m, RESP_NEEDED },
                     32:        { "Checked-in",         0,      cvs_client_checkedin, RESP_NEEDED },
                     33:        { "Updated",            0,      cvs_client_updated, RESP_NEEDED },
                     34:        { "Merged",             0,      cvs_client_merged, RESP_NEEDED },
                     35:        { "Removed",            0,      cvs_client_removed, RESP_NEEDED },
                     36:        { "Remove-entry",       0,      cvs_client_remove_entry, RESP_NEEDED },
                     37:
                     38:        /* unsupported responses until told otherwise */
                     39:        { "New-entry",                  0,      NULL, 0 },
                     40:        { "Created",                    0,      NULL, 0 },
                     41:        { "Update-existing",            0,      NULL, 0 },
                     42:        { "Rcs-diff",                   0,      NULL, 0 },
                     43:        { "Patched",                    0,      NULL, 0 },
                     44:        { "Mode",                       0,      NULL, 0 },
                     45:        { "Mod-time",                   0,      NULL, 0 },
                     46:        { "Checksum",                   0,      NULL, 0 },
                     47:        { "Copy-file",                  0,      NULL, 0 },
                     48:        { "Set-static-directory",       0,      NULL, 0 },
                     49:        { "Clear-static-directory",     0,      NULL, 0 },
                     50:        { "Set-sticky",                 0,      NULL, 0 },
                     51:        { "Clear-sticky",               0,      NULL, 0 },
                     52:        { "Template",                   0,      NULL, 0 },
                     53:        { "Set-checkin-prog",           0,      NULL, 0 },
                     54:        { "Set-update-prog",            0,      NULL, 0 },
                     55:        { "Notified",                   0,      NULL, 0 },
                     56:        { "Module-expansion",           0,      NULL, 0 },
                     57:        { "Wrapper-rcsOption",          0,      NULL, 0 },
                     58:        { "Mbinary",                    0,      NULL, 0 },
                     59:        { "F",                          0,      NULL, 0 },
                     60:        { "MT",                         0,      NULL, 0 },
                     61:        { "",                           -1,     NULL, 0 }
                     62: };
1.1       jfb        63:
1.29      joris      64: int    cvs_server(int, char **);
                     65: char   *cvs_server_path = NULL;
1.1       jfb        66:
1.29      joris      67: static char *server_currentdir = NULL;
                     68: static char *server_argv[CVS_CMD_MAXARG];
                     69: static int server_argc = 1;
1.8       joris      70:
1.17      jfb        71: struct cvs_cmd cvs_cmd_server = {
1.29      joris      72:        CVS_OP_SERVER, 0, "server", { "", "" },
                     73:        "server mode",
1.17      jfb        74:        NULL,
1.29      joris      75:        NULL,
                     76:        NULL,
                     77:        cvs_server
1.17      jfb        78: };
1.1       jfb        79:
1.14      joris      80:
1.1       jfb        81: int
                     82: cvs_server(int argc, char **argv)
                     83: {
1.29      joris      84:        int l;
                     85:        char *cmd, *data;
                     86:        struct cvs_req *req;
                     87:
                     88:        server_argv[0] = xstrdup("server");
                     89:
                     90:        cvs_server_path = xmalloc(MAXPATHLEN);
                     91:        l = snprintf(cvs_server_path, MAXPATHLEN, "%s/cvs-serv%d",
                     92:            cvs_tmpdir, getpid());
                     93:        if (l == -1 || l >= MAXPATHLEN)
                     94:                fatal("cvs_server: overflow in server path");
                     95:
                     96:        if (mkdir(cvs_server_path, 0700) == -1)
                     97:                fatal("failed to create temporary server directory: %s, %s",
                     98:                    cvs_server_path, strerror(errno));
                     99:
                    100:        if (chdir(cvs_server_path) == -1)
                    101:                fatal("failed to change directory to '%s'", cvs_server_path);
                    102:
                    103:        for (;;) {
                    104:                cmd = cvs_remote_input();
                    105:
                    106:                if ((data = strchr(cmd, ' ')) != NULL)
                    107:                        (*data++) = '\0';
                    108:
                    109:                req = cvs_remote_get_request_info(cmd);
                    110:                if (req == NULL)
                    111:                        fatal("request '%s' is not supported by our server",
                    112:                            cmd);
                    113:
                    114:                if (req->hdlr == NULL)
                    115:                        fatal("opencvs server does not support '%s'", cmd);
                    116:
                    117:                (*req->hdlr)(data);
                    118:                xfree(cmd);
1.14      joris     119:        }
                    120:
1.29      joris     121:        return (0);
                    122: }
                    123:
                    124: void
                    125: cvs_server_send_response(char *fmt, ...)
                    126: {
                    127:        va_list ap;
                    128:        char *data, *s;
                    129:        struct cvs_resp *resp;
                    130:
                    131:        va_start(ap, fmt);
                    132:        vasprintf(&data, fmt, ap);
                    133:        va_end(ap);
                    134:
                    135:        if ((s = strchr(data, ' ')) != NULL)
                    136:                *s = '\0';
                    137:
                    138:        resp = cvs_remote_get_response_info(data);
                    139:        if (resp == NULL)
                    140:                fatal("'%s' is an unknown response", data);
                    141:
                    142:        if (resp->supported != 1)
                    143:                fatal("remote cvs client does not support '%s'", data);
1.14      joris     144:
1.29      joris     145:        if (s != NULL)
                    146:                *s = ' ';
1.14      joris     147:
1.30      joris     148:        cvs_log(LP_TRACE, "%s", data);
1.29      joris     149:        cvs_remote_output(data);
                    150:        xfree(data);
                    151: }
                    152:
                    153: void
                    154: cvs_server_root(char *data)
                    155: {
                    156:        fatal("duplicate Root request from client, violates the protocol");
                    157: }
                    158:
                    159: void
                    160: cvs_server_validresp(char *data)
                    161: {
                    162:        int i;
                    163:        char *sp, *ep;
                    164:        struct cvs_resp *resp;
                    165:
                    166:        sp = data;
                    167:        do {
                    168:                if ((ep = strchr(sp, ' ')) != NULL)
                    169:                        *ep = '\0';
                    170:
                    171:                resp = cvs_remote_get_response_info(sp);
                    172:                if (resp != NULL)
                    173:                        resp->supported = 1;
                    174:
                    175:                if (ep != NULL)
                    176:                        sp = ep + 1;
                    177:        } while (ep != NULL);
                    178:
                    179:        for (i = 0; cvs_responses[i].supported != -1; i++) {
                    180:                resp = &cvs_responses[i];
                    181:                if ((resp->flags & RESP_NEEDED) &&
                    182:                    resp->supported != 1) {
                    183:                        fatal("client does not support required '%s'",
                    184:                            resp->name);
1.1       jfb       185:                }
1.29      joris     186:        }
                    187: }
1.1       jfb       188:
1.29      joris     189: void
                    190: cvs_server_validreq(char *data)
                    191: {
                    192:        BUF *bp;
                    193:        char *d;
                    194:        int i, first;
                    195:
                    196:        first = 0;
                    197:        bp = cvs_buf_alloc(512, BUF_AUTOEXT);
                    198:        for (i = 0; cvs_requests[i].supported != -1; i++) {
                    199:                if (cvs_requests[i].hdlr == NULL)
1.5       jfb       200:                        continue;
1.1       jfb       201:
1.29      joris     202:                if (first != 0)
                    203:                        cvs_buf_append(bp, " ", 1);
                    204:                else
                    205:                        first++;
                    206:
                    207:                cvs_buf_append(bp, cvs_requests[i].name,
                    208:                    strlen(cvs_requests[i].name));
                    209:        }
                    210:
                    211:        cvs_buf_putc(bp, '\0');
                    212:        d = cvs_buf_release(bp);
                    213:
                    214:        cvs_server_send_response("Valid-requests %s", d);
                    215:        cvs_server_send_response("ok");
                    216:        xfree(d);
                    217: }
                    218:
                    219: void
                    220: cvs_server_globalopt(char *data)
                    221: {
                    222:        if (!strcmp(data, "-t"))
                    223:                cvs_trace = 1;
                    224:
                    225:        if (!strcmp(data, "-n"))
                    226:                cvs_noexec = 1;
                    227:
                    228:        if (!strcmp(data, "-V"))
                    229:                verbosity = 2;
                    230: }
1.1       jfb       231:
1.29      joris     232: void
                    233: cvs_server_directory(char *data)
                    234: {
                    235:        int l;
                    236:        CVSENTRIES *entlist;
                    237:        char *dir, *repo, *parent, *entry, *dirn;
                    238:
                    239:        dir = cvs_remote_input();
                    240:        if (strlen(dir) < strlen(current_cvsroot->cr_dir) + 1)
                    241:                fatal("cvs_server_directory: bad Directory request");
                    242:
                    243:        repo = dir + strlen(current_cvsroot->cr_dir) + 1;
                    244:        cvs_mkpath(repo);
                    245:
                    246:        if ((dirn = basename(repo)) == NULL)
                    247:                fatal("cvs_server_directory: %s", strerror(errno));
                    248:
                    249:        if ((parent = dirname(repo)) == NULL)
                    250:                fatal("cvs_server_directory: %s", strerror(errno));
                    251:
                    252:        if (strcmp(parent, ".")) {
                    253:                entlist = cvs_ent_open(parent);
                    254:                entry = xmalloc(CVS_ENT_MAXLINELEN);
                    255:                l = snprintf(entry, CVS_ENT_MAXLINELEN, "D/%s////", dirn);
                    256:                if (l == -1 || l >= CVS_ENT_MAXLINELEN)
                    257:                        fatal("cvs_server_directory: overflow");
                    258:
                    259:                cvs_ent_add(entlist, entry);
                    260:                cvs_ent_close(entlist, ENT_SYNC);
                    261:                xfree(entry);
1.1       jfb       262:        }
                    263:
1.29      joris     264:        if (server_currentdir != NULL)
                    265:                xfree(server_currentdir);
                    266:        server_currentdir = xstrdup(repo);
                    267:
                    268:        xfree(dir);
                    269: }
                    270:
                    271: void
                    272: cvs_server_entry(char *data)
                    273: {
                    274:        CVSENTRIES *entlist;
                    275:
                    276:        entlist = cvs_ent_open(server_currentdir);
                    277:        cvs_ent_add(entlist, data);
                    278:        cvs_ent_close(entlist, ENT_SYNC);
                    279: }
                    280:
                    281: void
                    282: cvs_server_modified(char *data)
                    283: {
                    284:        BUF *bp;
                    285:        int fd, l;
                    286:        size_t flen;
                    287:        mode_t fmode;
                    288:        const char *errstr;
                    289:        char *mode, *len, *fpath;
                    290:
                    291:        mode = cvs_remote_input();
                    292:        len = cvs_remote_input();
                    293:
                    294:        cvs_strtomode(mode, &fmode);
                    295:        xfree(mode);
                    296:
                    297:        flen = strtonum(len, 0, INT_MAX, &errstr);
                    298:        if (errstr != NULL)
                    299:                fatal("cvs_server_modified: %s", errstr);
                    300:        xfree(len);
                    301:
                    302:        bp = cvs_remote_receive_file(flen);
                    303:
                    304:        fpath = xmalloc(MAXPATHLEN);
                    305:        l = snprintf(fpath, MAXPATHLEN, "%s/%s", server_currentdir, data);
                    306:        if (l == -1 || l >= MAXPATHLEN)
                    307:                fatal("cvs_server_modified: overflow");
                    308:
                    309:        if ((fd = open(fpath, O_WRONLY | O_CREAT | O_TRUNC)) == -1)
                    310:                fatal("cvs_server_modified: %s: %s", fpath, strerror(errno));
                    311:
                    312:        if (cvs_buf_write_fd(bp, fd) == -1)
                    313:                fatal("cvs_server_modified: failed to write file '%s'", fpath);
                    314:
                    315:        if (fchmod(fd, 0600) == -1)
                    316:                fatal("cvs_server_modified: failed to set file mode");
                    317:
                    318:        xfree(fpath);
                    319:        (void)close(fd);
                    320:        cvs_buf_free(bp);
                    321: }
                    322:
                    323: void
                    324: cvs_server_useunchanged(char *data)
                    325: {
                    326: }
                    327:
                    328: void
                    329: cvs_server_unchanged(char *data)
                    330: {
                    331:        int l, fd;
                    332:        char *fpath;
                    333:        CVSENTRIES *entlist;
                    334:        struct cvs_ent *ent;
                    335:        struct timeval tv[2];
                    336:
                    337:        fpath = xmalloc(MAXPATHLEN);
                    338:        l = snprintf(fpath, MAXPATHLEN, "%s/%s", server_currentdir, data);
                    339:        if (l == -1 || l >= MAXPATHLEN)
                    340:                fatal("cvs_server_unchanged: overflow");
                    341:
                    342:        if ((fd = open(fpath, O_RDWR | O_CREAT | O_TRUNC)) == -1)
                    343:                fatal("cvs_server_unchanged: %s: %s", fpath, strerror(errno));
                    344:
                    345:        entlist = cvs_ent_open(server_currentdir);
                    346:        ent = cvs_ent_get(entlist, data);
                    347:        if (ent == NULL)
                    348:                fatal("received Unchanged request for non-existing file");
                    349:        cvs_ent_close(entlist, ENT_NOSYNC);
                    350:
                    351:        tv[0].tv_sec = cvs_hack_time(ent->ce_mtime, 0);
                    352:        tv[0].tv_usec = 0;
                    353:        tv[1] = tv[0];
                    354:        if (futimes(fd, tv) == -1)
                    355:                fatal("cvs_server_unchanged: failed to set modified time");
                    356:
                    357:        if (fchmod(fd, 0600) == -1)
                    358:                fatal("cvs_server_unchanged: failed to set mode");
                    359:
                    360:        cvs_ent_free(ent);
                    361:        xfree(fpath);
                    362:        (void)close(fd);
                    363: }
                    364:
                    365: void
                    366: cvs_server_questionable(char *data)
                    367: {
                    368: }
                    369:
                    370: void
                    371: cvs_server_argument(char *data)
                    372: {
                    373:
                    374:        if (server_argc > CVS_CMD_MAXARG)
                    375:                fatal("cvs_server_argument: too many arguments sent");
                    376:
                    377:        server_argv[server_argc++] = xstrdup(data);
                    378: }
                    379:
                    380: void
1.31      xsa       381: cvs_server_add(char *data)
                    382: {
                    383:        if (chdir(server_currentdir) == -1)
                    384:                fatal("cvs_server_add: %s", strerror(errno));
                    385:
                    386:        cvs_cmdop = CVS_OP_ADD;
                    387:        cvs_add(server_argc, server_argv);
                    388:        cvs_server_send_response("ok");
                    389: }
                    390:
                    391: void
1.29      joris     392: cvs_server_commit(char *data)
                    393: {
                    394:        if (chdir(server_currentdir) == -1)
                    395:                fatal("cvs_server_commit: %s", strerror(errno));
                    396:
                    397:        cvs_cmdop = CVS_OP_COMMIT;
                    398:        cvs_commit(server_argc, server_argv);
                    399:        cvs_server_send_response("ok");
                    400: }
                    401:
                    402: void
                    403: cvs_server_diff(char *data)
                    404: {
                    405:        if (chdir(server_currentdir) == -1)
                    406:                fatal("cvs_server_diff: %s", strerror(errno));
                    407:
                    408:        cvs_cmdop = CVS_OP_DIFF;
                    409:        cvs_diff(server_argc, server_argv);
1.34    ! xsa       410:        cvs_server_send_response("ok");
        !           411: }
        !           412:
        !           413: void
        !           414: cvs_server_init(char *data)
        !           415: {
        !           416:        if (chdir(server_currentdir) == -1)
        !           417:                fatal("cvs_server_init: %s", strerror(errno));
        !           418:
        !           419:        cvs_cmdop = CVS_OP_INIT;
        !           420:        cvs_init(server_argc, server_argv);
1.31      xsa       421:        cvs_server_send_response("ok");
                    422: }
                    423:
                    424: void
                    425: cvs_server_remove(char *data)
                    426: {
                    427:        if (chdir(server_currentdir) == -1)
                    428:                fatal("cvs_server_remove: %s", strerror(errno));
                    429:
                    430:        cvs_cmdop = CVS_OP_REMOVE;
                    431:        cvs_remove(server_argc, server_argv);
1.29      joris     432:        cvs_server_send_response("ok");
                    433: }
                    434:
                    435: void
                    436: cvs_server_status(char *data)
                    437: {
                    438:        if (chdir(server_currentdir) == -1)
                    439:                fatal("cvs_server_status: %s", strerror(errno));
                    440:
                    441:        cvs_cmdop = CVS_OP_STATUS;
                    442:        cvs_status(server_argc, server_argv);
                    443:        cvs_server_send_response("ok");
                    444: }
                    445:
                    446: void
                    447: cvs_server_log(char *data)
                    448: {
                    449:        if (chdir(server_currentdir) == -1)
                    450:                fatal("cvs_server_log: %s", strerror(errno));
                    451:
                    452:        cvs_cmdop = CVS_OP_LOG;
1.32      xsa       453:        cvs_getlog(server_argc, server_argv);
                    454:        cvs_server_send_response("ok");
                    455: }
                    456:
                    457: void
                    458: cvs_server_tag(char *data)
                    459: {
                    460:        if (chdir(server_currentdir) == -1)
                    461:                fatal("cvs_server_tag: %s", strerror(errno));
                    462:
                    463:        cvs_cmdop = CVS_OP_TAG;
1.33      xsa       464:        cvs_tag(server_argc, server_argv);
1.29      joris     465:        cvs_server_send_response("ok");
                    466: }
                    467:
                    468: void
                    469: cvs_server_update(char *data)
                    470: {
                    471:        if (chdir(server_currentdir) == -1)
                    472:                fatal("cvs_server_update: %s", strerror(errno));
1.14      joris     473:
1.29      joris     474:        cvs_cmdop = CVS_OP_UPDATE;
                    475:        cvs_update(server_argc, server_argv);
                    476:        cvs_server_send_response("ok");
1.1       jfb       477: }