1997-12-21 12:11:13 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $Id$
|
1997-10-26 01:04:02 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
1997-06-25 19:30:05 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1997-06-25 19:30:05 +00:00
|
|
|
#include <errno.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/un.h>
|
1997-06-25 19:30:05 +00:00
|
|
|
#include <unistd.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1997-11-22 03:37:54 +00:00
|
|
|
#include "command.h"
|
1997-06-25 19:30:05 +00:00
|
|
|
#include "mbuf.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "loadalias.h"
|
1997-11-09 14:18:55 +00:00
|
|
|
#include "defs.h"
|
1997-06-25 19:30:05 +00:00
|
|
|
#include "vars.h"
|
|
|
|
#include "server.h"
|
1997-11-09 06:22:49 +00:00
|
|
|
#include "id.h"
|
1997-06-25 19:30:05 +00:00
|
|
|
|
1997-11-09 22:07:29 +00:00
|
|
|
int server = -1;
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1997-06-25 19:30:05 +00:00
|
|
|
static struct sockaddr_un ifsun;
|
|
|
|
static char *rm;
|
|
|
|
|
|
|
|
int
|
1997-06-30 03:03:38 +00:00
|
|
|
ServerLocalOpen(const char *name, mode_t mask)
|
1997-06-25 19:30:05 +00:00
|
|
|
{
|
1997-08-25 00:29:32 +00:00
|
|
|
int s;
|
1997-06-25 19:30:05 +00:00
|
|
|
|
1997-09-04 00:38:22 +00:00
|
|
|
if (VarLocalAuth == LOCAL_DENY) {
|
|
|
|
LogPrintf(LogERROR, "Local: Can't open socket %s: No password "
|
|
|
|
"in ppp.secret\n", name);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1997-11-11 22:58:14 +00:00
|
|
|
if (mode & MODE_INTER) {
|
1997-09-09 21:51:39 +00:00
|
|
|
LogPrintf(LogERROR, "Local: Can't open socket in interactive mode\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1997-12-21 02:27:32 +00:00
|
|
|
memset(&ifsun, '\0', sizeof ifsun);
|
1997-08-25 00:29:32 +00:00
|
|
|
ifsun.sun_len = strlen(name);
|
|
|
|
if (ifsun.sun_len > sizeof ifsun.sun_path - 1) {
|
|
|
|
LogPrintf(LogERROR, "Local: %s: Path too long\n", name);
|
1997-09-04 00:38:22 +00:00
|
|
|
return 2;
|
1997-08-25 00:29:32 +00:00
|
|
|
}
|
|
|
|
ifsun.sun_family = AF_LOCAL;
|
|
|
|
strcpy(ifsun.sun_path, name);
|
1997-06-25 19:30:05 +00:00
|
|
|
|
1997-11-09 06:22:49 +00:00
|
|
|
s = ID0socket(PF_LOCAL, SOCK_STREAM, 0);
|
1997-08-25 00:29:32 +00:00
|
|
|
if (s < 0) {
|
|
|
|
LogPrintf(LogERROR, "Local: socket: %s\n", strerror(errno));
|
1997-09-04 00:38:22 +00:00
|
|
|
return 3;
|
1997-08-25 00:29:32 +00:00
|
|
|
}
|
|
|
|
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &s, sizeof s);
|
1997-11-09 22:07:29 +00:00
|
|
|
if (mask != (mode_t)-1)
|
|
|
|
mask = umask(mask);
|
1997-08-25 00:29:32 +00:00
|
|
|
if (bind(s, (struct sockaddr *) & ifsun, sizeof(ifsun)) < 0) {
|
1997-11-09 22:07:29 +00:00
|
|
|
if (mask != (mode_t)-1)
|
|
|
|
umask(mask);
|
1997-08-25 00:29:32 +00:00
|
|
|
LogPrintf(LogERROR, "Local: bind: %s\n", strerror(errno));
|
|
|
|
if (errno == EADDRINUSE && VarTerm)
|
|
|
|
fprintf(VarTerm, "Wait for a while, then try again.\n");
|
|
|
|
close(s);
|
1997-09-04 00:38:22 +00:00
|
|
|
return 4;
|
1997-08-25 00:29:32 +00:00
|
|
|
}
|
1997-11-09 22:07:29 +00:00
|
|
|
if (mask != (mode_t)-1)
|
|
|
|
umask(mask);
|
1997-08-25 00:29:32 +00:00
|
|
|
if (listen(s, 5) != 0) {
|
|
|
|
LogPrintf(LogERROR, "Local: Unable to listen to socket - OS overload?\n");
|
|
|
|
close(s);
|
1997-11-09 06:22:49 +00:00
|
|
|
ID0unlink(name);
|
1997-09-04 00:38:22 +00:00
|
|
|
return 5;
|
1997-08-25 00:29:32 +00:00
|
|
|
}
|
|
|
|
ServerClose();
|
|
|
|
server = s;
|
|
|
|
rm = ifsun.sun_path;
|
|
|
|
LogPrintf(LogPHASE, "Listening at local socket %s.\n", name);
|
|
|
|
return 0;
|
1997-06-25 19:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ServerTcpOpen(int port)
|
|
|
|
{
|
1997-08-25 00:29:32 +00:00
|
|
|
struct sockaddr_in ifsin;
|
|
|
|
int s;
|
1997-06-25 19:30:05 +00:00
|
|
|
|
1997-09-04 00:38:22 +00:00
|
|
|
if (VarLocalAuth == LOCAL_DENY) {
|
|
|
|
LogPrintf(LogERROR, "Tcp: Can't open socket %d: No password "
|
|
|
|
"in ppp.secret\n", port);
|
|
|
|
return 6;
|
|
|
|
}
|
1997-09-09 21:51:39 +00:00
|
|
|
|
1997-11-11 22:58:14 +00:00
|
|
|
if (mode & MODE_INTER) {
|
1997-09-09 21:51:39 +00:00
|
|
|
LogPrintf(LogERROR, "Tcp: Can't open socket in interactive mode\n");
|
|
|
|
return 6;
|
|
|
|
}
|
|
|
|
|
1997-11-09 06:22:49 +00:00
|
|
|
s = ID0socket(PF_INET, SOCK_STREAM, 0);
|
1997-08-25 00:29:32 +00:00
|
|
|
if (s < 0) {
|
|
|
|
LogPrintf(LogERROR, "Tcp: socket: %s\n", strerror(errno));
|
1997-09-04 00:38:22 +00:00
|
|
|
return 7;
|
1997-08-25 00:29:32 +00:00
|
|
|
}
|
1997-12-21 02:27:32 +00:00
|
|
|
memset(&ifsin, '\0', sizeof ifsin);
|
1997-08-25 00:29:32 +00:00
|
|
|
ifsin.sin_family = AF_INET;
|
|
|
|
ifsin.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
ifsin.sin_port = htons(port);
|
|
|
|
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &s, sizeof s);
|
|
|
|
if (bind(s, (struct sockaddr *) & ifsin, sizeof(ifsin)) < 0) {
|
|
|
|
LogPrintf(LogERROR, "Tcp: bind: %s\n", strerror(errno));
|
|
|
|
if (errno == EADDRINUSE && VarTerm)
|
|
|
|
fprintf(VarTerm, "Wait for a while, then try again.\n");
|
|
|
|
close(s);
|
1997-09-04 00:38:22 +00:00
|
|
|
return 8;
|
1997-08-25 00:29:32 +00:00
|
|
|
}
|
|
|
|
if (listen(s, 5) != 0) {
|
|
|
|
LogPrintf(LogERROR, "Tcp: Unable to listen to socket - OS overload?\n");
|
|
|
|
close(s);
|
1997-09-04 00:38:22 +00:00
|
|
|
return 9;
|
1997-08-25 00:29:32 +00:00
|
|
|
}
|
|
|
|
ServerClose();
|
|
|
|
server = s;
|
|
|
|
LogPrintf(LogPHASE, "Listening at port %d.\n", port);
|
|
|
|
return 0;
|
1997-06-25 19:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServerClose()
|
|
|
|
{
|
|
|
|
if (server >= 0) {
|
|
|
|
close(server);
|
|
|
|
if (rm) {
|
1997-11-09 06:22:49 +00:00
|
|
|
ID0unlink(rm);
|
1997-06-25 19:30:05 +00:00
|
|
|
rm = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
server = -1;
|
|
|
|
}
|