|
version 1.1.1.2, 2013/07/22 08:44:29
|
version 1.1.1.4.2.1, 2023/09/27 11:08:01
|
|
Line 15
|
Line 15
|
| #include <libutil.h> |
#include <libutil.h> |
| #include <netdb.h> |
#include <netdb.h> |
| #include <tcpd.h> |
#include <tcpd.h> |
| #include <sys/wait.h> | #include <sys/limits.h> |
| | #include <sys/socket.h> |
| #include <sys/sysctl.h> |
#include <sys/sysctl.h> |
| |
#include <sys/wait.h> |
| #include <net/route.h> |
#include <net/route.h> |
| #include <netinet/if_ether.h> |
#include <netinet/if_ether.h> |
| |
#include <net/ethernet.h> |
| |
#include <osreldate.h> |
| |
|
| /* |
/* |
| * DEFINITIONS |
* DEFINITIONS |
|
Line 78 static const u_int16_t Crc16Table[256] = {
|
Line 82 static const u_int16_t Crc16Table[256] = {
|
| */ |
*/ |
| |
|
| static void Escape(char *line); |
static void Escape(char *line); |
| static char *ReadLine(FILE *fp, int *lineNum, char *result, int resultsize); | static char *ReadLine(FILE *fp, int *lineNum, char *result, size_t resultsize); |
| |
|
| static char HexVal(char c); |
static char HexVal(char c); |
| |
|
| static void IndexConfFile(FILE *fp, struct configfile **cf); |
static void IndexConfFile(FILE *fp, struct configfile **cf); |
| |
|
| struct configfiles *ConfigFilesIndex=NULL; | static struct configfiles *ConfigFilesIndex=NULL; |
| |
|
| #undef isspace |
#undef isspace |
| #define isspace(c) (((c)==' '||(c)=='\t'||(c)=='\n'||(c)=='\r')?1:0) |
#define isspace(c) (((c)==' '||(c)=='\t'||(c)=='\n'||(c)=='\r')?1:0) |
|
Line 116 int
|
Line 120 int
|
| ExecCmd(int log, const char *label, const char *fmt, ...) |
ExecCmd(int log, const char *label, const char *fmt, ...) |
| { |
{ |
| int rtn; |
int rtn; |
| char cmd[BIG_LINE_SIZE]; | char cmd[LINE_MAX]; |
| char cmdn[BIG_LINE_SIZE]; | char cmdn[LINE_MAX]; |
| va_list ap; |
va_list ap; |
| |
|
| va_start(ap, fmt); |
va_start(ap, fmt); |
|
Line 151 int
|
Line 155 int
|
| ExecCmdNosh(int log, const char *label, const char *fmt, ...) |
ExecCmdNosh(int log, const char *label, const char *fmt, ...) |
| { |
{ |
| int rtn; |
int rtn; |
| char cmd[BIG_LINE_SIZE]; | char cmd[LINE_MAX]; |
| char *cmdp = &(cmd[0]); |
char *cmdp = &(cmd[0]); |
| char *argv[256]; |
char *argv[256]; |
| char **arg; |
char **arg; |
|
Line 376 Escape(char *line)
|
Line 380 Escape(char *line)
|
| |
|
| int |
int |
| ReadFile(const char *filename, const char *target, |
ReadFile(const char *filename, const char *target, |
| int (*func)(Context ctx, int ac, char *av[], const char *file, int line), Context ctx) | int (*func)(Context ctx, int ac, const char *const av[], const char *file, int line), Context ctx) |
| { |
{ |
| FILE *fp; |
FILE *fp; |
| int ac; |
int ac; |
|
Line 389 ReadFile(const char *filename, const char *target,
|
Line 393 ReadFile(const char *filename, const char *target,
|
| /* Open file */ |
/* Open file */ |
| |
|
| if ((fp = OpenConfFile(filename, &cf)) == NULL) |
if ((fp = OpenConfFile(filename, &cf)) == NULL) |
| return(-1); | return(-2); |
| |
|
| /* Find label */ |
/* Find label */ |
| |
|
|
Line 407 ReadFile(const char *filename, const char *target,
|
Line 411 ReadFile(const char *filename, const char *target,
|
| break; |
break; |
| } |
} |
| ac = ParseLine(line, av, sizeof(av) / sizeof(*av), 0); |
ac = ParseLine(line, av, sizeof(av) / sizeof(*av), 0); |
| (*func)(ctx, ac, av, filename, lineNum); | (*func)(ctx, ac, (const char *const *)av, filename, lineNum); |
| } |
} |
| |
|
| /* Done */ |
/* Done */ |
|
Line 556 OpenConfFile(const char *name, struct configfile **cf)
|
Line 560 OpenConfFile(const char *name, struct configfile **cf)
|
| char * |
char * |
| ReadFullLine(FILE *fp, int *lineNum, char *result, int resultsize) |
ReadFullLine(FILE *fp, int *lineNum, char *result, int resultsize) |
| { |
{ |
| int len, linelen, resultlinesize, continuation; | int len, resultlinesize, continuation; |
| | unsigned linelen; |
| char line[BIG_LINE_SIZE]; |
char line[BIG_LINE_SIZE]; |
| char real_line[BIG_LINE_SIZE]; |
char real_line[BIG_LINE_SIZE]; |
| char *resultline; |
char *resultline; |
|
Line 624 ReadFullLine(FILE *fp, int *lineNum, char *result, int
|
Line 629 ReadFullLine(FILE *fp, int *lineNum, char *result, int
|
| */ |
*/ |
| |
|
| static char * |
static char * |
| ReadLine(FILE *fp, int *lineNum, char *result, int resultsize) | ReadLine(FILE *fp, int *lineNum, char *result, size_t resultsize) |
| { |
{ |
| int empty; |
int empty; |
| char *s; |
char *s; |
|
Line 851 GenerateMagic(void)
|
Line 856 GenerateMagic(void)
|
| int |
int |
| PIDCheck(const char *filename, int killem) |
PIDCheck(const char *filename, int killem) |
| { |
{ |
| int fd = -1, n_tries; | int n_tries; |
| | struct pidfh *pfh = NULL; |
| |
|
| /* Sanity */ |
/* Sanity */ |
| |
|
|
Line 859 PIDCheck(const char *filename, int killem)
|
Line 865 PIDCheck(const char *filename, int killem)
|
| |
|
| /* Atomically open and lock file */ |
/* Atomically open and lock file */ |
| |
|
| for (n_tries = 0; | for (n_tries = 0; n_tries < MAX_LOCK_ATTEMPTS; n_tries++) |
| n_tries < MAX_LOCK_ATTEMPTS | |
| && (fd = open(filename, O_RDWR|O_CREAT|O_EXLOCK|O_NONBLOCK, 0644)) < 0; | |
| n_tries++) | |
| { |
{ |
| int nscan, old_pid; | pid_t old_pid; |
| FILE *fp; | |
| |
|
| /* Abort on any unexpected errors */ | pfh = pidfile_open(filename, 0644, &old_pid); |
| if (pfh == NULL) { |
| if (errno != EAGAIN) | if (errno == EEXIST) { |
| { | if (!killem) { |
| Perror("%s: open(%s)", __FUNCTION__, filename); | Log(LG_ERR, ("already running as process %d", old_pid)); |
| return(-1); | return(-1); |
| | } |
| | if (kill(old_pid, SIGTERM) < 0) |
| | switch (errno) { |
| | case ESRCH: |
| | Log(LG_ERR, ("process %d no longer exists", old_pid)); |
| | break; |
| | default: |
| | Perror("%s: kill(%d)", __FUNCTION__, old_pid); |
| | return(-1); |
| | } |
| | /* Wait and try again */ |
| | Log(LG_ERR, ("waiting for process %d to die...", old_pid)); |
| | sleep(1); |
| | } else { |
| | Perror("cannot open pid file"); |
| | return(-1); |
| | } |
| | } else { |
| | pidfile_write(pfh); |
| | break; |
| } |
} |
| |
|
| /* We're already running ... see who it is */ |
|
| |
|
| if ((fp = fopen(filename, "r")) == NULL) |
|
| { |
|
| Perror("%s: fopen(%s)", __FUNCTION__, filename); |
|
| return(-1); |
|
| } |
|
| |
|
| /* If there's a PID in there, sniff it out */ |
|
| |
|
| nscan = fscanf(fp, "%d", &old_pid); |
|
| fclose(fp); |
|
| if (nscan != 1) |
|
| { |
|
| Log(LG_ERR, ("%s: contents mangled", filename)); |
|
| return(-1); |
|
| } |
|
| |
|
| /* Maybe kill the other guy */ |
|
| |
|
| if (!killem) |
|
| { |
|
| Log(LG_ERR, ("already running as process %d", old_pid)); |
|
| return(-1); |
|
| } |
|
| if (kill(old_pid, SIGTERM) < 0) |
|
| switch (errno) |
|
| { |
|
| case ESRCH: |
|
| Log(LG_ERR, ("process %d no longer exists", old_pid)); |
|
| break; |
|
| default: |
|
| Perror("%s: kill(%d)", __FUNCTION__, old_pid); |
|
| return(-1); |
|
| } |
|
| |
|
| /* Wait and try again */ |
|
| |
|
| Log(LG_ERR, ("waiting for process %d to die...", old_pid)); |
|
| sleep(1); |
|
| } |
} |
| if (n_tries == MAX_LOCK_ATTEMPTS) |
if (n_tries == MAX_LOCK_ATTEMPTS) |
| { |
{ |
| Log(LG_ERR, ("can't lock %s after %d attempts", filename, n_tries)); |
Log(LG_ERR, ("can't lock %s after %d attempts", filename, n_tries)); |
| return(-1); |
return(-1); |
| } |
} |
| |
|
| /* Close on exec */ |
|
| |
|
| (void) fcntl(fd, F_SETFD, 1); |
|
| |
|
| /* Create a stream on top of file descriptor */ |
|
| |
|
| if ((lockFp = fdopen(fd, "r+")) == NULL) |
|
| { |
|
| Perror("%s: fdopen", __FUNCTION__); |
|
| return(-1); |
|
| } |
|
| setbuf(lockFp, NULL); |
|
| |
|
| /* Write my PID in there */ |
|
| |
|
| rewind(lockFp); |
|
| fprintf(lockFp, "%u\n", (u_int) gPid); |
|
| fflush(lockFp); |
|
| (void) ftruncate(fileno(lockFp), ftell(lockFp)); |
|
| return(0); |
return(0); |
| } |
} |
| |
|
|
Line 961 GetInetSocket(int type, struct u_addr *addr, in_port_t
|
Line 922 GetInetSocket(int type, struct u_addr *addr, in_port_t
|
| |
|
| /* Get and bind non-blocking socket */ |
/* Get and bind non-blocking socket */ |
| |
|
| if ((sock = socket(sa.ss_family, type, type == SOCK_STREAM ? IPPROTO_TCP : 0)) < 0) | if ((sock = socket(sa.ss_family, socktype(type), type == SOCK_STREAM ? IPPROTO_TCP : 0)) < 0) |
| { |
{ |
| snprintf(ebuf, len, "socket: %s", strerror(errno)); |
snprintf(ebuf, len, "socket: %s", strerror(errno)); |
| return(-1); |
return(-1); |
|
Line 1019 TcpGetListenPort(struct u_addr *addr, in_port_t port,
|
Line 980 TcpGetListenPort(struct u_addr *addr, in_port_t port,
|
| |
|
| /* Make socket available for connections */ |
/* Make socket available for connections */ |
| |
|
| if (listen(sock, 2) < 0) | if (listen(sock, INT_MAX) < 0) |
| { |
{ |
| Perror("%s: listen", __FUNCTION__); |
Perror("%s: listen", __FUNCTION__); |
| (void) close(sock); |
(void) close(sock); |
|
Line 1092 ShowMesg(int log, const char *pref, const char *buf, i
|
Line 1053 ShowMesg(int log, const char *pref, const char *buf, i
|
| |
|
| if (len > 0) |
if (len > 0) |
| { |
{ |
| if (len > sizeof(mesg) - 1) | if (len > (int)(sizeof(mesg) - 1)) |
| len = sizeof(mesg) - 1; |
len = sizeof(mesg) - 1; |
| memcpy(mesg, buf, len); |
memcpy(mesg, buf, len); |
| mesg[len] = 0; |
mesg[len] = 0; |
|
Line 1135 Bin2Hex(const unsigned char *bin, size_t len)
|
Line 1096 Bin2Hex(const unsigned char *bin, size_t len)
|
| u_char * |
u_char * |
| Hex2Bin(char *hexstr) |
Hex2Bin(char *hexstr) |
| { |
{ |
| int i; | unsigned i; |
| u_char *binval; |
u_char *binval; |
| |
|
| binval = Malloc(MB_UTIL, strlen(hexstr) / 2); |
binval = Malloc(MB_UTIL, strlen(hexstr) / 2); |
|
Line 1218 GetAnyIpAddress(struct u_addr *ipaddr, const char *ifn
|
Line 1179 GetAnyIpAddress(struct u_addr *ipaddr, const char *ifn
|
| close(s); |
close(s); |
| return(-1); |
return(-1); |
| } |
} |
| ipa = ((struct sockaddr_in *)&ifreq.ifr_ifru.ifru_addr)->sin_addr; | ipa = ((struct sockaddr_in *)(void *)&ifreq.ifr_ifru.ifru_addr)->sin_addr; |
| if ((ntohl(ipa.s_addr)>>24) == 127) |
if ((ntohl(ipa.s_addr)>>24) == 127) |
| ipa.s_addr = 0; /* We don't like 127.0.0.1 */ |
ipa.s_addr = 0; /* We don't like 127.0.0.1 */ |
| } |
} |
|
Line 1238 GetAnyIpAddress(struct u_addr *ipaddr, const char *ifn
|
Line 1199 GetAnyIpAddress(struct u_addr *ipaddr, const char *ifn
|
| } |
} |
| |
|
| /* if used size is too close to allocated size retry with a larger buffer */ |
/* if used size is too close to allocated size retry with a larger buffer */ |
| if (ifc.ifc_len + 128 < buffsize) | if ((unsigned)ifc.ifc_len + 128 < buffsize) |
| break; |
break; |
| |
|
| Freee(ifs); |
Freee(ifs); |
|
Line 1338 GetEther(struct u_addr *addr, struct sockaddr_dl *hwad
|
Line 1299 GetEther(struct u_addr *addr, struct sockaddr_dl *hwad
|
| } |
} |
| |
|
| /* if used size is too close to allocated size retry with a larger buffer */ |
/* if used size is too close to allocated size retry with a larger buffer */ |
| if (ifc.ifc_len + 128 < buffsize) | if ((unsigned)ifc.ifc_len + 128 < buffsize) |
| break; |
break; |
| |
|
| Freee(ifs); |
Freee(ifs); |
|
Line 1473 GetPeerEther(struct u_addr *addr, struct sockaddr_dl *
|
Line 1434 GetPeerEther(struct u_addr *addr, struct sockaddr_dl *
|
| } |
} |
| lim = buf + needed; |
lim = buf + needed; |
| for (next = buf; next < lim; next += rtm->rtm_msglen) { |
for (next = buf; next < lim; next += rtm->rtm_msglen) { |
| rtm = (struct rt_msghdr *)next; | rtm = (struct rt_msghdr *)(void *)next; |
| sin2 = (struct sockaddr_inarp *)(rtm + 1); |
sin2 = (struct sockaddr_inarp *)(rtm + 1); |
| if (addr->u.ip4.s_addr == sin2->sin_addr.s_addr) { |
if (addr->u.ip4.s_addr == sin2->sin_addr.s_addr) { |
| sdl = (struct sockaddr_dl *)((char *)sin2 + SA_SIZE(sin2)); | sdl = (struct sockaddr_dl *)(void *)((char *)sin2 + SA_SIZE(sin2)); |
| memcpy(hwaddr, sdl, sdl->sdl_len); |
memcpy(hwaddr, sdl, sdl->sdl_len); |
| found_entry = 1; |
found_entry = 1; |
| break; |
break; |
|
Line 1493 void
|
Line 1454 void
|
| ppp_util_ascify(char *buf, size_t bsiz, const char *data, size_t len) |
ppp_util_ascify(char *buf, size_t bsiz, const char *data, size_t len) |
| { |
{ |
| char *bp; |
char *bp; |
| int i; | unsigned i; |
| |
|
| for (bp = buf, i = 0; i < len; i++) { |
for (bp = buf, i = 0; i < len; i++) { |
| const char ch = (char)data[i]; |
const char ch = (char)data[i]; |
|
Line 1531 ppp_util_ascify(char *buf, size_t bsiz, const char *da
|
Line 1492 ppp_util_ascify(char *buf, size_t bsiz, const char *da
|
| *bp = '\0'; |
*bp = '\0'; |
| } |
} |
| |
|
| |
#ifndef HAVE_NTOA_R |
| |
/* |
| |
* Convert a binary representation of an ethernet address to an ASCII string. |
| |
*/ |
| |
char * |
| |
ether_ntoa_r(const struct ether_addr *n, char *a) |
| |
{ |
| |
int i; |
| |
|
| |
i = sprintf(a, "%02x:%02x:%02x:%02x:%02x:%02x", n->octet[0], |
| |
n->octet[1], n->octet[2], n->octet[3], n->octet[4], n->octet[5]); |
| |
if (i < 17) |
| |
return (NULL); |
| |
return (a); |
| |
} |
| |
#endif |
| |
|
| |
int |
| |
IfaceSetFlag(const char *ifname, int value) |
| |
{ |
| |
struct ifreq my_ifr; |
| |
int s; |
| |
int flags; |
| |
|
| |
/* Get socket */ |
| |
if ((s = socket(PF_LOCAL, SOCK_DGRAM, 0)) < 0) { |
| |
Perror("Can't get socket to set flags"); |
| |
return(-1); |
| |
} |
| |
|
| |
memset(&my_ifr, 0, sizeof(my_ifr)); |
| |
(void) strlcpy(my_ifr.ifr_name, ifname, sizeof(my_ifr.ifr_name)); |
| |
|
| |
if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) { |
| |
Perror("ioctl (SIOCGIFFLAGS)"); |
| |
close(s); |
| |
return (-1); |
| |
} |
| |
flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16); |
| |
|
| |
if (value < 0) { |
| |
value = -value; |
| |
flags &= ~value; |
| |
} else |
| |
flags |= value; |
| |
my_ifr.ifr_flags = flags & 0xffff; |
| |
my_ifr.ifr_flagshigh = flags >> 16; |
| |
if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0) { |
| |
Perror("ioctl (SIOCSIFFLAGS)"); |
| |
close(s); |
| |
return (-1); |
| |
} |
| |
close(s); |
| |
return (0); |
| |
} |
| |
|
| |
/* |
| |
* Obtain some data, peer (source) and destination addresses of SOCK_DGRAM IPv4 UDP request. |
| |
*/ |
| |
ssize_t GetDataAddrs(int sock, void *dbuf, size_t dbufsize, |
| |
struct sockaddr_storage *peer, socklen_t peer_len, |
| |
struct u_addr *addr) |
| |
{ |
| |
struct { |
| |
struct msghdr msg; |
| |
struct iovec iov; |
| |
} b; |
| |
union { /* ensure correct alignment for space */ |
| |
struct cmsghdr cm; |
| |
char space[CMSG_SPACE(sizeof(struct in_addr))]; |
| |
} buf; |
| |
|
| |
struct cmsghdr *p; |
| |
ssize_t size; |
| |
|
| |
/* Sanity check */ |
| |
if (addr->family != AF_INET) { |
| |
errno = EAFNOSUPPORT; |
| |
return (-1); |
| |
} |
| |
|
| |
b.msg.msg_name = peer; |
| |
b.msg.msg_namelen = peer_len; |
| |
b.msg.msg_iov = &b.iov; |
| |
b.msg.msg_iovlen = 1; |
| |
b.msg.msg_control = &buf; |
| |
b.msg.msg_controllen = sizeof(buf); |
| |
b.msg.msg_flags = 0; |
| |
|
| |
b.iov.iov_base = dbuf; |
| |
b.iov.iov_len = dbufsize; |
| |
|
| |
if ((size = recvmsg(sock, &b.msg, 0)) < 0) { |
| |
Perror("%s: recvmsg: %s", __FUNCTION__, strerror(errno)); |
| |
return (size); |
| |
} |
| |
|
| |
p = CMSG_FIRSTHDR(&b.msg); |
| |
if (p && p->cmsg_level == IPPROTO_IP && p->cmsg_type == IP_RECVDSTADDR) |
| |
memcpy(&addr->u.ip4, CMSG_DATA(p), sizeof(addr->u.ip4)); |
| |
|
| |
return (size); |
| |
} |
| |
|
| |
uint16_t GetSystemIfaceMTU(const char *ifname) |
| |
{ |
| |
struct ifreq ifr; |
| |
static int sock = -1; |
| |
|
| |
if (sock == -1 && |
| |
(sock = socket(PF_INET, socktype(SOCK_DGRAM), 0)) == -1) { |
| |
Perror("[%s] %s: Socket creation error", ifname, __FUNCTION__); |
| |
return (0); |
| |
} |
| |
|
| |
memset(&ifr, 0, sizeof(ifr)); |
| |
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); |
| |
|
| |
if (ioctl(sock, SIOCGIFMTU, (caddr_t)&ifr) == -1) { |
| |
Perror("[%s] %s: SIOCGIFMTU failed", ifname, __FUNCTION__); |
| |
return (0); |
| |
} |
| |
/* Let _exit() close sock */ |
| |
return (ifr.ifr_mtu); |
| |
} |