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