[BACK]Return to proto.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cvs

Annotation of src/usr.bin/cvs/proto.c, Revision 1.82

1.82    ! joris       1: /*     $OpenBSD: proto.c,v 1.81 2005/12/20 18:17:01 xsa Exp $  */
1.1       jfb         2: /*
                      3:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
                      4:  * All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  *
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. The name of the author may not be used to endorse or promote products
                     13:  *    derived from this software without specific prior written permission.
                     14:  *
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     16:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     17:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
                     18:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     19:  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     20:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     21:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     22:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     23:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
                     24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     25:  */
                     26: /*
                     27:  * CVS client/server protocol
                     28:  * ==========================
                     29:  *
                     30:  * The following code implements the CVS client/server protocol, which is
                     31:  * documented at the following URL:
1.72      joris      32:  *     http://www.iam.unibe.ch/~til/documentation/cvs/cvsclient_toc.html
                     33:  *     http://www.elegosoft.com/cvs/cvsclient_toc.html
1.1       jfb        34:  *
                     35:  * The protocol is split up into two parts; the first part is the client side
                     36:  * of things and is composed of all the response handlers, which are all named
                     37:  * with a prefix of "cvs_resp_".  The second part is the set of request
                     38:  * handlers used by the server.  These handlers process the request and
                     39:  * generate the appropriate response to send back.  The prefix for request
                     40:  * handlers is "cvs_req_".
                     41:  *
                     42:  */
                     43:
                     44: #include <sys/types.h>
                     45: #include <sys/stat.h>
                     46:
1.56      xsa        47: #include <errno.h>
1.1       jfb        48: #include <fcntl.h>
1.56      xsa        49: #include <libgen.h>
                     50: #include <pwd.h>
1.1       jfb        51: #include <stdio.h>
                     52: #include <stdlib.h>
1.56      xsa        53: #include <string.h>
1.1       jfb        54: #include <unistd.h>
                     55:
                     56: #include "cvs.h"
                     57: #include "log.h"
1.13      jfb        58: #include "proto.h"
1.1       jfb        59:
                     60:
1.11      jfb        61: /* request flags */
1.68      xsa        62: #define CVS_REQF_RESP  0x01
1.11      jfb        63:
                     64:
1.68      xsa        65: static int     cvs_initlog(void);
1.1       jfb        66:
1.18      jfb        67: struct cvs_req cvs_requests[] = {
                     68:        { CVS_REQ_DIRECTORY,     "Directory",         0             },
                     69:        { CVS_REQ_MAXDOTDOT,     "Max-dotdot",        0             },
                     70:        { CVS_REQ_STATICDIR,     "Static-directory",  0             },
                     71:        { CVS_REQ_STICKY,        "Sticky",            0             },
                     72:        { CVS_REQ_ENTRY,         "Entry",             0             },
                     73:        { CVS_REQ_ENTRYEXTRA,    "EntryExtra",        0             },
                     74:        { CVS_REQ_CHECKINTIME,   "Checkin-time",      0             },
                     75:        { CVS_REQ_MODIFIED,      "Modified",          0             },
                     76:        { CVS_REQ_ISMODIFIED,    "Is-modified",       0             },
                     77:        { CVS_REQ_UNCHANGED,     "Unchanged",         0             },
                     78:        { CVS_REQ_USEUNCHANGED,  "UseUnchanged",      0             },
                     79:        { CVS_REQ_NOTIFY,        "Notify",            0             },
                     80:        { CVS_REQ_NOTIFYUSER,    "NotifyUser",        0             },
                     81:        { CVS_REQ_QUESTIONABLE,  "Questionable",      0             },
                     82:        { CVS_REQ_CASE,          "Case",              0             },
                     83:        { CVS_REQ_UTF8,          "Utf8",              0             },
                     84:        { CVS_REQ_ARGUMENT,      "Argument",          0             },
                     85:        { CVS_REQ_ARGUMENTX,     "Argumentx",         0             },
                     86:        { CVS_REQ_GLOBALOPT,     "Global_option",     0             },
                     87:        { CVS_REQ_GZIPSTREAM,    "Gzip-stream",       0             },
                     88:        { CVS_REQ_READCVSRC2,    "read-cvsrc2",       0             },
                     89:        { CVS_REQ_READWRAP,      "read-cvswrappers",  0             },
                     90:        { CVS_REQ_READIGNORE,    "read-cvsignore",    0             },
                     91:        { CVS_REQ_ERRIFREADER,   "Error-If-Reader",   0             },
                     92:        { CVS_REQ_VALIDRCSOPT,   "Valid-RcsOptions",  0             },
                     93:        { CVS_REQ_SET,           "Set",               0             },
                     94:        { CVS_REQ_XPANDMOD,      "expand-modules",    CVS_REQF_RESP },
1.26      jfb        95:        { CVS_REQ_LOG,           "log",               CVS_REQF_RESP },
1.18      jfb        96:        { CVS_REQ_CO,            "co",                CVS_REQF_RESP },
                     97:        { CVS_REQ_EXPORT,        "export",            CVS_REQF_RESP },
1.26      jfb        98:        { CVS_REQ_ANNOTATE,      "annotate",          CVS_REQF_RESP },
                     99:        { CVS_REQ_RDIFF,         "rdiff",             CVS_REQF_RESP },
1.18      jfb       100:        { CVS_REQ_RTAG,          "rtag",              CVS_REQF_RESP },
                    101:        { CVS_REQ_INIT,          "init",              CVS_REQF_RESP },
                    102:        { CVS_REQ_STATUS,        "status",            CVS_REQF_RESP },
                    103:        { CVS_REQ_UPDATE,        "update",            CVS_REQF_RESP },
1.26      jfb       104:        { CVS_REQ_HISTORY,       "history",           CVS_REQF_RESP },
1.18      jfb       105:        { CVS_REQ_IMPORT,        "import",            CVS_REQF_RESP },
                    106:        { CVS_REQ_ADD,           "add",               CVS_REQF_RESP },
                    107:        { CVS_REQ_REMOVE,        "remove",            CVS_REQF_RESP },
                    108:        { CVS_REQ_RELEASE,       "release",           CVS_REQF_RESP },
                    109:        { CVS_REQ_ROOT,          "Root",              0             },
                    110:        { CVS_REQ_VALIDRESP,     "Valid-responses",   0             },
                    111:        { CVS_REQ_VALIDREQ,      "valid-requests",    CVS_REQF_RESP },
                    112:        { CVS_REQ_VERSION,       "version",           CVS_REQF_RESP },
                    113:        { CVS_REQ_NOOP,          "noop",              CVS_REQF_RESP },
                    114:        { CVS_REQ_DIFF,          "diff",              CVS_REQF_RESP },
1.29      jfb       115:        { CVS_REQ_CI,            "ci",                CVS_REQF_RESP },
1.35      jfb       116:        { CVS_REQ_TAG,           "tag",               CVS_REQF_RESP },
1.41      joris     117:        { CVS_REQ_ADMIN,         "admin",             CVS_REQF_RESP },
1.76      xsa       118:        { CVS_REQ_WATCHERS,      "watchers",          CVS_REQF_RESP },
1.79      moritz    119:        { CVS_REQ_WATCH_ON,      "watch-on",          CVS_REQF_RESP },
                    120:        { CVS_REQ_WATCH_OFF,     "watch-off",         CVS_REQF_RESP },
                    121:        { CVS_REQ_WATCH_ADD,     "watch-add",         CVS_REQF_RESP },
                    122:        { CVS_REQ_WATCH_REMOVE,  "watch-remove",      CVS_REQF_RESP },
1.78      xsa       123:        { CVS_REQ_EDITORS,       "editors",           CVS_REQF_RESP },
1.1       jfb       124: };
                    125:
