[BACK]Return to sock.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cvs

Annotation of src/usr.bin/cvs/sock.c, Revision 1.7

1.1       jfb         1: /*     $OpenBSD$       */
                      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: #include <sys/types.h>
                     28: #include <sys/socket.h>
                     29: #include <sys/un.h>
                     30:
                     31: #include <poll.h>
                     32: #include <stdlib.h>
                     33: #include <stdio.h>
                     34: #include <unistd.h>
                     35: #include <errno.h>
                     36: #include <string.h>
                     37:
                     38: #include "log.h"
                     39: #include "sock.h"
                     40: #include "cvsd.h"
                     41: #include "event.h"
                     42:
                     43:
                     44: volatile sig_atomic_t  cvs_sock_doloop;
                     45:
                     46:
                     47: char     *cvsd_sock_path = CVSD_SOCK_PATH;
                     48:
                     49:
                     50:
                     51: /* daemon API */
1.4       jfb        52: #ifdef CVSD
1.2       jfb        53: int cvsd_sock = -1;
1.1       jfb        54: static struct sockaddr_un cvsd_sun;
1.4       jfb        55: #endif
1.1       jfb        56:
                     57: /* for client API */
1.4       jfb        58: #ifdef CVS
1.1       jfb        59: static int cvs_sock = -1;
                     60: static struct sockaddr_un cvs_sun;
1.4       jfb        61: #endif
1.1       jfb        62:
                     63:
1.3       jfb        64: #ifdef CVSD
1.1       jfb        65: /*
                     66:  * cvsd_sock_open()
                     67:  *
1.2       jfb        68:  * Open the daemon's local socket.  If the server socket is already opened,
                     69:  * we close it before reopening it.
                     70:  * Returns 0 on success, -1 on failure.
1.1       jfb        71:  */
                     72:
                     73: int
                     74: cvsd_sock_open(void)
                     75: {
1.4       jfb        76:        if (cvsd_sock >= 0)
1.2       jfb        77:                cvsd_sock_close();
                     78:
1.1       jfb        79:        cvsd_sun.sun_family = AF_LOCAL;
                     80:        strlcpy(cvsd_sun.sun_path, cvsd_sock_path, sizeof(cvsd_sun.sun_path));
                     81:
                     82:        cvsd_sock = socket(AF_LOCAL, SOCK_STREAM, 0);
                     83:        if (cvsd_sock == -1) {
                     84:                cvs_log(LP_ERRNO, "failed to open socket");
                     85:                return (-1);
                     86:        }
                     87:
                     88:        if (bind(cvsd_sock, (struct sockaddr *)&cvsd_sun,
                     89:            SUN_LEN(&cvsd_sun)) == -1) {
                     90:                cvs_log(LP_ERRNO, "failed to bind local socket to `%s'",
                     91:                    cvsd_sock_path);
                     92:                (void)close(cvsd_sock);
                     93:                return (-1);
                     94:        }
                     95:
1.5       jfb        96:        (void)listen(cvsd_sock, 10);
                     97:
                     98:        if (chown(cvsd_sock_path, getuid(), cvsd_gid) == -1) {
                     99:                cvs_log(LP_ERRNO, "failed to change owner of `%s'",
                    100:                    cvsd_sock_path);
                    101:                (void)close(cvsd_sock);
                    102:                (void)unlink(cvsd_sock_path);
                    103:                return (-1);
                    104:        }
1.1       jfb       105:
                    106:        if (chmod(cvsd_sock_path, CVSD_SOCK_PERMS) == -1) {
                    107:                cvs_log(LP_ERRNO, "failed to change mode of `%s'",
                    108:                    cvsd_sock_path);
                    109:                (void)close(cvsd_sock);
                    110:                (void)unlink(cvsd_sock_path);
                    111:                return (-1);
                    112:        }
                    113:
                    114:        cvs_log(LP_DEBUG, "opened local socket `%s'", cvsd_sock_path);
                    115:
                    116:        return (0);
                    117: }
                    118:
                    119:
                    120: /*
                    121:  * cvsd_sock_close()
                    122:  *
                    123:  * Close the local socket.
                    124:  */
                    125:
                    126: void
                    127: cvsd_sock_close(void)
                    128: {
                    129:        cvs_log(LP_DEBUG, "closing local socket `%s'", CVSD_SOCK_PATH);
                    130:        if (close(cvsd_sock) == -1) {
                    131:                cvs_log(LP_ERRNO, "failed to close local socket");
                    132:        }
1.6       jfb       133:        if (seteuid(0) == -1)
                    134:                cvs_log(LP_ERRNO, "failed to regain privileges");
                    135:        else if (unlink(cvsd_sock_path) == -1)
1.1       jfb       136:                cvs_log(LP_ERRNO, "failed to unlink local socket `%s'",
1.2       jfb       137:                    cvsd_sock_path);
1.1       jfb       138: }
                    139:
                    140:
                    141: /*
1.2       jfb       142:  * cvsd_sock_accept()
1.1       jfb       143:  *
1.2       jfb       144:  * Handler for connections made on the server's local domain socket.
                    145:  * It accepts connections and looks for a child process that is currently
                    146:  * idle to which it can dispatch the connection's descriptor.  If there are
                    147:  * no available child processes, a new one will be created unless the number
                    148:  * of children has attained the maximum.
1.1       jfb       149:  */
                    150:
1.2       jfb       151: int
                    152: cvsd_sock_accept(int fd)
1.1       jfb       153: {
1.2       jfb       154:        int cfd;
1.1       jfb       155:        socklen_t slen;
                    156:        struct sockaddr_un sun;
                    157:
1.2       jfb       158:        slen = sizeof(sun);
                    159:        cfd = accept(fd, (struct sockaddr *)&sun, &slen);
                    160:        if (cfd == -1) {
                    161:                cvs_log(LP_ERRNO, "failed to accept client connection");
1.1       jfb       162:                return (-1);
                    163:        }
                    164:
1.7     ! krapht    165:        return (cfd);
1.1       jfb       166: }
1.3       jfb       167: #endif
1.1       jfb       168:
1.3       jfb       169: #ifdef CVS
1.1       jfb       170: /*
                    171:  * cvs_sock_connect()
                    172:  *
                    173:  * Open a connection to the CVS server's local socket.
                    174:  */
                    175:
                    176: int
1.3       jfb       177: cvs_sock_connect(const char *path)
1.1       jfb       178: {
                    179:        cvs_sun.sun_family = AF_LOCAL;
1.3       jfb       180:        strlcpy(cvs_sun.sun_path, path, sizeof(cvs_sun.sun_path));
1.1       jfb       181:
                    182:        cvs_log(LP_INFO, "connecting to CVS server socket `%s'",
                    183:            cvs_sun.sun_path);
                    184:
                    185:        cvs_sock = socket(AF_LOCAL, SOCK_STREAM, 0);
                    186:        if (cvs_sock == -1) {
                    187:                cvs_log(LP_ERRNO, "failed to open local socket");
                    188:                return (-1);
                    189:        }
                    190:
                    191:        if (connect(cvs_sock, (struct sockaddr *)&cvs_sun,
                    192:            SUN_LEN(&cvs_sun)) == -1) {
                    193:                cvs_log(LP_ERRNO, "failed to connect to server socket `%s'",
                    194:                    cvs_sun.sun_path);
                    195:                (void)close(cvs_sock);
                    196:                return (-1);
                    197:        }
                    198:
                    199:        return (0);
                    200: }
                    201:
                    202:
                    203: /*
                    204:  * cvs_sock_disconnect()
                    205:  *
                    206:  * Disconnect from the open socket to the CVS server.
                    207:  */
                    208:
                    209: void
                    210: cvs_sock_disconnect(void)
                    211: {
                    212:        if (close(cvs_sock) == -1)
                    213:                cvs_log(LP_ERRNO, "failed to close local socket");
                    214: }
1.3       jfb       215: #endif