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

1.84    ! reyk        1: /*     $OpenBSD: proto.c,v 1.83 2005/12/30 16:47:36 joris 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))
1.84    ! reyk      223:                        fatal("failed to setup standard streams "
        !           224:                            "for cvs server");
1.82      joris     225:
1.13      jfb       226:                (void)close(infd[1]);
                    227:                (void)close(outfd[0]);
1.20      jfb       228:                (void)close(errfd[0]);
1.13      jfb       229:
                    230:                argc = 0;
                    231:                argv[argc++] = cvs_rsh;
                    232:
                    233:                if (root->cr_user != NULL) {
                    234:                        argv[argc++] = "-l";
                    235:                        argv[argc++] = root->cr_user;
                    236:                }
                    237:
                    238:                cvs_server_cmd = getenv("CVS_SERVER");
                    239:                if (cvs_server_cmd == NULL)
1.20      jfb       240:                        cvs_server_cmd = CVS_SERVER_DEFAULT;
1.13      jfb       241:
                    242:                argv[argc++] = root->cr_host;
                    243:                argv[argc++] = cvs_server_cmd;
                    244:                argv[argc++] = "server";
                    245:                argv[argc] = NULL;
1.71      xsa       246:
                    247:                if (cvs_trace == 1) {
                    248:                        tmsg[0] = '\0';
                    249:                        for (argc = 0; argv[argc] != NULL; argc++) {
1.82      joris     250:                                len = strlcat(tmsg, argv[argc], sizeof(tmsg));
                    251:                                if (len >= sizeof(tmsg))
                    252:                                        fatal("truncation in cvs_connect");
                    253:
                    254:                                len = strlcat(tmsg, " ", sizeof(tmsg));
                    255:                                if (len >= sizeof(tmsg))
                    256:                                        fatal("truncation in cvs_connect");
1.71      xsa       257:                        }
                    258:                }
1.82      joris     259:
1.71      xsa       260:                cvs_log(LP_TRACE, "Starting server: %s", tmsg);
1.13      jfb       261:
                    262:                execvp(argv[0], argv);
1.82      joris     263:                fatal("failed to execute cvs server");
1.13      jfb       264:        }
                    265:
                    266:        /* we are the parent */
                    267:        (void)close(infd[0]);
                    268:        (void)close(outfd[1]);
1.20      jfb       269:        (void)close(errfd[1]);
1.13      jfb       270:
                    271:        root->cr_srvin = fdopen(infd[1], "w");
1.82      joris     272:        if (root->cr_srvin == NULL)
                    273:                fatal("failed to create pipe stream");
1.13      jfb       274:
                    275:        root->cr_srvout = fdopen(outfd[0], "r");
1.82      joris     276:        if (root->cr_srvout == NULL)
                    277:                fatal("failed to create pipe stream");
1.20      jfb       278:
1.13      jfb       279:        /* make the streams line-buffered */
1.75      xsa       280:        (void)setvbuf(root->cr_srvin, NULL, _IOLBF, (size_t)0);
                    281:        (void)setvbuf(root->cr_srvout, NULL, _IOLBF, (size_t)0);
1.13      jfb       282:
                    283:        cvs_initlog();
                    284:
                    285:        /*
                    286:         * Send the server the list of valid responses, then ask for valid
                    287:         * requests.
                    288:         */
                    289:
1.82      joris     290:        vresp = cvs_resp_getvalid();
                    291:        cvs_sendreq(root, CVS_REQ_VALIDRESP, vresp);
1.80      joris     292:        xfree(vresp);
1.13      jfb       293:
1.82      joris     294:        cvs_sendreq(root, CVS_REQ_VALIDREQ, NULL);
1.13      jfb       295:
1.62      xsa       296:        /* send the CVSROOT to the server */
1.82      joris     297:        cvs_sendreq(root, CVS_REQ_ROOT, root->cr_dir);
1.54      joris     298:
1.40      jfb       299:        /* don't fail if this request doesn't work */
1.82      joris     300:        cvs_sendreq(root, CVS_REQ_VERSION, NULL);
1.17      jfb       301:
1.13      jfb       302:        /* now share our global options with the server */
1.82      joris     303:        if (verbosity <= 1)
                    304:                cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-q");
                    305:        if (verbosity == 0)
                    306:                cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-Q");
                    307:
                    308:        if (cvs_noexec == 1)
                    309:                cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-n");
                    310:
                    311:        if (cvs_nolog == 1)
                    312:                cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-l");
                    313:
                    314:        if (cvs_readonly == 1)
                    315:                cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-r");
1.40      jfb       316:
1.82      joris     317:        if (cvs_trace == 1)
                    318:                cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-t");
1.13      jfb       319:
                    320:        /* not sure why, but we have to send this */