1.18      jfb       126: struct cvs_resp cvs_responses[] = {
                    127:        { CVS_RESP_OK,         "ok"                     },
                    128:        { CVS_RESP_ERROR,      "error"                  },
                    129:        { CVS_RESP_VALIDREQ,   "Valid-requests"         },
                    130:        { CVS_RESP_M,          "M"                      },
                    131:        { CVS_RESP_MBINARY,    "Mbinary"                },
                    132:        { CVS_RESP_MT,         "MT"                     },
                    133:        { CVS_RESP_E,          "E"                      },
                    134:        { CVS_RESP_F,          "F"                      },
                    135:        { CVS_RESP_CREATED,    "Created"                },
                    136:        { CVS_RESP_UPDATED,    "Updated"                },
                    137:        { CVS_RESP_UPDEXIST,   "Update-existing"        },
                    138:        { CVS_RESP_MERGED,     "Merged"                 },
                    139:        { CVS_RESP_REMOVED,    "Removed"                },
1.63      joris     140:        { CVS_RESP_RMENTRY,    "Remove-entry"           },
1.18      jfb       141:        { CVS_RESP_CKSUM,      "Checksum"               },
                    142:        { CVS_RESP_CLRSTATDIR, "Clear-static-directory" },
                    143:        { CVS_RESP_SETSTATDIR, "Set-static-directory"   },
                    144:        { CVS_RESP_NEWENTRY,   "New-entry"              },
                    145:        { CVS_RESP_CHECKEDIN,  "Checked-in"             },
                    146:        { CVS_RESP_MODE,       "Mode"                   },
                    147:        { CVS_RESP_MODTIME,    "Mod-time"               },
                    148:        { CVS_RESP_MODXPAND,   "Module-expansion"       },
                    149:        { CVS_RESP_SETSTICKY,  "Set-sticky"             },
                    150:        { CVS_RESP_CLRSTICKY,  "Clear-sticky"           },
                    151:        { CVS_RESP_RCSDIFF,    "Rcs-diff"               },
                    152:        { CVS_RESP_TEMPLATE,   "Template"               },
1.34      jfb       153:        { CVS_RESP_COPYFILE,   "Copy-file"              },
1.1       jfb       154: };
                    155:
1.68      xsa       156: #define CVS_NBREQ      (sizeof(cvs_requests)/sizeof(cvs_requests[0]))
                    157: #define CVS_NBRESP     (sizeof(cvs_responses)/sizeof(cvs_responses[0]))
1.17      jfb       158:
                    159: /* hack to receive the remote version without outputting it */
1.18      jfb       160: u_int cvs_version_sent = 0;
1.17      jfb       161:
1.13      jfb       162: static char  cvs_proto_buf[4096];
                    163:
                    164: /*
1.37      david     165:  * Output files for protocol logging when the CVS_CLIENT_LOG environment
1.13      jfb       166:  * variable is set.
                    167:  */
                    168: static int   cvs_server_logon = 0;
                    169: static FILE *cvs_server_inlog = NULL;
                    170: static FILE *cvs_server_outlog = NULL;
                    171:
1.28      jfb       172: static pid_t cvs_subproc_pid;
                    173:
1.32      jfb       174: /* last directory sent with cvs_senddir() */
                    175: static char cvs_lastdir[MAXPATHLEN] = "";
                    176:
