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

1.79    ! joris       1: /*     $OpenBSD: client.c,v 1.78 2007/09/10 14:06:14 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 },
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:
1.76      tobias    282:        switch (verbosity) {
                    283:        case 0:
1.19      xsa       284:                cvs_client_send_request("Global_option -Q");
1.76      tobias    285:                break;
                    286:        case 1:
                    287:                /* Be quiet. This is the default in OpenCVS. */
                    288:                cvs_client_send_request("Global_option -q");
                    289:                break;
                    290:        default:
                    291:                break;
                    292:        }
1.19      xsa       293:
                    294:        if (cvs_readonly == 1)
                    295:                cvs_client_send_request("Global_option -r");
                    296:
                    297:        if (cvs_trace == 1)
1.9       joris     298:                cvs_client_send_request("Global_option -t");
1.26      xsa       299:
                    300:        /* XXX: If 'Set' is supported? */
                    301:        TAILQ_FOREACH(vp, &cvs_variables, cv_link)
                    302:                cvs_client_send_request("Set %s=%s", vp->cv_name, vp->cv_val);
1.1       jfb       303: }
                    304:
1.9       joris     305: void
                    306: cvs_client_send_request(char *fmt, ...)
                    307: {
                    308:        va_list ap;
                    309:        char *data, *s;
                    310:        struct cvs_req *req;
                    311:
                    312:        va_start(ap, fmt);
                    313:        vasprintf(&data, fmt, ap);
                    314:        va_end(ap);
1.1       jfb       315:
1.9       joris     316:        if ((s = strchr(data, ' ')) != NULL)
                    317:                *s = '\0';
1.1       jfb       318:
1.9       joris     319:        req = cvs_remote_get_request_info(data);
                    320:        if (req == NULL)
                    321:                fatal("'%s' is an unknown request", data);
1.1       jfb       322:
1.9       joris     323:        if (req->supported != 1)
                    324:                fatal("remote cvs server does not support '%s'", data);
1.1       jfb       325:
1.9       joris     326:        if (s != NULL)
                    327:                *s = ' ';
1.1       jfb       328:
1.12      joris     329:        cvs_log(LP_TRACE, "%s", data);
1.16      xsa       330:
1.9       joris     331:        cvs_remote_output(data);
                    332:        xfree(data);
                    333: }
1.1       jfb       334:
1.9       joris     335: void
                    336: cvs_client_read_response(void)
1.1       jfb       337: {
1.9       joris     338:        char *cmd, *data;
                    339:        struct cvs_resp *resp;
1.1       jfb       340:
1.9       joris     341:        cmd = cvs_remote_input();
                    342:        if ((data = strchr(cmd, ' ')) != NULL)
                    343:                (*data++) = '\0';
1.1       jfb       344:
1.9       joris     345:        resp = cvs_remote_get_response_info(cmd);
                    346:        if (resp == NULL)
                    347:                fatal("response '%s' is not supported by our client", cmd);
1.1       jfb       348:
1.9       joris     349:        if (resp->hdlr == NULL)
                    350:                fatal("opencvs client does not support '%s'", cmd);
1.16      xsa       351:
1.9       joris     352:        (*resp->hdlr)(data);
1.16      xsa       353:
1.9       joris     354:        xfree(cmd);
1.1       jfb       355: }
                    356:
1.9       joris     357: void
                    358: cvs_client_get_responses(void)
                    359: {
                    360:        while (end_of_response != 1)
                    361:                cvs_client_read_response();
1.1       jfb       362:
1.9       joris     363:        end_of_response = 0;
1.72      joris     364: }
                    365:
                    366: void
                    367: cvs_client_send_logmsg(char *msg)
                    368: {
                    369:        char *buf, *p, *q;
                    370:
                    371:        (void)xasprintf(&buf, "%s\n", msg);
                    372:
                    373:        cvs_client_send_request("Argument -m");
                    374:        if ((p = strchr(buf, '\n')) != NULL)
                    375:                *p++ = '\0';
                    376:        cvs_client_send_request("Argument %s", buf);
                    377:        for (q = p; p != NULL; q = p) {
                    378:                if ((p = strchr(q, '\n')) != NULL) {
                    379:                        *p++ = '\0';
                    380:                        cvs_client_send_request("Argumentx %s", q);
                    381:                }
                    382:        }
                    383:
1.73      joris     384:        xfree(buf);
1.9       joris     385: }
1.1       jfb       386:
1.9       joris     387: void
                    388: cvs_client_senddir(const char *dir)
