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

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