Compare commits
No commits in common. "70c3403c6fbaf8f4f0a37db8a5ad6db00de9559c" and "772f1ef657e0610e4eacb3ccbf7c6a39b5120bac" have entirely different histories.
70c3403c6f
...
772f1ef657
2 changed files with 25 additions and 41 deletions
14
Makefile
14
Makefile
|
@ -9,19 +9,15 @@ LDFLAGS +=
|
|||
|
||||
all: lewdfingerd
|
||||
|
||||
install: all
|
||||
install lewdfingerd $(BINDIR)
|
||||
|
||||
lewdfingerd: lewdfingerd.c
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o $@ $<
|
||||
|
||||
.PHONY: all install uninstall clean distclean
|
||||
|
||||
install: all
|
||||
mkdir -p $(BINDIR)
|
||||
install lewdfingerd $(BINDIR)
|
||||
|
||||
uninstall:
|
||||
rm -f $(BINDIR)/lewdfingerd
|
||||
.PHONY: all install clean distclean
|
||||
|
||||
clean:
|
||||
rm -f lewdfingerd
|
||||
rm lewdfingerd
|
||||
|
||||
distclean: clean
|
||||
|
|
|
@ -23,10 +23,6 @@ void log_error(const char * restrict format, ...) {
|
|||
va_end(args);
|
||||
}
|
||||
|
||||
void log_perror(const char *string) {
|
||||
perror(string);
|
||||
}
|
||||
|
||||
void setup_listen(void) {
|
||||
struct addrinfo hints;
|
||||
struct addrinfo *getaddrinfo_result;
|
||||
|
@ -54,33 +50,33 @@ void setup_listen(void) {
|
|||
// Create socket
|
||||
int sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
||||
if(sock == -1) {
|
||||
log_perror("socket");
|
||||
perror("socket");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Disable the IPv4 over IPv6, as that results in IPv4 and IPv6 sockets conflicting and one of them not being able to be set up
|
||||
if(res->ai_family == AF_INET6) {
|
||||
if(setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) == -1) {
|
||||
log_perror("setsockopt");
|
||||
perror("setsockopt");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Set reuseaddr
|
||||
if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) {
|
||||
log_perror("setsockopt");
|
||||
perror("setsockopt");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Bind onto given address
|
||||
if(bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
|
||||
log_perror("bind");
|
||||
perror("bind");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Listen for incoming connections
|
||||
if(listen(sock, 1) == -1) {
|
||||
log_perror("listen");
|
||||
perror("listen");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
@ -94,7 +90,7 @@ void setup_listen(void) {
|
|||
listens = realloc(listens, num_listens * sizeof(struct pollfd));
|
||||
|
||||
if(listens == NULL) {
|
||||
log_perror("realloc");
|
||||
perror("realloc");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
@ -111,11 +107,11 @@ void drop_privileges(void) {
|
|||
gid_t gid = getgid();
|
||||
|
||||
if(setresgid(gid, gid, gid) != 0) {
|
||||
log_perror("setresgid");
|
||||
perror("setresgid");
|
||||
exit(1);
|
||||
}
|
||||
if(setresuid(uid, uid, uid) != 0) {
|
||||
log_perror("setresuid");
|
||||
perror("setresuid");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
@ -134,20 +130,6 @@ ssize_t writeall(int fd, const char *buf, size_t amount) {
|
|||
return written;
|
||||
}
|
||||
|
||||
void handle_userlist(int sock) {
|
||||
const char *response = "Who do you want to finger?\r\n";
|
||||
if(writeall(sock, response, strlen(response)) < 0) {
|
||||
log_perror("write");
|
||||
}
|
||||
}
|
||||
|
||||
void handle_query(int sock, const char *username) {
|
||||
const char *response = "Lewd.\r\n";
|
||||
if(writeall(sock, response, strlen(response)) < 0) {
|
||||
log_perror("write");
|
||||
}
|
||||
}
|
||||
|
||||
void handle_connection(int sock) {
|
||||
size_t request_size = 0;
|
||||
char *request = NULL;
|
||||
|
@ -164,7 +146,7 @@ void handle_connection(int sock) {
|
|||
close(sock);
|
||||
return;
|
||||
} else if(amount_read < 0) {
|
||||
log_perror("read");
|
||||
perror("read");
|
||||
shutdown(sock, SHUT_RDWR);
|
||||
close(sock);
|
||||
return;
|
||||
|
@ -173,7 +155,7 @@ void handle_connection(int sock) {
|
|||
size_t index = request_size;
|
||||
request_size += amount_read;
|
||||
if((request = realloc(request, request_size)) == NULL) {
|
||||
log_perror("realloc");
|
||||
perror("realloc");
|
||||
exit(1);
|
||||
}
|
||||
memmove(request + index, iobuf, amount_read);
|
||||
|
@ -187,9 +169,15 @@ void handle_connection(int sock) {
|
|||
request[request_size-2] = '\0';
|
||||
|
||||
if(!strcmp(request, "")) {
|
||||
handle_userlist(sock);
|
||||
const char *response = "Who do you want to finger?\r\n";
|
||||
if(writeall(sock, response, strlen(response)) < 0) {
|
||||
perror("write");
|
||||
}
|
||||
} else {
|
||||
handle_query(sock, request);
|
||||
const char *response = "Lewd.\r\n";
|
||||
if(writeall(sock, response, strlen(response)) < 0) {
|
||||
perror("write");
|
||||
}
|
||||
}
|
||||
|
||||
shutdown(sock, SHUT_RDWR);
|
||||
|
@ -197,13 +185,13 @@ void handle_connection(int sock) {
|
|||
}
|
||||
|
||||
int main(void) {
|
||||
setup_listen();
|
||||
drop_privileges();
|
||||
setup_listen();
|
||||
|
||||
for(;;) {
|
||||
int amount_ready = poll(listens, num_listens, -1);
|
||||
if(amount_ready < 0) {
|
||||
log_perror("poll");
|
||||
perror("poll");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue