Make syslog a separate option, and do it better.
[fwd] / un.c
1 /* -*-c-*-
2 *
3 * $Id: un.c,v 1.2 1999/07/27 18:30:53 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.2 1999/07/27 18:30:53 mdw
33 * Various minor portability fixes.
34 *
35 * Revision 1.1 1999/07/26 23:34:11 mdw
36 * New socket address types.
37 *
38 */
39
40 /*----- Header files ------------------------------------------------------*/
41
42 #include "config.h"
43 #undef sun /* Cretins */
44
45 #include <ctype.h>
46 #include <errno.h>
47 #include <limits.h>
48 #include <stddef.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52
53 #include <sys/types.h>
54 #include <unistd.h>
55
56 #include <sys/socket.h>
57 #include <sys/un.h>
58 #include <arpa/inet.h>
59 #include <netdb.h>
60
61 #include <mLib/alloc.h>
62 #include <mLib/dstr.h>
63 #include <mLib/report.h>
64 #include <mLib/sub.h>
65
66 #include "addr.h"
67 #include "conf.h"
68 #include "fattr.h"
69 #include "fw.h"
70 #include "reffd.h"
71 #include "scan.h"
72 #include "un.h"
73
74 /*----- Data structures ---------------------------------------------------*/
75
76 typedef struct un_addr {
77 addr a;
78 struct sockaddr_un sun;
79 } un_addr;
80
81 typedef struct un_opts {
82 addr_opts ao;
83 fattr f;
84 } un_opts;
85
86 /*----- Protocol operations -----------------------------------------------*/
87
88 /* --- @read@ --- */
89
90 static addr *un_read(scanner *sc, unsigned type)
91 {
92 dstr d = DSTR_INIT;
93 un_addr *ua;
94
95 conf_name(sc, '/', &d);
96 ua = xmalloc(sizeof(addr) +
97 offsetof(struct sockaddr_un, sun_path) +
98 d.len + 1);
99 ua->a.ops = &un_ops;
100 ua->a.sz = offsetof(struct sockaddr_un, sun_path) + d.len + 1;
101 memset(&ua->sun, 0, ua->a.sz);
102 ua->sun.sun_family = AF_UNIX;
103 memcpy(ua->sun.sun_path, d.buf, d.len + 1);
104 dstr_destroy(&d);
105 return (&ua->a);
106 }
107
108 /* --- @destroy@ --- */
109
110 static void un_destroy(addr *a)
111 {
112 un_addr *ua = (un_addr *)a;
113 free(ua);
114 }
115
116 /* --- @print@ --- */
117
118 static void un_print(addr *a, unsigned type, dstr *d)
119 {
120 un_addr *ua = (un_addr *)a;
121 dstr_puts(d, "unix:");
122 dstr_puts(d, ua->sun.sun_path);
123 }
124
125 /* --- @initopts@ --- */
126
127 static addr_opts *un_initopts(void)
128 {
129 un_opts *uo = CREATE(un_opts);
130 uo->f = fattr_global;
131 return (&uo->ao);
132 }
133
134 /* --- @option@ --- */
135
136 static int un_option(scanner *sc, addr_opts *ao)
137 {
138 un_opts *uo = (un_opts *)ao;
139 CONF_BEGIN(sc, "unix", "Unix domain socket")
140
141 if (fattr_option(sc, uo ? &uo->f : &fattr_global))
142 CONF_ACCEPT;
143
144 CONF_END;
145 }
146
147 /* --- @accept@ --- */
148
149 static reffd *un_accept(int fd, addr_opts *ao, const char *desc)
150 {
151 int nfd;
152 un_opts *uo = (un_opts *)ao;
153
154 /* --- Accept the new connection --- */
155
156 {
157 char buf[PATH_MAX + sizeof(struct sockaddr)];
158 struct sockaddr_un *sun = (struct sockaddr_un *)buf;
159 int sunsz = sizeof(buf);
160
161 if ((nfd = accept(fd, (struct sockaddr *)sun, &sunsz)) < 0)
162 return (0);
163 }
164
165 /* --- Log the connection --- *
166 *
167 * It'd be really nice if I could find out who the user is, but I can't in
168 * anything like a portable way.
169 */
170
171 if (!(uo->ao.f & ADDRF_NOLOG))
172 fw_log(-1, "[%s] accepted", desc);
173 return (reffd_init(nfd));
174 }
175
176 /* --- @freeopts@ --- */
177
178 static void un_freeopts(addr_opts *ao)
179 {
180 un_opts *uo = (un_opts *)ao;
181 DESTROY(uo);
182 }
183
184 /* --- @bound@ --- */
185
186 static void un_bound(addr *a, addr_opts *ao)
187 {
188 un_addr *ua = (un_addr *)a;
189 un_opts *uo = (un_opts *)ao;
190 if (fattr_apply(ua->sun.sun_path, &uo->f)) {
191 dstr d = DSTR_INIT;
192 un_print(a, ADDR_SRC, &d);
193 fw_log(-1, "[%s] couldn't apply file attributes: %s",
194 d.buf, strerror(errno));
195 dstr_destroy(&d);
196 }
197 }
198
199 /* --- @unbind@ --- */
200
201 static void un_unbind(addr *a)
202 {
203 un_addr *ua = (un_addr *)a;
204 unlink(ua->sun.sun_path);
205 }
206
207 /* --- Protocol definition --- */
208
209 addr_ops un_ops = {
210 "unix", PF_UNIX,
211 un_read, un_destroy, un_print,
212 un_initopts, un_option, un_accept, un_freeopts, un_bound, un_unbind
213 };
214
215 /*----- That's all, folks -------------------------------------------------*/