1.82      joris     321:        cvs_sendreq(root, CVS_REQ_USEUNCHANGED, NULL);
1.13      jfb       322:
                    323:        cvs_log(LP_DEBUG, "connected to %s", root->cr_host);
                    324:
1.33      jfb       325:        root->cr_flags |= CVS_ROOT_CONNECTED;
1.13      jfb       326: }
                    327:
                    328:
                    329: /*
                    330:  * cvs_disconnect()
                    331:  *
                    332:  * Disconnect from the cvs server.
                    333:  */
                    334: void
                    335: cvs_disconnect(struct cvsroot *root)
                    336: {
1.33      jfb       337:        if (!(root->cr_flags & CVS_ROOT_CONNECTED))
                    338:                return;
                    339:
1.13      jfb       340:        cvs_log(LP_DEBUG, "closing connection to %s", root->cr_host);
1.82      joris     341:
1.13      jfb       342:        if (root->cr_srvin != NULL) {
                    343:                (void)fclose(root->cr_srvin);
                    344:                root->cr_srvin = NULL;
                    345:        }
                    346:        if (root->cr_srvout != NULL) {
                    347:                (void)fclose(root->cr_srvout);
                    348:                root->cr_srvout = NULL;
                    349:        }
1.33      jfb       350:
                    351:        root->cr_flags &= ~CVS_ROOT_CONNECTED;
1.13      jfb       352: }
                    353:
1.1       jfb       354:
                    355: /*
                    356:  * cvs_req_getbyid()
                    357:  *
                    358:  */
1.13      jfb       359: struct cvs_req*
1.1       jfb       360: cvs_req_getbyid(int reqid)
                    361: {
                    362:        u_int i;
                    363:
                    364:        for (i = 0; i < CVS_NBREQ; i++)
                    365:                if (cvs_requests[i].req_id == reqid)
1.13      jfb       366:                        return &(cvs_requests[i]);
1.31      tedu      367:
1.1       jfb       368:        return (NULL);
                    369: }
                    370:
                    371:
                    372: /*
                    373:  * cvs_req_getbyname()
                    374:  */
1.13      jfb       375: struct cvs_req*
1.1       jfb       376: cvs_req_getbyname(const char *rname)
                    377: {
                    378:        u_int i;
                    379:
                    380:        for (i = 0; i < CVS_NBREQ; i++)
                    381:                if (strcmp(cvs_requests[i].req_str, rname) == 0)
1.13      jfb       382:                        return &(cvs_requests[i]);
1.1       jfb       383:
1.13      jfb       384:        return (NULL);
1.1       jfb       385: }
                    386:
                    387:
                    388: /*
                    389:  * cvs_req_getvalid()
                    390:  *
                    391:  * Build a space-separated list of all the requests that this protocol
                    392:  * implementation supports.
                    393:  */
1.67      xsa       394: char *
1.1       jfb       395: cvs_req_getvalid(void)
                    396: {
                    397:        u_int i;
                    398:        size_t len;
                    399:        char *vrstr;
                    400:        BUF *buf;
                    401:
1.75      xsa       402:        buf = cvs_buf_alloc((size_t)512, BUF_AUTOEXT);
1.1       jfb       403:
                    404:        cvs_buf_set(buf, cvs_requests[0].req_str,
1.75      xsa       405:            strlen(cvs_requests[0].req_str), (size_t)0);
1.1       jfb       406:
                    407:        for (i = 1; i < CVS_NBREQ; i++) {
1.81      xsa       408:                cvs_buf_putc(buf, ' ');
                    409:                cvs_buf_append(buf, cvs_requests[i].req_str,
                    410:                    strlen(cvs_requests[i].req_str));
1.1       jfb       411:        }
                    412:
                    413:        /* NUL-terminate */
1.81      xsa       414:        cvs_buf_putc(buf, '\0');
1.1       jfb       415:
1.70      moritz    416:        len = cvs_buf_len(buf);
1.80      joris     417:        vrstr = (char *)xmalloc(len);
1.75      xsa       418:        cvs_buf_copy(buf, (size_t)0, vrstr, len);
1.1       jfb       419:        cvs_buf_free(buf);
                    420:
                    421:        return (vrstr);
                    422: }
                    423:
                    424:
                    425: /*
                    426:  * cvs_resp_getbyid()
                    427:  *
                    428:  */
1.13      jfb       429: struct cvs_resp*
1.1       jfb       430: cvs_resp_getbyid(int respid)
                    431: {
                    432:        u_int i;
                    433:
1.44      tedu      434:        for (i = 0; i < CVS_NBRESP; i++)
1.13      jfb       435:                if (cvs_responses[i].resp_id == (u_int)respid)
                    436:                        return &(cvs_responses[i]);
1.31      tedu      437:
1.1       jfb       438:        return (NULL);
                    439: }
                    440:
                    441:
                    442: /*
                    443:  * cvs_resp_getbyname()
                    444:  */