1.13      jfb       177:
                    178: /*
                    179:  * cvs_connect()
                    180:  *
                    181:  * Open a client connection to the cvs server whose address is given in
                    182:  * the <root> variable.  The method used to connect depends on the
                    183:  * setting of the CVS_RSH variable.
1.17      jfb       184:  * Once the connection has been established, we first send the list of
                    185:  * responses we support and request the list of supported requests from the
                    186:  * server.  Then, a version request is sent and various global flags are sent.
1.13      jfb       187:  */
1.82    ! joris     188: void
1.13      jfb       189: cvs_connect(struct cvsroot *root)
                    190: {
1.82    ! joris     191:        size_t len;
1.20      jfb       192:        int argc, infd[2], outfd[2], errfd[2];
1.71      xsa       193:        char *argv[16], *cvs_server_cmd, tmsg[1024], *vresp;
1.13      jfb       194:
1.82    ! joris     195:        if (root->cr_method == CVS_METHOD_PSERVER)
        !           196:                fatal("no pserver support due to security issues");
        !           197:        else if ((root->cr_method == CVS_METHOD_KSERVER) ||
1.36      jfb       198:            (root->cr_method == CVS_METHOD_GSERVER) ||
                    199:            (root->cr_method == CVS_METHOD_EXT) ||
1.82    ! joris     200:            (root->cr_method == CVS_METHOD_FORK))
        !           201:                fatal("connection method not supported yet");
1.33      jfb       202:
                    203:        if (root->cr_flags & CVS_ROOT_CONNECTED) {
                    204:                cvs_log(LP_NOTICE, "already connected to CVSROOT");
1.82    ! joris     205:                return;
1.33      jfb       206:        }
                    207:
1.82    ! joris     208:        if (pipe(infd) == -1)
        !           209:                fatal("failed to create input pipe for client connection");
1.13      jfb       210:
1.82    ! joris     211:        if (pipe(outfd) == -1)
        !           212:                fatal("failed to create output pipe for client connection");
1.13      jfb       213:
1.82    ! joris     214:        if (pipe(errfd) == -1)
        !           215:                fatal("failed to create error pipe for client connection");
1.20      jfb       216:
1.28      jfb       217:        cvs_subproc_pid = fork();
                    218:        if (cvs_subproc_pid == -1) {
1.82    ! joris     219:                fatal("failed to fork for cvs server connection");
1.30      deraadt   220:        } else if (cvs_subproc_pid == 0) {
1.13      jfb       221:                if ((dup2(infd[0], STDIN_FILENO) == -1) ||
1.82    ! joris     222:                    (dup2(outfd[1], STDOUT_FILENO) == -1))
        !           223:                        fatal("failed to setup standard streams for cvs server");
        !           224:
1.13      jfb       225:                (void)close(infd[1]);
                    226:                (void)close(outfd[0]);
1.20      jfb       227:                (void)close(errfd[0]);
1.13      jfb       228:
                    229:                argc = 0;
                    230:                argv[argc++] = cvs_rsh;
                    231:
                    232:                if (root->cr_user != NULL) {
                    233:                        argv[argc++] = "-l";
                    234:                        argv[argc++] = root->cr_user;
                    235:                }
                    236:
                    237:                cvs_server_cmd = getenv("CVS_SERVER");
                    238:                if (cvs_server_cmd == NULL)
1.20      jfb       239:                        cvs_server_cmd = CVS_SERVER_DEFAULT;
1.13      jfb       240:
                    241:                argv[argc++] = root->cr_host;
                    242:                argv[argc++] = cvs_server_cmd;
                    243:                argv[argc++] = "server";
                    244:                argv[argc] = NULL;
1.71      xsa       245:
                    246:                if (cvs_trace == 1) {
                    247:                        tmsg[0] = '\0';
                    248:                        for (argc = 0; argv[argc] != NULL; argc++) {
1.82    ! joris     249:                                len = strlcat(tmsg, argv[argc], sizeof(tmsg));
        !           250:                                if (len >= sizeof(tmsg))
        !           251:                                        fatal("truncation in cvs_connect");
        !           252:
        !           253:                                len = strlcat(tmsg, " ", sizeof(tmsg));
        !           254:                                if (len >= sizeof(tmsg))
        !           255:                                        fatal("truncation in cvs_connect");
1.71      xsa       256:                        }
                    257:                }
1.82    ! joris     258:
1.71      xsa       259:                cvs_log(LP_TRACE, "Starting server: %s", tmsg);
1.13      jfb       260:
                    261:                execvp(argv[0], argv);
1.82    ! joris     262:                fatal("failed to execute cvs server");
1.13      jfb       263:        }
                    264:
                    265:        /* we are the parent */
                    266:        (void)close(infd[0]);
                    267:        (void)close(outfd[1]);
1.20      jfb       268:        (void)close(errfd[1]);
1.13      jfb       269:
                    270:        root->cr_srvin = fdopen(infd[1], "w");
1.82    ! joris     271:        if (root->cr_srvin == NULL)
        !           272:                fatal("failed to create pipe stream");
1.13      jfb       273:
                    274:        root->cr_srvout = fdopen(outfd[0], "r");
1.82    ! joris     275:        if (root->cr_srvout == NULL)
        !           276:                fatal("failed to create pipe stream");
1.20      jfb       277:
1.13      jfb       278:        /* make the streams line-buffered */
1.75      xsa       279:        (void)setvbuf(root->cr_srvin, NULL, _IOLBF, (size_t)0);
                    280:        (void)setvbuf(root->cr_srvout, NULL, _IOLBF, (size_t)0);
1.13      jfb       281:
                    282:        cvs_initlog();
                    283:
                    284:        /*
                    285:         * Send the server the list of valid responses, then ask for valid
                    286:         * requests.
                    287:         */
                    288:
1.82    ! joris     289:        vresp = cvs_resp_getvalid();
        !           290:        cvs_sendreq(root, CVS_REQ_VALIDRESP, vresp);
1.80      joris     291:        xfree(vresp);
1.13      jfb       292:
1.82    ! joris     293:        cvs_sendreq(root, CVS_REQ_VALIDREQ, NULL);
1.13      jfb       294:
1.62      xsa       295:        /* send the CVSROOT to the server */
1.82    ! joris     296:        cvs_sendreq(root, CVS_REQ_ROOT, root->cr_dir);
1.54      joris     297:
1.40      jfb       298:        /* don't fail if this request doesn't work */
1.82    ! joris     299:        cvs_sendreq(root, CVS_REQ_VERSION, NULL);
1.17      jfb       300:
1.13      jfb       301:        /* now share our global options with the server */
1.82    ! joris     302:        if (verbosity <= 1)
        !           303:                cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-q");
        !           304:        if (verbosity == 0)
        !           305:                cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-Q");
        !           306:
        !           307:        if (cvs_noexec == 1)
        !           308:                cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-n");
        !           309:
        !           310:        if (cvs_nolog == 1)
        !           311:                cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-l");
        !           312:
        !           313:        if (cvs_readonly == 1)
        !           314:                cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-r");
1.40      jfb       315:
1.82    ! joris     316:        if (cvs_trace == 1)
        !           317:                cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-t");
1.13      jfb       318:
                    319:        /* not sure why, but we have to send this */
1.82    ! joris     320:        cvs_sendreq(root, CVS_REQ_USEUNCHANGED, NULL);
1.13      jfb       321:
                    322:        cvs_log(LP_DEBUG, "connected to %s", root->cr_host);
                    323:
1.33      jfb       324:        root->cr_flags |= CVS_ROOT_CONNECTED;
1.13      jfb       325: }
                    326:
                    327:
                    328: /*
                    329:  * cvs_disconnect()
                    330:  *
                    331:  * Disconnect from the cvs server.
                    332:  */
                    333: void
                    334: cvs_disconnect(struct cvsroot *root)
                    335: {
1.33      jfb       336:        if (!(root->cr_flags & CVS_ROOT_CONNECTED))
                    337:                return;
                    338:
1.13      jfb       339:        cvs_log(LP_DEBUG, "closing connection to %s", root->cr_host);
1.82    ! joris     340:
1.13      jfb       341:        if (root->cr_srvin != NULL) {
                    342:                (void)fclose(root->cr_srvin);
                    343:                root->cr_srvin = NULL;
                    344:        }
                    345:        if (root->cr_srvout != NULL) {
                    346:                (void)fclose(root->cr_srvout);
                    347:                root->cr_srvout = NULL;
                    348:        }
1.33      jfb       349:
                    350:        root->cr_flags &= ~CVS_ROOT_CONNECTED;
1.13      jfb       351: }
                    352:
