Annotation of src/usr.bin/cvs/sock.c, Revision 1.8
1.8 ! tedu 1: /* $OpenBSD: sock.c,v 1.7 2004/11/09 20:43:22 krapht 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>
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: /* daemon API */
1.4 jfb 50: #ifdef CVSD
1.2 jfb 51: int cvsd_sock = -1;
1.1 jfb 52: static struct sockaddr_un cvsd_sun;
1.4 jfb 53: #endif
1.1 jfb 54:
55: /* for client API */
1.4 jfb 56: #ifdef CVS
1.1 jfb 57: static int cvs_sock = -1;
58: static struct sockaddr_un cvs_sun;
1.4 jfb 59: #endif
1.1 jfb 60:
61:
1.3 jfb 62: #ifdef CVSD
1.1 jfb 63: /*
64: * cvsd_sock_open()
65: *
1.2 jfb 66: * Open the daemon's local socket. If the server socket is already opened,
67: * we close it before reopening it.
68: * Returns 0 on success, -1 on failure.
1.1 jfb 69: */
70: int
71: cvsd_sock_open(void)
72: {
1.4 jfb 73: if (cvsd_sock >= 0)
1.2 jfb 74: cvsd_sock_close();
75:
1.1 jfb 76: cvsd_sun.sun_family = AF_LOCAL;
77: strlcpy(cvsd_sun.sun_path, cvsd_sock_path, sizeof(cvsd_sun.sun_path));
78:
79: cvsd_sock = socket(AF_LOCAL, SOCK_STREAM, 0);
80: if (cvsd_sock == -1) {
1.8 ! tedu 81: cvs_log(LP_ERRNO, "failed to open socket");
1.1 jfb 82: return (-1);
83: }
84:
85: if (bind(cvsd_sock, (struct sockaddr *)&cvsd_sun,
86: SUN_LEN(&cvsd_sun)) == -1) {
87: cvs_log(LP_ERRNO, "failed to bind local socket to `%s'",
88: cvsd_sock_path);
89: (void)close(cvsd_sock);
90: return (-1);
91: }
92:
1.5 jfb 93: (void)listen(cvsd_sock, 10);
94:
95: if (chown(cvsd_sock_path, getuid(), cvsd_gid) == -1) {
96: cvs_log(LP_ERRNO, "failed to change owner of `%s'",
97: cvsd_sock_path);
98: (void)close(cvsd_sock);
99: (void)unlink(cvsd_sock_path);
100: return (-1);
101: }
1.1 jfb 102:
103: if (chmod(cvsd_sock_path, CVSD_SOCK_PERMS) == -1) {
104: cvs_log(LP_ERRNO, "failed to change mode of `%s'",
105: cvsd_sock_path);
106: (void)close(cvsd_sock);
107: (void)unlink(cvsd_sock_path);
108: return (-1);
109: }
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