Annotation of src/usr.bin/cvs/sock.c, Revision 1.12
1.12 ! jfb 1: /* $OpenBSD: sock.c,v 1.11 2005/02/15 20:14:49 jfb Exp $ */
1.1 jfb 2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.8 tedu 4: * All rights reserved.
1.1 jfb 5: *
1.8 tedu 6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
1.1 jfb 9: *
1.8 tedu 10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
1.1 jfb 12: * 2. The name of the author may not be used to endorse or promote products
1.8 tedu 13: * derived from this software without specific prior written permission.
1.1 jfb 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
1.8 tedu 24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 jfb 25: */
26:
27: #include <sys/types.h>
28: #include <sys/socket.h>
29: #include <sys/un.h>
30:
31: #include <poll.h>
1.11 jfb 32: #include <errno.h>
1.1 jfb 33: #include <stdio.h>
1.11 jfb 34: #include <fcntl.h>
1.1 jfb 35: #include <unistd.h>
36: #include <string.h>
1.11 jfb 37: #include <stdlib.h>
1.1 jfb 38:
39: #include "log.h"
40: #include "sock.h"
41: #include "cvsd.h"
42:
43:
1.10 jfb 44: char *cvsd_sock_path;
1.1 jfb 45:
46: /* daemon API */
1.4 jfb 47: #ifdef CVSD
1.2 jfb 48: int cvsd_sock = -1;
1.1 jfb 49: static struct sockaddr_un cvsd_sun;
1.4 jfb 50: #endif
1.1 jfb 51:
52: /* for client API */
1.4 jfb 53: #ifdef CVS
1.1 jfb 54: static int cvs_sock = -1;
55: static struct sockaddr_un cvs_sun;
1.4 jfb 56: #endif
1.1 jfb 57:
58:
1.3 jfb 59: #ifdef CVSD
1.1 jfb 60: /*
61: * cvsd_sock_open()
62: *
1.2 jfb 63: * Open the daemon's local socket. If the server socket is already opened,
64: * we close it before reopening it.
65: * Returns 0 on success, -1 on failure.
1.1 jfb 66: */
67: int
68: cvsd_sock_open(void)
69: {
1.4 jfb 70: if (cvsd_sock >= 0)
1.2 jfb 71: cvsd_sock_close();
72:
1.1 jfb 73: cvsd_sun.sun_family = AF_LOCAL;
74: strlcpy(cvsd_sun.sun_path, cvsd_sock_path, sizeof(cvsd_sun.sun_path));
75:
76: cvsd_sock = socket(AF_LOCAL, SOCK_STREAM, 0);
77: if (cvsd_sock == -1) {
1.8 tedu 78: cvs_log(LP_ERRNO, "failed to open socket");
1.1 jfb 79: return (-1);
80: }
81:
82: if (bind(cvsd_sock, (struct sockaddr *)&cvsd_sun,
83: SUN_LEN(&cvsd_sun)) == -1) {
84: cvs_log(LP_ERRNO, "failed to bind local socket to `%s'",
85: cvsd_sock_path);
86: (void)close(cvsd_sock);
87: return (-1);
88: }
89:
1.5 jfb 90: (void)listen(cvsd_sock, 10);
91:
92: if (chown(cvsd_sock_path, getuid(), cvsd_gid) == -1) {
93: cvs_log(LP_ERRNO, "failed to change owner of `%s'",
94: cvsd_sock_path);
95: (void)close(cvsd_sock);
96: (void)unlink(cvsd_sock_path);
97: return (-1);
98: }
1.1 jfb 99:
100: if (chmod(cvsd_sock_path, CVSD_SOCK_PERMS) == -1) {
101: cvs_log(LP_ERRNO, "failed to change mode of `%s'",
102: cvsd_sock_path);
103: (void)close(cvsd_sock);
104: (void)unlink(cvsd_sock_path);
105: return (-1);
106: }
1.11 jfb 107:
108: /* close on exec so children can't muck around with this */
109: (void)fcntl(cvsd_sock, F_SETFD, FD_CLOEXEC);
1.1 jfb 110:
111: cvs_log(LP_DEBUG, "opened local socket `%s'", cvsd_sock_path);
112:
113: return (0);
114: }
115:
116:
117: /*
118: * cvsd_sock_close()
119: *
120: * Close the local socket.
121: */
122: void
123: cvsd_sock_close(void)
124: {
125: cvs_log(LP_DEBUG, "closing local socket `%s'", CVSD_SOCK_PATH);
126: if (close(cvsd_sock) == -1) {
127: cvs_log(LP_ERRNO, "failed to close local socket");
128: }
1.6 jfb 129: if (seteuid(0) == -1)
130: cvs_log(LP_ERRNO, "failed to regain privileges");
131: else if (unlink(cvsd_sock_path) == -1)
1.1 jfb 132: cvs_log(LP_ERRNO, "failed to unlink local socket `%s'",
1.2 jfb 133: cvsd_sock_path);
1.1 jfb 134: }
135:
136:
137: /*
1.2 jfb 138: * cvsd_sock_accept()
1.1 jfb 139: *
1.2 jfb 140: * Handler for connections made on the server's local domain socket.
141: * It accepts connections and looks for a child process that is currently
142: * idle to which it can dispatch the connection's descriptor. If there are
143: * no available child processes, a new one will be created unless the number
144: * of children has attained the maximum.
1.1 jfb 145: */
1.2 jfb 146: int
147: cvsd_sock_accept(int fd)
1.1 jfb 148: {
1.2 jfb 149: int cfd;
1.1 jfb 150: socklen_t slen;
151: struct sockaddr_un sun;
152:
1.2 jfb 153: slen = sizeof(sun);
154: cfd = accept(fd, (struct sockaddr *)&sun, &slen);
155: if (cfd == -1) {
156: cvs_log(LP_ERRNO, "failed to accept client connection");
1.1 jfb 157: return (-1);
158: }
159:
1.7 krapht 160: return (cfd);
1.1 jfb 161: }
1.3 jfb 162: #endif
1.1 jfb 163:
1.3 jfb 164: #ifdef CVS
1.1 jfb 165: /*
166: * cvs_sock_connect()
167: *
168: * Open a connection to the CVS server's local socket.
169: */
170: int
1.3 jfb 171: cvs_sock_connect(const char *path)
1.1 jfb 172: {
173: cvs_sun.sun_family = AF_LOCAL;
1.3 jfb 174: strlcpy(cvs_sun.sun_path, path, sizeof(cvs_sun.sun_path));
1.1 jfb 175:
176: cvs_log(LP_INFO, "connecting to CVS server socket `%s'",
177: cvs_sun.sun_path);
178:
179: cvs_sock = socket(AF_LOCAL, SOCK_STREAM, 0);
180: if (cvs_sock == -1) {
181: cvs_log(LP_ERRNO, "failed to open local socket");
182: return (-1);
183: }
184:
185: if (connect(cvs_sock, (struct sockaddr *)&cvs_sun,
186: SUN_LEN(&cvs_sun)) == -1) {
187: cvs_log(LP_ERRNO, "failed to connect to server socket `%s'",
188: cvs_sun.sun_path);
189: (void)close(cvs_sock);
190: return (-1);
191: }
192:
193: return (0);
194: }
195:
196:
197: /*
198: * cvs_sock_disconnect()
199: *
200: * Disconnect from the open socket to the CVS server.
201: */
202: void
203: cvs_sock_disconnect(void)
204: {
205: if (close(cvs_sock) == -1)
206: cvs_log(LP_ERRNO, "failed to close local socket");
207: }
1.3 jfb 208: #endif