1.1       jfb       353:
                    354: /*
                    355:  * cvs_req_getbyid()
                    356:  *
                    357:  */
1.13      jfb       358: struct cvs_req*
1.1       jfb       359: cvs_req_getbyid(int reqid)
                    360: {
                    361:        u_int i;
                    362:
                    363:        for (i = 0; i < CVS_NBREQ; i++)
                    364:                if (cvs_requests[i].req_id == reqid)
1.13      jfb       365:                        return &(cvs_requests[i]);
1.31      tedu      366:
1.1       jfb       367:        return (NULL);
                    368: }
                    369:
                    370:
                    371: /*
                    372:  * cvs_req_getbyname()
                    373:  */
1.13      jfb       374: struct cvs_req*
1.1       jfb       375: cvs_req_getbyname(const char *rname)
                    376: {
                    377:        u_int i;
                    378:
                    379:        for (i = 0; i < CVS_NBREQ; i++)
                    380:                if (strcmp(cvs_requests[i].req_str, rname) == 0)
1.13      jfb       381:                        return &(cvs_requests[i]);
1.1       jfb       382:
1.13      jfb       383:        return (NULL);
1.1       jfb       384: }
                    385:
                    386:
                    387: /*
                    388:  * cvs_req_getvalid()
                    389:  *
                    390:  * Build a space-separated list of all the requests that this protocol
                    391:  * implementation supports.
                    392:  */
1.67      xsa       393: char *
1.1       jfb       394: cvs_req_getvalid(void)
                    395: {
                    396:        u_int i;
                    397:        size_t len;
                    398:        char *vrstr;
                    399:        BUF *buf;
                    400:
1.75      xsa       401:        buf = cvs_buf_alloc((size_t)512, BUF_AUTOEXT);
1.1       jfb       402:
                    403:        cvs_buf_set(buf, cvs_requests[0].req_str,
1.75      xsa       404:            strlen(cvs_requests[0].req_str), (size_t)0);
1.1       jfb       405:
                    406:        for (i = 1; i < CVS_NBREQ; i++) {
1.81      xsa       407:                cvs_buf_putc(buf, ' ');
                    408:                cvs_buf_append(buf, cvs_requests[i].req_str,
                    409:                    strlen(cvs_requests[i].req_str));
1.1       jfb       410:        }
                    411:
                    412:        /* NUL-terminate */
1.81      xsa       413:        cvs_buf_putc(buf, '\0');
1.1       jfb       414:
1.70      moritz    415:        len = cvs_buf_len(buf);
1.80      joris     416:        vrstr = (char *)xmalloc(len);
1.75      xsa       417:        cvs_buf_copy(buf, (size_t)0, vrstr, len);
1.1       jfb       418:        cvs_buf_free(buf);
                    419:
                    420:        return (vrstr);
                    421: }
                    422:
                    423:
                    424: /*
                    425:  * cvs_resp_getbyid()
                    426:  *
                    427:  */
1.13      jfb       428: struct cvs_resp*
1.1       jfb       429: cvs_resp_getbyid(int respid)
                    430: {
                    431:        u_int i;
                    432:
1.44      tedu      433:        for (i = 0; i < CVS_NBRESP; i++)
1.13      jfb       434:                if (cvs_responses[i].resp_id == (u_int)respid)
                    435:                        return &(cvs_responses[i]);
1.31      tedu      436:
1.1       jfb       437:        return (NULL);
                    438: }
                    439:
                    440:
                    441: /*
                    442:  * cvs_resp_getbyname()
                    443:  */
1.67      xsa       444: struct cvs_resp *
1.1       jfb       445: cvs_resp_getbyname(const char *rname)
                    446: {
                    447:        u_int i;
                    448:
1.44      tedu      449:        for (i = 0; i < CVS_NBRESP; i++)
1.1       jfb       450:                if (strcmp(cvs_responses[i].resp_str, rname) == 0)
1.13      jfb       451:                        return &(cvs_responses[i]);
1.1       jfb       452:
1.13      jfb       453:        return (NULL);
1.1       jfb       454: }
                    455:
                    456:
                    457: /*
                    458:  * cvs_resp_getvalid()
                    459:  *
                    460:  * Build a space-separated list of all the responses that this protocol
                    461:  * implementation supports.
                    462:  */
1.67      xsa       463: char *
1.1       jfb       464: cvs_resp_getvalid(void)
                    465: {
                    466:        u_int i;
                    467:        size_t len;
                    468:        char *vrstr;
                    469:        BUF *buf;
                    470:
1.75      xsa       471:        buf = cvs_buf_alloc((size_t)512, BUF_AUTOEXT);
1.1       jfb       472:
                    473:        cvs_buf_set(buf, cvs_responses[0].resp_str,
1.75      xsa       474:            strlen(cvs_responses[0].resp_str), (size_t)0);
1.1       jfb       475:
                    476:        for (i = 1; i < CVS_NBRESP; i++) {
1.81      xsa       477:                cvs_buf_putc(buf, ' ');
                    478:                cvs_buf_append(buf, cvs_responses[i].resp_str,
                    479:                    strlen(cvs_responses[i].resp_str));
1.1       jfb       480:        }
                    481:
                    482:        /* NUL-terminate */
1.81      xsa       483:        cvs_buf_putc(buf, '\0');
1.1       jfb       484:
1.70      moritz    485:        len = cvs_buf_len(buf);
1.80      joris     486:        vrstr = (char *)xmalloc(len);
1.75      xsa       487:        cvs_buf_copy(buf, (size_t)0, vrstr, len);
1.1       jfb       488:        cvs_buf_free(buf);
                    489:
                    490:        return (vrstr);
                    491: }
                    492:
                    493:
                    494: /*
                    495:  * cvs_sendfile()
                    496:  *
                    497:  * Send the mode and size of a file followed by the file's contents.
                    498:  * Returns 0 on success, or -1 on failure.
                    499:  */
