Provide a `--pidfile' option in `fw'.
[fwd] / un.c
CommitLineData
aa1f699e 1/* -*-c-*-
2 *
7481fc9c 3 * $Id: un.c,v 1.9 2004/04/08 01:36:25 mdw Exp $
aa1f699e 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
aa1f699e 29/*----- Header files ------------------------------------------------------*/
30
31#include "config.h"
e0ce9d38 32#undef sun /* Cretins */
aa1f699e 33
aa1f699e 34#include <errno.h>
35#include <limits.h>
36#include <stddef.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40
41#include <sys/types.h>
42#include <unistd.h>
43
44#include <sys/socket.h>
45#include <sys/un.h>
46#include <arpa/inet.h>
47#include <netdb.h>
48
49#include <mLib/alloc.h>
50#include <mLib/dstr.h>
51#include <mLib/report.h>
52#include <mLib/sub.h>
53
54#include "addr.h"
55#include "conf.h"
56#include "fattr.h"
57#include "fw.h"
58#include "reffd.h"
59#include "scan.h"
0ac54f22 60#include "socket.h"
aa1f699e 61#include "un.h"
62
63/*----- Data structures ---------------------------------------------------*/
64
65typedef struct un_addr {
66 addr a;
67 struct sockaddr_un sun;
68} un_addr;
69
70typedef struct un_opts {
71 addr_opts ao;
72 fattr f;
73} un_opts;
74
75/*----- Protocol operations -----------------------------------------------*/
76
77/* --- @read@ --- */
78
79static addr *un_read(scanner *sc, unsigned type)
80{
81 dstr d = DSTR_INIT;
82 un_addr *ua;
83
84 conf_name(sc, '/', &d);
85 ua = xmalloc(sizeof(addr) +
86 offsetof(struct sockaddr_un, sun_path) +
87 d.len + 1);
88 ua->a.ops = &un_ops;
89 ua->a.sz = offsetof(struct sockaddr_un, sun_path) + d.len + 1;
e0ce9d38 90 memset(&ua->sun, 0, ua->a.sz);
aa1f699e 91 ua->sun.sun_family = AF_UNIX;
92 memcpy(ua->sun.sun_path, d.buf, d.len + 1);
93 dstr_destroy(&d);
94 return (&ua->a);
95}
96
97/* --- @destroy@ --- */
98
99static void un_destroy(addr *a)
100{
101 un_addr *ua = (un_addr *)a;
b0805b27 102 xfree(ua);
aa1f699e 103}
104
105/* --- @print@ --- */
106
107static void un_print(addr *a, unsigned type, dstr *d)
108{
109 un_addr *ua = (un_addr *)a;
110 dstr_puts(d, "unix:");
111 dstr_puts(d, ua->sun.sun_path);
112}
113
114/* --- @initopts@ --- */
115
116static addr_opts *un_initopts(void)
117{
118 un_opts *uo = CREATE(un_opts);
119 uo->f = fattr_global;
120 return (&uo->ao);
121}
122
123/* --- @option@ --- */
124
0ac54f22 125static int srcopt(scanner *sc, addr_opts *ao)
aa1f699e 126{
127 un_opts *uo = (un_opts *)ao;
ad7fbf45 128 CONF_BEGIN(sc, "source", "Unix domain socket source")
aa1f699e 129
130 if (fattr_option(sc, uo ? &uo->f : &fattr_global))
131 CONF_ACCEPT;
132
133 CONF_END;
134}
135
0ac54f22 136static int un_option(scanner *sc, addr_opts *ao, unsigned type)
137{
138 CONF_BEGIN(sc, "unix", "Unix domain socket");
139 if (type != ADDR_DEST && srcopt(sc, ao))
140 CONF_ACCEPT;
141 CONF_END;
142}
143
aa1f699e 144/* --- @accept@ --- */
145
146static reffd *un_accept(int fd, addr_opts *ao, const char *desc)
147{
148 int nfd;
149 un_opts *uo = (un_opts *)ao;
150
151 /* --- Accept the new connection --- */
152
153 {
154 char buf[PATH_MAX + sizeof(struct sockaddr)];
155 struct sockaddr_un *sun = (struct sockaddr_un *)buf;
bc241e98 156 size_t sunsz = sizeof(buf);
aa1f699e 157
158 if ((nfd = accept(fd, (struct sockaddr *)sun, &sunsz)) < 0)
159 return (0);
160 }
161
162 /* --- Log the connection --- *
163 *
164 * It'd be really nice if I could find out who the user is, but I can't in
165 * anything like a portable way.
166 */
167
168 if (!(uo->ao.f & ADDRF_NOLOG))
169 fw_log(-1, "[%s] accepted", desc);
170 return (reffd_init(nfd));
171}
172
173/* --- @freeopts@ --- */
174
175static void un_freeopts(addr_opts *ao)
176{
177 un_opts *uo = (un_opts *)ao;
178 DESTROY(uo);
179}
180
0ac54f22 181/* --- @bind@ --- */
aa1f699e 182
0ac54f22 183static int un_bind(addr *a, addr_opts *ao)
aa1f699e 184{
185 un_addr *ua = (un_addr *)a;
186 un_opts *uo = (un_opts *)ao;
0ac54f22 187 int fd;
188
189 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
190 goto fail_0;
191 if (bind(fd, (struct sockaddr *)&ua->sun, sizeof(ua->sun)))
192 goto fail_1;
193 if (fattr_apply(ua->sun.sun_path, &uo->f))
194 goto fail_1;
195 return (fd);
196
197fail_1:
198 close(fd);
199fail_0:
200 return (-1);
aa1f699e 201}
202
203/* --- @unbind@ --- */
204
205static void un_unbind(addr *a)
206{
207 un_addr *ua = (un_addr *)a;
208 unlink(ua->sun.sun_path);
209}
210
0ac54f22 211/* --- @connect@ --- */
212
213static int un_connect(addr *a, addr_opts *ao, conn *c, endpt *e)
214{
215 un_addr *ua = (un_addr *)a;
216 int fd;
217
218 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
219 goto fail_0;
220 return (conn_init(c, sel, fd, (struct sockaddr *)&ua->sun, sizeof(ua->sun),
221 starget_connected, e));
222fail_0:
223 return (-1);
224}
225
aa1f699e 226/* --- Protocol definition --- */
227
228addr_ops un_ops = {
0ac54f22 229 "unix",
aa1f699e 230 un_read, un_destroy, un_print,
ee599f55 231 un_initopts, un_option, 0, un_freeopts, un_bind, un_unbind, un_accept,
0ac54f22 232 0, 0, un_connect
aa1f699e 233};
234
235/*----- That's all, folks -------------------------------------------------*/