[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.47

1.47    ! xsa         1: /*     $OpenBSD: client.c,v 1.46 2007/01/13 15:29:34 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.45      joris     178:
                    179:        if (cvs_server_active == 1)
                    180:                fatal("cvs_client_connect: I was already connected to server");
1.1       jfb       181:
1.9       joris     182:        switch (current_cvsroot->cr_method) {
                    183:        case CVS_METHOD_PSERVER:
                    184:        case CVS_METHOD_KSERVER:
                    185:        case CVS_METHOD_GSERVER:
                    186:        case CVS_METHOD_FORK:
                    187:        case CVS_METHOD_EXT:
1.14      joris     188:                fatal("the specified connection method is not supported");
1.9       joris     189:        default:
                    190:                break;
                    191:        }
                    192:
                    193:        if (pipe(ifd) == -1)
                    194:                fatal("cvs_client_connect: %s", strerror(errno));
                    195:        if (pipe(ofd) == -1)
                    196:                fatal("cvs_client_connect: %s", strerror(errno));
                    197:
                    198:        switch (fork()) {
                    199:        case -1:
                    200:                fatal("cvs_client_connect: fork failed: %s", strerror(errno));
                    201:        case 0:
                    202:                if (dup2(ifd[0], STDIN_FILENO) == -1)
                    203:                        fatal("cvs_client_connect: %s", strerror(errno));
                    204:                if (dup2(ofd[1], STDOUT_FILENO) == -1)
                    205:                        fatal("cvs_client_connect: %s", strerror(errno));
1.1       jfb       206:
1.9       joris     207:                close(ifd[1]);
                    208:                close(ofd[0]);
1.1       jfb       209:
1.9       joris     210:                if ((cmd = getenv("CVS_SERVER")) == NULL)
                    211:                        cmd = CVS_SERVER_DEFAULT;
1.1       jfb       212:
                    213:                argc = 0;
                    214:                argv[argc++] = cvs_rsh;
                    215:
1.9       joris     216:                if (current_cvsroot->cr_user != NULL) {
1.1       jfb       217:                        argv[argc++] = "-l";
1.9       joris     218:                        argv[argc++] = current_cvsroot->cr_user;
1.1       jfb       219:                }
                    220:
1.9       joris     221:                argv[argc++] = current_cvsroot->cr_host;
                    222:                argv[argc++] = cmd;
1.1       jfb       223:                argv[argc++] = "server";
                    224:                argv[argc] = NULL;
                    225:
1.9       joris     226:                cvs_log(LP_TRACE, "connecting to server %s",
                    227:                    current_cvsroot->cr_host);
                    228:
1.1       jfb       229:                execvp(argv[0], argv);
1.9       joris     230:                fatal("cvs_client_connect: failed to execute cvs server");
                    231:        default:
                    232:                break;
1.1       jfb       233:        }
                    234:
1.9       joris     235:        close(ifd[0]);
                    236:        close(ofd[1]);
                    237:
                    238:        if ((current_cvsroot->cr_srvin = fdopen(ifd[1], "w")) == NULL)
                    239:                fatal("cvs_client_connect: %s", strerror(errno));
                    240:        if ((current_cvsroot->cr_srvout = fdopen(ofd[0], "r")) == NULL)
                    241:                fatal("cvs_client_connect: %s", strerror(errno));
1.1       jfb       242:
1.9       joris     243:        setvbuf(current_cvsroot->cr_srvin, NULL,_IOLBF, 0);
                    244:        setvbuf(current_cvsroot->cr_srvout, NULL, _IOLBF, 0);
1.1       jfb       245:
1.16      xsa       246:        cvs_client_initlog();
                    247:
1.18      xsa       248:        if (cvs_cmdop != CVS_OP_INIT)
                    249:                cvs_client_send_request("Root %s", current_cvsroot->cr_dir);
1.1       jfb       250:
1.9       joris     251:        resp = client_get_supported_responses();
                    252:        cvs_client_send_request("Valid-responses %s", resp);
                    253:        xfree(resp);
1.1       jfb       254:
1.9       joris     255:        cvs_client_send_request("valid-requests");
                    256:        cvs_client_get_responses();
1.1       jfb       257:
1.28      xsa       258:        cvs_client_send_request("UseUnchanged");
                    259:
1.20      xsa       260:        if (cvs_nolog == 1)
                    261:                cvs_client_send_request("Global_option -l");
                    262:
1.19      xsa       263:        if (cvs_noexec == 1)
                    264:                cvs_client_send_request("Global_option -n");
                    265:
                    266:        if (verbosity == 0)
                    267:                cvs_client_send_request("Global_option -Q");
1.20      xsa       268:
                    269:        /* Be quiet. This is the default in OpenCVS. */
                    270:        cvs_client_send_request("Global_option -q");
1.19      xsa       271:
                    272:        if (cvs_readonly == 1)
                    273:                cvs_client_send_request("Global_option -r");
                    274:
                    275:        if (cvs_trace == 1)
1.9       joris     276:                cvs_client_send_request("Global_option -t");
1.1       jfb       277:
1.9       joris     278:        if (verbosity == 2)
                    279:                cvs_client_send_request("Global_option -V");
1.26      xsa       280:
                    281:        /* XXX: If 'Set' is supported? */
                    282:        TAILQ_FOREACH(vp, &cvs_variables, cv_link)
                    283:                cvs_client_send_request("Set %s=%s", vp->cv_name, vp->cv_val);
1.1       jfb       284: }
                    285:
