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