1.67      xsa       445: struct cvs_resp *
1.1       jfb       446: cvs_resp_getbyname(const char *rname)
                    447: {
                    448:        u_int i;
                    449:
1.44      tedu      450:        for (i = 0; i < CVS_NBRESP; i++)
1.1       jfb       451:                if (strcmp(cvs_responses[i].resp_str, rname) == 0)
1.13      jfb       452:                        return &(cvs_responses[i]);
1.1       jfb       453:
1.13      jfb       454:        return (NULL);
1.1       jfb       455: }
                    456:
                    457:
                    458: /*
                    459:  * cvs_resp_getvalid()
                    460:  *
                    461:  * Build a space-separated list of all the responses that this protocol
                    462:  * implementation supports.
                    463:  */
1.67      xsa       464: char *
1.1       jfb       465: cvs_resp_getvalid(void)
                    466: {
                    467:        u_int i;
                    468:        size_t len;
                    469:        char *vrstr;
                    470:        BUF *buf;
                    471:
1.75      xsa       472:        buf = cvs_buf_alloc((size_t)512, BUF_AUTOEXT);
1.1       jfb       473:
                    474:        cvs_buf_set(buf, cvs_responses[0].resp_str,
1.75      xsa       475:            strlen(cvs_responses[0].resp_str), (size_t)0);
1.1       jfb       476:
                    477:        for (i = 1; i < CVS_NBRESP; i++) {
1.81      xsa       478:                cvs_buf_putc(buf, ' ');
                    479:                cvs_buf_append(buf, cvs_responses[i].resp_str,
                    480:                    strlen(cvs_responses[i].resp_str));
1.1       jfb       481:        }
                    482:
                    483:        /* NUL-terminate */
1.81      xsa       484:        cvs_buf_putc(buf, '\0');
1.1       jfb       485:
1.70      moritz    486:        len = cvs_buf_len(buf);
1.80      joris     487:        vrstr = (char *)xmalloc(len);
1.75      xsa       488:        cvs_buf_copy(buf, (size_t)0, vrstr, len);
1.1       jfb       489:        cvs_buf_free(buf);
                    490:
                    491:        return (vrstr);
                    492: }
                    493:
                    494:
                    495: /*
                    496:  * cvs_sendfile()
                    497:  *
                    498:  * Send the mode and size of a file followed by the file's contents.
                    499:  * Returns 0 on success, or -1 on failure.
                    500:  */
1.82      joris     501: void
1.13      jfb       502: cvs_sendfile(struct cvsroot *root, const char *path)
1.1       jfb       503: {
1.51      xsa       504:        int fd, l;
1.1       jfb       505:        ssize_t ret;
                    506:        char buf[4096];
                    507:        struct stat st;
                    508:
1.38      jfb       509:        cvs_log(LP_TRACE, "Sending file `%s' to server", basename(path));
                    510:
1.1       jfb       511:        if (stat(path, &st) == -1) {
1.82      joris     512:                fatal("cvs_sendfile(): stat failed on '%s': %s",
                    513:                    path, strerror(errno));
1.1       jfb       514:        }
                    515:
1.82      joris     516:        cvs_modetostr(st.st_mode, buf, sizeof(buf));
1.24      jfb       517:
1.1       jfb       518:        fd = open(path, O_RDONLY, 0);
                    519:        if (fd == -1) {
1.82      joris     520:                fatal("cvs_sendfile(): failed to open '%s': %s",
                    521:                    path, strerror(errno));
1.1       jfb       522:        }
                    523:
1.82      joris     524:        cvs_sendln(root, buf);
                    525:
1.51      xsa       526:        l = snprintf(buf, sizeof(buf), "%lld\n", st.st_size);
                    527:        if (l == -1 || l >= (int)sizeof(buf))
1.82      joris     528:                fatal("overflow in cvs_sendfile");
1.51      xsa       529:
1.82      joris     530:        cvs_sendln(root, buf);
1.1       jfb       531:
                    532:        while ((ret = read(fd, buf, sizeof(buf))) != 0) {
                    533:                if (ret == -1) {
1.24      jfb       534:                        (void)close(fd);
1.82      joris     535:                        fatal("cvs_sendfile: read error on '%s'", path);
1.1       jfb       536:                }
                    537:
1.82      joris     538:                cvs_sendraw(root, buf, (size_t)ret);
1.1       jfb       539:        }
                    540:
                    541:        (void)close(fd);
                    542: }
                    543:
                    544:
                    545: /*
                    546:  * cvs_recvfile()
                    547:  *
                    548:  * Receive the mode and size of a file followed the file's contents and
                    549:  * create or update the file whose path is <path> with the received
                    550:  * information.
                    551:  */
