endif
.PHONY: all clean
-all: $(TARGET) $(if $(CC.w32),$(TARGET).exe,) setpass
+all: $(TARGET) setpass
clean:
- $(RM) $(TARGET) $(TARGET).exe setpass
+ $(RM) $(TARGET) setpass
$(TARGET): $(SOURCES) $(HEADERS)
$(CC) $(CFLAGS) $(LDFLAGS) $(SOURCES) $(LDLIBS) -o $@
$(TARGET)-cxx: $(SOURCES) $(HEADERS)
$(CXX) -x c++ $(CXXFLAGS) $(LDFLAGS) $(SOURCES) $(LDLIBS) -o $@
-$(TARGET).exe: $(SOURCES) $(HEADERS)
- $(CC.w32) $(CFLAGS.w32) $(LDFLAGS.w32) $(SOURCES) $(LDLIBS.w32) -o $@
setpass: src/setpass.c src/passwd.c src/crypt_blowfish.c src/passwd.h src/netsock.h
$(CC) $(CFLAGS) $(LDFLAGS) src/setpass.c src/passwd.c src/crypt_blowfish.c $(LDLIBS) -o $@
CFLAGS=$(WARNS) -DHAS_TLS -DNETPOLL_USE_POLL -D_GNU_SOURCE
CXXFLAGS=$(WARNSXX) -DHAS_TLS -DNETPOLL_USE_POLL -D_GNU_SOURCE
LDLIBS=-ltls
-CC.w32=i686-w64-mingw32-gcc
-CFLAGS.w32=$(WARNS) -DNETPOLL_USE_SELECT
-LDLIBS.w32=-lws2_32 -lbcrypt
USE_LUA=1
#include "common.h"
#include "job.h"
#include "passwd.h"
-#ifndef _WIN32
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
-#endif
struct jobent {
struct client_data *client;
static struct jobent *jobtab;
static int joblen = 0, jobcap = 0;
-#ifndef _WIN32
static pid_t current_child = -1;
static int current_result = 0;
static void job_child(int signum)
sigaddset(&mask, SIGCHLD);
sigprocmask(SIG_UNBLOCK, &mask, 0);
}
-#else
-void job_init(void)
-{
-}
-void job_process_begin(void)
-{
- struct jobent *ent = jobtab;
- while (joblen) {
- int res = ent->job(ent->client, ent->data, ent->len);
- memset(ent->data, 0, ent->len);
- free(ent->data);
- ent->then(ent->client, res);
- joblen--;
- ent++;
- }
-}
-void job_process_end(void)
-{
-}
-#endif
void job_add(struct client_data *cl, jobproc_t job, contproc_t then, char *data, size_t len)
{
{
struct jobent *p, *q;
p = q = jobtab;
-#ifndef _WIN32
if (joblen && jobtab[0].client == cl)
current_child = -1;
-#endif
while (p < jobtab+joblen) {
if (p->client != cl) {
*q++ = *p;
static int is_running = 1;
-#ifndef _WIN32
#include <signal.h>
static volatile sig_atomic_t is_siginted = 0;
static void on_sigint(int signum)
if (sigaction(SIGPIPE, &sa, NULL) == -1)
perror("sigaction");
}
-#else
-static int is_siginted_once = 0;
-static volatile int is_siginted = 0;
-static SOCKET self_sock = INVALID_SOCKET;
-static BOOL WINAPI on_sigint(DWORD dwCtrlType)
-{
- if ((dwCtrlType == CTRL_C_EVENT || dwCtrlType == CTRL_CLOSE_EVENT) && !is_siginted_once) {
- is_siginted_once = 1;
- is_siginted = 1;
- char x = 0;
- if (self_sock != INVALID_SOCKET) {
- send(self_sock, &x, 0, 0); // FIXME: this could EAGAIN in theory
- }
- return TRUE;
- } else {
- return FALSE;
- }
-}
-static void set_handler(void)
-{
- SetConsoleCtrlHandler(on_sigint, TRUE);
-
- self_sock = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0);
- if (self_sock == INVALID_SOCKET) {
- net_perror("ERROR CREATING SELF SOCK");
- return;
- }
- struct sockaddr_in sin;
- memset(&sin, 0, sizeof(sin));
- sin.sin_family = AF_INET;
- sin.sin_addr.s_addr = htonl(0x7F000069);
- int addrlen = sizeof(sin);
- if (bind(self_sock, (struct sockaddr *)&sin, sizeof(sin)) == -1)
- goto err;
- if (getsockname(self_sock, (struct sockaddr *)&sin, &addrlen) == -1)
- goto err;
- if (connect(self_sock, (struct sockaddr *)&sin, sizeof(sin)) == -1)
- goto err;
- if (net_nonblock(self_sock) == -1)
- goto err;
- net_poll_add(self_sock, SOCKTYPE_SELFPIPE, 0);
- net_poll_reg(self_sock, NETPOLL_IN);
- return;
-err:
- net_perror("ERROR CREATING SELF SOCK");
- closesocket(self_sock);
- self_sock = INVALID_SOCKET;
-}
-#endif
// Client linked list
struct client_data *clients = NULL, *dyingclients = NULL;
}
// Set socket options
int opt = 1;
-#ifndef _WIN32
if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&opt, sizeof(opt)) == -1) {
net_perror("ERROR SOCKET");
freeaddrinfo(res);
exit(EXIT_FAILURE);
}
-#endif
if(res->ai_family == AF_INET6 && setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&opt, sizeof(opt)) == -1) {
net_perror("ERROR SOCKET IPV6");
freeaddrinfo(res);
job_process_begin();
if (net_poll_begin() == -1) {
-#ifndef _WIN32
if (errno == EINTR) {
job_process_end();
continue;
}
-#endif
net_perror("ERROR SELECT");
job_process_end();
}
struct net_pollfd pfd;
while ((ev = net_poll_get(&pfd))) {
switch (pfd.type) {
-#ifdef _WIN32
- case SOCKTYPE_SELFPIPE:
- if (ev & NETPOLL_IN) {
- char x;
- while (recv(pfd.fd, &x, 0, 0) != -1)
- ;
- }
- break;
-#endif
case SOCKTYPE_LISTENER:
if (ev & NETPOLL_IN)
accept_from_sock((struct listener *)pfd.assoc);
}
}
-#ifdef _WIN32
- if (self_sock != INVALID_SOCKET) {
- net_poll_del(self_sock);
- closesocket(self_sock);
- }
-#endif
#ifdef HAS_LUA
admin_fini();
#endif
{
memcpy(&xreadfds, &readfds, sizeof(readfds));
memcpy(&xwritefds, &writefds, sizeof(writefds));
-#ifdef _WIN32
- int highfd = 0;
- int rv = select(highfd, &xreadfds, &xwritefds, 0, 0);
-#else
sigset_t mask;
sigprocmask(0, 0, &mask);
sigdelset(&mask, SIGCHLD);
int highfd = nfds ? fds[nfds-1].fd+1 : 0;
int rv = pselect(highfd, &xreadfds, &xwritefds, 0, 0, &mask);
-#endif
lastptr = 0;
return rv;
}
#include "netsock.h"
-#ifdef _WIN32
-void net_init(void)
-{
- WSADATA wsaData;
- int err = WSAStartup(MAKEWORD(2, 0), &wsaData);
- if (err) {
- fprintf(stderr, "Winsock API startup failed: %s\n", net_strerror(err));
- exit(EXIT_FAILURE);
- }
- fprintf(stderr, "[NetSock]: WinSock initialized V%d.%d!\n",LOBYTE(wsaData.wVersion), HIBYTE(wsaData.wVersion));
-}
-void net_cleanup(void)
-{
- WSACleanup();
-}
-const char *net_strerror(int e)
-{
- static char msg[256];
- if (!FormatMessageA(
- FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL, e, 0, msg, 256, NULL))
- sprintf(msg, "WSA Error %d", e);
- return msg;
-}
-#endif
-
int net_nonblock(SOCKET fd)
{
-#if !defined(_WIN32) && defined(O_NONBLOCK)
int f = fcntl(fd, F_GETFL);
if (f == -1)
return -1;
return fcntl(fd, F_SETFL, f | O_NONBLOCK);
-#else
- u_long v = 1;
- return ioctlsocket(fd, FIONBIO, &v);
-#endif
}
void net_perror(const char *p)
* Win32 has 64 as default, Linux has 1024. Increase the limit on Win32. */
#define FD_SETSIZE 1024
-/* Set target Win32 version to Vista.
- * In particular, inet_ntop and AI_NUMERICSERV require this. */
-#ifdef _WIN32
-#define WINVER 0x0600
-#define _WIN32_WINNT 0x0600
-#endif
-
/* Standard headers */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
-#ifdef _WIN32
-/* Win32 */
-/* Include winsock2.h first, because windows.h defauts to Winsock 1 */
-#include <winsock2.h>
-#include <ws2tcpip.h>
-#include <windows.h>
-#ifdef _WIN64
-#define PRISocket "llu"
-#else
-#define PRISocket "u"
-#endif
-#define net_errno() (WSAGetLastError())
-const char *net_strerror(int e);
-#define net_wouldblock() (WSAGetLastError() == WSAEWOULDBLOCK)
-void net_init(void);
-void net_cleanup(void);
-#define SHUT_RD SD_RECEIVE
-#define SHUT_WR SD_SEND
-#define SHUT_RDWR SD_BOTH
-#define SOCK_CLOEXEC 0
-#define FOPEN_CLOEXEC ""
-#define accept4(a,b,c,d) accept((a),(b),(c))
-#else
/* POSIX */
#include <unistd.h>
#include <netinet/in.h>
void net_perror(const char *p);
int net_nonblock(SOCKET fd);
-#endif
#include "netsock.h"
#include <stdio.h>
#include <string.h>
-#ifndef _WIN32
#include <unistd.h>
-#else
-#include <bcrypt.h>
-#endif
#include "passwd.h"
#include "crypt_blowfish.h"
char *password_crypt(const char *key, const char *setting)
"./0123456789ABCDEFGHIJKLMNOPQRST"
"UVWXYZabcdefghijklmnopqrstuvwxyz";
unsigned char ubytes[16];
-#ifndef _WIN32
if (getentropy(ubytes, 16) == -1)
return NULL;
-#else
- if (BCryptGenRandom(0, ubytes, 16, BCRYPT_USE_SYSTEM_PREFERRED_RNG))
- return NULL;
-#endif
char salt[7 + 22 + 1]; // 20 for SHA512
if (algo == 2) {
memcpy(salt, "$2b$10$", 7);