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