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

1.79    ! moritz      1: /*     $OpenBSD: proto.c,v 1.78 2005/10/10 17:51:53 xsa Exp $  */
1.1       jfb         2: /*
                      3:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
                      4:  * All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  *
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. The name of the author may not be used to endorse or promote products
                     13:  *    derived from this software without specific prior written permission.
                     14:  *
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     16:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     17:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
                     18:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     19:  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     20:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     21:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     22:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     23:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
                     24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     25:  */
                     26: /*
                     27:  * CVS client/server protocol
                     28:  * ==========================
                     29:  *
                     30:  * The following code implements the CVS client/server protocol, which is
                     31:  * documented at the following URL:
1.72      joris      32:  *     http://www.iam.unibe.ch/~til/documentation/cvs/cvsclient_toc.html
                     33:  *     http://www.elegosoft.com/cvs/cvsclient_toc.html
1.1       jfb        34:  *
                     35:  * The protocol is split up into two parts; the first part is the client side
                     36:  * of things and is composed of all the response handlers, which are all named
                     37:  * with a prefix of "cvs_resp_".  The second part is the set of request
                     38:  * handlers used by the server.  These handlers process the request and
                     39:  * generate the appropriate response to send back.  The prefix for request
                     40:  * handlers is "cvs_req_".
                     41:  *
                     42:  */
                     43:
                     44: #include <sys/types.h>
                     45: #include <sys/stat.h>
                     46:
1.56      xsa        47: #include <errno.h>
1.1       jfb        48: #include <fcntl.h>
1.56      xsa        49: #include <libgen.h>
                     50: #include <pwd.h>
1.1       jfb        51: #include <stdio.h>
                     52: #include <stdlib.h>
1.56      xsa        53: #include <string.h>
1.1       jfb        54: #include <unistd.h>
                     55:
                     56: #include "cvs.h"
                     57: #include "log.h"
1.13      jfb        58: #include "proto.h"
1.1       jfb        59:
                     60:
1.11      jfb        61: /* request flags */
1.68      xsa        62: #define CVS_REQF_RESP  0x01
1.11      jfb        63:
                     64:
1.68      xsa        65: static int     cvs_initlog(void);
1.1       jfb        66:
1.18      jfb        67: struct cvs_req cvs_requests[] = {
                     68:        { CVS_REQ_DIRECTORY,     "Directory",         0             },
                     69:        { CVS_REQ_MAXDOTDOT,     "Max-dotdot",        0             },
                     70:        { CVS_REQ_STATICDIR,     "Static-directory",  0             },
                     71:        { CVS_REQ_STICKY,        "Sticky",            0             },
                     72:        { CVS_REQ_ENTRY,         "Entry",             0             },
                     73:        { CVS_REQ_ENTRYEXTRA,    "EntryExtra",        0             },
                     74:        { CVS_REQ_CHECKINTIME,   "Checkin-time",      0             },
                     75:        { CVS_REQ_MODIFIED,      "Modified",          0             },
                     76:        { CVS_REQ_ISMODIFIED,    "Is-modified",       0             },
                     77:        { CVS_REQ_UNCHANGED,     "Unchanged",         0             },
                     78:        { CVS_REQ_USEUNCHANGED,  "UseUnchanged",      0             },
                     79:        { CVS_REQ_NOTIFY,        "Notify",            0             },
                     80:        { CVS_REQ_NOTIFYUSER,    "NotifyUser",        0             },
                     81:        { CVS_REQ_QUESTIONABLE,  "Questionable",      0             },
                     82:        { CVS_REQ_CASE,          "Case",              0             },
                     83:        { CVS_REQ_UTF8,          "Utf8",              0             },
                     84:        { CVS_REQ_ARGUMENT,      "Argument",          0             },
                     85:        { CVS_REQ_ARGUMENTX,     "Argumentx",         0             },
                     86:        { CVS_REQ_GLOBALOPT,     "Global_option",     0             },
                     87:        { CVS_REQ_GZIPSTREAM,    "Gzip-stream",       0             },
                     88:        { CVS_REQ_READCVSRC2,    "read-cvsrc2",       0             },
                     89:        { CVS_REQ_READWRAP,      "read-cvswrappers",  0             },
                     90:        { CVS_REQ_READIGNORE,    "read-cvsignore",    0             },
                     91:        { CVS_REQ_ERRIFREADER,   "Error-If-Reader",   0             },
                     92:        { CVS_REQ_VALIDRCSOPT,   "Valid-RcsOptions",  0             },
                     93:        { CVS_REQ_SET,           "Set",               0             },
                     94:        { CVS_REQ_XPANDMOD,      "expand-modules",    CVS_REQF_RESP },
1.26      jfb        95:        { CVS_REQ_LOG,           "log",               CVS_REQF_RESP },
1.18      jfb        96:        { CVS_REQ_CO,            "co",                CVS_REQF_RESP },
                     97:        { CVS_REQ_EXPORT,        "export",            CVS_REQF_RESP },
1.26      jfb        98:        { CVS_REQ_ANNOTATE,      "annotate",          CVS_REQF_RESP },
                     99:        { CVS_REQ_RDIFF,         "rdiff",             CVS_REQF_RESP },
1.18      jfb       100:        { CVS_REQ_RTAG,          "rtag",              CVS_REQF_RESP },
                    101:        { CVS_REQ_INIT,          "init",              CVS_REQF_RESP },
                    102:        { CVS_REQ_STATUS,        "status",            CVS_REQF_RESP },
                    103:        { CVS_REQ_UPDATE,        "update",            CVS_REQF_RESP },
1.26      jfb       104:        { CVS_REQ_HISTORY,       "history",           CVS_REQF_RESP },
1.18      jfb       105:        { CVS_REQ_IMPORT,        "import",            CVS_REQF_RESP },
                    106:        { CVS_REQ_ADD,           "add",               CVS_REQF_RESP },
                    107:        { CVS_REQ_REMOVE,        "remove",            CVS_REQF_RESP },
                    108:        { CVS_REQ_RELEASE,       "release",           CVS_REQF_RESP },
                    109:        { CVS_REQ_ROOT,          "Root",              0             },
                    110:        { CVS_REQ_VALIDRESP,     "Valid-responses",   0             },
                    111:        { CVS_REQ_VALIDREQ,      "valid-requests",    CVS_REQF_RESP },
                    112:        { CVS_REQ_VERSION,       "version",           CVS_REQF_RESP },
                    113:        { CVS_REQ_NOOP,          "noop",              CVS_REQF_RESP },
                    114:        { CVS_REQ_DIFF,          "diff",              CVS_REQF_RESP },
1.29      jfb       115:        { CVS_REQ_CI,            "ci",                CVS_REQF_RESP },
1.35      jfb       116:        { CVS_REQ_TAG,           "tag",               CVS_REQF_RESP },
1.41      joris     117:        { CVS_REQ_ADMIN,         "admin",             CVS_REQF_RESP },
1.76      xsa       118:        { CVS_REQ_WATCHERS,      "watchers",          CVS_REQF_RESP },
1.79    ! moritz    119:        { CVS_REQ_WATCH_ON,      "watch-on",          CVS_REQF_RESP },
        !           120:        { CVS_REQ_WATCH_OFF,     "watch-off",         CVS_REQF_RESP },
        !           121:        { CVS_REQ_WATCH_ADD,     "watch-add",         CVS_REQF_RESP },
        !           122:        { CVS_REQ_WATCH_REMOVE,  "watch-remove",      CVS_REQF_RESP },
1.78      xsa       123:        { CVS_REQ_EDITORS,       "editors",           CVS_REQF_RESP },
1.1       jfb       124: };
                    125:
1.18      jfb       126: struct cvs_resp cvs_responses[] = {
                    127:        { CVS_RESP_OK,         "ok"                     },
                    128:        { CVS_RESP_ERROR,      "error"                  },
                    129:        { CVS_RESP_VALIDREQ,   "Valid-requests"         },
                    130:        { CVS_RESP_M,          "M"                      },
                    131:        { CVS_RESP_MBINARY,    "Mbinary"                },
                    132:        { CVS_RESP_MT,         "MT"                     },
                    133:        { CVS_RESP_E,          "E"                      },
                    134:        { CVS_RESP_F,          "F"                      },
                    135:        { CVS_RESP_CREATED,    "Created"                },
                    136:        { CVS_RESP_UPDATED,    "Updated"                },
                    137:        { CVS_RESP_UPDEXIST,   "Update-existing"        },
                    138:        { CVS_RESP_MERGED,     "Merged"                 },
                    139:        { CVS_RESP_REMOVED,    "Removed"                },
1.63      joris     140:        { CVS_RESP_RMENTRY,    "Remove-entry"           },
1.18      jfb       141:        { CVS_RESP_CKSUM,      "Checksum"               },
                    142:        { CVS_RESP_CLRSTATDIR, "Clear-static-directory" },
                    143:        { CVS_RESP_SETSTATDIR, "Set-static-directory"   },
                    144:        { CVS_RESP_NEWENTRY,   "New-entry"              },
                    145:        { CVS_RESP_CHECKEDIN,  "Checked-in"             },
                    146:        { CVS_RESP_MODE,       "Mode"                   },
                    147:        { CVS_RESP_MODTIME,    "Mod-time"               },
                    148:        { CVS_RESP_MODXPAND,   "Module-expansion"       },
                    149:        { CVS_RESP_SETSTICKY,  "Set-sticky"             },
                    150:        { CVS_RESP_CLRSTICKY,  "Clear-sticky"           },
                    151:        { CVS_RESP_RCSDIFF,    "Rcs-diff"               },
                    152:        { CVS_RESP_TEMPLATE,   "Template"               },
1.34      jfb       153:        { CVS_RESP_COPYFILE,   "Copy-file"              },
1.1       jfb       154: };
                    155:
1.68      xsa       156: #define CVS_NBREQ      (sizeof(cvs_requests)/sizeof(cvs_requests[0]))
                    157: #define CVS_NBRESP     (sizeof(cvs_responses)/sizeof(cvs_responses[0]))
1.17      jfb       158:
                    159: /* hack to receive the remote version without outputting it */
1.18      jfb       160: u_int cvs_version_sent = 0;
1.17      jfb       161:
1.13      jfb       162: static char  cvs_proto_buf[4096];
                    163:
                    164: /*
1.37      david     165:  * Output files for protocol logging when the CVS_CLIENT_LOG environment
1.13      jfb       166:  * variable is set.
                    167:  */
                    168: static int   cvs_server_logon = 0;
                    169: static FILE *cvs_server_inlog = NULL;
                    170: static FILE *cvs_server_outlog = NULL;
                    171:
1.28      jfb       172: static pid_t cvs_subproc_pid;
                    173:
1.32      jfb       174: /* last directory sent with cvs_senddir() */
                    175: static char cvs_lastdir[MAXPATHLEN] = "";
                    176:
1.13      jfb       177:
                    178: /*
                    179:  * cvs_connect()
                    180:  *
                    181:  * Open a client connection to the cvs server whose address is given in
                    182:  * the <root> variable.  The method used to connect depends on the
                    183:  * setting of the CVS_RSH variable.
1.17      jfb       184:  * Once the connection has been established, we first send the list of
                    185:  * responses we support and request the list of supported requests from the
                    186:  * server.  Then, a version request is sent and various global flags are sent.
1.13      jfb       187:  * 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");
                    316:                free(vresp);
                    317:                return (-1);
1.13      jfb       318:        }
                    319:        free(vresp);
                    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.1       jfb       462:        vrstr = (char *)malloc(len);
1.27      jfb       463:        if (vrstr == NULL) {
                    464:                cvs_buf_free(buf);
                    465:                return (NULL);
                    466:        }
1.1       jfb       467:
1.75      xsa       468:        cvs_buf_copy(buf, (size_t)0, vrstr, len);
1.1       jfb       469:        cvs_buf_free(buf);
                    470:
                    471:        return (vrstr);
                    472: }
                    473:
                    474:
                    475: /*
                    476:  * cvs_resp_getbyid()
                    477:  *
                    478:  */
1.13      jfb       479: struct cvs_resp*
1.1       jfb       480: cvs_resp_getbyid(int respid)
                    481: {
                    482:        u_int i;
                    483:
1.44      tedu      484:        for (i = 0; i < CVS_NBRESP; i++)
1.13      jfb       485:                if (cvs_responses[i].resp_id == (u_int)respid)
                    486:                        return &(cvs_responses[i]);
1.31      tedu      487:
1.1       jfb       488:        return (NULL);
                    489: }
                    490:
                    491:
                    492: /*
                    493:  * cvs_resp_getbyname()
                    494:  */
1.67      xsa       495: struct cvs_resp *
1.1       jfb       496: cvs_resp_getbyname(const char *rname)
                    497: {
                    498:        u_int i;
                    499:
1.44      tedu      500:        for (i = 0; i < CVS_NBRESP; i++)
1.1       jfb       501:                if (strcmp(cvs_responses[i].resp_str, rname) == 0)
1.13      jfb       502:                        return &(cvs_responses[i]);
1.1       jfb       503:
1.13      jfb       504:        return (NULL);
1.1       jfb       505: }
                    506:
                    507:
                    508: /*
                    509:  * cvs_resp_getvalid()
                    510:  *
                    511:  * Build a space-separated list of all the responses that this protocol
                    512:  * implementation supports.
                    513:  */
1.67      xsa       514: char *
1.1       jfb       515: cvs_resp_getvalid(void)
                    516: {
                    517:        u_int i;
                    518:        size_t len;
                    519:        char *vrstr;
                    520:        BUF *buf;
                    521:
1.75      xsa       522:        buf = cvs_buf_alloc((size_t)512, BUF_AUTOEXT);
1.1       jfb       523:        if (buf == NULL)
                    524:                return (NULL);
                    525:
                    526:        cvs_buf_set(buf, cvs_responses[0].resp_str,
1.75      xsa       527:            strlen(cvs_responses[0].resp_str), (size_t)0);
1.1       jfb       528:
                    529:        for (i = 1; i < CVS_NBRESP; i++) {
                    530:                if ((cvs_buf_putc(buf, ' ') < 0) ||
                    531:                    (cvs_buf_append(buf, cvs_responses[i].resp_str,
                    532:                    strlen(cvs_responses[i].resp_str)) < 0)) {
                    533:                        cvs_buf_free(buf);
                    534:                        return (NULL);
                    535:                }
                    536:        }
                    537:
                    538:        /* NUL-terminate */
                    539:        if (cvs_buf_putc(buf, '\0') < 0) {
                    540:                cvs_buf_free(buf);
                    541:                return (NULL);
                    542:        }
                    543:
1.70      moritz    544:        len = cvs_buf_len(buf);
1.1       jfb       545:        vrstr = (char *)malloc(len);
1.27      jfb       546:        if (vrstr == NULL) {
                    547:                cvs_buf_free(buf);
                    548:                return (NULL);
                    549:        }
1.1       jfb       550:
1.75      xsa       551:        cvs_buf_copy(buf, (size_t)0, vrstr, len);
1.1       jfb       552:        cvs_buf_free(buf);
                    553:
                    554:        return (vrstr);
                    555: }
                    556:
                    557:
                    558: /*
                    559:  * cvs_sendfile()
                    560:  *
                    561:  * Send the mode and size of a file followed by the file's contents.
                    562:  * Returns 0 on success, or -1 on failure.
                    563:  */
                    564: int