1.9       joris     286: void
                    287: cvs_client_send_request(char *fmt, ...)
                    288: {
                    289:        va_list ap;
                    290:        char *data, *s;
                    291:        struct cvs_req *req;
                    292:
                    293:        va_start(ap, fmt);
                    294:        vasprintf(&data, fmt, ap);
                    295:        va_end(ap);
1.1       jfb       296:
1.9       joris     297:        if ((s = strchr(data, ' ')) != NULL)
                    298:                *s = '\0';
1.1       jfb       299:
1.9       joris     300:        req = cvs_remote_get_request_info(data);
                    301:        if (req == NULL)
                    302:                fatal("'%s' is an unknown request", data);
1.1       jfb       303:
1.9       joris     304:        if (req->supported != 1)
                    305:                fatal("remote cvs server does not support '%s'", data);
1.1       jfb       306:
1.9       joris     307:        if (s != NULL)
                    308:                *s = ' ';
1.1       jfb       309:
1.12      joris     310:        cvs_log(LP_TRACE, "%s", data);
1.16      xsa       311:
                    312:        if (cvs_client_inlog_fd != -1) {
                    313:                BUF *bp;
                    314:
                    315:                bp = cvs_buf_alloc(strlen(data), BUF_AUTOEXT);
                    316:
                    317:                if (cvs_buf_append(bp, data, strlen(data)) < 0)
                    318:                        fatal("cvs_client_send_request: cvs_buf_append");
                    319:
                    320:                cvs_buf_putc(bp, '\n');
                    321:
                    322:                if (cvs_buf_write_fd(bp, cvs_client_inlog_fd) < 0)
                    323:                        fatal("cvs_client_send_request: cvs_buf_write_fd");
                    324:
                    325:                cvs_buf_free(bp);
                    326:        }
                    327:
1.9       joris     328:        cvs_remote_output(data);
                    329:        xfree(data);
                    330: }
1.1       jfb       331:
1.9       joris     332: void
                    333: cvs_client_read_response(void)
1.1       jfb       334: {
1.9       joris     335:        char *cmd, *data;
                    336:        struct cvs_resp *resp;
1.1       jfb       337:
1.9       joris     338:        cmd = cvs_remote_input();
                    339:        if ((data = strchr(cmd, ' ')) != NULL)
                    340:                (*data++) = '\0';
1.1       jfb       341:
1.9       joris     342:        resp = cvs_remote_get_response_info(cmd);
                    343:        if (resp == NULL)
                    344:                fatal("response '%s' is not supported by our client", cmd);
1.1       jfb       345:
1.9       joris     346:        if (resp->hdlr == NULL)
                    347:                fatal("opencvs client does not support '%s'", cmd);
1.16      xsa       348:
1.9       joris     349:        (*resp->hdlr)(data);
1.16      xsa       350:
1.9       joris     351:        xfree(cmd);
1.1       jfb       352: }
                    353:
1.9       joris     354: void
                    355: cvs_client_get_responses(void)
                    356: {
                    357:        while (end_of_response != 1)
                    358:                cvs_client_read_response();
1.1       jfb       359:
1.9       joris     360:        end_of_response = 0;
                    361: }
