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

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