Annotation of src/usr.bin/cvs/sock.c, Revision 1.15
1.15 ! deraadt 1: /* $OpenBSD: sock.c,v 1.14 2005/03/10 22:37:00 deraadt 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.13 deraadt 70: mode_t old_umask;
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:
1.15 ! deraadt 84: old_umask = umask(S_IRWXO);
1.1 jfb 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);
1.13 deraadt 90: umask(old_umask);
1.1 jfb 91: return (-1);
92: }
1.13 deraadt 93: umask(old_umask);
1.1 jfb 94:
1.5 jfb 95: (void)listen(cvsd_sock, 10);
96:
97: if (chown(cvsd_sock_path, getuid(), cvsd_gid) == -1) {
98: cvs_log(LP_ERRNO, "failed to change owner of `%s'",
99: cvsd_sock_path);
100: (void)close(cvsd_sock);
101: (void)unlink(cvsd_sock_path);
102: return (-1);
103: }
1.1 jfb 104:
105: if (chmod(cvsd_sock_path, CVSD_SOCK_PERMS) == -1) {
106: cvs_log(LP_ERRNO, "failed to change mode of `%s'",
107: cvsd_sock_path);
108: (void)close(cvsd_sock);
109: (void)unlink(cvsd_sock_path);
110: return (-1);
111: }
1.11 jfb 112:
113: /* close on exec so children can't muck around with this */
114: (void)fcntl(cvsd_sock, F_SETFD, FD_CLOEXEC);
1.1 jfb 115:
116: cvs_log(LP_DEBUG, "opened local socket `%s'", cvsd_sock_path);
117:
118: return (0);
119: }
120:
121:
122: /*
123: * cvsd_sock_close()
124: *
125: * Close the local socket.
126: */
127: void
128: cvsd_sock_close(void)
129: {
130: cvs_log(LP_DEBUG, "closing local socket `%s'", CVSD_SOCK_PATH);
131: if (close(cvsd_sock) == -1) {
132: cvs_log(LP_ERRNO, "failed to close local socket");
133: }
1.6 jfb 134: if (seteuid(0) == -1)
135: cvs_log(LP_ERRNO, "failed to regain privileges");
136: else if (unlink(cvsd_sock_path) == -1)
1.1 jfb 137: cvs_log(LP_ERRNO, "failed to unlink local socket `%s'",
1.2 jfb 138: cvsd_sock_path);
1.1 jfb 139: }
140:
141:
142: /*
1.2 jfb 143: * cvsd_sock_accept()
1.1 jfb 144: *
1.2 jfb 145: * Handler for connections made on the server's local domain socket.
146: * It accepts connections and looks for a child process that is currently
147: * idle to which it can dispatch the connection's descriptor. If there are
148: * no available child processes, a new one will be created unless the number
149: * of children has attained the maximum.
1.1 jfb 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: int
1.3 jfb 176: cvs_sock_connect(const char *path)
1.1 jfb 177: {
178: cvs_sun.sun_family = AF_LOCAL;
1.3 jfb 179: strlcpy(cvs_sun.sun_path, path, sizeof(cvs_sun.sun_path));
1.1 jfb 180:
181: cvs_log(LP_INFO, "connecting to CVS server socket `%s'",
182: cvs_sun.sun_path);
183:
184: cvs_sock = socket(AF_LOCAL, SOCK_STREAM, 0);
185: if (cvs_sock == -1) {
186: cvs_log(LP_ERRNO, "failed to open local socket");
187: return (-1);
188: }
189:
190: if (connect(cvs_sock, (struct sockaddr *)&cvs_sun,
191: SUN_LEN(&cvs_sun)) == -1) {
192: cvs_log(LP_ERRNO, "failed to connect to server socket `%s'",
193: cvs_sun.sun_path);
194: (void)close(cvs_sock);
195: return (-1);
196: }
197:
198: return (0);
199: }
200:
201:
202: /*
203: * cvs_sock_disconnect()
204: *
205: * Disconnect from the open socket to the CVS server.
206: */
207: void
208: cvs_sock_disconnect(void)
209: {
210: if (close(cvs_sock) == -1)
211: cvs_log(LP_ERRNO, "failed to close local socket");
212: }
1.3 jfb 213: #endif