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

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