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

Annotation of src/usr.bin/cvs/client.c, Revision 1.43

1.43    ! joris       1: /*     $OpenBSD: client.c,v 1.42 2007/01/03 19:27:28 joris Exp $       */
1.1       jfb         2: /*
1.9       joris       3:  * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
1.1       jfb         4:  *
1.9       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.
                      8:  *
                      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.9       joris      18: #include "includes.h"
1.1       jfb        19:
                     20: #include "cvs.h"
                     21: #include "log.h"
1.9       joris      22: #include "diff.h"
                     23: #include "remote.h"
1.1       jfb        24:
1.9       joris      25: struct cvs_req cvs_requests[] = {
                     26:        /* this is what our client will use, the server should support it */
                     27:        { "Root",               1,      cvs_server_root, REQ_NEEDED },
                     28:        { "Valid-responses",    1,      cvs_server_validresp, REQ_NEEDED },
                     29:        { "valid-requests",     1,      cvs_server_validreq, REQ_NEEDED },
                     30:        { "Directory",          0,      cvs_server_directory, REQ_NEEDED },
1.32      xsa        31:        { "Static-directory",   0,      cvs_server_static_directory, REQ_NEEDED },
1.30      xsa        32:        { "Sticky",             0,      cvs_server_sticky, REQ_NEEDED },
1.9       joris      33:        { "Entry",              0,      cvs_server_entry, REQ_NEEDED },
                     34:        { "Modified",           0,      cvs_server_modified, REQ_NEEDED },
                     35:        { "UseUnchanged",       0,      cvs_server_useunchanged, REQ_NEEDED },
                     36:        { "Unchanged",          0,      cvs_server_unchanged, REQ_NEEDED },
                     37:        { "Questionable",       0,      cvs_server_questionable, REQ_NEEDED },
                     38:        { "Argument",           0,      cvs_server_argument, REQ_NEEDED },
1.24      xsa        39:        { "Argumentx",          0,      cvs_server_argumentx, REQ_NEEDED },
                     40:        { "Global_option",      0,      cvs_server_globalopt, REQ_NEEDED },
1.26      xsa        41:        { "Set",                0,      cvs_server_set, REQ_NEEDED },
1.1       jfb        42:
1.9       joris      43:        /*
                     44:         * used to tell the server what is going on in our
                     45:         * working copy, unsupported until we are told otherwise
                     46:         */
                     47:        { "Max-dotdot",                 0,      NULL, 0 },
                     48:        { "Checkin-prog",               0,      NULL, 0 },
                     49:        { "Update-prog",                0,      NULL, 0 },
                     50:        { "Kopt",                       0,      NULL, 0 },
                     51:        { "Checkin-time",               0,      NULL, 0 },
                     52:        { "Is-modified",                0,      NULL, 0 },
                     53:        { "Notify",                     0,      NULL, 0 },
                     54:        { "Case",                       0,      NULL, 0 },
                     55:        { "Gzip-stream",                0,      NULL, 0 },
1.25      xsa        56:        { "wrapper-sendme-rcsOptions",  0,      NULL, 0 },
1.9       joris      57:        { "Kerberos-encrypt",           0,      NULL, 0 },
                     58:        { "Gssapi-encrypt",             0,      NULL, 0 },
                     59:        { "Gssapi-authenticate",        0,      NULL, 0 },
                     60:        { "expand-modules",             0,      NULL, 0 },
                     61:
                     62:        /* commands that might be supported */
                     63:        { "ci",                         0,      cvs_server_commit, 0 },
                     64:        { "co",                         0,      NULL, 0 },
1.25      xsa        65:        { "update",                     0,      cvs_server_update, 0 },
1.9       joris      66:        { "diff",                       0,      cvs_server_diff, 0 },
1.25      xsa        67:        { "log",                        0,      cvs_server_log, 0 },
                     68:        { "rlog",                       0,      NULL, 0 },
                     69:        { "add",                        0,      cvs_server_add, 0 },
                     70:        { "remove",                     0,      cvs_server_remove, 0 },
1.33      xsa        71:        { "update-patches",             0,      cvs_server_update_patches, 0 },
1.25      xsa        72:        { "gzip-file-contents",         0,      NULL, 0 },
1.9       joris      73:        { "status",                     0,      cvs_server_status, 0 },
                     74:        { "rdiff",                      0,      NULL, 0 },
1.25      xsa        75:        { "tag",                        0,      cvs_server_tag, 0 },
1.9       joris      76:        { "rtag",                       0,      NULL, 0 },
                     77:        { "import",                     0,      NULL, 0 },
1.25      xsa        78:        { "admin",                      0,      cvs_server_admin, 0 },
                     79:        { "export",                     0,      NULL, 0 },
                     80:        { "history",                    0,      NULL, 0 },
                     81:        { "release",                    0,      NULL, 0 },
1.9       joris      82:        { "watch-on",                   0,      NULL, 0 },
                     83:        { "watch-off",                  0,      NULL, 0 },
                     84:        { "watch-add",                  0,      NULL, 0 },
                     85:        { "watch-remove",               0,      NULL, 0 },
1.25      xsa        86:        { "watchers",                   0,      NULL, 0 },
                     87:        { "editors",                    0,      NULL, 0 },
                     88:        { "init",                       0,      cvs_server_init, 0 },