1.14      jfb       552: BUF*
                    553: cvs_recvfile(struct cvsroot *root, mode_t *mode)
1.1       jfb       554: {
                    555:        size_t len;
                    556:        ssize_t ret;
                    557:        off_t fsz, cnt;
                    558:        char buf[4096], *ep;
1.14      jfb       559:        BUF *fbuf;
                    560:
                    561:        fbuf = cvs_buf_alloc(sizeof(buf), BUF_AUTOEXT);
1.1       jfb       562:
1.83      joris     563:        cvs_getln(root, buf, sizeof(buf));
                    564:        cvs_strtomode(buf, mode);
1.1       jfb       565:
1.83      joris     566:        cvs_getln(root, buf, sizeof(buf));
1.1       jfb       567:
                    568:        fsz = (off_t)strtol(buf, &ep, 10);
1.83      joris     569:        if (*ep != '\0')
                    570:                fatal("parse error in file size transmission");
1.1       jfb       571:
                    572:        cnt = 0;
                    573:        do {
                    574:                len = MIN(sizeof(buf), (size_t)(fsz - cnt));
1.6       jfb       575:                if (len == 0)
                    576:                        break;
1.13      jfb       577:                ret = cvs_recvraw(root, buf, len);
1.83      joris     578:                cvs_buf_append(fbuf, buf, (size_t)ret);
1.1       jfb       579:                cnt += (off_t)ret;
                    580:        } while (cnt < fsz);
                    581:
1.14      jfb       582:        return (fbuf);
1.1       jfb       583: }
1.12      jfb       584:
                    585: /*
                    586:  * cvs_sendreq()
                    587:  *
                    588:  * Send a request to the server of type <rid>, with optional arguments
                    589:  * contained in <arg>, which should not be terminated by a newline.
                    590:  * Returns 0 on success, or -1 on failure.
                    591:  */
1.82      joris     592: void
1.13      jfb       593: cvs_sendreq(struct cvsroot *root, u_int rid, const char *arg)
1.12      jfb       594: {
1.51      xsa       595:        int ret, l;
1.13      jfb       596:        struct cvs_req *req;
1.12      jfb       597:
1.82      joris     598:        if (root->cr_srvin == NULL)
                    599:                fatal("cannot send request %u: Not connected", rid);
1.12      jfb       600:
1.13      jfb       601:        req = cvs_req_getbyid(rid);
1.82      joris     602:        if (req == NULL)
                    603:                fatal("unsupported request type %u", rid);
1.12      jfb       604:
1.18      jfb       605:        /* is this request supported by the server? */
                    606:        if (!CVS_GETVR(root, req->req_id)) {
1.57      joris     607:                if (rid == CVS_REQ_VERSION) {
1.82      joris     608:                        cvs_sendreq(root, CVS_REQ_NOOP, arg);
1.57      joris     609:                } else {
                    610:                        cvs_log(LP_WARN,
                    611:                            "remote end does not support request `%s'",
                    612:                            req->req_str);
                    613:                }
1.84    ! reyk      614:
1.82      joris     615:                return;
1.18      jfb       616:        }
                    617:
1.51      xsa       618:        l = snprintf(cvs_proto_buf, sizeof(cvs_proto_buf), "%s%s%s\n",
1.13      jfb       619:            req->req_str, (arg == NULL) ? "" : " ", (arg == NULL) ? "" : arg);
1.51      xsa       620:        if (l == -1 || l >= (int)sizeof(cvs_proto_buf))
1.82      joris     621:                fatal("overflow in cvs_sendreq");
1.12      jfb       622:
                    623:        if (cvs_server_inlog != NULL)
1.13      jfb       624:                fputs(cvs_proto_buf, cvs_server_inlog);
1.12      jfb       625:
1.13      jfb       626:        ret = fputs(cvs_proto_buf, root->cr_srvin);
1.82      joris     627:        if (ret == EOF)
                    628:                fatal("failed to send request to server");
1.17      jfb       629:
                    630:        if (rid == CVS_REQ_VERSION)
                    631:                cvs_version_sent = 1;
1.12      jfb       632:
1.13      jfb       633:        if (req->req_flags & CVS_REQF_RESP)
1.82      joris     634:                cvs_getresp(root);
1.12      jfb       635: }
                    636:
                    637:
                    638: /*
                    639:  * cvs_getresp()
                    640:  *
                    641:  * Get a response from the server.  This call will actually read and handle
                    642:  * responses from the server until one of the response handlers returns
1.37      david     643:  * non-zero (either an error occurred or the end of the response was reached).
1.12      jfb       644:  * Returns the number of handled commands on success, or -1 on failure.
                    645:  */