1.13      jfb       565: cvs_sendfile(struct cvsroot *root, const char *path)
1.1       jfb       566: {
1.51      xsa       567:        int fd, l;
1.1       jfb       568:        ssize_t ret;
                    569:        char buf[4096];
                    570:        struct stat st;
                    571:
1.38      jfb       572:        cvs_log(LP_TRACE, "Sending file `%s' to server", basename(path));
                    573:
1.1       jfb       574:        if (stat(path, &st) == -1) {
                    575:                cvs_log(LP_ERRNO, "failed to stat `%s'", path);
                    576:                return (-1);
                    577:        }
                    578:
1.24      jfb       579:        if (cvs_modetostr(st.st_mode, buf, sizeof(buf)) < 0)
                    580:                return (-1);
                    581:
1.1       jfb       582:        fd = open(path, O_RDONLY, 0);
                    583:        if (fd == -1) {
1.38      jfb       584:                cvs_log(LP_ERRNO, "failed to open `%s'", path);
1.1       jfb       585:                return (-1);
                    586:        }
                    587:
1.38      jfb       588:        if (cvs_sendln(root, buf) < 0) {
                    589:                (void)close(fd);
                    590:                return (-1);
                    591:        }
1.51      xsa       592:        l = snprintf(buf, sizeof(buf), "%lld\n", st.st_size);
                    593:        if (l == -1 || l >= (int)sizeof(buf))
                    594:                return (-1);
                    595:
1.38      jfb       596:        if (cvs_sendln(root, buf) < 0) {
                    597:                (void)close(fd);
                    598:                return (-1);
                    599:        }
1.1       jfb       600:
                    601:        while ((ret = read(fd, buf, sizeof(buf))) != 0) {
                    602:                if (ret == -1) {
1.24      jfb       603:                        (void)close(fd);
1.1       jfb       604:                        cvs_log(LP_ERRNO, "failed to read file `%s'", path);
                    605:                        return (-1);
                    606:                }
                    607:
1.38      jfb       608:                if (cvs_sendraw(root, buf, (size_t)ret) < 0) {
                    609:                        (void)close(fd);
                    610:                        return (-1);
                    611:                }
1.1       jfb       612:        }
                    613:
                    614:        (void)close(fd);
                    615:        return (0);
                    616: }
                    617:
                    618:
                    619: /*
                    620:  * cvs_recvfile()
                    621:  *
                    622:  * Receive the mode and size of a file followed the file's contents and
                    623:  * create or update the file whose path is <path> with the received
                    624:  * information.
                    625:  */
1.14      jfb       626: BUF*
                    627: cvs_recvfile(struct cvsroot *root, mode_t *mode)
1.1       jfb       628: {
                    629:        size_t len;
                    630:        ssize_t ret;
                    631:        off_t fsz, cnt;
                    632:        char buf[4096], *ep;
1.14      jfb       633:        BUF *fbuf;
                    634:
                    635:        fbuf = cvs_buf_alloc(sizeof(buf), BUF_AUTOEXT);
                    636:        if (fbuf == NULL)
                    637:                return (NULL);
1.1       jfb       638:
1.13      jfb       639:        if ((cvs_getln(root, buf, sizeof(buf)) < 0) ||
1.14      jfb       640:            (cvs_strtomode(buf, mode) < 0)) {
1.44      tedu      641:                cvs_buf_free(fbuf);
1.14      jfb       642:                return (NULL);
1.1       jfb       643:        }
                    644:
1.50      joris     645:        if (cvs_getln(root, buf, sizeof(buf)) < 0) {
                    646:                cvs_buf_free(fbuf);
                    647:                return (NULL);
                    648:        }
1.1       jfb       649:
                    650:        fsz = (off_t)strtol(buf, &ep, 10);
                    651:        if (*ep != '\0') {
                    652:                cvs_log(LP_ERR, "parse error in file size transmission");
1.44      tedu      653:                cvs_buf_free(fbuf);
1.14      jfb       654:                return (NULL);
1.1       jfb       655:        }
                    656:
                    657:        cnt = 0;
                    658:        do {
                    659:                len = MIN(sizeof(buf), (size_t)(fsz - cnt));
1.6       jfb       660:                if (len == 0)
                    661:                        break;
1.13      jfb       662:                ret = cvs_recvraw(root, buf, len);
1.1       jfb       663:                if (ret == -1) {
1.14      jfb       664:                        cvs_buf_free(fbuf);
                    665:                        return (NULL);
1.1       jfb       666:                }
                    667:
1.14      jfb       668:                if (cvs_buf_append(fbuf, buf, (size_t)ret) == -1) {
                    669:                        cvs_log(LP_ERR,
                    670:                            "failed to append received file data");
                    671:                        cvs_buf_free(fbuf);
                    672:                        return (NULL);
1.1       jfb       673:                }
                    674:
                    675:                cnt += (off_t)ret;
                    676:        } while (cnt < fsz);
                    677:
1.14      jfb       678:        return (fbuf);
1.1       jfb       679: }
1.12      jfb       680:
                    681: /*
                    682:  * cvs_sendreq()
                    683:  *
                    684:  * Send a request to the server of type <rid>, with optional arguments
                    685:  * contained in <arg>, which should not be terminated by a newline.
                    686:  * Returns 0 on success, or -1 on failure.
                    687:  */
                    688: int