1.1       jfb       362:
1.9       joris     363: void
                    364: cvs_client_senddir(const char *dir)
1.1       jfb       365: {
1.34      xsa       366:        struct stat st;
1.29      xsa       367:        int nb;
1.34      xsa       368:        char *d, *date, *fpath, *repo, *tag;
1.29      xsa       369:
                    370:        d = NULL;
1.9       joris     371:
                    372:        if (lastdir != NULL && !strcmp(dir, lastdir))
                    373:                return;
1.1       jfb       374:
1.9       joris     375:        repo = xmalloc(MAXPATHLEN);
                    376:        cvs_get_repository_path(dir, repo, MAXPATHLEN);
1.31      xsa       377:
                    378:        cvs_client_send_request("Directory %s\n%s", dir, repo);
                    379:
1.9       joris     380:        xfree(repo);
1.34      xsa       381:
                    382:        fpath = xmalloc(MAXPATHLEN);
                    383:        if (cvs_path_cat(dir, CVS_PATH_STATICENTRIES, fpath, MAXPATHLEN) >=
                    384:            MAXPATHLEN)
                    385:                fatal("cvs_client_senddir: truncation");
                    386:
                    387:        if (stat(fpath, &st) == 0 && (st.st_mode & (S_IRUSR|S_IRGRP|S_IROTH)))
                    388:                cvs_client_send_request("Static-directory");
                    389:
                    390:        xfree(fpath);
1.29      xsa       391:
                    392:        d = xstrdup(dir);
                    393:        cvs_parse_tagfile(d, &tag, &date, &nb);
                    394:
                    395:        if (tag != NULL || date != NULL) {
                    396:                char buf[128];
                    397:
                    398:                if (tag != NULL && nb != NULL) {
                    399:                        if (strlcpy(buf, "N", sizeof(buf)) >= sizeof(buf))
                    400:                                fatal("cvs_client_senddir: truncation");
                    401:                } else if (tag != NULL) {
                    402:                        if (strlcpy(buf, "T", sizeof(buf)) >= sizeof(buf))
                    403:                                fatal("cvs_client_senddir: truncation");
                    404:                } else {
                    405:                        if (strlcpy(buf, "D", sizeof(buf)) >= sizeof(buf))
                    406:                                fatal("cvs_client_senddir: truncation");
                    407:                }
                    408:
                    409:                if (strlcat(buf, tag ? tag : date, sizeof(buf)) >= sizeof(buf))
                    410:                        fatal("cvs_client_senddir: truncation");
                    411:
                    412:                cvs_client_send_request("Sticky %s", buf);
                    413:
                    414:                if (tag != NULL)
                    415:                        xfree(tag);
                    416:                if (date != NULL)
                    417:                        xfree(date);
                    418:        }
                    419:        if (d != NULL)
                    420:                xfree(d);
1.1       jfb       421:
1.9       joris     422:        if (lastdir != NULL)
                    423:                xfree(lastdir);
                    424:        lastdir = xstrdup(dir);
                    425: }
1.1       jfb       426:
1.9       joris     427: void
                    428: cvs_client_sendfile(struct cvs_file *cf)
