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

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