Fix source option prefix.
[fwd] / un.c
1 /* -*-c-*-
2 *
3 * $Id: un.c,v 1.8 2003/11/30 00:25:27 mdw Exp $
4 *
5 * Protocol specific definitions for Unix-domain sockets
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the `fw' port forwarder.
13 *
14 * `fw' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * `fw' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with `fw'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
29 /*----- Revision history --------------------------------------------------*
30 *
31 * $Log: un.c,v $
32 * Revision 1.8 2003/11/30 00:25:27 mdw
33 * Fix source option prefix.
34 *
35 * Revision 1.7 2003/11/29 20:36:07 mdw
36 * Privileged outgoing connections.
37 *
38 * Revision 1.6 2003/11/25 14:08:23 mdw
39 * Debianization. Socket target options. Internet binding.
40 *
41 * Revision 1.5 2002/02/22 23:43:32 mdw
42 * Call @xfree@ rather than @free@.
43 *
44 * Revision 1.4 2000/08/01 17:59:56 mdw
45 * Switch over to using `size_t' for socket address lengths.
46 *
47 * Revision 1.3 2000/08/01 17:58:32 mdw
48 * Remove unnecessary <ctype.h> header.
49 *
50 * Revision 1.2 1999/07/27 18:30:53 mdw
51 * Various minor portability fixes.
52 *
53 * Revision 1.1 1999/07/26 23:34:11 mdw
54 * New socket address types.
55 *
56 */
57
58 /*----- Header files ------------------------------------------------------*/
59
60 #include "config.h"
61 #undef sun /* Cretins */
62
63 #include <errno.h>
64 #include <limits.h>
65 #include <stddef.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69
70 #include <sys/types.h>
71 #include <unistd.h>
72
73 #include <sys/socket.h>
74 #include <sys/un.h>
75 #include <arpa/inet.h>
76 #include <netdb.h>
77
78 #include <mLib/alloc.h>
79 #include <mLib/dstr.h>
80 #include <mLib/report.h>
81 #include <mLib/sub.h>
82
83 #include "addr.h"
84 #include "conf.h"
85 #include "fattr.h"
86 #include "fw.h"
87 #include "reffd.h"
88 #include "scan.h"
89 #include "socket.h"
90 #include "un.h"
91
92 /*----- Data structures ---------------------------------------------------*/
93
94 typedef struct un_addr {
95 addr a;
96 struct sockaddr_un sun;
97 } un_addr;
98
99 typedef struct un_opts {
100 addr_opts ao;
101 fattr f;
102 } un_opts;
103
104 /*----- Protocol operations -----------------------------------------------*/
105
106 /* --- @read@ --- */
107
108 static addr *un_read(scanner *sc, unsigned type)
109 {
110 dstr d = DSTR_INIT;
111 un_addr *ua;
112
113 conf_name(sc, '/', &d);
114 ua = xmalloc(sizeof(addr) +
115 offsetof(struct sockaddr_un, sun_path) +
116 d.len + 1);
117 ua->a.ops = &un_ops;
118 ua->a.sz = offsetof(struct sockaddr_un, sun_path) + d.len + 1;
119 memset(&ua->sun, 0, ua->a.sz);
120 ua->sun.sun_family = AF_UNIX;
121 memcpy(ua->sun.sun_path, d.buf, d.len + 1);
122 dstr_destroy(&d);
123 return (&ua->a);
124 }
125
126 /* --- @destroy@ --- */
127
128 static void un_destroy(addr *a)
129 {
130 un_addr *ua = (un_addr *)a;
131 xfree(ua);
132 }
133
134 /* --- @print@ --- */
135
136 static void un_print(addr *a, unsigned type, dstr *d)
137 {
138 un_addr *ua = (un_addr *)a;
139 dstr_puts(d, "unix:");
140 dstr_puts(d, ua->sun.sun_path);
141 }
142
143 /* --- @initopts@ --- */
144
145 static addr_opts *un_initopts(void)
146 {
147 un_opts *uo = CREATE(un_opts);
148 uo->f = fattr_global;
149 return (&uo->ao);
150 }
151
152 /* --- @option@ --- */
153
154 static int srcopt(scanner *sc, addr_opts *ao)
155 {
156 un_opts *uo = (un_opts *)ao;
157 CONF_BEGIN(sc, "source", "Unix domain socket source")
158
159 if (fattr_option(sc, uo ? &uo->f : &fattr_global))
160 CONF_ACCEPT;
161
162 CONF_END;
163 }
164
165 static int un_option(scanner *sc, addr_opts *ao, unsigned type)
166 {
167 CONF_BEGIN(sc, "unix", "Unix domain socket");
168 if (type != ADDR_DEST && srcopt(sc, ao))
169 CONF_ACCEPT;
170 CONF_END;
171 }
172
173 /* --- @accept@ --- */
174
175 static reffd *un_accept(int fd, addr_opts *ao, const char *desc)
176 {
177 int nfd;
178 un_opts *uo = (un_opts *)ao;
179
180 /* --- Accept the new connection --- */
181
182 {
183 char buf[PATH_MAX + sizeof(struct sockaddr)];
184 struct sockaddr_un *sun = (struct sockaddr_un *)buf;
185 size_t sunsz = sizeof(buf);
186
187 if ((nfd = accept(fd, (struct sockaddr *)sun, &sunsz)) < 0)
188 return (0);
189 }
190
191 /* --- Log the connection --- *
192 *
193 * It'd be really nice if I could find out who the user is, but I can't in
194 * anything like a portable way.
195 */
196
197 if (!(uo->ao.f & ADDRF_NOLOG))
198 fw_log(-1, "[%s] accepted", desc);
199 return (reffd_init(nfd));
200 }
201
202 /* --- @freeopts@ --- */
203
204 static void un_freeopts(addr_opts *ao)
205 {
206 un_opts *uo = (un_opts *)ao;
207 DESTROY(uo);
208 }
209
210 /* --- @bind@ --- */
211
212 static int un_bind(addr *a, addr_opts *ao)
213 {
214 un_addr *ua = (un_addr *)a;
215 un_opts *uo = (un_opts *)ao;
216 int fd;
217
218 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
219 goto fail_0;
220 if (bind(fd, (struct sockaddr *)&ua->sun, sizeof(ua->sun)))
221 goto fail_1;
222 if (fattr_apply(ua->sun.sun_path, &uo->f))
223 goto fail_1;
224 return (fd);
225
226 fail_1:
227 close(fd);
228 fail_0:
229 return (-1);
230 }
231
232 /* --- @unbind@ --- */
233
234 static void un_unbind(addr *a)
235 {
236 un_addr *ua = (un_addr *)a;
237 unlink(ua->sun.sun_path);
238 }
239
240 /* --- @connect@ --- */
241
242 static int un_connect(addr *a, addr_opts *ao, conn *c, endpt *e)
243 {
244 un_addr *ua = (un_addr *)a;
245 int fd;
246
247 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
248 goto fail_0;
249 return (conn_init(c, sel, fd, (struct sockaddr *)&ua->sun, sizeof(ua->sun),
250 starget_connected, e));
251 fail_0:
252 return (-1);
253 }
254
255 /* --- Protocol definition --- */
256
257 addr_ops un_ops = {
258 "unix",
259 un_read, un_destroy, un_print,
260 un_initopts, un_option, 0, un_freeopts, un_bind, un_unbind, un_accept,
261 0, 0, un_connect
262 };
263
264 /*----- That's all, folks -------------------------------------------------*/