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