server.c (85602e5267f413a849370d3c6fe3d0a87cdd5b49) server.c (565e35e50e2cdac423588a3d18742544bde128b0)
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.2.13 1998/04/07 00:54:17 brian Exp $
26 * $Id: server.c,v 1.16.2.14 1998/04/07 23:46:09 brian Exp $
27 */
28
29#include <sys/types.h>
30#include <sys/socket.h>
31#include <netinet/in.h>
32#include <arpa/inet.h>
33#include <netinet/in_systm.h>
34#include <netinet/ip.h>

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

83 struct server *s = descriptor2server(d);
84 return s->fd >= 0 && FD_ISSET(s->fd, fdset);
85}
86
87#define IN_SIZE sizeof(struct sockaddr_in)
88#define UN_SIZE sizeof(struct sockaddr_in)
89#define ADDRSZ (IN_SIZE > UN_SIZE ? IN_SIZE : UN_SIZE)
90
27 */
28
29#include <sys/types.h>
30#include <sys/socket.h>
31#include <netinet/in.h>
32#include <arpa/inet.h>
33#include <netinet/in_systm.h>
34#include <netinet/ip.h>

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

83 struct server *s = descriptor2server(d);
84 return s->fd >= 0 && FD_ISSET(s->fd, fdset);
85}
86
87#define IN_SIZE sizeof(struct sockaddr_in)
88#define UN_SIZE sizeof(struct sockaddr_in)
89#define ADDRSZ (IN_SIZE > UN_SIZE ? IN_SIZE : UN_SIZE)
90
91static char *rm;
92
93static void
94server_Read(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
95{
96 struct server *s = descriptor2server(d);
97 char hisaddr[ADDRSZ];
98 struct sockaddr *sa = (struct sockaddr *)hisaddr;
99 struct sockaddr_in *sin = (struct sockaddr_in *)hisaddr;
100 int ssize = ADDRSZ, wfd;

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

130 }
131
132 if ((p = prompt_Create(s, bundle, wfd)) == NULL) {
133 write(wfd, "Connection refused.\n", 20);
134 close(wfd);
135 } else {
136 switch (sa->sa_family) {
137 case AF_LOCAL:
91static void
92server_Read(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
93{
94 struct server *s = descriptor2server(d);
95 char hisaddr[ADDRSZ];
96 struct sockaddr *sa = (struct sockaddr *)hisaddr;
97 struct sockaddr_in *sin = (struct sockaddr_in *)hisaddr;
98 int ssize = ADDRSZ, wfd;

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

128 }
129
130 if ((p = prompt_Create(s, bundle, wfd)) == NULL) {
131 write(wfd, "Connection refused.\n", 20);
132 close(wfd);
133 } else {
134 switch (sa->sa_family) {
135 case AF_LOCAL:
138 snprintf(p->who, sizeof p->who, "local (%s)", rm);
136 p->src.type = "local";
137 strncpy(p->src.from, s->rm, sizeof p->src.from - 1);
138 p->src.from[sizeof p->src.from - 1] = '\0';
139 break;
140 case AF_INET:
139 break;
140 case AF_INET:
141 snprintf(p->who, sizeof p->who, "TCP (%s:%u)",
141 p->src.type = "tcp";
142 snprintf(p->src.from, sizeof p->src.from, "%s:%u",
142 inet_ntoa(sin->sin_addr), sin->sin_port);
143 break;
144 }
145 prompt_TtyCommandMode(p);
146 prompt_Required(p);
147 }
148}
149

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

161 server_UpdateSet,
162 server_IsSet,
163 server_Read,
164 server_Write
165 },
166 -1
167};
168
143 inet_ntoa(sin->sin_addr), sin->sin_port);
144 break;
145 }
146 prompt_TtyCommandMode(p);
147 prompt_Required(p);
148 }
149}
150

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