1.13      jfb       689: cvs_sendreq(struct cvsroot *root, u_int rid, const char *arg)
1.12      jfb       690: {
1.51      xsa       691:        int ret, l;
1.13      jfb       692:        struct cvs_req *req;
1.12      jfb       693:
                    694:        if (root->cr_srvin == NULL) {
1.36      jfb       695:                cvs_log(LP_ERR, "cannot send request %u: Not connected", rid);
1.12      jfb       696:                return (-1);
                    697:        }
                    698:
1.13      jfb       699:        req = cvs_req_getbyid(rid);
                    700:        if (req == NULL) {
1.12      jfb       701:                cvs_log(LP_ERR, "unsupported request type %u", rid);
                    702:                return (-1);
                    703:        }
                    704:
1.18      jfb       705:        /* is this request supported by the server? */
                    706:        if (!CVS_GETVR(root, req->req_id)) {
1.57      joris     707:                if (rid == CVS_REQ_VERSION) {
                    708:                        ret = cvs_sendreq(root, CVS_REQ_NOOP, arg);
                    709:                } else {
                    710:                        cvs_log(LP_WARN,
                    711:                            "remote end does not support request `%s'",
                    712:                            req->req_str);
                    713:                        ret = -1;
                    714:                }
                    715:                return (ret);
1.18      jfb       716:        }
                    717:
1.51      xsa       718:        l = snprintf(cvs_proto_buf, sizeof(cvs_proto_buf), "%s%s%s\n",
1.13      jfb       719:            req->req_str, (arg == NULL) ? "" : " ", (arg == NULL) ? "" : arg);
1.51      xsa       720:        if (l == -1 || l >= (int)sizeof(cvs_proto_buf))
                    721:                return (-1);
1.12      jfb       722:
                    723:        if (cvs_server_inlog != NULL)
1.13      jfb       724:                fputs(cvs_proto_buf, cvs_server_inlog);
1.12      jfb       725:
1.13      jfb       726:        ret = fputs(cvs_proto_buf, root->cr_srvin);
1.12      jfb       727:        if (ret == EOF) {
                    728:                cvs_log(LP_ERRNO, "failed to send request to server");
                    729:                return (-1);
                    730:        }
1.17      jfb       731:
                    732:        if (rid == CVS_REQ_VERSION)
                    733:                cvs_version_sent = 1;
1.12      jfb       734:
1.13      jfb       735:        if (req->req_flags & CVS_REQF_RESP)
1.12      jfb       736:                ret = cvs_getresp(root);
                    737:
1.13      jfb       738:        return (ret);
1.12      jfb       739: }
                    740:
                    741:
                    742: /*
                    743:  * cvs_getresp()
                    744:  *
                    745:  * Get a response from the server.  This call will actually read and handle
                    746:  * responses from the server until one of the response handlers returns
1.37      david     747:  * non-zero (either an error occurred or the end of the response was reached).
1.12      jfb       748:  * Returns the number of handled commands on success, or -1 on failure.
                    749:  */
                    750: int
1.13      jfb       751: cvs_getresp(struct cvsroot *root)
1.12      jfb       752: {
1.13      jfb       753:        int nbcmd, ret;
                    754:        size_t len;
1.12      jfb       755:
                    756:        nbcmd = 0;
                    757:
                    758:        do {
                    759:                /* wait for incoming data */
1.74      xsa       760:                if (fgets(cvs_proto_buf, (int)sizeof(cvs_proto_buf),
1.12      jfb       761:                    root->cr_srvout) == NULL) {
                    762:                        if (feof(root->cr_srvout))
                    763:                                return (0);
                    764:                        cvs_log(LP_ERRNO,
                    765:                            "failed to read response from server");
                    766:                        return (-1);
                    767:                }
                    768:
                    769:                if (cvs_server_outlog != NULL)
1.13      jfb       770:                        fputs(cvs_proto_buf, cvs_server_outlog);
1.12      jfb       771:
1.13      jfb       772:                if ((len = strlen(cvs_proto_buf)) != 0) {
1.42      joris     773:                        /* if len - 1 != '\n' the line is truncated */
1.43      joris     774:                        if (cvs_proto_buf[len - 1] == '\n')
1.13      jfb       775:                                cvs_proto_buf[--len] = '\0';
1.12      jfb       776:                }
                    777:
1.13      jfb       778:                ret = cvs_resp_handle(root, cvs_proto_buf);
1.12      jfb       779:                nbcmd++;
                    780:        } while (ret == 0);
                    781:
                    782:        if (ret > 0)
                    783:                ret = nbcmd;
                    784:        return (ret);
                    785: }
                    786:
                    787:
                    788: /*
1.13      jfb       789:  * cvs_getln()
                    790:  *
1.18      jfb       791:  * Get a line from the remote end and store it in <lbuf>.  The terminating
1.13      jfb       792:  * newline character is stripped from the result.
1.55      jfb       793:  * Returns the length in bytes of the line (not including the NUL byte), or
                    794:  * -1 on failure.
1.13      jfb       795:  */
                    796: int
                    797: cvs_getln(struct cvsroot *root, char *lbuf, size_t len)
                    798: {
                    799:        size_t rlen;
1.18      jfb       800:        FILE *in;
1.13      jfb       801:
1.18      jfb       802:        if (cvs_cmdop == CVS_OP_SERVER)
                    803:                in = stdin;
                    804:        else
                    805:                in = root->cr_srvout;
                    806:
1.74      xsa       807:        if (fgets(lbuf, (int)len, in) == NULL) {
1.18      jfb       808:                if (ferror(in)) {
                    809:                        cvs_log(LP_ERRNO, "failed to read line");
1.13      jfb       810:                        return (-1);
                    811:                }
                    812:
1.18      jfb       813:                if (feof(in))
1.13      jfb       814:                        *lbuf = '\0';
                    815:        }
                    816:
                    817:        if (cvs_server_outlog != NULL)
                    818:                fputs(lbuf, cvs_server_outlog);
                    819:
                    820:        rlen = strlen(lbuf);
                    821:        if ((rlen > 0) && (lbuf[rlen - 1] == '\n'))
                    822:                lbuf[--rlen] = '\0';
                    823:
1.55      jfb       824:        return (rlen);
1.13      jfb       825: }
                    826:
1.19      jfb       827:
1.13      jfb       828: /*
1.12      jfb       829:  * cvs_sendresp()
                    830:  *
1.19      jfb       831:  * Send a response of type <rid> to the client, with optional arguments
1.12      jfb       832:  * contained in <arg>, which should not be terminated by a newline.
                    833:  * Returns 0 on success, or -1 on failure.
                    834:  */
                    835: int
                    836: cvs_sendresp(u_int rid, const char *arg)
                    837: {
                    838:        int ret;
1.19      jfb       839:        struct cvs_resp *resp;
1.12      jfb       840:
                    841:        resp = cvs_resp_getbyid(rid);
1.13      jfb       842:        if (resp == NULL) {
1.12      jfb       843:                cvs_log(LP_ERR, "unsupported response type %u", rid);
                    844:                return (-1);
                    845:        }
                    846:
1.19      jfb       847:        ret = fputs(resp->resp_str, stdout);
1.12      jfb       848:        if (ret == EOF) {
                    849:                cvs_log(LP_ERRNO, "failed to send response to client");
1.30      deraadt   850:        } else {
1.19      jfb       851:                if (arg != NULL) {
                    852:                        putc(' ', stdout);
                    853:                        fputs(arg, stdout);
                    854:                }
1.12      jfb       855:                putc('\n', stdout);
                    856:        }
                    857:        return (0);
                    858: }
                    859:
                    860:
1.19      jfb       861: #ifdef notyet
1.12      jfb       862: /*
                    863:  * cvs_getreq()
                    864:  *
                    865:  * Get a request from the client.
                    866:  */
                    867: int
                    868: cvs_getreq(void)
                    869: {
                    870:        int nbcmd;
                    871:
                    872:        nbcmd = 0;
                    873:
                    874:        do {
                    875:                /* wait for incoming data */
1.74      xsa       876:                if (fgets(cvs_proto_buf, (int)sizeof(cvs_proto_buf),
1.12      jfb       877:                    stdin) == NULL) {
                    878:                        if (feof(stdin))
                    879:                                return (0);
                    880:                        cvs_log(LP_ERRNO,
                    881:                            "failed to read request from client");
                    882:                        return (-1);
                    883:                }
                    884:
1.13      jfb       885:                if ((len = strlen(cvs_proto_buf)) != 0) {
                    886:                        if (cvs_proto_buf[len - 1] != '\n') {
1.12      jfb       887:                                /* truncated line */
1.30      deraadt   888:                        } else
1.13      jfb       889:                                cvs_proto_buf[--len] = '\0';
1.12      jfb       890:                }
                    891:
1.13      jfb       892:                ret = cvs_resp_handle(cvs_proto_buf);
1.12      jfb       893:        } while (ret == 0);
                    894: }
                    895: #endif
                    896:
                    897:
                    898: /*
                    899:  * cvs_sendln()
                    900:  *
1.18      jfb       901:  * Send a single line <line> string to the remote end.  The line is sent as is,
1.12      jfb       902:  * without any modifications.
                    903:  * Returns 0 on success, or -1 on failure.
                    904:  */
                    905: int
                    906: cvs_sendln(struct cvsroot *root, const char *line)
                    907: {
                    908:        int nl;
                    909:        size_t len;
1.18      jfb       910:        FILE *out;
                    911:
                    912:        if (cvs_cmdop == CVS_OP_SERVER)
                    913:                out = stdout;
                    914:        else
                    915:                out = root->cr_srvin;
1.12      jfb       916:
                    917:        nl = 0;
                    918:        len = strlen(line);
                    919:
                    920:        if ((len > 0) && (line[len - 1] != '\n'))
                    921:                nl = 1;
                    922:
                    923:        if (cvs_server_inlog != NULL) {
                    924:                fputs(line, cvs_server_inlog);
                    925:                if (nl)
1.55      jfb       926:                        putc('\n', cvs_server_inlog);
1.12      jfb       927:        }
1.18      jfb       928:        fputs(line, out);
1.12      jfb       929:        if (nl)
1.55      jfb       930:                putc('\n', out);
1.12      jfb       931:        return (0);
                    932: }
                    933:
                    934:
                    935: /*
1.13      jfb       936:  * cvs_sendraw()
1.12      jfb       937:  *
                    938:  * Send the first <len> bytes from the buffer <src> to the server.
                    939:  */
                    940: int
1.13      jfb       941: cvs_sendraw(struct cvsroot *root, const void *src, size_t len)
1.12      jfb       942: {
1.18      jfb       943:        FILE *out;
                    944:
                    945:        if (cvs_cmdop == CVS_OP_SERVER)
                    946:                out = stdout;
                    947:        else
                    948:                out = root->cr_srvin;
                    949:
1.12      jfb       950:        if (cvs_server_inlog != NULL)
                    951:                fwrite(src, sizeof(char), len, cvs_server_inlog);
1.18      jfb       952:        if (fwrite(src, sizeof(char), len, out) < len) {
1.38      jfb       953:                cvs_log(LP_ERR, "failed to send data");
1.12      jfb       954:                return (-1);
                    955:        }
                    956:
                    957:        return (0);
                    958: }
                    959:
                    960:
                    961: /*
1.13      jfb       962:  * cvs_recvraw()
1.12      jfb       963:  *
                    964:  * Receive the first <len> bytes from the buffer <src> to the server.
                    965:  */
                    966: ssize_t
