aa1f699e |
1 | /* -*-c-*- |
2 | * |
aa1f699e |
3 | * Protocol specific definitions for Unix-domain sockets |
4 | * |
5 | * (c) 1999 Straylight/Edgeware |
6 | */ |
7 | |
206212ca |
8 | /*----- Licensing notice --------------------------------------------------* |
aa1f699e |
9 | * |
10 | * This file is part of the `fw' port forwarder. |
11 | * |
12 | * `fw' is free software; you can redistribute it and/or modify |
13 | * it under the terms of the GNU General Public License as published by |
14 | * the Free Software Foundation; either version 2 of the License, or |
15 | * (at your option) any later version. |
206212ca |
16 | * |
aa1f699e |
17 | * `fw' is distributed in the hope that it will be useful, |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 | * GNU General Public License for more details. |
206212ca |
21 | * |
aa1f699e |
22 | * You should have received a copy of the GNU General Public License |
23 | * along with `fw'; if not, write to the Free Software Foundation, |
24 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | */ |
26 | |
aa1f699e |
27 | #include "fw.h" |
aa1f699e |
28 | |
29 | /*----- Data structures ---------------------------------------------------*/ |
30 | |
31 | typedef struct un_addr { |
32 | addr a; |
33 | struct sockaddr_un sun; |
34 | } un_addr; |
35 | |
36 | typedef struct un_opts { |
37 | addr_opts ao; |
38 | fattr f; |
39 | } un_opts; |
40 | |
41 | /*----- Protocol operations -----------------------------------------------*/ |
42 | |
43 | /* --- @read@ --- */ |
44 | |
45 | static addr *un_read(scanner *sc, unsigned type) |
46 | { |
47 | dstr d = DSTR_INIT; |
48 | un_addr *ua; |
49 | |
50 | conf_name(sc, '/', &d); |
51 | ua = xmalloc(sizeof(addr) + |
52 | offsetof(struct sockaddr_un, sun_path) + |
53 | d.len + 1); |
54 | ua->a.ops = &un_ops; |
55 | ua->a.sz = offsetof(struct sockaddr_un, sun_path) + d.len + 1; |
e0ce9d38 |
56 | memset(&ua->sun, 0, ua->a.sz); |
aa1f699e |
57 | ua->sun.sun_family = AF_UNIX; |
58 | memcpy(ua->sun.sun_path, d.buf, d.len + 1); |
59 | dstr_destroy(&d); |
60 | return (&ua->a); |
61 | } |
62 | |
63 | /* --- @destroy@ --- */ |
64 | |
65 | static void un_destroy(addr *a) |
66 | { |
67 | un_addr *ua = (un_addr *)a; |
b0805b27 |
68 | xfree(ua); |
aa1f699e |
69 | } |
70 | |
71 | /* --- @print@ --- */ |
72 | |
73 | static void un_print(addr *a, unsigned type, dstr *d) |
74 | { |
75 | un_addr *ua = (un_addr *)a; |
76 | dstr_puts(d, "unix:"); |
77 | dstr_puts(d, ua->sun.sun_path); |
78 | } |
79 | |
80 | /* --- @initopts@ --- */ |
81 | |
82 | static addr_opts *un_initopts(void) |
83 | { |
84 | un_opts *uo = CREATE(un_opts); |
85 | uo->f = fattr_global; |
86 | return (&uo->ao); |
87 | } |
88 | |
89 | /* --- @option@ --- */ |
90 | |
0ac54f22 |
91 | static int srcopt(scanner *sc, addr_opts *ao) |
aa1f699e |
92 | { |
93 | un_opts *uo = (un_opts *)ao; |
ad7fbf45 |
94 | CONF_BEGIN(sc, "source", "Unix domain socket source") |
aa1f699e |
95 | |
96 | if (fattr_option(sc, uo ? &uo->f : &fattr_global)) |
97 | CONF_ACCEPT; |
98 | |
99 | CONF_END; |
100 | } |
101 | |
0ac54f22 |
102 | static int un_option(scanner *sc, addr_opts *ao, unsigned type) |
103 | { |
104 | CONF_BEGIN(sc, "unix", "Unix domain socket"); |
105 | if (type != ADDR_DEST && srcopt(sc, ao)) |
106 | CONF_ACCEPT; |
107 | CONF_END; |
108 | } |
109 | |
aa1f699e |
110 | /* --- @accept@ --- */ |
111 | |
112 | static reffd *un_accept(int fd, addr_opts *ao, const char *desc) |
113 | { |
114 | int nfd; |
115 | un_opts *uo = (un_opts *)ao; |
116 | |
117 | /* --- Accept the new connection --- */ |
118 | |
119 | { |
120 | char buf[PATH_MAX + sizeof(struct sockaddr)]; |
121 | struct sockaddr_un *sun = (struct sockaddr_un *)buf; |
bc241e98 |
122 | size_t sunsz = sizeof(buf); |
206212ca |
123 | |
aa1f699e |
124 | if ((nfd = accept(fd, (struct sockaddr *)sun, &sunsz)) < 0) |
125 | return (0); |
126 | } |
127 | |
128 | /* --- Log the connection --- * |
129 | * |
130 | * It'd be really nice if I could find out who the user is, but I can't in |
131 | * anything like a portable way. |
132 | */ |
133 | |
134 | if (!(uo->ao.f & ADDRF_NOLOG)) |
135 | fw_log(-1, "[%s] accepted", desc); |
136 | return (reffd_init(nfd)); |
137 | } |
138 | |
139 | /* --- @freeopts@ --- */ |
140 | |
141 | static void un_freeopts(addr_opts *ao) |
142 | { |
143 | un_opts *uo = (un_opts *)ao; |
144 | DESTROY(uo); |
145 | } |
146 | |
0ac54f22 |
147 | /* --- @bind@ --- */ |
aa1f699e |
148 | |
0ac54f22 |
149 | static int un_bind(addr *a, addr_opts *ao) |
aa1f699e |
150 | { |
151 | un_addr *ua = (un_addr *)a; |
152 | un_opts *uo = (un_opts *)ao; |
0ac54f22 |
153 | int fd; |
154 | |
155 | if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) |
156 | goto fail_0; |
157 | if (bind(fd, (struct sockaddr *)&ua->sun, sizeof(ua->sun))) |
158 | goto fail_1; |
159 | if (fattr_apply(ua->sun.sun_path, &uo->f)) |
160 | goto fail_1; |
161 | return (fd); |
162 | |
163 | fail_1: |
164 | close(fd); |
165 | fail_0: |
166 | return (-1); |
aa1f699e |
167 | } |
168 | |
169 | /* --- @unbind@ --- */ |
170 | |
171 | static void un_unbind(addr *a) |
172 | { |
173 | un_addr *ua = (un_addr *)a; |
174 | unlink(ua->sun.sun_path); |
175 | } |
176 | |
0ac54f22 |
177 | /* --- @connect@ --- */ |
178 | |
179 | static int un_connect(addr *a, addr_opts *ao, conn *c, endpt *e) |
180 | { |
181 | un_addr *ua = (un_addr *)a; |
182 | int fd; |
183 | |
184 | if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) |
185 | goto fail_0; |
186 | return (conn_init(c, sel, fd, (struct sockaddr *)&ua->sun, sizeof(ua->sun), |
187 | starget_connected, e)); |
188 | fail_0: |
189 | return (-1); |
190 | } |
191 | |
aa1f699e |
192 | /* --- Protocol definition --- */ |
193 | |
194 | addr_ops un_ops = { |
0ac54f22 |
195 | "unix", |
aa1f699e |
196 | un_read, un_destroy, un_print, |
ee599f55 |
197 | un_initopts, un_option, 0, un_freeopts, un_bind, un_unbind, un_accept, |
0ac54f22 |
198 | 0, 0, un_connect |
aa1f699e |
199 | }; |
200 | |
201 | /*----- That's all, folks -------------------------------------------------*/ |