1.82    ! joris     500: void
1.13      jfb       501: cvs_sendfile(struct cvsroot *root, const char *path)
1.1       jfb       502: {
1.51      xsa       503:        int fd, l;
1.1       jfb       504:        ssize_t ret;
                    505:        char buf[4096];
                    506:        struct stat st;
                    507:
1.38      jfb       508:        cvs_log(LP_TRACE, "Sending file `%s' to server", basename(path));
                    509:
1.1       jfb       510:        if (stat(path, &st) == -1) {
1.82    ! joris     511:                fatal("cvs_sendfile(): stat failed on '%s': %s",
        !           512:                    path, strerror(errno));
1.1       jfb       513:        }
                    514:
1.82    ! joris     515:        cvs_modetostr(st.st_mode, buf, sizeof(buf));
1.24      jfb       516:
1.1       jfb       517:        fd = open(path, O_RDONLY, 0);
                    518:        if (fd == -1) {
1.82    ! joris     519:                fatal("cvs_sendfile(): failed to open '%s': %s",
        !           520:                    path, strerror(errno));
1.1       jfb       521:        }
                    522:
1.82    ! joris     523:        cvs_sendln(root, buf);
        !           524:
1.51      xsa       525:        l = snprintf(buf, sizeof(buf), "%lld\n", st.st_size);
                    526:        if (l == -1 || l >= (int)sizeof(buf))
1.82    ! joris     527:                fatal("overflow in cvs_sendfile");
1.51      xsa       528:
1.82    ! joris     529:        cvs_sendln(root, buf);
1.1       jfb       530:
                    531:        while ((ret = read(fd, buf, sizeof(buf))) != 0) {
                    532:                if (ret == -1) {
1.24      jfb       533:                        (void)close(fd);
1.82    ! joris     534:                        fatal("cvs_sendfile: read error on '%s'", path);
1.1       jfb       535:                }
                    536:
1.82    ! joris     537:                cvs_sendraw(root, buf, (size_t)ret);
1.1       jfb       538:        }
                    539:
                    540:        (void)close(fd);
                    541: }
                    542:
                    543:
                    544: /*
                    545:  * cvs_recvfile()
                    546:  *
                    547:  * Receive the mode and size of a file followed the file's contents and
                    548:  * create or update the file whose path is <path> with the received
                    549:  * information.
                    550:  */
1.14      jfb       551: BUF*
                    552: cvs_recvfile(struct cvsroot *root, mode_t *mode)
1.1       jfb       553: {
                    554:        size_t len;
                    555:        ssize_t ret;
                    556:        off_t fsz, cnt;
                    557:        char buf[4096], *ep;
1.14      jfb       558:        BUF *fbuf;
                    559:
                    560:        fbuf = cvs_buf_alloc(sizeof(buf), BUF_AUTOEXT);
1.1       jfb       561:
1.13      jfb       562:        if ((cvs_getln(root, buf, sizeof(buf)) < 0) ||
1.14      jfb       563:            (cvs_strtomode(buf, mode) < 0)) {
1.44      tedu      564:                cvs_buf_free(fbuf);
1.14      jfb       565:                return (NULL);
1.1       jfb       566:        }
                    567:
1.50      joris     568:        if (cvs_getln(root, buf, sizeof(buf)) < 0) {
                    569:                cvs_buf_free(fbuf);
                    570:                return (NULL);
                    571:        }
1.1       jfb       572:
                    573:        fsz = (off_t)strtol(buf, &ep, 10);
                    574:        if (*ep != '\0') {
                    575:                cvs_log(LP_ERR, "parse error in file size transmission");
1.44      tedu      576:                cvs_buf_free(fbuf);
1.14      jfb       577:                return (NULL);
1.1       jfb       578:        }
                    579:
                    580:        cnt = 0;
                    581:        do {
                    582:                len = MIN(sizeof(buf), (size_t)(fsz - cnt));
1.6       jfb       583:                if (len == 0)
                    584:                        break;
1.13      jfb       585:                ret = cvs_recvraw(root, buf, len);
1.1       jfb       586:                if (ret == -1) {
1.14      jfb       587:                        cvs_buf_free(fbuf);
                    588:                        return (NULL);
1.1       jfb       589:                }
                    590:
1.14      jfb       591:                if (cvs_buf_append(fbuf, buf, (size_t)ret) == -1) {
                    592:                        cvs_log(LP_ERR,
                    593:                            "failed to append received file data");
                    594:                        cvs_buf_free(fbuf);
                    595:                        return (NULL);
1.1       jfb       596:                }
                    597:
                    598:                cnt += (off_t)ret;
                    599:        } while (cnt < fsz);
                    600:
1.14      jfb       601:        return (fbuf);
1.1       jfb       602: }
1.12      jfb       603:
                    604: /*
                    605:  * cvs_sendreq()
                    606:  *
                    607:  * Send a request to the server of type <rid>, with optional arguments
                    608:  * contained in <arg>, which should not be terminated by a newline.
                    609:  * Returns 0 on success, or -1 on failure.
                    610:  */
1.82    ! joris     611: void
1.13      jfb       612: cvs_sendreq(struct cvsroot *root, u_int rid, const char *arg)
1.12      jfb       613: {
1.51      xsa       614:        int ret, l;
1.13      jfb       615:        struct cvs_req *req;
1.12      jfb       616:
1.82    ! joris     617:        if (root->cr_srvin == NULL)
        !           618:                fatal("cannot send request %u: Not connected", rid);
1.12      jfb       619:
1.13      jfb       620:        req = cvs_req_getbyid(rid);
1.82    ! joris     621:        if (req == NULL)
        !           622:                fatal("unsupported request type %u", rid);
1.12      jfb       623:
1.18      jfb       624:        /* is this request supported by the server? */
                    625:        if (!CVS_GETVR(root, req->req_id)) {
1.57      joris     626:                if (rid == CVS_REQ_VERSION) {
1.82    ! joris     627:                        cvs_sendreq(root, CVS_REQ_NOOP, arg);
1.57      joris     628:                } else {
                    629:                        cvs_log(LP_WARN,
                    630:                            "remote end does not support request `%s'",
                    631:                            req->req_str);
                    632:                }
1.82    ! joris     633:
        !           634:                return;
1.18      jfb       635:        }
                    636:
1.51      xsa       637:        l = snprintf(cvs_proto_buf, sizeof(cvs_proto_buf), "%s%s%s\n",
1.13      jfb       638:            req->req_str, (arg == NULL) ? "" : " ", (arg == NULL) ? "" : arg);
1.51      xsa       639:        if (l == -1 || l >= (int)sizeof(cvs_proto_buf))
1.82    ! joris     640:                fatal("overflow in cvs_sendreq");
1.12      jfb       641:
                    642:        if (cvs_server_inlog != NULL)
1.13      jfb       643:                fputs(cvs_proto_buf, cvs_server_inlog);
1.12      jfb       644:
1.13      jfb       645:        ret = fputs(cvs_proto_buf, root->cr_srvin);
1.82    ! joris     646:        if (ret == EOF)
        !           647:                fatal("failed to send request to server");
1.17      jfb       648:
                    649:        if (rid == CVS_REQ_VERSION)
                    650:                cvs_version_sent = 1;
1.12      jfb       651:
1.13      jfb       652:        if (req->req_flags & CVS_REQF_RESP)
1.82    ! joris     653:                cvs_getresp(root);
1.12      jfb       654: }
                    655:
                    656:
                    657: /*
                    658:  * cvs_getresp()
                    659:  *
                    660:  * Get a response from the server.  This call will actually read and handle
                    661:  * responses from the server until one of the response handlers returns
1.37      david     662:  * non-zero (either an error occurred or the end of the response was reached).
1.12      jfb       663:  * Returns the number of handled commands on success, or -1 on failure.
                    664:  */