1.1       jfb       389: {
1.34      xsa       390:        struct stat st;
1.29      xsa       391:        int nb;
1.50      otto      392:        char *d, *date, fpath[MAXPATHLEN], repo[MAXPATHLEN], *tag;
1.29      xsa       393:
                    394:        d = NULL;
1.9       joris     395:
                    396:        if (lastdir != NULL && !strcmp(dir, lastdir))
                    397:                return;
1.1       jfb       398:
1.9       joris     399:        cvs_get_repository_path(dir, repo, MAXPATHLEN);
1.31      xsa       400:
1.77      tobias    401:        if (cvs_cmdop == CVS_OP_CHECKOUT && strcmp(dir, "."))
                    402:                return;
                    403:        else if (cvs_cmdop != CVS_OP_RLOG)
1.70      xsa       404:                cvs_client_send_request("Directory %s\n%s", dir, repo);
1.31      xsa       405:
1.58      xsa       406:        (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
                    407:            dir, CVS_PATH_STATICENTRIES);
1.34      xsa       408:
                    409:        if (stat(fpath, &st) == 0 && (st.st_mode & (S_IRUSR|S_IRGRP|S_IROTH)))
                    410:                cvs_client_send_request("Static-directory");
                    411:
1.29      xsa       412:        d = xstrdup(dir);
                    413:        cvs_parse_tagfile(d, &tag, &date, &nb);
                    414:
                    415:        if (tag != NULL || date != NULL) {
                    416:                char buf[128];
                    417:
                    418:                if (tag != NULL && nb != NULL) {
                    419:                        if (strlcpy(buf, "N", sizeof(buf)) >= sizeof(buf))
                    420:                                fatal("cvs_client_senddir: truncation");
                    421:                } else if (tag != NULL) {
                    422:                        if (strlcpy(buf, "T", sizeof(buf)) >= sizeof(buf))
                    423:                                fatal("cvs_client_senddir: truncation");
                    424:                } else {
                    425:                        if (strlcpy(buf, "D", sizeof(buf)) >= sizeof(buf))
                    426:                                fatal("cvs_client_senddir: truncation");
                    427:                }
                    428:
                    429:                if (strlcat(buf, tag ? tag : date, sizeof(buf)) >= sizeof(buf))
                    430:                        fatal("cvs_client_senddir: truncation");
                    431:
                    432:                cvs_client_send_request("Sticky %s", buf);
                    433:
                    434:                if (tag != NULL)
                    435:                        xfree(tag);
                    436:                if (date != NULL)
                    437:                        xfree(date);
                    438:        }
                    439:        if (d != NULL)
                    440:                xfree(d);
1.1       jfb       441:
1.9       joris     442:        if (lastdir != NULL)
                    443:                xfree(lastdir);
                    444:        lastdir = xstrdup(dir);
                    445: }
1.1       jfb       446:
1.9       joris     447: void
                    448: cvs_client_sendfile(struct cvs_file *cf)
