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

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