1.27      xsa        89:        { "annotate",                   0,      cvs_server_annotate, 0 },
1.25      xsa        90:        { "rannotate",                  0,      NULL, 0 },
1.9       joris      91:        { "noop",                       0,      NULL, 0 },
1.25      xsa        92:        { "version",                    0,      cvs_server_version, 0 },
1.9       joris      93:        { "",                           -1,     NULL, 0 }
                     94: };
                     95:
1.16      xsa        96: static void     client_check_directory(char *);
                     97: static char    *client_get_supported_responses(void);
                     98: static char    *lastdir = NULL;
                     99: static int      end_of_response = 0;
                    100:
                    101: static void    cvs_client_initlog(void);
                    102:
                    103: /*
                    104:  * File descriptors for protocol logging when the CVS_CLIENT_LOG environment
                    105:  * variable is set.
                    106:  */
                    107: static int     cvs_client_logon = 0;
1.42      joris     108: int    cvs_client_inlog_fd = -1;
                    109: int    cvs_client_outlog_fd = -1;
1.16      xsa       110:
1.9       joris     111:
1.37      joris     112: int server_response = SERVER_OK;
                    113:
1.9       joris     114: static char *
                    115: client_get_supported_responses(void)
                    116: {
                    117:        BUF *bp;
                    118:        char *d;
                    119:        int i, first;
                    120:
                    121:        first = 0;
                    122:        bp = cvs_buf_alloc(512, BUF_AUTOEXT);
                    123:        for (i = 0; cvs_responses[i].supported != -1; i++) {
                    124:                if (cvs_responses[i].hdlr == NULL)
                    125:                        continue;
                    126:
                    127:                if (first != 0)
                    128:                        cvs_buf_append(bp, " ", 1);
                    129:                else
                    130:                        first++;
                    131:                cvs_buf_append(bp, cvs_responses[i].name,
                    132:                    strlen(cvs_responses[i].name));
                    133:        }
                    134:
                    135:        cvs_buf_putc(bp, '\0');
                    136:        d = cvs_buf_release(bp);
                    137:        return (d);
                    138: }
1.1       jfb       139:
1.10      joris     140: static void
                    141: client_check_directory(char *data)
                    142: {
                    143:        int l;
                    144:        CVSENTRIES *entlist;
                    145:        char *entry, *parent, *base;
                    146:
                    147:        STRIP_SLASH(data);
                    148:
                    149:        cvs_mkpath(data);
                    150:
                    151:        if ((base = basename(data)) == NULL)
                    152:                fatal("client_check_directory: overflow");
                    153:
                    154:        if ((parent = dirname(data)) == NULL)
                    155:                fatal("client_check_directory: overflow");
1.11      joris     156:
                    157:        if (!strcmp(parent, "."))
                    158:                return;
1.10      joris     159:
                    160:        entry = xmalloc(CVS_ENT_MAXLINELEN);
                    161:        l = snprintf(entry, CVS_ENT_MAXLINELEN, "D/%s////", base);
                    162:        if (l == -1 || l >= CVS_ENT_MAXLINELEN)
                    163:                fatal("client_check_directory: overflow");
                    164:
                    165:        entlist = cvs_ent_open(parent);
                    166:        cvs_ent_add(entlist, entry);
                    167:        cvs_ent_close(entlist, ENT_SYNC);
                    168:
                    169:        xfree(entry);
                    170: }
                    171:
1.9       joris     172: void
                    173: cvs_client_connect_to_server(void)
                    174: {
1.26      xsa       175:        struct cvs_var *vp;
1.9       joris     176:        char *cmd, *argv[9], *resp;
                    177:        int ifd[2], ofd[2], argc;
1.1       jfb       178:
1.9       joris     179:        switch (current_cvsroot->cr_method) {
                    180:        case CVS_METHOD_PSERVER:
                    181:        case CVS_METHOD_KSERVER:
                    182:        case CVS_METHOD_GSERVER:
                    183:        case CVS_METHOD_FORK:
                    184:        case CVS_METHOD_EXT:
1.14      joris     185:                fatal("the specified connection method is not supported");
1.9       joris     186:        default:
                    187:                break;
                    188:        }
                    189:
                    190:        if (pipe(ifd) == -1)
                    191:                fatal("cvs_client_connect: %s", strerror(errno));
                    192:        if (pipe(ofd) == -1)
                    193:                fatal("cvs_client_connect: %s", strerror(errno));
                    194:
                    195:        switch (fork()) {
                    196:        case -1:
                    197:                fatal("cvs_client_connect: fork failed: %s", strerror(errno));
                    198:        case 0:
                    199:                if (dup2(ifd[0], STDIN_FILENO) == -1)
                    200:                        fatal("cvs_client_connect: %s", strerror(errno));
                    201:                if (dup2(ofd[1], STDOUT_FILENO) == -1)
                    202:                        fatal("cvs_client_connect: %s", strerror(errno));
1.1       jfb       203:
1.9       joris     204:                close(ifd[1]);
                    205:                close(ofd[0]);
1.1       jfb       206:
1.9       joris     207:                if ((cmd = getenv("CVS_SERVER")) == NULL)
                    208:                        cmd = CVS_SERVER_DEFAULT;
1.1       jfb       209:
                    210:                argc = 0;
                    211:                argv[argc++] = cvs_rsh;
                    212:
1.9       joris     213:                if (current_cvsroot->cr_user != NULL) {
1.1       jfb       214:                        argv[argc++] = "-l";
1.9       joris     215:                        argv[argc++] = current_cvsroot->cr_user;
1.1       jfb       216:                }
                    217:
1.9       joris     218:                argv[argc++] = current_cvsroot->cr_host;
                    219:                argv[argc++] = cmd;
1.1       jfb       220:                argv[argc++] = "server";
                    221:                argv[argc] = NULL;
                    222:
1.9       joris     223:                cvs_log(LP_TRACE, "connecting to server %s",
                    224:                    current_cvsroot->cr_host);
                    225:
1.1       jfb       226:                execvp(argv[0], argv);
1.9       joris     227:                fatal("cvs_client_connect: failed to execute cvs server");
                    228:        default:
                    229:                break;
1.1       jfb       230:        }
                    231:
1.9       joris     232:        close(ifd[0]);
                    233:        close(ofd[1]);
                    234:
                    235:        if ((current_cvsroot->cr_srvin = fdopen(ifd[1], "w")) == NULL)
                    236:                fatal("cvs_client_connect: %s", strerror(errno));
                    237:        if ((current_cvsroot->cr_srvout = fdopen(ofd[0], "r")) == NULL)
                    238:                fatal("cvs_client_connect: %s", strerror(errno));
1.1       jfb       239:
1.9       joris     240:        setvbuf(current_cvsroot->cr_srvin, NULL,_IOLBF, 0);
                    241:        setvbuf(current_cvsroot->cr_srvout, NULL, _IOLBF, 0);
1.1       jfb       242:
1.16      xsa       243:        cvs_client_initlog();
                    244:
1.18      xsa       245:        if (cvs_cmdop != CVS_OP_INIT)
                    246:                cvs_client_send_request("Root %s", current_cvsroot->cr_dir);
1.1       jfb       247:
1.9       joris     248:        resp = client_get_supported_responses();
                    249:        cvs_client_send_request("Valid-responses %s", resp);
                    250:        xfree(resp);
1.1       jfb       251:
1.9       joris     252:        cvs_client_send_request("valid-requests");
                    253:        cvs_client_get_responses();
1.1       jfb       254:
1.28      xsa       255:        cvs_client_send_request("UseUnchanged");
                    256:
1.20      xsa       257:        if (cvs_nolog == 1)
                    258:                cvs_client_send_request("Global_option -l");
                    259:
1.19      xsa       260:        if (cvs_noexec == 1)
                    261:                cvs_client_send_request("Global_option -n");
                    262:
                    263:        if (verbosity == 0)
                    264:                cvs_client_send_request("Global_option -Q");
1.20      xsa       265:
                    266:        /* Be quiet. This is the default in OpenCVS. */
                    267:        cvs_client_send_request("Global_option -q");
1.19      xsa       268:
                    269:        if (cvs_readonly == 1)
                    270:                cvs_client_send_request("Global_option -r");
                    271:
                    272:        if (cvs_trace == 1)
1.9       joris     273:                cvs_client_send_request("Global_option -t");
1.1       jfb       274:
1.9       joris     275:        if (verbosity == 2)
                    276:                cvs_client_send_request("Global_option -V");
1.26      xsa       277:
                    278:        /* XXX: If 'Set' is supported? */
                    279:        TAILQ_FOREACH(vp, &cvs_variables, cv_link)
                    280:                cvs_client_send_request("Set %s=%s", vp->cv_name, vp->cv_val);
1.1       jfb       281: }
                    282:
1.9       joris     283: void
                    284: cvs_client_send_request(char *fmt, ...)
                    285: {
                    286:        va_list ap;
                    287:        char *data, *s;
                    288:        struct cvs_req *req;
                    289:
                    290:        va_start(ap, fmt);
                    291:        vasprintf(&data, fmt, ap);
                    292:        va_end(ap);
1.1       jfb       293:
1.9       joris     294:        if ((s = strchr(data, ' ')) != NULL)
                    295:                *s = '\0';
1.1       jfb       296:
1.9       joris     297:        req = cvs_remote_get_request_info(data);
                    298:        if (req == NULL)
                    299:                fatal("'%s' is an unknown request", data);
1.1       jfb       300:
1.9       joris     301:        if (req->supported != 1)
                    302:                fatal("remote cvs server does not support '%s'", data);
1.1       jfb       303:
1.9       joris     304:        if (s != NULL)
                    305:                *s = ' ';
1.1       jfb       306:
1.12      joris     307:        cvs_log(LP_TRACE, "%s", data);
1.16      xsa       308:
                    309:        if (cvs_client_inlog_fd != -1) {
                    310:                BUF *bp;
                    311:
                    312:                bp = cvs_buf_alloc(strlen(data), BUF_AUTOEXT);
                    313:
                    314:                if (cvs_buf_append(bp, data, strlen(data)) < 0)
                    315:                        fatal("cvs_client_send_request: cvs_buf_append");
                    316:
                    317:                cvs_buf_putc(bp, '\n');
                    318:
                    319:                if (cvs_buf_write_fd(bp, cvs_client_inlog_fd) < 0)
                    320:                        fatal("cvs_client_send_request: cvs_buf_write_fd");
                    321:
                    322:                cvs_buf_free(bp);
                    323:        }
                    324:
1.9       joris     325:        cvs_remote_output(data);
                    326:        xfree(data);
                    327: }
1.1       jfb       328:
1.9       joris     329: void
                    330: cvs_client_read_response(void)
