3 * $Id: un.c,v 1.7 2003/11/29 20:36:07 mdw Exp $
5 * Protocol specific definitions for Unix-domain sockets
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the `fw' port forwarder.
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.
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.
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.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.7 2003/11/29 20:36:07 mdw
33 * Privileged outgoing connections.
35 * Revision 1.6 2003/11/25 14:08:23 mdw
36 * Debianization. Socket target options. Internet binding.
38 * Revision 1.5 2002/02/22 23:43:32 mdw
39 * Call @xfree@ rather than @free@.
41 * Revision 1.4 2000/08/01 17:59:56 mdw
42 * Switch over to using `size_t' for socket address lengths.
44 * Revision 1.3 2000/08/01 17:58:32 mdw
45 * Remove unnecessary <ctype.h> header.
47 * Revision 1.2 1999/07/27 18:30:53 mdw
48 * Various minor portability fixes.
50 * Revision 1.1 1999/07/26 23:34:11 mdw
51 * New socket address types.
55 /*----- Header files ------------------------------------------------------*/
58 #undef sun /* Cretins */
67 #include <sys/types.h>
70 #include <sys/socket.h>
72 #include <arpa/inet.h>
75 #include <mLib/alloc.h>
76 #include <mLib/dstr.h>
77 #include <mLib/report.h>
89 /*----- Data structures ---------------------------------------------------*/
91 typedef struct un_addr {
93 struct sockaddr_un sun;
96 typedef struct un_opts {
101 /*----- Protocol operations -----------------------------------------------*/
105 static addr *un_read(scanner *sc, unsigned type)
110 conf_name(sc, '/', &d);
111 ua = xmalloc(sizeof(addr) +
112 offsetof(struct sockaddr_un, sun_path) +
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);
123 /* --- @destroy@ --- */
125 static void un_destroy(addr *a)
127 un_addr *ua = (un_addr *)a;
131 /* --- @print@ --- */
133 static void un_print(addr *a, unsigned type, dstr *d)
135 un_addr *ua = (un_addr *)a;
136 dstr_puts(d, "unix:");
137 dstr_puts(d, ua->sun.sun_path);
140 /* --- @initopts@ --- */
142 static addr_opts *un_initopts(void)
144 un_opts *uo = CREATE(un_opts);
145 uo->f = fattr_global;
149 /* --- @option@ --- */
151 static int srcopt(scanner *sc, addr_opts *ao)
153 un_opts *uo = (un_opts *)ao;
154 CONF_BEGIN(sc, "unix", "Unix domain socket")
156 if (fattr_option(sc, uo ? &uo->f : &fattr_global))
162 static int un_option(scanner *sc, addr_opts *ao, unsigned type)
164 CONF_BEGIN(sc, "unix", "Unix domain socket");
165 if (type != ADDR_DEST && srcopt(sc, ao))
170 /* --- @accept@ --- */
172 static reffd *un_accept(int fd, addr_opts *ao, const char *desc)
175 un_opts *uo = (un_opts *)ao;
177 /* --- Accept the new connection --- */
180 char buf[PATH_MAX + sizeof(struct sockaddr)];
181 struct sockaddr_un *sun = (struct sockaddr_un *)buf;
182 size_t sunsz = sizeof(buf);
184 if ((nfd = accept(fd, (struct sockaddr *)sun, &sunsz)) < 0)
188 /* --- Log the connection --- *
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.
194 if (!(uo->ao.f & ADDRF_NOLOG))
195 fw_log(-1, "[%s] accepted", desc);
196 return (reffd_init(nfd));
199 /* --- @freeopts@ --- */
201 static void un_freeopts(addr_opts *ao)
203 un_opts *uo = (un_opts *)ao;
209 static int un_bind(addr *a, addr_opts *ao)
211 un_addr *ua = (un_addr *)a;
212 un_opts *uo = (un_opts *)ao;
215 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
217 if (bind(fd, (struct sockaddr *)&ua->sun, sizeof(ua->sun)))
219 if (fattr_apply(ua->sun.sun_path, &uo->f))
229 /* --- @unbind@ --- */
231 static void un_unbind(addr *a)
233 un_addr *ua = (un_addr *)a;
234 unlink(ua->sun.sun_path);
237 /* --- @connect@ --- */
239 static int un_connect(addr *a, addr_opts *ao, conn *c, endpt *e)
241 un_addr *ua = (un_addr *)a;
244 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
246 return (conn_init(c, sel, fd, (struct sockaddr *)&ua->sun, sizeof(ua->sun),
247 starget_connected, e));
252 /* --- Protocol definition --- */
256 un_read, un_destroy, un_print,
257 un_initopts, un_option, 0, un_freeopts, un_bind, un_unbind, un_accept,
261 /*----- That's all, folks -------------------------------------------------*/