server.c (7a6f872047b686aa10864866e8c80b7d591f4f45) server.c (77ff88ad4bb6f3a307795d6f590534f223b49833)
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 unchanged lines hidden (view full) ---

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 *
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 unchanged lines hidden (view full) ---

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 * $Id: server.c,v 1.16 1998/01/21 02:15:27 brian Exp $
26 * $Id: server.c,v 1.16.2.1 1998/02/02 19:32:15 brian Exp $
27 */
28
29#include <sys/param.h>
30#include <sys/socket.h>
31#include <netinet/in.h>
32#include <arpa/inet.h>
33#include <netinet/in_systm.h>
34

--- 5 unchanged lines hidden (view full) ---

40#include <unistd.h>
41
42#include "command.h"
43#include "mbuf.h"
44#include "log.h"
45#include "loadalias.h"
46#include "defs.h"
47#include "vars.h"
27 */
28
29#include <sys/param.h>
30#include <sys/socket.h>
31#include <netinet/in.h>
32#include <arpa/inet.h>
33#include <netinet/in_systm.h>
34

--- 5 unchanged lines hidden (view full) ---

40#include <unistd.h>
41
42#include "command.h"
43#include "mbuf.h"
44#include "log.h"
45#include "loadalias.h"
46#include "defs.h"
47#include "vars.h"
48#include "descriptor.h"
48#include "server.h"
49#include "id.h"
50
49#include "server.h"
50#include "id.h"
51
51int server = -1;
52static int
53server_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
54{
55 struct server *s = descriptor2server(d);
52
56
57 LogPrintf(LogDEBUG, "descriptor2server; %p -> %p\n", d, s);
58 if (s->fd >= 0) {
59 FD_SET(s->fd, r);
60 if (*n < s->fd + 1)
61 *n = s->fd + 1;
62 return 1;
63 }
64 return 0;
65}
66
67static int
68server_IsSet(struct descriptor *d, fd_set *fdset)
69{
70 struct server *s = descriptor2server(d);
71
72 LogPrintf(LogDEBUG, "descriptor2server; %p -> %p\n", d, s);
73 return s->fd >= 0 && FD_ISSET(s->fd, fdset);
74}
75
76#define IN_SIZE sizeof(struct sockaddr_in)
77#define UN_SIZE sizeof(struct sockaddr_in)
78#define ADDRSZ (IN_SIZE > UN_SIZE ? IN_SIZE : UN_SIZE)
79
80static void
81server_Read(struct descriptor *d, struct bundle *bundle)
82{
83 struct server *s = descriptor2server(d);
84 char hisaddr[ADDRSZ];
85 struct sockaddr *sa = (struct sockaddr *)hisaddr;
86 struct sockaddr_in *sin = (struct sockaddr_in *)hisaddr;
87 int ssize = ADDRSZ, wfd;
88
89 LogPrintf(LogDEBUG, "descriptor2server; %p -> %p\n", d, s);
90
91 wfd = accept(s->fd, sa, &ssize);
92 if (wfd < 0) {
93 LogPrintf(LogERROR, "server_Read: accept(): %s\n", strerror(errno));
94 return;
95 }
96
97 switch (sa->sa_family) {
98 case AF_LOCAL:
99 LogPrintf(LogPHASE, "Connected to local client.\n");
100 break;
101
102 case AF_INET:
103 if (ntohs(sin->sin_port) < 1024) {
104 LogPrintf(LogALERT, "Rejected client connection from %s:%u"
105 "(invalid port number) !\n",
106 inet_ntoa(sin->sin_addr), ntohs(sin->sin_port));
107 close(wfd);
108 return;
109 }
110 LogPrintf(LogPHASE, "Connected to client from %s:%u\n",
111 inet_ntoa(sin->sin_addr), sin->sin_port);
112 break;
113
114 default:
115 write(wfd, "Unrecognised access !\n", 22);
116 close(wfd);
117 return;
118 }
119
120 if (netfd >= 0) {
121 write(wfd, "Connection already in use.\n", 27);
122 close(wfd);
123 } else {
124 netfd = wfd;
125 VarTerm = fdopen(netfd, "a+");
126 LocalAuthInit();
127 IsInteractive(1);
128 Prompt(bundle);
129 }
130}
131
132static void
133server_Write(struct descriptor *d)
134{
135 /* We never want to write here ! */
136 LogPrintf(LogERROR, "server_Write: Internal error: Bad call !\n");
137}
138
139struct server server = {
140 {
141 SERVER_DESCRIPTOR,
142 NULL,
143 server_UpdateSet,
144 server_IsSet,
145 server_Read,
146 server_Write
147 },
148 -1
149};
150
53static struct sockaddr_un ifsun;
54static char *rm;
55
56int
57ServerLocalOpen(const char *name, mode_t mask)
58{
59 int s;
60

--- 38 unchanged lines hidden (view full) ---

99 umask(mask);
100 if (listen(s, 5) != 0) {
101 LogPrintf(LogERROR, "Local: Unable to listen to socket - BUNDLE overload?\n");
102 close(s);
103 ID0unlink(name);
104 return 5;
105 }
106 ServerClose();
151static struct sockaddr_un ifsun;
152static char *rm;
153
154int
155ServerLocalOpen(const char *name, mode_t mask)
156{
157 int s;
158

--- 38 unchanged lines hidden (view full) ---

197 umask(mask);
198 if (listen(s, 5) != 0) {
199 LogPrintf(LogERROR, "Local: Unable to listen to socket - BUNDLE overload?\n");
200 close(s);
201 ID0unlink(name);
202 return 5;
203 }
204 ServerClose();
107 server = s;
205 server.fd = s;
108 rm = ifsun.sun_path;
109 LogPrintf(LogPHASE, "Listening at local socket %s.\n", name);
110 return 0;
111}
112
113int
114ServerTcpOpen(int port)
115{

--- 29 unchanged lines hidden (view full) ---

145 return 8;
146 }
147 if (listen(s, 5) != 0) {
148 LogPrintf(LogERROR, "Tcp: Unable to listen to socket - BUNDLE overload?\n");
149 close(s);
150 return 9;
151 }
152 ServerClose();
206 rm = ifsun.sun_path;
207 LogPrintf(LogPHASE, "Listening at local socket %s.\n", name);
208 return 0;
209}
210
211int
212ServerTcpOpen(int port)
213{

--- 29 unchanged lines hidden (view full) ---

243 return 8;
244 }
245 if (listen(s, 5) != 0) {
246 LogPrintf(LogERROR, "Tcp: Unable to listen to socket - BUNDLE overload?\n");
247 close(s);
248 return 9;
249 }
250 ServerClose();
153 server = s;
251 server.fd = s;
154 LogPrintf(LogPHASE, "Listening at port %d.\n", port);
155 return 0;
156}
157
252 LogPrintf(LogPHASE, "Listening at port %d.\n", port);
253 return 0;
254}
255
158void
256int
159ServerClose()
160{
257ServerClose()
258{
161 if (server >= 0) {
162 close(server);
259 if (server.fd >= 0) {
260 close(server.fd);
163 if (rm) {
164 ID0unlink(rm);
165 rm = 0;
166 }
261 if (rm) {
262 ID0unlink(rm);
263 rm = 0;
264 }
265 server.fd = -1;
266 return 1;
167 }
267 }
168 server = -1;
268 return 0;
169}
269}