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