162 server_UpdateSet,
163 server_IsSet,
164 server_Read,
165 server_Write
166 },
167 -1
168};
169
169static struct sockaddr_un ifsun;
170
171int
172ServerLocalOpen(struct bundle *bundle, const char *name, mode_t mask)
173{
174 int s;
175
170int
171ServerLocalOpen(struct bundle *bundle, const char *name, mode_t mask)
172{
173 int s;
174
176 memset(&ifsun, '\0', sizeof ifsun);
177 ifsun.sun_len = strlen(name);
178 if (ifsun.sun_len > sizeof ifsun.sun_path - 1) {
175 if (server.rm && !strcmp(server.rm, name)) {
176 if (chmod(server.rm, mask))
177 LogPrintf(LogERROR, "Local: chmod: %s\n", strerror(errno));
178 return 0;
179 }
180
181 memset(&server.ifsun, '\0', sizeof server.ifsun);
182 server.ifsun.sun_len = strlen(name);
183 if (server.ifsun.sun_len > sizeof server.ifsun.sun_path - 1) {
179 LogPrintf(LogERROR, "Local: %s: Path too long\n", name);
180 return 2;
181 }
184 LogPrintf(LogERROR, "Local: %s: Path too long\n", name);
185 return 2;
186 }
182 ifsun.sun_family = AF_LOCAL;
183 strcpy(ifsun.sun_path, name);
187 server.ifsun.sun_family = AF_LOCAL;
188 strcpy(server.ifsun.sun_path, name);
184
185 s = ID0socket(PF_LOCAL, SOCK_STREAM, 0);
186 if (s < 0) {
187 LogPrintf(LogERROR, "Local: socket: %s\n", strerror(errno));
188 return 3;
189 }
190 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &s, sizeof s);
191 if (mask != (mode_t)-1)
192 mask = umask(mask);
189
190 s = ID0socket(PF_LOCAL, SOCK_STREAM, 0);
191 if (s < 0) {
192 LogPrintf(LogERROR, "Local: socket: %s\n", strerror(errno));
193 return 3;
194 }
195 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &s, sizeof s);
196 if (mask != (mode_t)-1)
197 mask = umask(mask);
193 if (bind(s, (struct sockaddr *)&ifsun, sizeof ifsun) < 0) {
198 if (bind(s, (struct sockaddr *)&server.ifsun, sizeof server.ifsun) < 0) {
194 if (mask != (mode_t)-1)
195 umask(mask);
196 LogPrintf(LogWARN, "Local: bind: %s\n", strerror(errno));
197 close(s);
198 return 4;
199 }
200 if (mask != (mode_t)-1)
201 umask(mask);
202 if (listen(s, 5) != 0) {
203 LogPrintf(LogERROR, "Local: Unable to listen to socket - BUNDLE overload?\n");
204 close(s);
205 ID0unlink(name);
206 return 5;
207 }
208 ServerClose(bundle);
209 server.fd = s;
199 if (mask != (mode_t)-1)
200 umask(mask);
201 LogPrintf(LogWARN, "Local: bind: %s\n", strerror(errno));
202 close(s);
203 return 4;
204 }
205 if (mask != (mode_t)-1)
206 umask(mask);
207 if (listen(s, 5) != 0) {
208 LogPrintf(LogERROR, "Local: Unable to listen to socket - BUNDLE overload?\n");
209 close(s);
210 ID0unlink(name);
211 return 5;
212 }
213 ServerClose(bundle);
214 server.fd = s;
210 rm = ifsun.sun_path;
215 server.rm = server.ifsun.sun_path;
211 LogPrintf(LogPHASE, "Listening at local socket %s.\n", name);
212 return 0;
213}
214
215int
216ServerTcpOpen(struct bundle *bundle, int port)
217{
218 struct sockaddr_in ifsin;
219 int s;
220
216 LogPrintf(LogPHASE, "Listening at local socket %s.\n", name);
217 return 0;
218}
219
220int
221ServerTcpOpen(struct bundle *bundle, int port)
222{
223 struct sockaddr_in ifsin;
224 int s;
225
226 if (server.port == port)
227 return 0;
228
221 s = ID0socket(PF_INET, SOCK_STREAM, 0);
222 if (s < 0) {
223 LogPrintf(LogERROR, "Tcp: socket: %s\n", strerror(errno));
224 return 7;
225 }
226 memset(&ifsin, '\0', sizeof ifsin);
227 ifsin.sin_family = AF_INET;
228 ifsin.sin_addr.s_addr = INADDR_ANY;

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

235 }
236 if (listen(s, 5) != 0) {
237 LogPrintf(LogERROR, "Tcp: Unable to listen to socket - BUNDLE overload?\n");
238 close(s);
239 return 9;
240 }
241 ServerClose(bundle);
242 server.fd = s;
229 s = ID0socket(PF_INET, SOCK_STREAM, 0);
230 if (s < 0) {
231 LogPrintf(LogERROR, "Tcp: socket: %s\n", strerror(errno));
232 return 7;
233 }
234 memset(&ifsin, '\0', sizeof ifsin);
235 ifsin.sin_family = AF_INET;
236 ifsin.sin_addr.s_addr = INADDR_ANY;

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

243 }
244 if (listen(s, 5) != 0) {
245 LogPrintf(LogERROR, "Tcp: Unable to listen to socket - BUNDLE overload?\n");
246 close(s);
247 return 9;
248 }
249 ServerClose(bundle);
250 server.fd = s;
251 server.port = port;
243 LogPrintf(LogPHASE, "Listening at port %d.\n", port);
244 return 0;
245}
246
247int
248ServerClose(struct bundle *bundle)
249{
250 if (server.fd >= 0) {
251 close(server.fd);
252 LogPrintf(LogPHASE, "Listening at port %d.\n", port);
253 return 0;
254}
255
256int
257ServerClose(struct bundle *bundle)
258{
259 if (server.fd >= 0) {
260 close(server.fd);
252 if (rm) {
253 ID0unlink(rm);
254 rm = 0;
261 if (server.rm) {
262 ID0unlink(server.rm);
263 server.rm = NULL;
255 }
256 server.fd = -1;
264 }
265 server.fd = -1;
266 server.port = 0;
257 /* Drop associated prompts */
258 bundle_DelPromptDescriptors(bundle, &server);
259 return 1;
260 }
261 return 0;
262}
267 /* Drop associated prompts */
268 bundle_DelPromptDescriptors(bundle, &server);
269 return 1;
270 }
271 return 0;
272}