1.82      joris     646: void
1.13      jfb       647: cvs_getresp(struct cvsroot *root)
1.12      jfb       648: {
1.82      joris     649:        int ret;
1.13      jfb       650:        size_t len;
1.12      jfb       651:
                    652:        do {
                    653:                /* wait for incoming data */
1.74      xsa       654:                if (fgets(cvs_proto_buf, (int)sizeof(cvs_proto_buf),
1.12      jfb       655:                    root->cr_srvout) == NULL) {
                    656:                        if (feof(root->cr_srvout))
1.82      joris     657:                                return;
                    658:                        fatal("failed to read response from server");
1.12      jfb       659:                }
                    660:
                    661:                if (cvs_server_outlog != NULL)
1.13      jfb       662:                        fputs(cvs_proto_buf, cvs_server_outlog);
1.12      jfb       663:
1.13      jfb       664:                if ((len = strlen(cvs_proto_buf)) != 0) {
1.42      joris     665:                        /* if len - 1 != '\n' the line is truncated */
1.43      joris     666:                        if (cvs_proto_buf[len - 1] == '\n')
1.13      jfb       667:                                cvs_proto_buf[--len] = '\0';
1.12      jfb       668:                }
                    669:
1.13      jfb       670:                ret = cvs_resp_handle(root, cvs_proto_buf);
1.12      jfb       671:        } while (ret == 0);
                    672: }
                    673:
                    674:
                    675: /*
1.13      jfb       676:  * cvs_getln()
                    677:  *
1.18      jfb       678:  * Get a line from the remote end and store it in <lbuf>.  The terminating
1.13      jfb       679:  * newline character is stripped from the result.
                    680:  */
1.83      joris     681: void
1.13      jfb       682: cvs_getln(struct cvsroot *root, char *lbuf, size_t len)
                    683: {
                    684:        size_t rlen;
1.18      jfb       685:        FILE *in;
1.13      jfb       686:
1.18      jfb       687:        if (cvs_cmdop == CVS_OP_SERVER)
                    688:                in = stdin;
                    689:        else
                    690:                in = root->cr_srvout;
                    691:
1.74      xsa       692:        if (fgets(lbuf, (int)len, in) == NULL) {
1.18      jfb       693:                if (ferror(in)) {
1.83      joris     694:                        fatal("cvs_getln: error reading server: %s",
                    695:                            strerror(errno));
1.13      jfb       696:                }
                    697:
1.18      jfb       698:                if (feof(in))
1.13      jfb       699:                        *lbuf = '\0';
                    700:        }
                    701:
                    702:        if (cvs_server_outlog != NULL)
                    703:                fputs(lbuf, cvs_server_outlog);
                    704:
                    705:        rlen = strlen(lbuf);
                    706:        if ((rlen > 0) && (lbuf[rlen - 1] == '\n'))
                    707:                lbuf[--rlen] = '\0';
                    708: }
                    709:
1.19      jfb       710:
1.13      jfb       711: /*
1.12      jfb       712:  * cvs_sendresp()
                    713:  *
1.19      jfb       714:  * Send a response of type <rid> to the client, with optional arguments
1.12      jfb       715:  * contained in <arg>, which should not be terminated by a newline.
                    716:  * Returns 0 on success, or -1 on failure.
                    717:  */
                    718: int
                    719: cvs_sendresp(u_int rid, const char *arg)
                    720: {
                    721:        int ret;
1.19      jfb       722:        struct cvs_resp *resp;
1.12      jfb       723:
                    724:        resp = cvs_resp_getbyid(rid);
1.13      jfb       725:        if (resp == NULL) {
1.12      jfb       726:                cvs_log(LP_ERR, "unsupported response type %u", rid);
                    727:                return (-1);
                    728:        }
                    729:
1.19      jfb       730:        ret = fputs(resp->resp_str, stdout);
1.12      jfb       731:        if (ret == EOF) {
                    732:                cvs_log(LP_ERRNO, "failed to send response to client");
1.30      deraadt   733:        } else {
1.19      jfb       734:                if (arg != NULL) {
                    735:                        putc(' ', stdout);
                    736:                        fputs(arg, stdout);
                    737:                }
1.12      jfb       738:                putc('\n', stdout);
                    739:        }
                    740:        return (0);
                    741: }
                    742:
                    743:
1.19      jfb       744: #ifdef notyet
1.12      jfb       745: /*
                    746:  * cvs_getreq()
                    747:  *
                    748:  * Get a request from the client.
                    749:  */
                    750: int
                    751: cvs_getreq(void)
                    752: {
                    753:        int nbcmd;
                    754:
                    755:        nbcmd = 0;
                    756:
                    757:        do {
                    758:                /* wait for incoming data */
1.74      xsa       759:                if (fgets(cvs_proto_buf, (int)sizeof(cvs_proto_buf),
1.12      jfb       760:                    stdin) == NULL) {
                    761:                        if (feof(stdin))
                    762:                                return (0);
                    763:                        cvs_log(LP_ERRNO,
                    764:                            "failed to read request from client");
                    765:                        return (-1);
                    766:                }
                    767:
1.13      jfb       768:                if ((len = strlen(cvs_proto_buf)) != 0) {
                    769:                        if (cvs_proto_buf[len - 1] != '\n') {
1.12      jfb       770:                                /* truncated line */
1.30      deraadt   771:                        } else
1.13      jfb       772:                                cvs_proto_buf[--len] = '\0';
1.12      jfb       773:                }
                    774:
1.13      jfb       775:                ret = cvs_resp_handle(cvs_proto_buf);
1.12      jfb       776:        } while (ret == 0);
                    777: }
                    778: #endif
                    779:
                    780:
                    781: /*
                    782:  * cvs_sendln()
                    783:  *
1.18      jfb       784:  * Send a single line <line> string to the remote end.  The line is sent as is,
1.12      jfb       785:  * without any modifications.
                    786:  */
1.82      joris     787: void
1.12      jfb       788: cvs_sendln(struct cvsroot *root, const char *line)
                    789: {
                    790:        int nl;
                    791:        size_t len;
1.18      jfb       792:        FILE *out;
                    793:
                    794:        if (cvs_cmdop == CVS_OP_SERVER)
                    795:                out = stdout;
                    796:        else
                    797:                out = root->cr_srvin;
1.12      jfb       798:
                    799:        nl = 0;
                    800:        len = strlen(line);
                    801:
                    802:        if ((len > 0) && (line[len - 1] != '\n'))
                    803:                nl = 1;
                    804:
                    805:        if (cvs_server_inlog != NULL) {
                    806:                fputs(line, cvs_server_inlog);
                    807:                if (nl)
1.55      jfb       808:                        putc('\n', cvs_server_inlog);
1.12      jfb       809:        }
1.18      jfb       810:        fputs(line, out);
1.12      jfb       811:        if (nl)
1.55      jfb       812:                putc('\n', out);
1.12      jfb       813: }
                    814:
                    815:
                    816: /*
1.13      jfb       817:  * cvs_sendraw()
1.12      jfb       818:  *
                    819:  * Send the first <len> bytes from the buffer <src> to the server.
                    820:  */
1.82      joris     821: void
1.13      jfb       822: cvs_sendraw(struct cvsroot *root, const void *src, size_t len)
1.12      jfb       823: {
1.18      jfb       824:        FILE *out;
                    825:
                    826:        if (cvs_cmdop == CVS_OP_SERVER)
                    827:                out = stdout;
                    828:        else
                    829:                out = root->cr_srvin;
                    830:
1.12      jfb       831:        if (cvs_server_inlog != NULL)
                    832:                fwrite(src, sizeof(char), len, cvs_server_inlog);
1.82      joris     833:        if (fwrite(src, sizeof(char), len, out) < len)
                    834:                fatal("failed to send data");
1.12      jfb       835: }
                    836:
                    837:
                    838: /*
1.13      jfb       839:  * cvs_recvraw()
1.12      jfb       840:  *
                    841:  * Receive the first <len> bytes from the buffer <src> to the server.
                    842:  */
