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

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