1.1       jfb       449: {
1.9       joris     450:        size_t len;
1.67      xsa       451:        char rev[CVS_REV_BUFSZ], timebuf[CVS_TIME_BUFSZ], sticky[32];
1.1       jfb       452:
1.9       joris     453:        if (cf->file_type != CVS_FILE)
                    454:                return;
1.1       jfb       455:
1.9       joris     456:        cvs_client_senddir(cf->file_wd);
                    457:        cvs_remote_classify_file(cf);
                    458:
1.10      joris     459:        if (cf->file_type == CVS_DIR)
                    460:                return;
                    461:
1.9       joris     462:        if (cf->file_ent != NULL) {
                    463:                if (cf->file_status == FILE_ADDED) {
                    464:                        len = strlcpy(rev, "0", sizeof(rev));
                    465:                        if (len >= sizeof(rev))
                    466:                                fatal("cvs_client_sendfile: truncation");
                    467:
                    468:                        len = strlcpy(timebuf, "Initial ", sizeof(timebuf));
                    469:                        if (len >= sizeof(timebuf))
                    470:                                fatal("cvs_client_sendfile: truncation");
                    471:
                    472:                        len = strlcat(timebuf, cf->file_name, sizeof(timebuf));
                    473:                        if (len >= sizeof(timebuf))
                    474:                                fatal("cvs_client_sendfile: truncation");
                    475:                } else {
                    476:                        rcsnum_tostr(cf->file_ent->ce_rev, rev, sizeof(rev));
                    477:                        ctime_r(&cf->file_ent->ce_mtime, timebuf);
                    478:                }
1.1       jfb       479:
1.9       joris     480:                if (cf->file_ent->ce_conflict == NULL) {
                    481:                        if (timebuf[strlen(timebuf) - 1] == '\n')
                    482:                                timebuf[strlen(timebuf) - 1] = '\0';
                    483:                } else {
                    484:                        len = strlcpy(timebuf, cf->file_ent->ce_conflict,
                    485:                            sizeof(timebuf));
                    486:                        if (len >= sizeof(timebuf))
                    487:                                fatal("cvs_client_sendfile: truncation");
1.56      joris     488:                        len = strlcat(timebuf, "+", sizeof(timebuf));
                    489:                        if (len >= sizeof(timebuf))
                    490:                                fatal("cvs_client_sendfile: truncation");
1.9       joris     491:                }
1.1       jfb       492:
1.9       joris     493:                sticky[0] = '\0';
                    494:                if (cf->file_ent->ce_tag != NULL) {
1.57      xsa       495:                        (void)xsnprintf(sticky, sizeof(sticky), "T%s",
1.9       joris     496:                            cf->file_ent->ce_tag);
1.1       jfb       497:                }
                    498:
1.55      otto      499:                cvs_client_send_request("Entry /%s/%s%s/%s/%s/%s",
1.9       joris     500:                    cf->file_name, (cf->file_status == FILE_REMOVED) ? "-" : "",
1.53      xsa       501:                    rev, timebuf, cf->file_ent->ce_opts ?
                    502:                    cf->file_ent->ce_opts : "", sticky);
1.9       joris     503:        }
                    504:
                    505:        switch (cf->file_status) {
                    506:        case FILE_UNKNOWN:
                    507:                if (cf->fd != -1)
                    508:                        cvs_client_send_request("Questionable %s",
                    509:                            cf->file_name);
                    510:                break;
                    511:        case FILE_ADDED:
                    512:        case FILE_MODIFIED:
                    513:                cvs_client_send_request("Modified %s", cf->file_name);
                    514:                cvs_remote_send_file(cf->file_path);
                    515:                break;
                    516:        case FILE_UPTODATE:
                    517:                cvs_client_send_request("Unchanged %s", cf->file_name);
                    518:                break;
1.1       jfb       519:        }
1.9       joris     520: }
1.1       jfb       521:
1.9       joris     522: void
                    523: cvs_client_send_files(char **argv, int argc)
                    524: {
                    525:        int i;
1.1       jfb       526:
1.9       joris     527:        for (i = 0; i < argc; i++)
                    528:                cvs_client_send_request("Argument %s", argv[i]);
                    529: }
1.1       jfb       530:
1.9       joris     531: void
                    532: cvs_client_ok(char *data)
                    533: {
                    534:        end_of_response = 1;
1.37      joris     535:        server_response = SERVER_OK;
1.1       jfb       536: }
                    537:
1.9       joris     538: void
                    539: cvs_client_error(char *data)
                    540: {
                    541:        end_of_response = 1;
1.37      joris     542:        server_response = SERVER_ERROR;
1.9       joris     543: }
1.1       jfb       544:
1.9       joris     545: void
                    546: cvs_client_validreq(char *data)
1.1       jfb       547: {
1.9       joris     548:        int i;
                    549:        char *sp, *ep;
                    550:        struct cvs_req *req;
                    551:
1.62      ray       552:        if ((sp = data) == NULL)
                    553:                fatal("Missing argument for Valid-requests");
                    554:
1.9       joris     555:        do {
                    556:                if ((ep = strchr(sp, ' ')) != NULL)
                    557:                        *ep = '\0';
                    558:
                    559:                req = cvs_remote_get_request_info(sp);
                    560:                if (req != NULL)
                    561:                        req->supported = 1;
                    562:
                    563:                if (ep != NULL)
                    564:                        sp = ep + 1;
                    565:        } while (ep != NULL);
                    566:
                    567:        for (i = 0; cvs_requests[i].supported != -1; i++) {
                    568:                req = &cvs_requests[i];
                    569:                if ((req->flags & REQ_NEEDED) &&
                    570:                    req->supported != 1) {
                    571:                        fatal("server does not support required '%s'",
                    572:                            req->name);
                    573:                }
1.1       jfb       574:        }
1.9       joris     575: }
1.1       jfb       576:
1.9       joris     577: void
                    578: cvs_client_e(char *data)
                    579: {
1.62      ray       580:        if (data == NULL)
                    581:                fatal("Missing argument for E");
                    582:
1.9       joris     583:        cvs_printf("%s\n", data);
                    584: }