1.83      joris     843: size_t
1.13      jfb       844: cvs_recvraw(struct cvsroot *root, void *dst, size_t len)
1.12      jfb       845: {
                    846:        size_t ret;
1.18      jfb       847:        FILE *in;
                    848:
                    849:        if (cvs_cmdop == CVS_OP_SERVER)
                    850:                in = stdin;
                    851:        else
                    852:                in = root->cr_srvout;
1.12      jfb       853:
1.18      jfb       854:        ret = fread(dst, sizeof(char), len, in);
1.83      joris     855:        if (ret == 0) {
                    856:                if (ferror(in)) {
                    857:                        fatal("cvs_recvraw: error reading from server: %s",
                    858:                            strerror(errno));
                    859:                }
                    860:        } else {
                    861:                if (cvs_server_outlog != NULL)
                    862:                        fwrite(dst, sizeof(char), len, cvs_server_outlog);
                    863:        }
                    864:
                    865:        return (ret);
1.12      jfb       866: }
                    867:
                    868:
                    869: /*
1.13      jfb       870:  * cvs_senddir()
1.12      jfb       871:  *
1.32      jfb       872:  * Send a `Directory' request along with the 2 paths that follow it.  If
                    873:  * the directory info to be sent is the same as the last info sent, the
                    874:  * call does nothing and simply returns without an error.
1.12      jfb       875:  */
1.82      joris     876: void
1.31      tedu      877: cvs_senddir(struct cvsroot *root, CVSFILE *dir)
1.12      jfb       878: {
1.60      xsa       879:        size_t len;
1.28      jfb       880:        char lbuf[MAXPATHLEN], rbuf[MAXPATHLEN];
1.12      jfb       881:
1.47      jfb       882:        if (dir->cf_type != DT_DIR)
1.82      joris     883:                fatal("cvs_senddir(): cf_type != DT_DIR: %d", dir->cf_type);
1.47      jfb       884:
1.32      jfb       885:        cvs_file_getpath(dir, lbuf, sizeof(lbuf));
1.45      joris     886:        if (strcmp(lbuf, cvs_lastdir) == 0 && cvs_cmdop != CVS_OP_CHECKOUT)
1.82      joris     887:                return;
1.32      jfb       888:
1.82      joris     889:        if (dir->cf_repo == NULL) {
                    890:                len = strlcpy(rbuf, root->cr_dir, sizeof(rbuf));
                    891:                if (len >= sizeof(rbuf))
                    892:                        fatal("cvs_senddir: path truncation");
                    893:        } else {
1.60      xsa       894:                len = cvs_path_cat(root->cr_dir, dir->cf_repo, rbuf,
                    895:                    sizeof(rbuf));
                    896:                if (len >= sizeof(rbuf))
1.82      joris     897:                        fatal("cvs_senddir: path truncation");
1.51      xsa       898:        }
1.12      jfb       899:
1.82      joris     900:        cvs_sendreq(root, CVS_REQ_DIRECTORY, lbuf);
                    901:        cvs_sendln(root, rbuf);
1.28      jfb       902:
1.82      joris     903:        len = strlcpy(cvs_lastdir, lbuf, sizeof(cvs_lastdir));
                    904:        if (len >= sizeof(lbuf))
                    905:                fatal("path truncation in cvs_senddir");
1.12      jfb       906: }
                    907:
                    908:
                    909: /*
1.13      jfb       910:  * cvs_sendarg()
1.12      jfb       911:  *
                    912:  * Send the argument <arg> to the server.  The argument <append> is used to
                    913:  * determine if the argument should be simply appended to the last argument
                    914:  * sent or if it should be created as a new argument (0).
                    915:  */
1.82      joris     916: void
1.13      jfb       917: cvs_sendarg(struct cvsroot *root, const char *arg, int append)
1.12      jfb       918: {
1.82      joris     919:        cvs_sendreq(root, ((append == 0) ? CVS_REQ_ARGUMENT :
                    920:            CVS_REQ_ARGUMENTX), arg);
1.12      jfb       921: }
                    922:
                    923:
                    924: /*
1.13      jfb       925:  * cvs_sendentry()
1.12      jfb       926:  *
                    927:  * Send an `Entry' request to the server along with the mandatory fields from
                    928:  * the CVS entry <ent> (which are the name and revision).
                    929:  */
1.82      joris     930: void
1.47      jfb       931: cvs_sendentry(struct cvsroot *root, const CVSFILE *file)
1.12      jfb       932: {
1.51      xsa       933:        int l;
1.77      xsa       934:        char ebuf[CVS_ENT_MAXLINELEN], numbuf[64];
1.12      jfb       935:
1.82      joris     936:        if (file->cf_type != DT_REG)
                    937:                fatal("cvs_sendentry: cf_type != DT_REG: %d", file->cf_type);
1.48      joris     938:
                    939:        /* don't send Entry for unknown files */
                    940:        if (file->cf_cvstat == CVS_FST_UNKNOWN)
1.82      joris     941:                return;
1.47      jfb       942:
1.51      xsa       943:        l = snprintf(ebuf, sizeof(ebuf), "/%s/%s%s///", file->cf_name,
1.49      joris     944:            (file->cf_cvstat == CVS_FST_REMOVED) ? "-" : "",
1.47      jfb       945:            rcsnum_tostr(file->cf_lrev, numbuf, sizeof(numbuf)));
1.82      joris     946:        if (l == -1 || l >= (int)sizeof(ebuf))
                    947:                fatal("cvs_sendentry: overflow when creating entry buffer");
1.12      jfb       948:
1.82      joris     949:        cvs_sendreq(root, CVS_REQ_ENTRY, ebuf);
1.12      jfb       950: }
                    951:
                    952:
                    953: /*
1.13      jfb       954:  * cvs_initlog()
1.12      jfb       955:  *
                    956:  * Initialize protocol logging if the CVS_CLIENT_LOG environment variable is
                    957:  * set.  In this case, the variable's value is used as a path to which the
                    958:  * appropriate suffix is added (".in" for server input and ".out" for server
                    959:  * output.
                    960:  * Returns 0 on success, or -1 on failure.
                    961:  */
                    962: static int