1.13      jfb       967: cvs_recvraw(struct cvsroot *root, void *dst, size_t len)
1.12      jfb       968: {
                    969:        size_t ret;
1.18      jfb       970:        FILE *in;
                    971:
                    972:        if (cvs_cmdop == CVS_OP_SERVER)
                    973:                in = stdin;
                    974:        else
                    975:                in = root->cr_srvout;
1.12      jfb       976:
1.18      jfb       977:        ret = fread(dst, sizeof(char), len, in);
1.12      jfb       978:        if (ret == 0)
                    979:                return (-1);
                    980:        if (cvs_server_outlog != NULL)
                    981:                fwrite(dst, sizeof(char), len, cvs_server_outlog);
                    982:        return (ssize_t)ret;
                    983: }
                    984:
                    985:
                    986: /*
1.13      jfb       987:  * cvs_senddir()
1.12      jfb       988:  *
1.32      jfb       989:  * Send a `Directory' request along with the 2 paths that follow it.  If
                    990:  * the directory info to be sent is the same as the last info sent, the
                    991:  * call does nothing and simply returns without an error.
1.12      jfb       992:  */
                    993: int
1.31      tedu      994: cvs_senddir(struct cvsroot *root, CVSFILE *dir)
1.12      jfb       995: {
1.60      xsa       996:        size_t len;
1.28      jfb       997:        char lbuf[MAXPATHLEN], rbuf[MAXPATHLEN];
1.12      jfb       998:
1.47      jfb       999:        if (dir->cf_type != DT_DIR)
                   1000:                return (-1);
                   1001:
1.32      jfb      1002:        cvs_file_getpath(dir, lbuf, sizeof(lbuf));
1.45      joris    1003:        if (strcmp(lbuf, cvs_lastdir) == 0 && cvs_cmdop != CVS_OP_CHECKOUT)
1.32      jfb      1004:                return (0);
                   1005:
1.47      jfb      1006:        if (dir->cf_repo == NULL)
1.28      jfb      1007:                strlcpy(rbuf, root->cr_dir, sizeof(rbuf));
1.51      xsa      1008:        else {
1.60      xsa      1009:                len = cvs_path_cat(root->cr_dir, dir->cf_repo, rbuf,
                   1010:                    sizeof(rbuf));
                   1011:                if (len >= sizeof(rbuf))
1.51      xsa      1012:                        return (-1);
                   1013:        }
1.12      jfb      1014:
1.28      jfb      1015:
                   1016:        if ((cvs_sendreq(root, CVS_REQ_DIRECTORY, lbuf) < 0) ||
                   1017:            (cvs_sendln(root, rbuf) < 0))
1.12      jfb      1018:                return (-1);
1.32      jfb      1019:
                   1020:        strlcpy(cvs_lastdir, lbuf, sizeof(cvs_lastdir));
1.12      jfb      1021:
                   1022:        return (0);
                   1023: }
                   1024:
                   1025:
                   1026: /*
1.13      jfb      1027:  * cvs_sendarg()
1.12      jfb      1028:  *
                   1029:  * Send the argument <arg> to the server.  The argument <append> is used to
                   1030:  * determine if the argument should be simply appended to the last argument
                   1031:  * sent or if it should be created as a new argument (0).
                   1032:  */
                   1033: int
1.13      jfb      1034: cvs_sendarg(struct cvsroot *root, const char *arg, int append)
1.12      jfb      1035: {
1.13      jfb      1036:        return cvs_sendreq(root, ((append == 0) ?
                   1037:            CVS_REQ_ARGUMENT : CVS_REQ_ARGUMENTX), arg);
1.12      jfb      1038: }
                   1039:
                   1040:
                   1041: /*
1.13      jfb      1042:  * cvs_sendentry()
1.12      jfb      1043:  *
                   1044:  * Send an `Entry' request to the server along with the mandatory fields from
                   1045:  * the CVS entry <ent> (which are the name and revision).
                   1046:  */
                   1047: int
1.47      jfb      1048: cvs_sendentry(struct cvsroot *root, const CVSFILE *file)
1.12      jfb      1049: {
1.51      xsa      1050:        int l;
1.77      xsa      1051:        char ebuf[CVS_ENT_MAXLINELEN], numbuf[64];
1.12      jfb      1052:
1.47      jfb      1053:        if (file->cf_type != DT_REG) {
                   1054:                cvs_log(LP_ERR, "attempt to send Entry for non-regular file");
                   1055:                return (-1);
                   1056:        }
1.48      joris    1057:
                   1058:        /* don't send Entry for unknown files */
                   1059:        if (file->cf_cvstat == CVS_FST_UNKNOWN)
                   1060:                return (0);
1.47      jfb      1061:
1.51      xsa      1062:        l = snprintf(ebuf, sizeof(ebuf), "/%s/%s%s///", file->cf_name,
1.49      joris    1063:            (file->cf_cvstat == CVS_FST_REMOVED) ? "-" : "",
1.47      jfb      1064:            rcsnum_tostr(file->cf_lrev, numbuf, sizeof(numbuf)));
1.51      xsa      1065:        if (l == -1 || l >= (int)sizeof(ebuf)) {
                   1066:                errno = ENAMETOOLONG;
                   1067:                cvs_log(LP_ERRNO, "%s", ebuf);
                   1068:                return (-1);
                   1069:        }
1.12      jfb      1070:
1.13      jfb      1071:        return cvs_sendreq(root, CVS_REQ_ENTRY, ebuf);
1.12      jfb      1072: }
                   1073:
                   1074:
                   1075: /*
1.13      jfb      1076:  * cvs_initlog()
1.12      jfb      1077:  *
                   1078:  * Initialize protocol logging if the CVS_CLIENT_LOG environment variable is
                   1079:  * set.  In this case, the variable's value is used as a path to which the
                   1080:  * appropriate suffix is added (".in" for server input and ".out" for server
                   1081:  * output.
                   1082:  * Returns 0 on success, or -1 on failure.
                   1083:  */
                   1084: static int