1.1       jfb       331: {
1.9       joris     332:        char *cmd, *data;
                    333:        struct cvs_resp *resp;
1.1       jfb       334:
1.9       joris     335:        cmd = cvs_remote_input();
                    336:        if ((data = strchr(cmd, ' ')) != NULL)
                    337:                (*data++) = '\0';
1.1       jfb       338:
1.9       joris     339:        resp = cvs_remote_get_response_info(cmd);
                    340:        if (resp == NULL)
                    341:                fatal("response '%s' is not supported by our client", cmd);
1.1       jfb       342:
1.9       joris     343:        if (resp->hdlr == NULL)
                    344:                fatal("opencvs client does not support '%s'", cmd);
1.16      xsa       345:
1.9       joris     346:        (*resp->hdlr)(data);
1.16      xsa       347:
1.9       joris     348:        xfree(cmd);
1.1       jfb       349: }
                    350:
1.9       joris     351: void
                    352: cvs_client_get_responses(void)
                    353: {
                    354:        while (end_of_response != 1)
                    355:                cvs_client_read_response();
1.1       jfb       356:
1.9       joris     357:        end_of_response = 0;
                    358: }
1.1       jfb       359:
1.9       joris     360: void
                    361: cvs_client_senddir(const char *dir)
1.1       jfb       362: {
1.34      xsa       363:        struct stat st;
1.29      xsa       364:        int nb;
1.34      xsa       365:        char *d, *date, *fpath, *repo, *tag;
1.29      xsa       366:
                    367:        d = NULL;
1.9       joris     368:
                    369:        if (lastdir != NULL && !strcmp(dir, lastdir))
                    370:                return;
1.1       jfb       371:
1.9       joris     372:        repo = xmalloc(MAXPATHLEN);
                    373:        cvs_get_repository_path(dir, repo, MAXPATHLEN);
1.31      xsa       374:
                    375:        cvs_client_send_request("Directory %s\n%s", dir, repo);
                    376:
1.9       joris     377:        xfree(repo);
1.34      xsa       378:
                    379:        fpath = xmalloc(MAXPATHLEN);
                    380:        if (cvs_path_cat(dir, CVS_PATH_STATICENTRIES, fpath, MAXPATHLEN) >=
                    381:            MAXPATHLEN)
                    382:                fatal("cvs_client_senddir: truncation");
                    383:
                    384:        if (stat(fpath, &st) == 0 && (st.st_mode & (S_IRUSR|S_IRGRP|S_IROTH)))
                    385:                cvs_client_send_request("Static-directory");
                    386:
                    387:        xfree(fpath);
1.29      xsa       388:
                    389:        d = xstrdup(dir);
                    390:        cvs_parse_tagfile(d, &tag, &date, &nb);
                    391:
                    392:        if (tag != NULL || date != NULL) {
                    393:                char buf[128];
                    394:
                    395:                if (tag != NULL && nb != NULL) {
                    396:                        if (strlcpy(buf, "N", sizeof(buf)) >= sizeof(buf))
                    397:                                fatal("cvs_client_senddir: truncation");
                    398:                } else if (tag != NULL) {
                    399:                        if (strlcpy(buf, "T", sizeof(buf)) >= sizeof(buf))
                    400:                                fatal("cvs_client_senddir: truncation");
                    401:                } else {
                    402:                        if (strlcpy(buf, "D", sizeof(buf)) >= sizeof(buf))
                    403:                                fatal("cvs_client_senddir: truncation");
                    404:                }
                    405:
                    406:                if (strlcat(buf, tag ? tag : date, sizeof(buf)) >= sizeof(buf))
                    407:                        fatal("cvs_client_senddir: truncation");
                    408:
                    409:                cvs_client_send_request("Sticky %s", buf);
                    410:
                    411:                if (tag != NULL)
                    412:                        xfree(tag);
                    413:                if (date != NULL)
                    414:                        xfree(date);
                    415:        }
                    416:        if (d != NULL)
                    417:                xfree(d);
1.1       jfb       418:
1.9       joris     419:        if (lastdir != NULL)
                    420:                xfree(lastdir);
                    421:        lastdir = xstrdup(dir);
                    422: }
1.1       jfb       423:
1.9       joris     424: void
                    425: cvs_client_sendfile(struct cvs_file *cf)