1.82    ! joris     665: void
1.13      jfb       666: cvs_getresp(struct cvsroot *root)
1.12      jfb       667: {
1.82    ! joris     668:        int ret;
1.13      jfb       669:        size_t len;
1.12      jfb       670:
                    671:        do {
                    672:                /* wait for incoming data */
1.74      xsa       673:                if (fgets(cvs_proto_buf, (int)sizeof(cvs_proto_buf),
1.12      jfb       674:                    root->cr_srvout) == NULL) {
                    675:                        if (feof(root->cr_srvout))
1.82    ! joris     676:                                return;
        !           677:                        fatal("failed to read response from server");
1.12      jfb       678:                }
                    679:
                    680:                if (cvs_server_outlog != NULL)
1.13      jfb       681:                        fputs(cvs_proto_buf, cvs_server_outlog);
1.12      jfb       682:
1.13      jfb       683:                if ((len = strlen(cvs_proto_buf)) != 0) {
1.42      joris     684:                        /* if len - 1 != '\n' the line is truncated */
1.43      joris     685:                        if (cvs_proto_buf[len - 1] == '\n')
1.13      jfb       686:                                cvs_proto_buf[--len] = '\0';
1.12      jfb       687:                }
                    688:
1.13      jfb       689:                ret = cvs_resp_handle(root, cvs_proto_buf);
1.12      jfb       690:        } while (ret == 0);
                    691: }
                    692:
                    693:
                    694: /*
1.13      jfb       695:  * cvs_getln()
                    696:  *
1.18      jfb       697:  * Get a line from the remote end and store it in <lbuf>.  The terminating
1.13      jfb       698:  * newline character is stripped from the result.
1.55      jfb       699:  * Returns the length in bytes of the line (not including the NUL byte), or
                    700:  * -1 on failure.
1.13      jfb       701:  */
                    702: int
                    703: cvs_getln(struct cvsroot *root, char *lbuf, size_t len)
                    704: {
                    705:        size_t rlen;
1.18      jfb       706:        FILE *in;
1.13      jfb       707:
1.18      jfb       708:        if (cvs_cmdop == CVS_OP_SERVER)
                    709:                in = stdin;
                    710:        else
                    711:                in = root->cr_srvout;
                    712:
1.74      xsa       713:        if (fgets(lbuf, (int)len, in) == NULL) {
1.18      jfb       714:                if (ferror(in)) {
                    715:                        cvs_log(LP_ERRNO, "failed to read line");
1.13      jfb       716:                        return (-1);
                    717:                }
                    718:
1.18      jfb       719:                if (feof(in))
1.13      jfb       720:                        *lbuf = '\0';
                    721:        }
                    722:
                    723:        if (cvs_server_outlog != NULL)
                    724:                fputs(lbuf, cvs_server_outlog);
                    725:
                    726:        rlen = strlen(lbuf);
                    727:        if ((rlen > 0) && (lbuf[rlen - 1] == '\n'))
                    728:                lbuf[--rlen] = '\0';
                    729:
1.55      jfb       730:        return (rlen);
1.13      jfb       731: }
                    732:
1.19      jfb       733:
1.13      jfb       734: /*
1.12      jfb       735:  * cvs_sendresp()
                    736:  *
1.19      jfb       737:  * Send a response of type <rid> to the client, with optional arguments
1.12      jfb       738:  * contained in <arg>, which should not be terminated by a newline.
                    739:  * Returns 0 on success, or -1 on failure.
                    740:  */
                    741: int
                    742: cvs_sendresp(u_int rid, const char *arg)
                    743: {
                    744:        int ret;
1.19      jfb       745:        struct cvs_resp *resp;
1.12      jfb       746:
                    747:        resp = cvs_resp_getbyid(rid);
1.13      jfb       748:        if (resp == NULL) {
1.12      jfb       749:                cvs_log(LP_ERR, "unsupported response type %u", rid);
                    750:                return (-1);
                    751:        }
                    752:
1.19      jfb       753:        ret = fputs(resp->resp_str, stdout);
1.12      jfb       754:        if (ret == EOF) {
                    755:                cvs_log(LP_ERRNO, "failed to send response to client");
1.30      deraadt   756:        } else {
1.19      jfb       757:                if (arg != NULL) {
                    758:                        putc(' ', stdout);
                    759:                        fputs(arg, stdout);
                    760:                }
1.12      jfb       761:                putc('\n', stdout);
                    762:        }
                    763:        return (0);
                    764: }
                    765:
                    766:
1.19      jfb       767: #ifdef notyet
1.12      jfb       768: /*
                    769:  * cvs_getreq()
                    770:  *
                    771:  * Get a request from the client.
                    772:  */
                    773: int
                    774: cvs_getreq(void)
                    775: {
                    776:        int nbcmd;
                    777:
                    778:        nbcmd = 0;
                    779:
                    780:        do {
                    781:                /* wait for incoming data */
1.74      xsa       782:                if (fgets(cvs_proto_buf, (int)sizeof(cvs_proto_buf),
1.12      jfb       783:                    stdin) == NULL) {
                    784:                        if (feof(stdin))
                    785:                                return (0);
                    786:                        cvs_log(LP_ERRNO,
                    787:                            "failed to read request from client");
                    788:                        return (-1);
                    789:                }
                    790:
1.13      jfb       791:                if ((len = strlen(cvs_proto_buf)) != 0) {
                    792:                        if (cvs_proto_buf[len - 1] != '\n') {
1.12      jfb       793:                                /* truncated line */
1.30      deraadt   794:                        } else
1.13      jfb       795:                                cvs_proto_buf[--len] = '\0';
1.12      jfb       796:                }
                    797:
1.13      jfb       798:                ret = cvs_resp_handle(cvs_proto_buf);
1.12      jfb       799:        } while (ret == 0);
                    800: }
                    801: #endif
                    802:
                    803:
                    804: /*
                    805:  * cvs_sendln()
                    806:  *
1.18      jfb       807:  * Send a single line <line> string to the remote end.  The line is sent as is,
1.12      jfb       808:  * without any modifications.
                    809:  */
