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