xref: /freebsd/usr.sbin/ppp/server.c (revision 74bf4e164ba5851606a27d4feff27717452583e5)
1 /*-
2  * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/param.h>
30 
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <sys/un.h>
34 
35 #include <errno.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <sys/stat.h>
40 #include <termios.h>
41 #include <unistd.h>
42 
43 #include "log.h"
44 #include "descriptor.h"
45 #include "server.h"
46 #include "prompt.h"
47 #include "ncpaddr.h"
48 #include "probe.h"
49 
50 static int
51 server_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
52 {
53   struct server *s = descriptor2server(d);
54   struct prompt *p;
55   int sets;
56 
57   sets = 0;
58   if (r && s->fd >= 0) {
59     if (*n < s->fd + 1)
60       *n = s->fd + 1;
61     FD_SET(s->fd, r);
62     log_Printf(LogTIMER, "server: fdset(r) %d\n", s->fd);
63     sets++;
64   }
65 
66   for (p = log_PromptList(); p; p = p->next)
67     sets += descriptor_UpdateSet(&p->desc, r, w, e, n);
68 
69   return sets;
70 }
71 
72 static int
73 server_IsSet(struct fdescriptor *d, const fd_set *fdset)
74 {
75   struct server *s = descriptor2server(d);
76   struct prompt *p;
77 
78   if (s->fd >= 0 && FD_ISSET(s->fd, fdset))
79     return 1;
80 
81   for (p = log_PromptList(); p; p = p->next)
82     if (descriptor_IsSet(&p->desc, fdset))
83       return 1;
84 
85   return 0;
86 }
87 
88 static void
89 server_Read(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset)
90 {
91   struct server *s = descriptor2server(d);
92   struct sockaddr_storage ss;
93   struct sockaddr *sa = (struct sockaddr *)&ss;
94   struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
95 #ifndef NOINET6
96   struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
97 #endif
98   int ssize = sizeof ss, wfd;
99   struct prompt *p;
100   struct ncpaddr addr;
101 
102   if (s->fd >= 0 && FD_ISSET(s->fd, fdset)) {
103     wfd = accept(s->fd, sa, &ssize);
104     if (wfd < 0)
105       log_Printf(LogERROR, "server_Read: accept(): %s\n", strerror(errno));
106     else if (sa->sa_len == 0) {
107       close(wfd);
108       wfd = -1;
109     }
110   } else
111     wfd = -1;
112 
113   if (wfd >= 0)
114     switch (sa->sa_family) {
115       case AF_LOCAL:
116         log_Printf(LogPHASE, "Connected to local client.\n");
117         break;
118 
119       case AF_INET:
120         ncpaddr_setsa(&addr, sa);
121         if (ntohs(sin->sin_port) < 1024) {
122           log_Printf(LogALERT, "Rejected client connection from %s:%u"
123                     "(invalid port number) !\n",
124                     ncpaddr_ntoa(&addr), ntohs(sin->sin_port));
125           close(wfd);
126           wfd = -1;
127           break;
128         }
129         log_Printf(LogPHASE, "Connected to client from %s:%u\n",
130                   ncpaddr_ntoa(&addr), ntohs(sin->sin_port));
131         break;
132 
133 #ifndef NOINET6
134       case AF_INET6:
135         ncpaddr_setsa(&addr, sa);
136         if (ntohs(sin6->sin6_port) < 1024) {
137           log_Printf(LogALERT, "Rejected client connection from %s:%u"
138                     "(invalid port number) !\n",
139                     ncpaddr_ntoa(&addr), ntohs(sin6->sin6_port));
140           close(wfd);
141           wfd = -1;
142           break;
143         }
144         log_Printf(LogPHASE, "Connected to client from %s:%u\n",
145                   ncpaddr_ntoa(&addr), ntohs(sin6->sin6_port));
146         break;
147 #endif
148 
149       default:
150         write(wfd, "Unrecognised access !\n", 22);
151         close(wfd);
152         wfd = -1;
153         break;
154     }
155 
156   if (wfd >= 0) {
157     if ((p = prompt_Create(s, bundle, wfd)) == NULL) {
158       write(wfd, "Connection refused.\n", 20);
159       close(wfd);
160     } else {
161       switch (sa->sa_family) {
162         case AF_LOCAL:
163           p->src.type = "local";
164           strncpy(p->src.from, s->cfg.sockname, sizeof p->src.from - 1);
165           p->src.from[sizeof p->src.from - 1] = '\0';
166           break;
167         case AF_INET:
168           p->src.type = "ip";
169           snprintf(p->src.from, sizeof p->src.from, "%s:%u",
170                    ncpaddr_ntoa(&addr), ntohs(sin->sin_port));
171           break;
172 #ifndef NOINET6
173         case AF_INET6:
174           p->src.type = "ip6";
175           snprintf(p->src.from, sizeof p->src.from, "%s:%u",
176                    ncpaddr_ntoa(&addr), ntohs(sin6->sin6_port));
177           break;
178 #endif
179       }
180       prompt_TtyCommandMode(p);
181       prompt_Required(p);
182     }
183   }
184 
185   log_PromptListChanged = 0;
186   for (p = log_PromptList(); p; p = p->next)
187     if (descriptor_IsSet(&p->desc, fdset)) {
188       descriptor_Read(&p->desc, bundle, fdset);
189       if (log_PromptListChanged)
190         break;
191     }
192 }
193 
194 static int
195 server_Write(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset)
196 {
197   /* We never want to write here ! */
198   log_Printf(LogALERT, "server_Write: Internal error: Bad call !\n");
199   return 0;
200 }
201 
202 struct server server = {
203   {
204     SERVER_DESCRIPTOR,
205     server_UpdateSet,
206     server_IsSet,
207     server_Read,
208     server_Write
209   },
210   -1
211 };
212 
213 enum server_stat
214 server_Reopen(struct bundle *bundle)
215 {
216   char name[sizeof server.cfg.sockname];
217   struct stat st;
218   u_short port;
219   mode_t mask;
220   enum server_stat ret;
221 
222   if (server.cfg.sockname[0] != '\0') {
223     strcpy(name, server.cfg.sockname);
224     mask = server.cfg.mask;
225     server_Close(bundle);
226     if (server.cfg.sockname[0] != '\0' && stat(server.cfg.sockname, &st) == 0)
227       if (!(st.st_mode & S_IFSOCK) || unlink(server.cfg.sockname) != 0)
228         return SERVER_FAILED;
229     ret = server_LocalOpen(bundle, name, mask);
230   } else if (server.cfg.port != 0) {
231     port = server.cfg.port;
232     server_Close(bundle);
233     ret = server_TcpOpen(bundle, port);
234   } else
235     ret = SERVER_UNSET;
236 
237   return ret;
238 }
239 
240 enum server_stat
241 server_LocalOpen(struct bundle *bundle, const char *name, mode_t mask)
242 {
243   struct sockaddr_un ifsun;
244   mode_t oldmask;
245   int s;
246 
247   oldmask = (mode_t)-1;		/* Silence compiler */
248 
249   if (server.cfg.sockname && !strcmp(server.cfg.sockname, name))
250     server_Close(bundle);
251 
252   memset(&ifsun, '\0', sizeof ifsun);
253   ifsun.sun_len = strlen(name);
254   if (ifsun.sun_len > sizeof ifsun.sun_path - 1) {
255     log_Printf(LogERROR, "Local: %s: Path too long\n", name);
256     return SERVER_INVALID;
257   }
258   ifsun.sun_family = AF_LOCAL;
259   strcpy(ifsun.sun_path, name);
260 
261   s = socket(PF_LOCAL, SOCK_STREAM, 0);
262   if (s < 0) {
263     log_Printf(LogERROR, "Local: socket: %s\n", strerror(errno));
264     goto failed;
265   }
266   setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &s, sizeof s);
267   if (mask != (mode_t)-1)
268     oldmask = umask(mask);
269   if (bind(s, (struct sockaddr *)&ifsun, sizeof ifsun) < 0) {
270     if (mask != (mode_t)-1)
271       umask(oldmask);
272     log_Printf(LogWARN, "Local: bind: %s\n", strerror(errno));
273     close(s);
274     goto failed;
275   }
276   if (mask != (mode_t)-1)
277     umask(oldmask);
278   if (listen(s, 5) != 0) {
279     log_Printf(LogERROR, "Local: Unable to listen to socket -"
280                " BUNDLE overload?\n");
281     close(s);
282     unlink(name);
283     goto failed;
284   }
285   server_Close(bundle);
286   server.fd = s;
287   server.cfg.port = 0;
288   strncpy(server.cfg.sockname, ifsun.sun_path, sizeof server.cfg.sockname - 1);
289   server.cfg.sockname[sizeof server.cfg.sockname - 1] = '\0';
290   server.cfg.mask = mask;
291   log_Printf(LogPHASE, "Listening at local socket %s.\n", name);
292 
293   return SERVER_OK;
294 
295 failed:
296   if (server.fd == -1) {
297     server.fd = -1;
298     server.cfg.port = 0;
299     strncpy(server.cfg.sockname, ifsun.sun_path,
300             sizeof server.cfg.sockname - 1);
301     server.cfg.sockname[sizeof server.cfg.sockname - 1] = '\0';
302     server.cfg.mask = mask;
303   }
304   return SERVER_FAILED;
305 }
306 
307 enum server_stat
308 server_TcpOpen(struct bundle *bundle, u_short port)
309 {
310   struct sockaddr_storage ss;
311   struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
312 #ifndef NOINET6
313   struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
314 #endif
315   int s, sz;
316 
317   if (server.cfg.port == port)
318     server_Close(bundle);
319 
320   if (port == 0)
321     return SERVER_INVALID;
322 
323   memset(&ss, '\0', sizeof ss);
324 #ifndef NOINET6
325   if (probe.ipv6_available) {
326     sin6->sin6_family = AF_INET6;
327     sin6->sin6_port = htons(port);
328     sin6->sin6_len = (u_int8_t)sizeof ss;
329     sz = sizeof *sin6;
330     s = socket(PF_INET6, SOCK_STREAM, 0);
331   } else
332 #endif
333   {
334     sin->sin_family = AF_INET;
335     sin->sin_port = htons(port);
336     sin->sin_len = (u_int8_t)sizeof ss;
337     sin->sin_addr.s_addr = INADDR_ANY;
338     sz = sizeof *sin;
339     s = socket(PF_INET, SOCK_STREAM, 0);
340   }
341 
342   if (s < 0) {
343     log_Printf(LogERROR, "Tcp: socket: %s\n", strerror(errno));
344     goto failed;
345   }
346 
347   setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &s, sizeof s);
348   if (bind(s, (struct sockaddr *)&ss, sz) < 0) {
349     log_Printf(LogWARN, "Tcp: bind: %s\n", strerror(errno));
350     close(s);
351     goto failed;
352   }
353   if (listen(s, 5) != 0) {
354     log_Printf(LogERROR, "Tcp: Unable to listen to socket: %s\n",
355                strerror(errno));
356     close(s);
357     goto failed;
358   }
359   server_Close(bundle);
360   server.fd = s;
361   server.cfg.port = port;
362   *server.cfg.sockname = '\0';
363   server.cfg.mask = 0;
364   log_Printf(LogPHASE, "Listening at port %d.\n", port);
365   return SERVER_OK;
366 
367 failed:
368   if (server.fd == -1) {
369     server.fd = -1;
370     server.cfg.port = port;
371     *server.cfg.sockname = '\0';
372     server.cfg.mask = 0;
373   }
374   return SERVER_FAILED;
375 }
376 
377 int
378 server_Close(struct bundle *bundle)
379 {
380   if (server.fd >= 0) {
381     if (*server.cfg.sockname != '\0') {
382       struct sockaddr_un un;
383       int sz = sizeof un;
384 
385       if (getsockname(server.fd, (struct sockaddr *)&un, &sz) == 0 &&
386           un.sun_family == AF_LOCAL && sz == sizeof un)
387         unlink(un.sun_path);
388     }
389     close(server.fd);
390     server.fd = -1;
391     /* Drop associated prompts */
392     log_DestroyPrompts(&server);
393 
394     return 1;
395   }
396 
397   return 0;
398 }
399 
400 int
401 server_Clear(struct bundle *bundle)
402 {
403   int ret;
404 
405   ret = server_Close(bundle);
406 
407   server.fd = -1;
408   server.cfg.port = 0;
409   *server.cfg.sockname = '\0';
410   server.cfg.mask = 0;
411 
412   return ret;
413 }
414