1.82    ! joris     810: void
1.12      jfb       811: cvs_sendln(struct cvsroot *root, const char *line)
                    812: {
                    813:        int nl;
                    814:        size_t len;
1.18      jfb       815:        FILE *out;
                    816:
                    817:        if (cvs_cmdop == CVS_OP_SERVER)
                    818:                out = stdout;
                    819:        else
                    820:                out = root->cr_srvin;
1.12      jfb       821:
                    822:        nl = 0;
                    823:        len = strlen(line);
                    824:
                    825:        if ((len > 0) && (line[len - 1] != '\n'))
                    826:                nl = 1;
                    827:
                    828:        if (cvs_server_inlog != NULL) {
                    829:                fputs(line, cvs_server_inlog);
                    830:                if (nl)
1.55      jfb       831:                        putc('\n', cvs_server_inlog);
1.12      jfb       832:        }
1.18      jfb       833:        fputs(line, out);
1.12      jfb       834:        if (nl)
1.55      jfb       835:                putc('\n', out);
1.12      jfb       836: }
                    837:
                    838:
                    839: /*
1.13      jfb       840:  * cvs_sendraw()
1.12      jfb       841:  *
                    842:  * Send the first <len> bytes from the buffer <src> to the server.
                    843:  */
1.82    ! joris     844: void
1.13      jfb       845: cvs_sendraw(struct cvsroot *root, const void *src, size_t len)
1.12      jfb       846: {
1.18      jfb       847:        FILE *out;
                    848:
                    849:        if (cvs_cmdop == CVS_OP_SERVER)
                    850:                out = stdout;
                    851:        else
                    852:                out = root->cr_srvin;
                    853:
1.12      jfb       854:        if (cvs_server_inlog != NULL)
                    855:                fwrite(src, sizeof(char), len, cvs_server_inlog);
1.82    ! joris     856:        if (fwrite(src, sizeof(char), len, out) < len)
        !           857:                fatal("failed to send data");
1.12      jfb       858: }
                    859:
                    860:
                    861: /*
1.13      jfb       862:  * cvs_recvraw()
1.12      jfb       863:  *
                    864:  * Receive the first <len> bytes from the buffer <src> to the server.
                    865:  */
                    866: ssize_t
1.13      jfb       867: cvs_recvraw(struct cvsroot *root, void *dst, size_t len)
1.12      jfb       868: {
                    869:        size_t ret;
1.18      jfb       870:        FILE *in;
                    871:
                    872:        if (cvs_cmdop == CVS_OP_SERVER)
                    873:                in = stdin;
                    874:        else
                    875:                in = root->cr_srvout;
1.12      jfb       876:
1.18      jfb       877:        ret = fread(dst, sizeof(char), len, in);
1.12      jfb       878:        if (ret == 0)
                    879:                return (-1);
                    880:        if (cvs_server_outlog != NULL)
                    881:                fwrite(dst, sizeof(char), len, cvs_server_outlog);
                    882:        return (ssize_t)ret;
                    883: }
                    884:
                    885:
                    886: /*
1.13      jfb       887:  * cvs_senddir()
1.12      jfb       888:  *
1.32      jfb       889:  * Send a `Directory' request along with the 2 paths that follow it.  If
                    890:  * the directory info to be sent is the same as the last info sent, the
                    891:  * call does nothing and simply returns without an error.
1.12      jfb       892:  */
1.82    ! joris     893: void
1.31      tedu      894: cvs_senddir(struct cvsroot *root, CVSFILE *dir)
1.12      jfb       895: {
1.60      xsa       896:        size_t len;
1.28      jfb       897:        char lbuf[MAXPATHLEN], rbuf[MAXPATHLEN];
1.12      jfb       898:
1.47      jfb       899:        if (dir->cf_type != DT_DIR)
1.82    ! joris     900:                fatal("cvs_senddir(): cf_type != DT_DIR: %d", dir->cf_type);
1.47      jfb       901:
1.32      jfb       902:        cvs_file_getpath(dir, lbuf, sizeof(lbuf));
1.45      joris     903:        if (strcmp(lbuf, cvs_lastdir) == 0 && cvs_cmdop != CVS_OP_CHECKOUT)
1.82    ! joris     904:                return;
1.32      jfb       905:
1.82    ! joris     906:        if (dir->cf_repo == NULL) {
        !           907:                len = strlcpy(rbuf, root->cr_dir, sizeof(rbuf));
        !           908:                if (len >= sizeof(rbuf))
        !           909:                        fatal("cvs_senddir: path truncation");
        !           910:        } else {
1.60      xsa       911:                len = cvs_path_cat(root->cr_dir, dir->cf_repo, rbuf,
                    912:                    sizeof(rbuf));
                    913:                if (len >= sizeof(rbuf))
1.82    ! joris     914:                        fatal("cvs_senddir: path truncation");
1.51      xsa       915:        }
1.12      jfb       916:
1.82    ! joris     917:        cvs_sendreq(root, CVS_REQ_DIRECTORY, lbuf);
        !           918:        cvs_sendln(root, rbuf);
1.28      jfb       919:
1.82    ! joris     920:        len = strlcpy(cvs_lastdir, lbuf, sizeof(cvs_lastdir));
        !           921:        if (len >= sizeof(lbuf))
        !           922:                fatal("path truncation in cvs_senddir");
1.12      jfb       923: }
                    924:
                    925:
                    926: /*
1.13      jfb       927:  * cvs_sendarg()
1.12      jfb       928:  *
                    929:  * Send the argument <arg> to the server.  The argument <append> is used to
                    930:  * determine if the argument should be simply appended to the last argument
                    931:  * sent or if it should be created as a new argument (0).
                    932:  */
1.82    ! joris     933: void
1.13      jfb       934: cvs_sendarg(struct cvsroot *root, const char *arg, int append)
1.12      jfb       935: {
1.82    ! joris     936:        cvs_sendreq(root, ((append == 0) ? CVS_REQ_ARGUMENT :
        !           937:            CVS_REQ_ARGUMENTX), arg);
1.12      jfb       938: }
                    939:
                    940:
                    941: /*
1.13      jfb       942:  * cvs_sendentry()
1.12      jfb       943:  *
                    944:  * Send an `Entry' request to the server along with the mandatory fields from
                    945:  * the CVS entry <ent> (which are the name and revision).
                    946:  */