1.1       jfb       429: {
1.9       joris     430:        int l;
                    431:        size_t len;
                    432:        char rev[16], timebuf[64], sticky[32];
1.1       jfb       433:
1.9       joris     434:        if (cf->file_type != CVS_FILE)
                    435:                return;
1.1       jfb       436:
1.9       joris     437:        cvs_client_senddir(cf->file_wd);
                    438:        cvs_remote_classify_file(cf);
                    439:
1.10      joris     440:        if (cf->file_type == CVS_DIR)
                    441:                return;
                    442:
1.9       joris     443:        if (cf->file_ent != NULL) {
                    444:                if (cf->file_status == FILE_ADDED) {
                    445:                        len = strlcpy(rev, "0", sizeof(rev));
                    446:                        if (len >= sizeof(rev))
                    447:                                fatal("cvs_client_sendfile: truncation");
                    448:
                    449:                        len = strlcpy(timebuf, "Initial ", sizeof(timebuf));
                    450:                        if (len >= sizeof(timebuf))
                    451:                                fatal("cvs_client_sendfile: truncation");
                    452:
                    453:                        len = strlcat(timebuf, cf->file_name, sizeof(timebuf));
                    454:                        if (len >= sizeof(timebuf))
                    455:                                fatal("cvs_client_sendfile: truncation");
                    456:                } else {
                    457:                        rcsnum_tostr(cf->file_ent->ce_rev, rev, sizeof(rev));
                    458:                        ctime_r(&cf->file_ent->ce_mtime, timebuf);
                    459:                }
1.1       jfb       460:
1.9       joris     461:                if (cf->file_ent->ce_conflict == NULL) {
                    462:                        if (timebuf[strlen(timebuf) - 1] == '\n')
                    463:                                timebuf[strlen(timebuf) - 1] = '\0';
                    464:                } else {
                    465:                        len = strlcpy(timebuf, cf->file_ent->ce_conflict,
                    466:                            sizeof(timebuf));
                    467:                        if (len >= sizeof(timebuf))
                    468:                                fatal("cvs_client_sendfile: truncation");
                    469:                }
1.1       jfb       470:
1.9       joris     471:                sticky[0] = '\0';
                    472:                if (cf->file_ent->ce_tag != NULL) {
                    473:                        l = snprintf(sticky, sizeof(sticky), "T%s",
                    474:                            cf->file_ent->ce_tag);
                    475:                        if (l == -1 || l >= (int)sizeof(sticky))
                    476:                                fatal("cvs_client_sendfile: overflow");
1.1       jfb       477:                }
                    478:
1.13      joris     479:                cvs_client_send_request("Entry /%s/%s%s/%s//%s",
1.9       joris     480:                    cf->file_name, (cf->file_status == FILE_REMOVED) ? "-" : "",
                    481:                   rev, timebuf, sticky);
                    482:        }
                    483:
                    484:        switch (cf->file_status) {
                    485:        case FILE_UNKNOWN:
                    486:                if (cf->fd != -1)
                    487:                        cvs_client_send_request("Questionable %s",
                    488:                            cf->file_name);
                    489:                break;
                    490:        case FILE_ADDED:
                    491:        case FILE_MODIFIED:
                    492:                cvs_client_send_request("Modified %s", cf->file_name);
                    493:                cvs_remote_send_file(cf->file_path);
                    494:                break;
                    495:        case FILE_UPTODATE:
                    496:                cvs_client_send_request("Unchanged %s", cf->file_name);
                    497:                break;
1.1       jfb       498:        }
1.9       joris     499: }
1.1       jfb       500:
1.9       joris     501: void
                    502: cvs_client_send_files(char **argv, int argc)
                    503: {
                    504:        int i;
1.1       jfb       505:
1.9       joris     506:        for (i = 0; i < argc; i++)
                    507:                cvs_client_send_request("Argument %s", argv[i]);
                    508: }
1.1       jfb       509:
1.9       joris     510: void
                    511: cvs_client_ok(char *data)
                    512: {
                    513:        end_of_response = 1;
1.37      joris     514:        server_response = SERVER_OK;
1.1       jfb       515: }
                    516:
1.9       joris     517: void
                    518: cvs_client_error(char *data)
                    519: {
                    520:        end_of_response = 1;
1.37      joris     521:        server_response = SERVER_ERROR;
1.9       joris     522: }
1.1       jfb       523:
1.9       joris     524: void
                    525: cvs_client_validreq(char *data)
1.1       jfb       526: {
1.9       joris     527:        int i;
                    528:        char *sp, *ep;
                    529:        struct cvs_req *req;
                    530:
                    531:        sp = data;
                    532:        do {
                    533:                if ((ep = strchr(sp, ' ')) != NULL)
                    534:                        *ep = '\0';
                    535:
                    536:                req = cvs_remote_get_request_info(sp);
                    537:                if (req != NULL)
                    538:                        req->supported = 1;
                    539:
                    540:                if (ep != NULL)
                    541:                        sp = ep + 1;
                    542:        } while (ep != NULL);
                    543:
                    544:        for (i = 0; cvs_requests[i].supported != -1; i++) {
                    545:                req = &cvs_requests[i];
                    546:                if ((req->flags & REQ_NEEDED) &&
                    547:                    req->supported != 1) {
                    548:                        fatal("server does not support required '%s'",
                    549:                            req->name);
                    550:                }
1.1       jfb       551:        }
1.9       joris     552: }
1.1       jfb       553:
1.9       joris     554: void
                    555: cvs_client_e(char *data)
                    556: {
                    557:        cvs_printf("%s\n", data);
                    558: }