1.13      jfb      1085: cvs_initlog(void)
1.12      jfb      1086: {
1.52      joris    1087:        int l;
                   1088:        u_int i;
                   1089:        char *env, *envdup, buf[MAXPATHLEN], fpath[MAXPATHLEN];
                   1090:        char rpath[MAXPATHLEN], *s;
                   1091:        struct stat st;
                   1092:        time_t now;
                   1093:        struct passwd *pwd;
1.12      jfb      1094:
1.13      jfb      1095:        /* avoid doing it more than once */
                   1096:        if (cvs_server_logon)
                   1097:                return (0);
                   1098:
1.12      jfb      1099:        env = getenv("CVS_CLIENT_LOG");
                   1100:        if (env == NULL)
                   1101:                return (0);
                   1102:
1.52      joris    1103:        if ((envdup = strdup(env)) == NULL)
                   1104:                return (-1);
                   1105:
                   1106:        if ((s = strchr(envdup, '%')) != NULL)
                   1107:                *s = '\0';
                   1108:
                   1109:        strlcpy(buf, env, sizeof(buf));
                   1110:        strlcpy(rpath, envdup, sizeof(rpath));
1.69      moritz   1111:        free(envdup);
1.52      joris    1112:
                   1113:        s = buf;
                   1114:        while ((s = strchr(s, '%')) != NULL) {
                   1115:                *s++;
                   1116:                switch (*s) {
                   1117:                case 'c':
                   1118:                        strlcpy(fpath, cvs_command, sizeof(fpath));
                   1119:                        break;
                   1120:                case 'd':
                   1121:                        time(&now);
1.59      niallo   1122:                        strlcpy(fpath, ctime(&now), sizeof(fpath));
1.52      joris    1123:                        break;
                   1124:                case 'p':
                   1125:                        snprintf(fpath, sizeof(fpath), "%d", getpid());
                   1126:                        break;
                   1127:                case 'u':
                   1128:                        if ((pwd = getpwuid(getuid())) != NULL)
                   1129:                                strlcpy(fpath, pwd->pw_name, sizeof(fpath));
                   1130:                        else
                   1131:                                fpath[0] = '\0';
                   1132:                        endpwent();
                   1133:                        break;
                   1134:                default:
                   1135:                        fpath[0] = '\0';
                   1136:                        break;
1.58      joris    1137:                }
1.52      joris    1138:
                   1139:                if (fpath[0] != '\0') {
                   1140:                        strlcat(rpath, "-", sizeof(rpath));
                   1141:                        strlcat(rpath, fpath, sizeof(rpath));
                   1142:                }
                   1143:        }
                   1144:
                   1145:        for (i = 0; i < UINT_MAX; i++) {
                   1146:                l = snprintf(fpath, sizeof(fpath), "%s-%d.in", rpath, i);
                   1147:                if (l == -1 || l >= (int)sizeof(fpath)) {
                   1148:                        errno = ENAMETOOLONG;
                   1149:                        cvs_log(LP_ERRNO, "%s", fpath);
                   1150:                        return (-1);
                   1151:                }
                   1152:
                   1153:                if (stat(fpath, &st) != -1)
                   1154:                        continue;
                   1155:
                   1156:                if (errno != ENOENT)
                   1157:                        return (-1);
                   1158:
                   1159:                break;
                   1160:        }
                   1161:
1.12      jfb      1162:        cvs_server_inlog = fopen(fpath, "w");
                   1163:        if (cvs_server_inlog == NULL) {
                   1164:                cvs_log(LP_ERRNO, "failed to open server input log `%s'",
                   1165:                    fpath);
                   1166:                return (-1);
                   1167:        }
                   1168:
1.52      joris    1169:        for (i = 0; i < UINT_MAX; i++) {
                   1170:                l = snprintf(fpath, sizeof(fpath), "%s-%d.out", rpath, i);
                   1171:                if (l == -1 || l >= (int)sizeof(fpath)) {
                   1172:                        errno = ENAMETOOLONG;
                   1173:                        cvs_log(LP_ERRNO, "%s", fpath);
                   1174:                        return (-1);
                   1175:                }
                   1176:
                   1177:                if (stat(fpath, &st) != -1)
                   1178:                        continue;
                   1179:
                   1180:                if (errno != ENOENT)
                   1181:                        return (-1);
                   1182:
                   1183:                break;
                   1184:        }
                   1185:
1.12      jfb      1186:        cvs_server_outlog = fopen(fpath, "w");
                   1187:        if (cvs_server_outlog == NULL) {
                   1188:                cvs_log(LP_ERRNO, "failed to open server output log `%s'",
                   1189:                    fpath);
                   1190:                return (-1);
                   1191:        }
                   1192:
                   1193:        /* make the streams line-buffered */
1.75      xsa      1194:        setvbuf(cvs_server_inlog, NULL, _IOLBF, (size_t)0);
                   1195:        setvbuf(cvs_server_outlog, NULL, _IOLBF, (size_t)0);
1.12      jfb      1196:
1.13      jfb      1197:        cvs_server_logon = 1;
1.12      jfb      1198:
                   1199:        return (0);
                   1200: }