1.82    ! joris     947: void
1.47      jfb       948: cvs_sendentry(struct cvsroot *root, const CVSFILE *file)
1.12      jfb       949: {
1.51      xsa       950:        int l;
1.77      xsa       951:        char ebuf[CVS_ENT_MAXLINELEN], numbuf[64];
1.12      jfb       952:
1.82    ! joris     953:        if (file->cf_type != DT_REG)
        !           954:                fatal("cvs_sendentry: cf_type != DT_REG: %d", file->cf_type);
1.48      joris     955:
                    956:        /* don't send Entry for unknown files */
                    957:        if (file->cf_cvstat == CVS_FST_UNKNOWN)
1.82    ! joris     958:                return;
1.47      jfb       959:
1.51      xsa       960:        l = snprintf(ebuf, sizeof(ebuf), "/%s/%s%s///", file->cf_name,
1.49      joris     961:            (file->cf_cvstat == CVS_FST_REMOVED) ? "-" : "",
1.47      jfb       962:            rcsnum_tostr(file->cf_lrev, numbuf, sizeof(numbuf)));
1.82    ! joris     963:        if (l == -1 || l >= (int)sizeof(ebuf))
        !           964:                fatal("cvs_sendentry: overflow when creating entry buffer");
1.12      jfb       965:
1.82    ! joris     966:        cvs_sendreq(root, CVS_REQ_ENTRY, ebuf);
1.12      jfb       967: }
                    968:
                    969:
                    970: /*
1.13      jfb       971:  * cvs_initlog()
1.12      jfb       972:  *
                    973:  * Initialize protocol logging if the CVS_CLIENT_LOG environment variable is
                    974:  * set.  In this case, the variable's value is used as a path to which the
                    975:  * appropriate suffix is added (".in" for server input and ".out" for server
                    976:  * output.
                    977:  * Returns 0 on success, or -1 on failure.
                    978:  */
                    979: static int
1.13      jfb       980: cvs_initlog(void)
1.12      jfb       981: {
1.52      joris     982:        int l;
                    983:        u_int i;
                    984:        char *env, *envdup, buf[MAXPATHLEN], fpath[MAXPATHLEN];
                    985:        char rpath[MAXPATHLEN], *s;
                    986:        struct stat st;
                    987:        time_t now;
                    988:        struct passwd *pwd;
1.12      jfb       989:
1.13      jfb       990:        /* avoid doing it more than once */
                    991:        if (cvs_server_logon)
                    992:                return (0);
                    993:
1.12      jfb       994:        env = getenv("CVS_CLIENT_LOG");
                    995:        if (env == NULL)
                    996:                return (0);
                    997:
1.80      joris     998:        envdup = xstrdup(env);
1.52      joris     999:        if ((s = strchr(envdup, '%')) != NULL)
                   1000:                *s = '\0';
                   1001:
                   1002:        strlcpy(buf, env, sizeof(buf));
                   1003:        strlcpy(rpath, envdup, sizeof(rpath));
1.80      joris    1004:        xfree(envdup);
1.52      joris    1005:
                   1006:        s = buf;
                   1007:        while ((s = strchr(s, '%')) != NULL) {
                   1008:                *s++;
                   1009:                switch (*s) {
                   1010:                case 'c':
                   1011:                        strlcpy(fpath, cvs_command, sizeof(fpath));
                   1012:                        break;
                   1013:                case 'd':
                   1014:                        time(&now);
1.59      niallo   1015:                        strlcpy(fpath, ctime(&now), sizeof(fpath));
1.52      joris    1016:                        break;
                   1017:                case 'p':
                   1018:                        snprintf(fpath, sizeof(fpath), "%d", getpid());
                   1019:                        break;
                   1020:                case 'u':
                   1021:                        if ((pwd = getpwuid(getuid())) != NULL)
                   1022:                                strlcpy(fpath, pwd->pw_name, sizeof(fpath));
                   1023:                        else
                   1024:                                fpath[0] = '\0';
                   1025:                        endpwent();
                   1026:                        break;
                   1027:                default:
                   1028:                        fpath[0] = '\0';
                   1029:                        break;
1.58      joris    1030:                }
1.52      joris    1031:
                   1032:                if (fpath[0] != '\0') {
                   1033:                        strlcat(rpath, "-", sizeof(rpath));
                   1034:                        strlcat(rpath, fpath, sizeof(rpath));
                   1035:                }
                   1036:        }
                   1037:
                   1038:        for (i = 0; i < UINT_MAX; i++) {
                   1039:                l = snprintf(fpath, sizeof(fpath), "%s-%d.in", rpath, i);
                   1040:                if (l == -1 || l >= (int)sizeof(fpath)) {
                   1041:                        errno = ENAMETOOLONG;
                   1042:                        cvs_log(LP_ERRNO, "%s", fpath);
                   1043:                        return (-1);
                   1044:                }
                   1045:
                   1046:                if (stat(fpath, &st) != -1)
                   1047:                        continue;
                   1048:
                   1049:                if (errno != ENOENT)
                   1050:                        return (-1);
                   1051:
                   1052:                break;
                   1053:        }
                   1054:
1.12      jfb      1055:        cvs_server_inlog = fopen(fpath, "w");
                   1056:        if (cvs_server_inlog == NULL) {
                   1057:                cvs_log(LP_ERRNO, "failed to open server input log `%s'",
                   1058:                    fpath);
                   1059:                return (-1);
                   1060:        }
                   1061:
1.52      joris    1062:        for (i = 0; i < UINT_MAX; i++) {
                   1063:                l = snprintf(fpath, sizeof(fpath), "%s-%d.out", rpath, i);
                   1064:                if (l == -1 || l >= (int)sizeof(fpath)) {
                   1065:                        errno = ENAMETOOLONG;
                   1066:                        cvs_log(LP_ERRNO, "%s", fpath);
                   1067:                        return (-1);
                   1068:                }
                   1069:
                   1070:                if (stat(fpath, &st) != -1)
                   1071:                        continue;
                   1072:
                   1073:                if (errno != ENOENT)
                   1074:                        return (-1);
                   1075:
                   1076:                break;
                   1077:        }
                   1078:
1.12      jfb      1079:        cvs_server_outlog = fopen(fpath, "w");
                   1080:        if (cvs_server_outlog == NULL) {
                   1081:                cvs_log(LP_ERRNO, "failed to open server output log `%s'",
                   1082:                    fpath);
                   1083:                return (-1);
                   1084:        }
                   1085:
                   1086:        /* make the streams line-buffered */
1.75      xsa      1087:        setvbuf(cvs_server_inlog, NULL, _IOLBF, (size_t)0);
                   1088:        setvbuf(cvs_server_outlog, NULL, _IOLBF, (size_t)0);
1.12      jfb      1089:
1.13      jfb      1090:        cvs_server_logon = 1;
1.12      jfb      1091:
                   1092:        return (0);
                   1093: }