1.1       jfb       559:
1.9       joris     560: void
                    561: cvs_client_m(char *data)
                    562: {
                    563:        cvs_printf("%s\n", data);
                    564: }
1.1       jfb       565:
1.9       joris     566: void
                    567: cvs_client_checkedin(char *data)
                    568: {
                    569:        int l;
                    570:        CVSENTRIES *entlist;
                    571:        struct cvs_ent *ent, *newent;
                    572:        char *dir, *entry, rev[16], timebuf[64], sticky[16];
1.1       jfb       573:
1.9       joris     574:        dir = cvs_remote_input();
                    575:        entry = cvs_remote_input();
                    576:        xfree(dir);
1.1       jfb       577:
1.9       joris     578:        entlist = cvs_ent_open(data);
                    579:        newent = cvs_ent_parse(entry);
                    580:        ent = cvs_ent_get(entlist, newent->ce_name);
                    581:        xfree(entry);
1.1       jfb       582:
1.9       joris     583:        entry = xmalloc(CVS_ENT_MAXLINELEN);
1.1       jfb       584:
1.9       joris     585:        rcsnum_tostr(newent->ce_rev, rev, sizeof(rev));
                    586:        ctime_r(&ent->ce_mtime, timebuf);
                    587:        if (timebuf[strlen(timebuf) - 1] == '\n')
                    588:                timebuf[strlen(timebuf) - 1] = '\0';
1.1       jfb       589:
1.9       joris     590:        sticky[0] = '\0';
                    591:        if (ent->ce_tag != NULL) {
                    592:                l = snprintf(sticky, sizeof(sticky), "T%s", ent->ce_tag);
                    593:                if (l == -1 || l >= (int)sizeof(sticky))
                    594:                        fatal("cvs_client_checkedin: overflow");
1.2       jfb       595:        }
1.1       jfb       596:
1.44      joris     597:        l = snprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s%s/%s//%s/",
                    598:            newent->ce_name, (newent->ce_status == CVS_ENT_REMOVED) ? "-" : "",
                    599:            rev, timebuf, sticky);
1.9       joris     600:        if (l == -1 || l >= CVS_ENT_MAXLINELEN)
                    601:                fatal("cvs_client_checkedin: overflow");
                    602:
                    603:        cvs_ent_free(ent);
                    604:        cvs_ent_free(newent);
                    605:        cvs_ent_add(entlist, entry);
                    606:        cvs_ent_close(entlist, ENT_SYNC);
1.1       jfb       607:
1.9       joris     608:        xfree(entry);
1.1       jfb       609: }
                    610:
1.9       joris     611: void
                    612: cvs_client_updated(char *data)
                    613: {
                    614:        int l, fd;
                    615:        time_t now;
                    616:        mode_t fmode;
                    617:        size_t flen;
                    618:        CVSENTRIES *ent;
                    619:        struct cvs_ent *e;
                    620:        const char *errstr;
                    621:        struct timeval tv[2];
                    622:        char timebuf[32], *repo, *rpath, *entry, *mode;
                    623:        char revbuf[32], *len, *fpath, *wdir;
                    624:
1.10      joris     625:        client_check_directory(data);
1.9       joris     626:
                    627:        rpath = cvs_remote_input();
                    628:        entry = cvs_remote_input();
                    629:        mode = cvs_remote_input();
                    630:        len = cvs_remote_input();
                    631:
                    632:        repo = xmalloc(MAXPATHLEN);
                    633:        cvs_get_repository_path(".", repo, MAXPATHLEN);
                    634:
                    635:        if (strlen(repo) + 1 > strlen(rpath))
                    636:                fatal("received a repository path that is too short");
                    637:
                    638:        fpath = rpath + strlen(repo) + 1;
                    639:        if ((wdir = dirname(fpath)) == NULL)
                    640:                fatal("cvs_client_updated: dirname: %s", strerror(errno));
                    641:        xfree(repo);
                    642:
                    643:        flen = strtonum(len, 0, INT_MAX, &errstr);
                    644:        if (errstr != NULL)
                    645:                fatal("cvs_client_updated: %s: %s", len, errstr);
                    646:        xfree(len);
                    647:
                    648:        cvs_strtomode(mode, &fmode);
                    649:        xfree(mode);
                    650:
                    651:        time(&now);
                    652:        now = cvs_hack_time(now, 0);
                    653:        ctime_r(&now, timebuf);
                    654:        if (timebuf[strlen(timebuf) - 1] == '\n')
                    655:                timebuf[strlen(timebuf) - 1] = '\0';
                    656:
                    657:        e = cvs_ent_parse(entry);
                    658:        xfree(entry);
                    659:        rcsnum_tostr(e->ce_rev, revbuf, sizeof(revbuf));
                    660:        entry = xmalloc(CVS_ENT_MAXLINELEN);
                    661:        l = snprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s//", e->ce_name,
                    662:            revbuf, timebuf);
                    663:        if (l == -1 || l >= CVS_ENT_MAXLINELEN)
                    664:                fatal("cvs_client_updated: overflow");
                    665:
                    666:        cvs_ent_free(e);
                    667:        ent = cvs_ent_open(wdir);
                    668:        cvs_ent_add(ent, entry);
                    669:        cvs_ent_close(ent, ENT_SYNC);
                    670:        xfree(entry);
                    671:
                    672:        if ((fd = open(fpath, O_CREAT | O_WRONLY | O_TRUNC)) == -1)
                    673:                fatal("cvs_client_updated: open: %s: %s",
                    674:                    fpath, strerror(errno));