1.1       jfb       426: {
1.9       joris     427:        int l;
                    428:        size_t len;
                    429:        char rev[16], timebuf[64], sticky[32];
1.1       jfb       430:
1.9       joris     431:        if (cf->file_type != CVS_FILE)
                    432:                return;
1.1       jfb       433:
1.9       joris     434:        cvs_client_senddir(cf->file_wd);
                    435:        cvs_remote_classify_file(cf);
                    436:
1.10      joris     437:        if (cf->file_type == CVS_DIR)
                    438:                return;
                    439:
1.9       joris     440:        if (cf->file_ent != NULL) {
                    441:                if (cf->file_status == FILE_ADDED) {
                    442:                        len = strlcpy(rev, "0", sizeof(rev));
                    443:                        if (len >= sizeof(rev))
                    444:                                fatal("cvs_client_sendfile: truncation");
                    445:
                    446:                        len = strlcpy(timebuf, "Initial ", sizeof(timebuf));
                    447:                        if (len >= sizeof(timebuf))
                    448:                                fatal("cvs_client_sendfile: truncation");
                    449:
                    450:                        len = strlcat(timebuf, cf->file_name, sizeof(timebuf));
                    451:                        if (len >= sizeof(timebuf))
                    452:                                fatal("cvs_client_sendfile: truncation");
                    453:                } else {
                    454:                        rcsnum_tostr(cf->file_ent->ce_rev, rev, sizeof(rev));
                    455:                        ctime_r(&cf->file_ent->ce_mtime, timebuf);
                    456:                }
1.1       jfb       457:
1.9       joris     458:                if (cf->file_ent->ce_conflict == NULL) {
                    459:                        if (timebuf[strlen(timebuf) - 1] == '\n')
                    460:                                timebuf[strlen(timebuf) - 1] = '\0';
                    461:                } else {
                    462:                        len = strlcpy(timebuf, cf->file_ent->ce_conflict,
                    463:                            sizeof(timebuf));
                    464:                        if (len >= sizeof(timebuf))
                    465:                                fatal("cvs_client_sendfile: truncation");
                    466:                }
1.1       jfb       467:
1.9       joris     468:                sticky[0] = '\0';
                    469:                if (cf->file_ent->ce_tag != NULL) {
                    470:                        l = snprintf(sticky, sizeof(sticky), "T%s",
                    471:                            cf->file_ent->ce_tag);
                    472:                        if (l == -1 || l >= (int)sizeof(sticky))
                    473:                                fatal("cvs_client_sendfile: overflow");
1.1       jfb       474:                }
                    475:
1.13      joris     476:                cvs_client_send_request("Entry /%s/%s%s/%s//%s",
1.9       joris     477:                    cf->file_name, (cf->file_status == FILE_REMOVED) ? "-" : "",
                    478:                   rev, timebuf, sticky);
                    479:        }
                    480:
                    481:        switch (cf->file_status) {
                    482:        case FILE_UNKNOWN:
                    483:                if (cf->fd != -1)
                    484:                        cvs_client_send_request("Questionable %s",
                    485:                            cf->file_name);
                    486:                break;
                    487:        case FILE_ADDED:
                    488:        case FILE_MODIFIED:
                    489:                cvs_client_send_request("Modified %s", cf->file_name);
                    490:                cvs_remote_send_file(cf->file_path);
                    491:                break;
                    492:        case FILE_UPTODATE:
                    493:                cvs_client_send_request("Unchanged %s", cf->file_name);
                    494:                break;
1.1       jfb       495:        }
1.9       joris     496: }
1.1       jfb       497:
1.9       joris     498: void
                    499: cvs_client_send_files(char **argv, int argc)
                    500: {
                    501:        int i;
1.1       jfb       502:
1.9       joris     503:        for (i = 0; i < argc; i++)
                    504:                cvs_client_send_request("Argument %s", argv[i]);
                    505: }
1.1       jfb       506:
1.9       joris     507: void
                    508: cvs_client_ok(char *data)
                    509: {
                    510:        end_of_response = 1;
1.37      joris     511:        server_response = SERVER_OK;
1.1       jfb       512: }
                    513:
1.9       joris     514: void
                    515: cvs_client_error(char *data)
                    516: {
                    517:        end_of_response = 1;
1.37      joris     518:        server_response = SERVER_ERROR;
1.9       joris     519: }
1.1       jfb       520:
1.9       joris     521: void
                    522: cvs_client_validreq(char *data)
1.1       jfb       523: {
1.9       joris     524:        int i;
                    525:        char *sp, *ep;
                    526:        struct cvs_req *req;
                    527:
                    528:        sp = data;
                    529:        do {
                    530:                if ((ep = strchr(sp, ' ')) != NULL)
                    531:                        *ep = '\0';
                    532:
                    533:                req = cvs_remote_get_request_info(sp);
                    534:                if (req != NULL)
                    535:                        req->supported = 1;
                    536:
                    537:                if (ep != NULL)
                    538:                        sp = ep + 1;
                    539:        } while (ep != NULL);
                    540:
                    541:        for (i = 0; cvs_requests[i].supported != -1; i++) {
                    542:                req = &cvs_requests[i];
                    543:                if ((req->flags & REQ_NEEDED) &&
                    544:                    req->supported != 1) {
                    545:                        fatal("server does not support required '%s'",
                    546:                            req->name);
                    547:                }
1.1       jfb       548:        }
1.9       joris     549: }
1.1       jfb       550:
1.9       joris     551: void
                    552: cvs_client_e(char *data)
                    553: {
                    554:        cvs_printf("%s\n", data);
                    555: }
1.1       jfb       556:
1.9       joris     557: void
                    558: cvs_client_m(char *data)
                    559: {
                    560:        cvs_printf("%s\n", data);
                    561: }
