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

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