1.1       jfb       675:
1.46      joris     676:        cvs_remote_receive_file(fd, flen);
1.1       jfb       677:
1.9       joris     678:        now = cvs_hack_time(now, 0);
                    679:        tv[0].tv_sec = now;
                    680:        tv[0].tv_usec = 0;
                    681:        tv[1] = tv[0];
1.1       jfb       682:
1.9       joris     683:        if (futimes(fd, tv) == -1)
                    684:                fatal("cvs_client_updated: futimes: %s", strerror(errno));
1.1       jfb       685:
1.9       joris     686:        if (fchmod(fd, fmode) == -1)
                    687:                fatal("cvs_client_updated: fchmod: %s", strerror(errno));
1.1       jfb       688:
1.9       joris     689:        (void)close(fd);
1.1       jfb       690:
1.9       joris     691:        xfree(rpath);
1.1       jfb       692: }
                    693:
1.9       joris     694: void
                    695: cvs_client_merged(char *data)
                    696: {
                    697: }
1.1       jfb       698:
1.9       joris     699: void
                    700: cvs_client_removed(char *data)
                    701: {
1.13      joris     702:        char *dir;
                    703:
                    704:        dir = cvs_remote_input();
                    705:        xfree(dir);
1.9       joris     706: }
1.1       jfb       707:
1.9       joris     708: void
                    709: cvs_client_remove_entry(char *data)
