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

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