1.13      jfb       963: cvs_initlog(void)
1.12      jfb       964: {
1.52      joris     965:        int l;
                    966:        u_int i;
                    967:        char *env, *envdup, buf[MAXPATHLEN], fpath[MAXPATHLEN];
                    968:        char rpath[MAXPATHLEN], *s;
                    969:        struct stat st;
                    970:        time_t now;
                    971:        struct passwd *pwd;
1.12      jfb       972:
1.13      jfb       973:        /* avoid doing it more than once */
                    974:        if (cvs_server_logon)
                    975:                return (0);
                    976:
1.12      jfb       977:        env = getenv("CVS_CLIENT_LOG");
                    978:        if (env == NULL)
                    979:                return (0);
                    980:
1.80      joris     981:        envdup = xstrdup(env);
1.52      joris     982:        if ((s = strchr(envdup, '%')) != NULL)
                    983:                *s = '\0';
                    984:
                    985:        strlcpy(buf, env, sizeof(buf));
                    986:        strlcpy(rpath, envdup, sizeof(rpath));
1.80      joris     987:        xfree(envdup);
1.52      joris     988:
                    989:        s = buf;
                    990:        while ((s = strchr(s, '%')) != NULL) {
                    991:                *s++;
                    992:                switch (*s) {
                    993:                case 'c':
                    994:                        strlcpy(fpath, cvs_command, sizeof(fpath));
                    995:                        break;
                    996:                case 'd':
                    997:                        time(&now);
1.59      niallo    998:                        strlcpy(fpath, ctime(&now), sizeof(fpath));
1.52      joris     999:                        break;
                   1000:                case 'p':
                   1001:                        snprintf(fpath, sizeof(fpath), "%d", getpid());
                   1002:                        break;
                   1003:                case 'u':
                   1004:                        if ((pwd = getpwuid(getuid())) != NULL)
                   1005:                                strlcpy(fpath, pwd->pw_name, sizeof(fpath));
                   1006:                        else
                   1007:                                fpath[0] = '\0';
                   1008:                        endpwent();
                   1009:                        break;
                   1010:                default:
                   1011:                        fpath[0] = '\0';
                   1012:                        break;
1.58      joris    1013:                }
1.52      joris    1014:
                   1015:                if (fpath[0] != '\0') {
                   1016:                        strlcat(rpath, "-", sizeof(rpath));
                   1017:                        strlcat(rpath, fpath, sizeof(rpath));
                   1018:                }
                   1019:        }
                   1020:
                   1021:        for (i = 0; i < UINT_MAX; i++) {
                   1022:                l = snprintf(fpath, sizeof(fpath), "%s-%d.in", rpath, i);
                   1023:                if (l == -1 || l >= (int)sizeof(fpath)) {
                   1024:                        errno = ENAMETOOLONG;
                   1025:                        cvs_log(LP_ERRNO, "%s", fpath);
                   1026:                        return (-1);
                   1027:                }
                   1028:
                   1029:                if (stat(fpath, &st) != -1)
                   1030:                        continue;
                   1031:
                   1032:                if (errno != ENOENT)
                   1033:                        return (-1);
                   1034:
                   1035:                break;
                   1036:        }
                   1037:
1.12      jfb      1038:        cvs_server_inlog = fopen(fpath, "w");
                   1039:        if (cvs_server_inlog == NULL) {
                   1040:                cvs_log(LP_ERRNO, "failed to open server input log `%s'",
                   1041:                    fpath);
                   1042:                return (-1);
                   1043:        }
                   1044:
1.52      joris    1045:        for (i = 0; i < UINT_MAX; i++) {
                   1046:                l = snprintf(fpath, sizeof(fpath), "%s-%d.out", rpath, i);
                   1047:                if (l == -1 || l >= (int)sizeof(fpath)) {
                   1048:                        errno = ENAMETOOLONG;
                   1049:                        cvs_log(LP_ERRNO, "%s", fpath);
                   1050:                        return (-1);
                   1051:                }
                   1052:
                   1053:                if (stat(fpath, &st) != -1)
                   1054:                        continue;
                   1055:
                   1056:                if (errno != ENOENT)
                   1057:                        return (-1);
                   1058:
                   1059:                break;
                   1060:        }
                   1061:
1.12      jfb      1062:        cvs_server_outlog = fopen(fpath, "w");
                   1063:        if (cvs_server_outlog == NULL) {
                   1064:                cvs_log(LP_ERRNO, "failed to open server output log `%s'",
                   1065:                    fpath);
                   1066:                return (-1);
                   1067:        }
                   1068:
                   1069:        /* make the streams line-buffered */
1.75      xsa      1070:        setvbuf(cvs_server_inlog, NULL, _IOLBF, (size_t)0);
                   1071:        setvbuf(cvs_server_outlog, NULL, _IOLBF, (size_t)0);
1.12      jfb      1072:
1.13      jfb      1073:        cvs_server_logon = 1;
1.12      jfb      1074:
                   1075:        return (0);
                   1076: }