1.1       jfb       585:
1.9       joris     586: void
                    587: cvs_client_m(char *data)
                    588: {
1.62      ray       589:        if (data == NULL)
                    590:                fatal("Missing argument for M");
                    591:
1.9       joris     592:        cvs_printf("%s\n", data);
                    593: }
1.1       jfb       594:
1.9       joris     595: void
                    596: cvs_client_checkedin(char *data)
                    597: {
                    598:        CVSENTRIES *entlist;
                    599:        struct cvs_ent *ent, *newent;
1.67      xsa       600:        char *dir, *e, entry[CVS_ENT_MAXLINELEN], rev[CVS_REV_BUFSZ];
                    601:        char sticky[CVS_ENT_MAXLINELEN], timebuf[CVS_TIME_BUFSZ];
1.1       jfb       602:
1.62      ray       603:        if (data == NULL)
                    604:                fatal("Missing argument for Checked-in");
                    605:
1.9       joris     606:        dir = cvs_remote_input();
1.54      otto      607:        e = cvs_remote_input();
1.9       joris     608:        xfree(dir);
1.1       jfb       609:
1.9       joris     610:        entlist = cvs_ent_open(data);
1.54      otto      611:        newent = cvs_ent_parse(e);
1.9       joris     612:        ent = cvs_ent_get(entlist, newent->ce_name);
1.54      otto      613:        xfree(e);
1.1       jfb       614:
1.9       joris     615:        rcsnum_tostr(newent->ce_rev, rev, sizeof(rev));
                    616:        ctime_r(&ent->ce_mtime, timebuf);
                    617:        if (timebuf[strlen(timebuf) - 1] == '\n')
                    618:                timebuf[strlen(timebuf) - 1] = '\0';
1.1       jfb       619:
1.9       joris     620:        sticky[0] = '\0';
1.57      xsa       621:        if (ent->ce_tag != NULL)
                    622:                (void)xsnprintf(sticky, sizeof(sticky), "T%s", ent->ce_tag);
1.1       jfb       623:
1.57      xsa       624:        (void)xsnprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s%s/%s/%s/%s",
1.44      joris     625:            newent->ce_name, (newent->ce_status == CVS_ENT_REMOVED) ? "-" : "",
1.53      xsa       626:            rev, timebuf, ent->ce_opts ? ent->ce_opts : "", sticky);
1.9       joris     627:
                    628:        cvs_ent_free(ent);
                    629:        cvs_ent_free(newent);
                    630:        cvs_ent_add(entlist, entry);
                    631:        cvs_ent_close(entlist, ENT_SYNC);
1.1       jfb       632: }
                    633:
1.9       joris     634: void
                    635: cvs_client_updated(char *data)
                    636: {
1.57      xsa       637:        int fd;
1.9       joris     638:        time_t now;
1.51      otto      639:        mode_t fmode, mask;
1.9       joris     640:        size_t flen;
                    641:        CVSENTRIES *ent;
                    642:        struct cvs_ent *e;
                    643:        const char *errstr;
                    644:        struct timeval tv[2];
1.67      xsa       645:        char repo[MAXPATHLEN], entry[CVS_ENT_MAXLINELEN];
                    646:        char timebuf[CVS_TIME_BUFSZ], revbuf[CVS_REV_BUFSZ];
                    647:        char *en, *mode, *len, *fpath, *rpath, *wdir;
1.69      joris     648:        char sticky[CVS_ENT_MAXLINELEN];
1.9       joris     649:
1.62      ray       650:        if (data == NULL)
                    651:                fatal("Missing argument for Updated");
                    652:
1.10      joris     653:        client_check_directory(data);
1.9       joris     654:
                    655:        rpath = cvs_remote_input();
1.54      otto      656:        en = cvs_remote_input();
1.9       joris     657:        mode = cvs_remote_input();
                    658:        len = cvs_remote_input();
                    659:
                    660:        cvs_get_repository_path(".", repo, MAXPATHLEN);
1.48      joris     661:
                    662:        STRIP_SLASH(repo);
1.9       joris     663:
                    664:        if (strlen(repo) + 1 > strlen(rpath))
                    665:                fatal("received a repository path that is too short");
                    666:
                    667:        fpath = rpath + strlen(repo) + 1;
                    668:        if ((wdir = dirname(fpath)) == NULL)
                    669:                fatal("cvs_client_updated: dirname: %s", strerror(errno));
                    670:
                    671:        flen = strtonum(len, 0, INT_MAX, &errstr);
                    672:        if (errstr != NULL)
                    673:                fatal("cvs_client_updated: %s: %s", len, errstr);
                    674:        xfree(len);
                    675:
                    676:        cvs_strtomode(mode, &fmode);
                    677:        xfree(mode);
1.51      otto      678:        mask = umask(0);
                    679:        umask(mask);
                    680:        fmode &= ~mask;
1.9       joris     681:
                    682:        time(&now);
1.52      joris     683:        asctime_r(gmtime(&now), timebuf);
1.9       joris     684:        if (timebuf[strlen(timebuf) - 1] == '\n')
                    685:                timebuf[strlen(timebuf) - 1] = '\0';
                    686:
1.54      otto      687:        e = cvs_ent_parse(en);
                    688:        xfree(en);
1.69      joris     689:
                    690:        sticky[0] = '\0';
                    691:        if (e->ce_tag != NULL)
                    692:                (void)xsnprintf(sticky, sizeof(sticky), "T%s", e->ce_tag);
                    693:
1.9       joris     694:        rcsnum_tostr(e->ce_rev, revbuf, sizeof(revbuf));
1.69      joris     695:        (void)xsnprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s/%s/%s",
                    696:            e->ce_name, revbuf, timebuf,
                    697:            e->ce_opts ? e->ce_opts : "", sticky);
1.9       joris     698:
                    699:        cvs_ent_free(e);
                    700:        ent = cvs_ent_open(wdir);
                    701:        cvs_ent_add(ent, entry);
                    702:        cvs_ent_close(ent, ENT_SYNC);
                    703:
                    704:        if ((fd = open(fpath, O_CREAT | O_WRONLY | O_TRUNC)) == -1)
                    705:                fatal("cvs_client_updated: open: %s: %s",
                    706:                    fpath, strerror(errno));
1.1       jfb       707:
1.46      joris     708:        cvs_remote_receive_file(fd, flen);
1.1       jfb       709:
1.9       joris     710:        tv[0].tv_sec = now;
                    711:        tv[0].tv_usec = 0;
                    712:        tv[1] = tv[0];
1.1       jfb       713:
1.9       joris     714:        if (futimes(fd, tv) == -1)
                    715:                fatal("cvs_client_updated: futimes: %s", strerror(errno));
1.1       jfb       716:
1.9       joris     717:        if (fchmod(fd, fmode) == -1)
                    718:                fatal("cvs_client_updated: fchmod: %s", strerror(errno));
1.1       jfb       719:
1.9       joris     720:        (void)close(fd);
1.1       jfb       721:
1.9       joris     722:        xfree(rpath);
1.1       jfb       723: }
                    724:
1.9       joris     725: void
                    726: cvs_client_merged(char *data)
                    727: {
1.56      joris     728:        int fd;
                    729:        time_t now;
                    730:        mode_t fmode;
                    731:        size_t flen;
                    732:        CVSENTRIES *ent;
                    733:        const char *errstr;
                    734:        struct timeval tv[2];
1.67      xsa       735:        char timebuf[CVS_TIME_BUFSZ], *repo, *rpath, *entry, *mode;
1.56      joris     736:        char *len, *fpath, *wdir;
                    737:
1.62      ray       738:        if (data == NULL)
                    739:                fatal("Missing argument for Merged");
                    740:
1.56      joris     741:        client_check_directory(data);
                    742:
                    743:        rpath = cvs_remote_input();
                    744:        entry = cvs_remote_input();
                    745:        mode = cvs_remote_input();
                    746:        len = cvs_remote_input();
                    747:
                    748:        repo = xmalloc(MAXPATHLEN);
                    749:        cvs_get_repository_path(".", repo, MAXPATHLEN);
                    750:
                    751:        STRIP_SLASH(repo);
                    752:
                    753:        if (strlen(repo) + 1 > strlen(rpath))
                    754:                fatal("received a repository path that is too short");
                    755:
                    756:        fpath = rpath + strlen(repo) + 1;
                    757:        if ((wdir = dirname(fpath)) == NULL)
                    758:                fatal("cvs_client_merged: dirname: %s", strerror(errno));
                    759:        xfree(repo);
                    760:
                    761:        flen = strtonum(len, 0, INT_MAX, &errstr);
                    762:        if (errstr != NULL)
                    763:                fatal("cvs_client_merged: %s: %s", len, errstr);
                    764:        xfree(len);
                    765:
                    766:        cvs_strtomode(mode, &fmode);
                    767:        xfree(mode);
                    768:
                    769:        time(&now);
                    770:        asctime_r(gmtime(&now), timebuf);
                    771:        if (timebuf[strlen(timebuf) - 1] == '\n')
                    772:                timebuf[strlen(timebuf) - 1] = '\0';
                    773:
                    774:        ent = cvs_ent_open(wdir);
                    775:        cvs_ent_add(ent, entry);
                    776:        cvs_ent_close(ent, ENT_SYNC);
                    777:
                    778:        if ((fd = open(fpath, O_CREAT | O_WRONLY | O_TRUNC)) == -1)
                    779:                fatal("cvs_client_merged: open: %s: %s",
                    780:                    fpath, strerror(errno));
                    781:
                    782:        cvs_remote_receive_file(fd, flen);
                    783:
                    784:        tv[0].tv_sec = now;
                    785:        tv[0].tv_usec = 0;
                    786:        tv[1] = tv[0];
                    787:
                    788:        if (futimes(fd, tv) == -1)
                    789:                fatal("cvs_client_merged: futimes: %s", strerror(errno));
                    790:
                    791:        if (fchmod(fd, fmode) == -1)
                    792:                fatal("cvs_client_merged: fchmod: %s", strerror(errno));
                    793:
                    794:        (void)close(fd);
                    795:
                    796:        xfree(rpath);
1.9       joris     797: }
1.1       jfb       798:
1.9       joris     799: void
                    800: cvs_client_removed(char *data)
                    801: {
1.65      joris     802:        CVSENTRIES *entlist;
                    803:        char *rpath, *filename, fpath[MAXPATHLEN];
                    804:
                    805:        rpath = cvs_remote_input();
                    806:        if ((filename = strrchr(rpath, '/')) == NULL)
                    807:                fatal("bad rpath in cvs_client_removed: %s", rpath);
                    808:        filename++;
                    809:
                    810:        entlist = cvs_ent_open(data);
                    811:        cvs_ent_remove(entlist, filename);
                    812:        cvs_ent_close(entlist, ENT_SYNC);
                    813:
                    814:        (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s", data, filename);
                    815:        (void)unlink(fpath);
1.13      joris     816:
1.65      joris     817:        xfree(rpath);
1.9       joris     818: }
1.1       jfb       819:
1.9       joris     820: void
                    821: cvs_client_remove_entry(char *data)
1.1       jfb       822: {
1.38      xsa       823:        CVSENTRIES *entlist;
1.44      joris     824:        char *filename, *rpath;
1.41      joris     825:
1.62      ray       826:        if (data == NULL)
                    827:                fatal("Missing argument for Remove-entry");
                    828:
1.41      joris     829:        rpath = cvs_remote_input();
                    830:        if ((filename = strrchr(rpath, '/')) == NULL)
                    831:                fatal("bad rpath in cvs_client_remove_entry: %s", rpath);
1.61      ray       832:        filename++;
1.1       jfb       833:
1.38      xsa       834:        entlist = cvs_ent_open(data);
1.44      joris     835:        cvs_ent_remove(entlist, filename);
1.38      xsa       836:        cvs_ent_close(entlist, ENT_SYNC);
                    837:
1.44      joris     838:        xfree(rpath);
1.36      xsa       839: }
                    840:
                    841: void
                    842: cvs_client_set_static_directory(char *data)
                    843: {
                    844:        FILE *fp;
1.50      otto      845:        char *dir, fpath[MAXPATHLEN];
1.36      xsa       846:
                    847:        if (cvs_cmdop == CVS_OP_EXPORT)
                    848:                return;
                    849:
1.62      ray       850:        if (data == NULL)
                    851:                fatal("Missing argument for Set-static-directory");
                    852:
1.36      xsa       853:        STRIP_SLASH(data);
                    854:
                    855:        dir = cvs_remote_input();
                    856:        xfree(dir);
                    857:
1.58      xsa       858:        (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
                    859:            data, CVS_PATH_STATICENTRIES);
1.36      xsa       860:
                    861:        if ((fp = fopen(fpath, "w+")) == NULL) {
                    862:                cvs_log(LP_ERRNO, "%s", fpath);
1.50      otto      863:                return;
1.36      xsa       864:        }
                    865:        (void)fclose(fp);
1.16      xsa       866: }
1.35      xsa       867:
                    868: void
                    869: cvs_client_clear_static_directory(char *data)
                    870: {
1.50      otto      871:        char *dir, fpath[MAXPATHLEN];
1.35      xsa       872:
                    873:        if (cvs_cmdop == CVS_OP_EXPORT)
                    874:                return;
                    875:
1.62      ray       876:        if (data == NULL)
                    877:                fatal("Missing argument for Clear-static-directory");
                    878:
1.35      xsa       879:        STRIP_SLASH(data);
                    880:
                    881:        dir = cvs_remote_input();
                    882:        xfree(dir);
                    883:
1.58      xsa       884:        (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
                    885:            data, CVS_PATH_STATICENTRIES);
1.35      xsa       886:
                    887:        (void)cvs_unlink(fpath);
                    888: }
                    889:
                    890: void
                    891: cvs_client_set_sticky(char *data)
                    892: {
                    893:        FILE *fp;
1.50      otto      894:        char *dir, *tag, tagpath[MAXPATHLEN];
1.35      xsa       895:
1.47      xsa       896:        if (cvs_cmdop == CVS_OP_EXPORT)
                    897:                return;
                    898:
1.62      ray       899:        if (data == NULL)
                    900:                fatal("Missing argument for Set-sticky");
                    901:
1.35      xsa       902:        STRIP_SLASH(data);
                    903:
                    904:        dir = cvs_remote_input();
                    905:        xfree(dir);
                    906:        tag = cvs_remote_input();
                    907:
1.69      joris     908:        client_check_directory(data);
                    909:
1.58      xsa       910:        (void)xsnprintf(tagpath, MAXPATHLEN, "%s/%s", data, CVS_PATH_TAG);
1.35      xsa       911:
                    912:        if ((fp = fopen(tagpath, "w+")) == NULL) {
                    913:                cvs_log(LP_ERRNO, "%s", tagpath);
                    914:                goto out;
                    915:        }
                    916:
                    917:        (void)fprintf(fp, "%s\n", tag);
                    918:        (void)fclose(fp);
                    919: out:
                    920:        xfree(tag);
                    921: }
                    922:
                    923: void
                    924: cvs_client_clear_sticky(char *data)
                    925: {
1.50      otto      926:        char *dir, tagpath[MAXPATHLEN];
1.47      xsa       927:
                    928:        if (cvs_cmdop == CVS_OP_EXPORT)
                    929:                return;
1.62      ray       930:
                    931:        if (data == NULL)
                    932:                fatal("Missing argument for Clear-sticky");
1.35      xsa       933:
                    934:        STRIP_SLASH(data);
                    935:
                    936:        dir = cvs_remote_input();
                    937:        xfree(dir);
                    938:
1.69      joris     939:        client_check_directory(data);
                    940:
1.58      xsa       941:        (void)xsnprintf(tagpath, MAXPATHLEN, "%s/%s", data, CVS_PATH_TAG);
1.69      joris     942:        (void)unlink(tagpath);
1.35      xsa       943: }
                    944:
                    945:
1.16      xsa       946: /*
                    947:  * cvs_client_initlog()
                    948:  *
                    949:  * Initialize protocol logging if the CVS_CLIENT_LOG environment variable is
                    950:  * set.  In this case, the variable's value is used as a path to which the
                    951:  * appropriate suffix is added (".in" for client input and ".out" for server
                    952:  * output).
                    953:  */
                    954: static void
                    955: cvs_client_initlog(void)
                    956: {
                    957:        u_int i;
                    958:        char *env, *envdup, buf[MAXPATHLEN], fpath[MAXPATHLEN];
                    959:        char rpath[MAXPATHLEN], *s;
                    960:        struct stat st;
                    961:        time_t now;
                    962:        struct passwd *pwd;
                    963:
                    964:        /* avoid doing it more than once */
                    965:        if (cvs_client_logon)
                    966:                return;
                    967:
                    968:        if ((env = getenv("CVS_CLIENT_LOG")) == NULL)
                    969:                return;
                    970:
                    971:        envdup = xstrdup(env);
                    972:        if ((s = strchr(envdup, '%')) != NULL)
                    973:                *s = '\0';
                    974:
                    975:        if (strlcpy(buf, env, sizeof(buf)) >= sizeof(buf))
                    976:                fatal("cvs_client_initlog: truncation");
                    977:
                    978:        if (strlcpy(rpath, envdup, sizeof(rpath)) >= sizeof(rpath))
                    979:                fatal("cvs_client_initlog: truncation");
                    980:
                    981:        xfree(envdup);
                    982:
                    983:        s = buf;
                    984:        while ((s = strchr(s, '%')) != NULL) {
                    985:                s++;
                    986:                switch (*s) {
                    987:                case 'c':
                    988:                        if (strlcpy(fpath, cvs_command, sizeof(fpath)) >=
                    989:                            sizeof(fpath))
                    990:                                fatal("cvs_client_initlog: truncation");
                    991:                        break;
                    992:                case 'd':
                    993:                        time(&now);
                    994:                        if (strlcpy(fpath, ctime(&now), sizeof(fpath)) >=
                    995:                            sizeof(fpath))
                    996:                                fatal("cvs_client_initlog: truncation");
                    997:                        break;
                    998:                case 'p':
1.57      xsa       999:                        (void)xsnprintf(fpath, sizeof(fpath), "%d", getpid());
1.16      xsa      1000:                        break;
                   1001:                case 'u':
                   1002:                        if ((pwd = getpwuid(getuid())) != NULL) {
                   1003:                                if (strlcpy(fpath, pwd->pw_name,
                   1004:                                    sizeof(fpath)) >= sizeof(fpath))
                   1005:                                        fatal("cvs_client_initlog: truncation");
                   1006:                        } else {
                   1007:                                fpath[0] = '\0';
                   1008:                        }
                   1009:                        endpwent();
                   1010:                        break;
                   1011:                default:
                   1012:                        fpath[0] = '\0';
                   1013:                        break;
                   1014:                }
                   1015:
                   1016:                if (fpath[0] != '\0') {
                   1017:                        if (strlcat(rpath, "-", sizeof(rpath)) >= sizeof(rpath))
                   1018:                                fatal("cvs_client_initlog: truncation");
                   1019:
                   1020:                        if (strlcat(rpath, fpath, sizeof(rpath))
                   1021:                            >= sizeof(rpath))
                   1022:                                fatal("cvs_client_initlog: truncation");
                   1023:                }
                   1024:        }
                   1025:
                   1026:        for (i = 0; i < UINT_MAX; i++) {
1.57      xsa      1027:                (void)xsnprintf(fpath, sizeof(fpath), "%s-%d.in", rpath, i);
1.16      xsa      1028:
                   1029:                if (stat(fpath, &st) != -1)
                   1030:                        continue;
                   1031:
                   1032:                if (errno != ENOENT)
                   1033:                        fatal("cvs_client_initlog() stat failed '%s'",
                   1034:                            strerror(errno));
                   1035:
                   1036:                break;
                   1037:        }
                   1038:
                   1039:        if ((cvs_client_inlog_fd = open(fpath,
1.60      xsa      1040:            O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1) {
1.16      xsa      1041:                fatal("cvs_client_initlog: open `%s': %s",
                   1042:                    fpath, strerror(errno));
                   1043:        }
                   1044:
                   1045:        for (i = 0; i < UINT_MAX; i++) {
1.57      xsa      1046:                (void)xsnprintf(fpath, sizeof(fpath), "%s-%d.out", rpath, i);
1.16      xsa      1047:
                   1048:                if (stat(fpath, &st) != -1)
                   1049:                        continue;
                   1050:
                   1051:                if (errno != ENOENT)
                   1052:                        fatal("cvs_client_initlog() stat failed '%s'",
                   1053:                            strerror(errno));
                   1054:
                   1055:                break;
                   1056:        }
                   1057:
                   1058:        if ((cvs_client_outlog_fd = open(fpath,
1.60      xsa      1059:            O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1) {
1.16      xsa      1060:                fatal("cvs_client_initlog: open `%s': %s",
                   1061:                    fpath, strerror(errno));
                   1062:        }
                   1063:
                   1064:        cvs_client_logon = 1;
1.1       jfb      1065: }