1.1       jfb       562:
1.9       joris     563: void
                    564: cvs_client_checkedin(char *data)
                    565: {
                    566:        int l;
                    567:        CVSENTRIES *entlist;
                    568:        struct cvs_ent *ent, *newent;
                    569:        char *dir, *entry, rev[16], timebuf[64], sticky[16];
1.1       jfb       570:
1.9       joris     571:        dir = cvs_remote_input();
                    572:        entry = cvs_remote_input();
                    573:        xfree(dir);
1.1       jfb       574:
1.9       joris     575:        entlist = cvs_ent_open(data);
                    576:        newent = cvs_ent_parse(entry);
                    577:        ent = cvs_ent_get(entlist, newent->ce_name);
                    578:        xfree(entry);
1.1       jfb       579:
1.9       joris     580:        entry = xmalloc(CVS_ENT_MAXLINELEN);
1.1       jfb       581:
1.9       joris     582:        rcsnum_tostr(newent->ce_rev, rev, sizeof(rev));
                    583:        ctime_r(&ent->ce_mtime, timebuf);
                    584:        if (timebuf[strlen(timebuf) - 1] == '\n')
                    585:                timebuf[strlen(timebuf) - 1] = '\0';
1.1       jfb       586:
1.9       joris     587:        sticky[0] = '\0';
                    588:        if (ent->ce_tag != NULL) {
                    589:                l = snprintf(sticky, sizeof(sticky), "T%s", ent->ce_tag);
                    590:                if (l == -1 || l >= (int)sizeof(sticky))
                    591:                        fatal("cvs_client_checkedin: overflow");
1.2       jfb       592:        }
1.1       jfb       593:
1.9       joris     594:        l = snprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s//%s/",
                    595:            newent->ce_name, rev, timebuf, sticky);
                    596:        if (l == -1 || l >= CVS_ENT_MAXLINELEN)
                    597:                fatal("cvs_client_checkedin: overflow");
                    598:
                    599:        cvs_ent_free(ent);
                    600:        cvs_ent_free(newent);
                    601:        cvs_ent_add(entlist, entry);
                    602:        cvs_ent_close(entlist, ENT_SYNC);
1.1       jfb       603:
1.9       joris     604:        xfree(entry);
1.1       jfb       605: }
                    606:
1.9       joris     607: void
                    608: cvs_client_updated(char *data)
                    609: {
                    610:        BUF *bp;
                    611:        int l, fd;
                    612:        time_t now;
                    613:        mode_t fmode;
                    614:        size_t flen;
                    615:        CVSENTRIES *ent;
                    616:        struct cvs_ent *e;
                    617:        const char *errstr;
                    618:        struct timeval tv[2];
                    619:        char timebuf[32], *repo, *rpath, *entry, *mode;
                    620:        char revbuf[32], *len, *fpath, *wdir;
                    621:
1.10      joris     622:        client_check_directory(data);
1.9       joris     623:
                    624:        rpath = cvs_remote_input();
                    625:        entry = cvs_remote_input();
                    626:        mode = cvs_remote_input();
                    627:        len = cvs_remote_input();
                    628:
                    629:        repo = xmalloc(MAXPATHLEN);
                    630:        cvs_get_repository_path(".", repo, MAXPATHLEN);
                    631:
                    632:        if (strlen(repo) + 1 > strlen(rpath))
                    633:                fatal("received a repository path that is too short");
                    634:
                    635:        fpath = rpath + strlen(repo) + 1;
                    636:        if ((wdir = dirname(fpath)) == NULL)
                    637:                fatal("cvs_client_updated: dirname: %s", strerror(errno));
                    638:        xfree(repo);
                    639:
                    640:        flen = strtonum(len, 0, INT_MAX, &errstr);
                    641:        if (errstr != NULL)
                    642:                fatal("cvs_client_updated: %s: %s", len, errstr);
                    643:        xfree(len);
                    644:
                    645:        cvs_strtomode(mode, &fmode);
                    646:        xfree(mode);
                    647:
                    648:        time(&now);
                    649:        now = cvs_hack_time(now, 0);
                    650:        ctime_r(&now, timebuf);
                    651:        if (timebuf[strlen(timebuf) - 1] == '\n')
                    652:                timebuf[strlen(timebuf) - 1] = '\0';
                    653:
                    654:        e = cvs_ent_parse(entry);
                    655:        xfree(entry);
                    656:        rcsnum_tostr(e->ce_rev, revbuf, sizeof(revbuf));
                    657:        entry = xmalloc(CVS_ENT_MAXLINELEN);
                    658:        l = snprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s//", e->ce_name,
                    659:            revbuf, timebuf);
                    660:        if (l == -1 || l >= CVS_ENT_MAXLINELEN)
                    661:                fatal("cvs_client_updated: overflow");
                    662:
                    663:        cvs_ent_free(e);
                    664:        ent = cvs_ent_open(wdir);
                    665:        cvs_ent_add(ent, entry);
                    666:        cvs_ent_close(ent, ENT_SYNC);
                    667:        xfree(entry);
                    668:
                    669:        bp = cvs_remote_receive_file(flen);
                    670:        if ((fd = open(fpath, O_CREAT | O_WRONLY | O_TRUNC)) == -1)
                    671:                fatal("cvs_client_updated: open: %s: %s",
                    672:                    fpath, strerror(errno));
1.1       jfb       673:
1.9       joris     674:        if (cvs_buf_write_fd(bp, fd) == -1)
                    675:                fatal("cvs_client_updated: cvs_buf_write_fd failed for %s",
                    676:                    fpath);
1.1       jfb       677:
1.9       joris     678:        cvs_buf_free(bp);
1.1       jfb       679:
1.9       joris     680:        now = cvs_hack_time(now, 0);
                    681:        tv[0].tv_sec = now;
                    682:        tv[0].tv_usec = 0;
                    683:        tv[1] = tv[0];
1.1       jfb       684:
1.9       joris     685:        if (futimes(fd, tv) == -1)
                    686:                fatal("cvs_client_updated: futimes: %s", strerror(errno));
1.1       jfb       687:
1.9       joris     688:        if (fchmod(fd, fmode) == -1)
                    689:                fatal("cvs_client_updated: fchmod: %s", strerror(errno));
1.1       jfb       690:
1.9       joris     691:        (void)close(fd);
1.1       jfb       692:
1.9       joris     693:        xfree(rpath);
1.1       jfb       694: }
                    695:
1.9       joris     696: void
                    697: cvs_client_merged(char *data)
                    698: {
                    699: }
1.1       jfb       700:
1.9       joris     701: void
                    702: cvs_client_removed(char *data)
                    703: {
1.13      joris     704:        char *dir;
                    705:
                    706:        dir = cvs_remote_input();
                    707:        xfree(dir);
1.9       joris     708: }
1.1       jfb       709:
1.9       joris     710: void
                    711: cvs_client_remove_entry(char *data)
1.1       jfb       712: {
1.41      joris     713:        int l;
1.38      xsa       714:        CVSENTRIES *entlist;
1.41      joris     715:        char *filename, *rpath, *fpath;
                    716:
                    717:        rpath = cvs_remote_input();
                    718:        if ((filename = strrchr(rpath, '/')) == NULL)
                    719:                fatal("bad rpath in cvs_client_remove_entry: %s", rpath);
                    720:        *filename++;
1.1       jfb       721:
1.41      joris     722:        fpath = xmalloc(MAXPATHLEN);
                    723:        l = snprintf(fpath, MAXPATHLEN, "%s%s", data, filename);
                    724:        if (l == -1 || l >= MAXPATHLEN)
                    725:                fatal("cvs_client_remove_entry: overflow");
1.38      xsa       726:
1.41      joris     727:        xfree(rpath);
1.38      xsa       728:
                    729:        entlist = cvs_ent_open(data);
1.41      joris     730:        cvs_ent_remove(entlist, fpath);
1.38      xsa       731:        cvs_ent_close(entlist, ENT_SYNC);
                    732:
1.41      joris     733:        xfree(fpath);
1.36      xsa       734: }
                    735:
                    736: void
                    737: cvs_client_set_static_directory(char *data)
                    738: {
                    739:        FILE *fp;
                    740:        char *dir, *fpath;
                    741:
                    742:        if (cvs_cmdop == CVS_OP_EXPORT)
                    743:                return;
                    744:
                    745:        STRIP_SLASH(data);
                    746:
                    747:        dir = cvs_remote_input();
                    748:        xfree(dir);
                    749:
                    750:        fpath = xmalloc(MAXPATHLEN);
                    751:        if (cvs_path_cat(data, CVS_PATH_STATICENTRIES, fpath, MAXPATHLEN) >=
                    752:            MAXPATHLEN)
                    753:                fatal("cvs_client_set_static_directory: truncation");
                    754:
                    755:        if ((fp = fopen(fpath, "w+")) == NULL) {
                    756:                cvs_log(LP_ERRNO, "%s", fpath);
                    757:                goto out;
                    758:        }
                    759:        (void)fclose(fp);
                    760: out:
                    761:        xfree(fpath);
1.16      xsa       762: }
1.35      xsa       763:
                    764: void
                    765: cvs_client_clear_static_directory(char *data)
                    766: {
                    767:        char *dir, *fpath;
                    768:
                    769:        if (cvs_cmdop == CVS_OP_EXPORT)
                    770:                return;
                    771:
                    772:        STRIP_SLASH(data);
                    773:
                    774:        dir = cvs_remote_input();
                    775:        xfree(dir);
                    776:
                    777:        fpath = xmalloc(MAXPATHLEN);
                    778:        if (cvs_path_cat(data, CVS_PATH_STATICENTRIES, fpath, MAXPATHLEN) >=
                    779:            MAXPATHLEN)
                    780:                fatal("cvs_client_clear_static_directory: truncation");
                    781:
                    782:        (void)cvs_unlink(fpath);
                    783:
                    784:        xfree(fpath);
                    785: }
                    786:
                    787: void
                    788: cvs_client_set_sticky(char *data)
                    789: {
                    790:        FILE *fp;
                    791:        char *dir, *tag, *tagpath;
                    792:
                    793:        STRIP_SLASH(data);
                    794:
                    795:        dir = cvs_remote_input();
                    796:        xfree(dir);
                    797:        tag = cvs_remote_input();
                    798:
                    799:        tagpath = xmalloc(MAXPATHLEN);
                    800:        if (cvs_path_cat(data, CVS_PATH_TAG, tagpath, MAXPATHLEN) >= MAXPATHLEN)
                    801:                fatal("cvs_client_clear_sticky: truncation");
                    802:
                    803:        if ((fp = fopen(tagpath, "w+")) == NULL) {
                    804:                cvs_log(LP_ERRNO, "%s", tagpath);
                    805:                goto out;
                    806:        }
                    807:
                    808:        (void)fprintf(fp, "%s\n", tag);
                    809:        (void)fclose(fp);
                    810: out:
                    811:        xfree(tagpath);
                    812:        xfree(tag);
                    813: }
                    814:
                    815: void
                    816: cvs_client_clear_sticky(char *data)
                    817: {
                    818:        char *dir, *tagpath;
                    819:
                    820:        STRIP_SLASH(data);
                    821:
                    822:        dir = cvs_remote_input();
                    823:        xfree(dir);
                    824:
                    825:        tagpath = xmalloc(MAXPATHLEN);
                    826:        if (cvs_path_cat(data, CVS_PATH_TAG, tagpath, MAXPATHLEN) >= MAXPATHLEN)
                    827:                fatal("cvs_client_clear_sticky: truncation");
                    828:
                    829:        (void)cvs_unlink(tagpath);
                    830:
                    831:        xfree(tagpath);
                    832: }
                    833:
                    834:
1.16      xsa       835: /*
                    836:  * cvs_client_initlog()
                    837:  *
                    838:  * Initialize protocol logging if the CVS_CLIENT_LOG environment variable is
                    839:  * set.  In this case, the variable's value is used as a path to which the
                    840:  * appropriate suffix is added (".in" for client input and ".out" for server
                    841:  * output).
                    842:  */
                    843: static void
                    844: cvs_client_initlog(void)
                    845: {
                    846:        int l;
                    847:        u_int i;
                    848:        char *env, *envdup, buf[MAXPATHLEN], fpath[MAXPATHLEN];
                    849:        char rpath[MAXPATHLEN], *s;
                    850:        struct stat st;
                    851:        time_t now;
                    852:        struct passwd *pwd;
                    853:
                    854:        /* avoid doing it more than once */
                    855:        if (cvs_client_logon)
                    856:                return;
                    857:
                    858:        if ((env = getenv("CVS_CLIENT_LOG")) == NULL)
                    859:                return;
                    860:
                    861:        envdup = xstrdup(env);
                    862:        if ((s = strchr(envdup, '%')) != NULL)
                    863:                *s = '\0';
                    864:
                    865:        if (strlcpy(buf, env, sizeof(buf)) >= sizeof(buf))
                    866:                fatal("cvs_client_initlog: truncation");
                    867:
                    868:        if (strlcpy(rpath, envdup, sizeof(rpath)) >= sizeof(rpath))
                    869:                fatal("cvs_client_initlog: truncation");
                    870:
                    871:        xfree(envdup);
                    872:
                    873:        s = buf;
                    874:        while ((s = strchr(s, '%')) != NULL) {
                    875:                s++;
                    876:                switch (*s) {
                    877:                case 'c':
                    878:                        if (strlcpy(fpath, cvs_command, sizeof(fpath)) >=
                    879:                            sizeof(fpath))
                    880:                                fatal("cvs_client_initlog: truncation");
                    881:                        break;
                    882:                case 'd':
                    883:                        time(&now);
                    884:                        if (strlcpy(fpath, ctime(&now), sizeof(fpath)) >=
                    885:                            sizeof(fpath))
                    886:                                fatal("cvs_client_initlog: truncation");
                    887:                        break;
                    888:                case 'p':
                    889:                        snprintf(fpath, sizeof(fpath), "%d", getpid());
                    890:                        break;
                    891:                case 'u':
                    892:                        if ((pwd = getpwuid(getuid())) != NULL) {
                    893:                                if (strlcpy(fpath, pwd->pw_name,
                    894:                                    sizeof(fpath)) >= sizeof(fpath))
                    895:                                        fatal("cvs_client_initlog: truncation");
                    896:                        } else {
                    897:                                fpath[0] = '\0';
                    898:                        }
                    899:                        endpwent();
                    900:                        break;
                    901:                default:
                    902:                        fpath[0] = '\0';
                    903:                        break;
                    904:                }
                    905:
                    906:                if (fpath[0] != '\0') {
                    907:                        if (strlcat(rpath, "-", sizeof(rpath)) >= sizeof(rpath))
                    908:                                fatal("cvs_client_initlog: truncation");
                    909:
                    910:                        if (strlcat(rpath, fpath, sizeof(rpath))
                    911:                            >= sizeof(rpath))
                    912:                                fatal("cvs_client_initlog: truncation");
                    913:                }
                    914:        }
                    915:
                    916:        for (i = 0; i < UINT_MAX; i++) {
                    917:                l = snprintf(fpath, sizeof(fpath), "%s-%d.in", rpath, i);
                    918:                if (l == -1 || l >= (int)sizeof(fpath))
                    919:                        fatal("cvs_client_initlog: overflow");
                    920:
                    921:                if (stat(fpath, &st) != -1)
                    922:                        continue;
                    923:
                    924:                if (errno != ENOENT)
                    925:                        fatal("cvs_client_initlog() stat failed '%s'",
                    926:                            strerror(errno));
                    927:
                    928:                break;
                    929:        }
                    930:
                    931:        if ((cvs_client_inlog_fd = open(fpath,
                    932:            O_RDWR | O_CREAT | O_TRUNC, 0644)) == NULL) {
                    933:                fatal("cvs_client_initlog: open `%s': %s",
                    934:                    fpath, strerror(errno));
                    935:        }
                    936:
                    937:        for (i = 0; i < UINT_MAX; i++) {
                    938:                l = snprintf(fpath, sizeof(fpath), "%s-%d.out", rpath, i);
                    939:                if (l == -1 || l >= (int)sizeof(fpath))
                    940:                        fatal("cvs_client_initlog: overflow");
                    941:
                    942:                if (stat(fpath, &st) != -1)
                    943:                        continue;
                    944:
                    945:                if (errno != ENOENT)
                    946:                        fatal("cvs_client_initlog() stat failed '%s'",
                    947:                            strerror(errno));
                    948:
                    949:                break;
                    950:        }
                    951:
                    952:        if ((cvs_client_outlog_fd = open(fpath,
                    953:            O_RDWR | O_CREAT | O_TRUNC, 0644)) == NULL) {
                    954:                fatal("cvs_client_initlog: open `%s': %s",
                    955:                    fpath, strerror(errno));
                    956:        }
                    957:
                    958:        cvs_client_logon = 1;
1.1       jfb       959: }