1.1       jfb       710: {
1.38      xsa       711:        CVSENTRIES *entlist;
1.44      joris     712:        char *filename, *rpath;
1.41      joris     713:
                    714:        rpath = cvs_remote_input();
                    715:        if ((filename = strrchr(rpath, '/')) == NULL)
                    716:                fatal("bad rpath in cvs_client_remove_entry: %s", rpath);
                    717:        *filename++;
1.1       jfb       718:
1.38      xsa       719:        entlist = cvs_ent_open(data);
1.44      joris     720:        cvs_ent_remove(entlist, filename);
1.38      xsa       721:        cvs_ent_close(entlist, ENT_SYNC);
                    722:
1.44      joris     723:        xfree(rpath);
1.36      xsa       724: }
                    725:
                    726: void
                    727: cvs_client_set_static_directory(char *data)
                    728: {
                    729:        FILE *fp;
                    730:        char *dir, *fpath;
                    731:
                    732:        if (cvs_cmdop == CVS_OP_EXPORT)
                    733:                return;
                    734:
                    735:        STRIP_SLASH(data);
                    736:
                    737:        dir = cvs_remote_input();
                    738:        xfree(dir);
                    739:
                    740:        fpath = xmalloc(MAXPATHLEN);
                    741:        if (cvs_path_cat(data, CVS_PATH_STATICENTRIES, fpath, MAXPATHLEN) >=
                    742:            MAXPATHLEN)
                    743:                fatal("cvs_client_set_static_directory: truncation");
                    744:
                    745:        if ((fp = fopen(fpath, "w+")) == NULL) {
                    746:                cvs_log(LP_ERRNO, "%s", fpath);
                    747:                goto out;
                    748:        }
                    749:        (void)fclose(fp);
                    750: out:
                    751:        xfree(fpath);
1.16      xsa       752: }
1.35      xsa       753:
                    754: void
                    755: cvs_client_clear_static_directory(char *data)
                    756: {
                    757:        char *dir, *fpath;
                    758:
                    759:        if (cvs_cmdop == CVS_OP_EXPORT)
                    760:                return;
                    761:
                    762:        STRIP_SLASH(data);
                    763:
                    764:        dir = cvs_remote_input();
                    765:        xfree(dir);
                    766:
                    767:        fpath = xmalloc(MAXPATHLEN);
                    768:        if (cvs_path_cat(data, CVS_PATH_STATICENTRIES, fpath, MAXPATHLEN) >=
                    769:            MAXPATHLEN)
                    770:                fatal("cvs_client_clear_static_directory: truncation");
                    771:
                    772:        (void)cvs_unlink(fpath);
                    773:
                    774:        xfree(fpath);
                    775: }
                    776:
                    777: void
                    778: cvs_client_set_sticky(char *data)
                    779: {
                    780:        FILE *fp;
                    781:        char *dir, *tag, *tagpath;
                    782:
1.47    ! xsa       783:        if (cvs_cmdop == CVS_OP_EXPORT)
        !           784:                return;
        !           785:
1.35      xsa       786:        STRIP_SLASH(data);
                    787:
                    788:        dir = cvs_remote_input();
                    789:        xfree(dir);
                    790:        tag = cvs_remote_input();
                    791:
                    792:        tagpath = xmalloc(MAXPATHLEN);
                    793:        if (cvs_path_cat(data, CVS_PATH_TAG, tagpath, MAXPATHLEN) >= MAXPATHLEN)
                    794:                fatal("cvs_client_clear_sticky: truncation");
                    795:
                    796:        if ((fp = fopen(tagpath, "w+")) == NULL) {
                    797:                cvs_log(LP_ERRNO, "%s", tagpath);
                    798:                goto out;
                    799:        }
                    800:
                    801:        (void)fprintf(fp, "%s\n", tag);
                    802:        (void)fclose(fp);
                    803: out:
                    804:        xfree(tagpath);
                    805:        xfree(tag);
                    806: }
                    807:
                    808: void
                    809: cvs_client_clear_sticky(char *data)
                    810: {
                    811:        char *dir, *tagpath;
1.47    ! xsa       812:
        !           813:        if (cvs_cmdop == CVS_OP_EXPORT)
        !           814:                return;
1.35      xsa       815:
                    816:        STRIP_SLASH(data);
                    817:
                    818:        dir = cvs_remote_input();
                    819:        xfree(dir);
                    820:
                    821:        tagpath = xmalloc(MAXPATHLEN);
                    822:        if (cvs_path_cat(data, CVS_PATH_TAG, tagpath, MAXPATHLEN) >= MAXPATHLEN)
                    823:                fatal("cvs_client_clear_sticky: truncation");
                    824:
                    825:        (void)cvs_unlink(tagpath);
                    826:
                    827:        xfree(tagpath);
                    828: }
                    829:
                    830:
1.16      xsa       831: /*
                    832:  * cvs_client_initlog()
                    833:  *
                    834:  * Initialize protocol logging if the CVS_CLIENT_LOG environment variable is
                    835:  * set.  In this case, the variable's value is used as a path to which the
                    836:  * appropriate suffix is added (".in" for client input and ".out" for server
                    837:  * output).
                    838:  */
                    839: static void
                    840: cvs_client_initlog(void)
                    841: {
                    842:        int l;
                    843:        u_int i;
                    844:        char *env, *envdup, buf[MAXPATHLEN], fpath[MAXPATHLEN];
                    845:        char rpath[MAXPATHLEN], *s;
                    846:        struct stat st;
                    847:        time_t now;
                    848:        struct passwd *pwd;
                    849:
                    850:        /* avoid doing it more than once */
                    851:        if (cvs_client_logon)
                    852:                return;
                    853:
                    854:        if ((env = getenv("CVS_CLIENT_LOG")) == NULL)
                    855:                return;
                    856:
                    857:        envdup = xstrdup(env);
                    858:        if ((s = strchr(envdup, '%')) != NULL)
                    859:                *s = '\0';
                    860:
                    861:        if (strlcpy(buf, env, sizeof(buf)) >= sizeof(buf))
                    862:                fatal("cvs_client_initlog: truncation");
                    863:
                    864:        if (strlcpy(rpath, envdup, sizeof(rpath)) >= sizeof(rpath))
                    865:                fatal("cvs_client_initlog: truncation");
                    866:
                    867:        xfree(envdup);
                    868:
                    869:        s = buf;
                    870:        while ((s = strchr(s, '%')) != NULL) {
                    871:                s++;
                    872:                switch (*s) {
                    873:                case 'c':
                    874:                        if (strlcpy(fpath, cvs_command, sizeof(fpath)) >=
                    875:                            sizeof(fpath))
                    876:                                fatal("cvs_client_initlog: truncation");
                    877:                        break;
                    878:                case 'd':
                    879:                        time(&now);
                    880:                        if (strlcpy(fpath, ctime(&now), sizeof(fpath)) >=
                    881:                            sizeof(fpath))
                    882:                                fatal("cvs_client_initlog: truncation");
                    883:                        break;
                    884:                case 'p':
                    885:                        snprintf(fpath, sizeof(fpath), "%d", getpid());
                    886:                        break;
                    887:                case 'u':
                    888:                        if ((pwd = getpwuid(getuid())) != NULL) {
                    889:                                if (strlcpy(fpath, pwd->pw_name,
                    890:                                    sizeof(fpath)) >= sizeof(fpath))
                    891:                                        fatal("cvs_client_initlog: truncation");
                    892:                        } else {
                    893:                                fpath[0] = '\0';
                    894:                        }
                    895:                        endpwent();
                    896:                        break;
                    897:                default:
                    898:                        fpath[0] = '\0';
                    899:                        break;
                    900:                }
                    901:
                    902:                if (fpath[0] != '\0') {
                    903:                        if (strlcat(rpath, "-", sizeof(rpath)) >= sizeof(rpath))
                    904:                                fatal("cvs_client_initlog: truncation");
                    905:
                    906:                        if (strlcat(rpath, fpath, sizeof(rpath))
                    907:                            >= sizeof(rpath))
                    908:                                fatal("cvs_client_initlog: truncation");
                    909:                }
                    910:        }
                    911:
                    912:        for (i = 0; i < UINT_MAX; i++) {
                    913:                l = snprintf(fpath, sizeof(fpath), "%s-%d.in", rpath, i);
                    914:                if (l == -1 || l >= (int)sizeof(fpath))
                    915:                        fatal("cvs_client_initlog: overflow");
                    916:
                    917:                if (stat(fpath, &st) != -1)
                    918:                        continue;
                    919:
                    920:                if (errno != ENOENT)
                    921:                        fatal("cvs_client_initlog() stat failed '%s'",
                    922:                            strerror(errno));
                    923:
                    924:                break;
                    925:        }
                    926:
                    927:        if ((cvs_client_inlog_fd = open(fpath,
                    928:            O_RDWR | O_CREAT | O_TRUNC, 0644)) == NULL) {
                    929:                fatal("cvs_client_initlog: open `%s': %s",
                    930:                    fpath, strerror(errno));
                    931:        }
                    932:
                    933:        for (i = 0; i < UINT_MAX; i++) {
                    934:                l = snprintf(fpath, sizeof(fpath), "%s-%d.out", rpath, i);
                    935:                if (l == -1 || l >= (int)sizeof(fpath))
                    936:                        fatal("cvs_client_initlog: overflow");
                    937:
                    938:                if (stat(fpath, &st) != -1)
                    939:                        continue;
                    940:
                    941:                if (errno != ENOENT)
                    942:                        fatal("cvs_client_initlog() stat failed '%s'",
                    943:                            strerror(errno));
                    944:
                    945:                break;
                    946:        }
                    947:
                    948:        if ((cvs_client_outlog_fd = open(fpath,
                    949:            O_RDWR | O_CREAT | O_TRUNC, 0644)) == NULL) {
                    950:                fatal("cvs_client_initlog: open `%s': %s",
                    951:                    fpath, strerror(errno));
                    952:        }
                    953:
                    954:        cvs_client_logon = 1;
1.1       jfb       955: }