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