Check if user has .finger-response

This commit is contained in:
Juhani Krekelä 2018-07-16 18:52:44 +00:00
parent 9f057f691f
commit 4365a00b2a
1 changed files with 57 additions and 16 deletions

View File

@ -1,7 +1,9 @@
#define _GNU_SOURCE
#define _BSD_SOURCE
#include <errno.h>
#include <netdb.h>
#include <poll.h>
#include <pwd.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
@ -135,38 +137,77 @@ ssize_t writeall(int fd, const char *buf, size_t amount) {
return written;
}
ssize_t writeall_str(int fd, const char *s) {
return writeall(fd, s, strlen(s));
}
void handle_userlist(int sock) {
// Go over users' home dirs and see if .finger-response is present
FILE *passwd = fopen("/etc/passwd", "r");
for(;;) {
char *line = NULL;
size_t line_length;
errno = 0;
if(getline(&line, &line_length, passwd) < 0) {
struct passwd *passwd_entry = getpwent();
if(passwd_entry == NULL) {
if(errno == 0) {
// EOF reached
// End of file
endpwent();
break;
} else {
perror("getline");
free(line);
log_perror("getpwent");
return;
}
}
printf("%s", line); //debg
char *finger_response_file;
if(asprintf(&finger_response_file, "%s/.finger-response", passwd_entry->pw_dir) < 0) {
log_perror("asprintf");
return;
}
free(line);
if(access(finger_response_file, F_OK) == 0) {
if(writeall_str(sock, passwd_entry->pw_name) < 0) {
log_perror("writeall_str");
return;
}
if(writeall_str(sock, "\r\n") < 0) {
log_perror("writeall_str");
return;
}
}
}
fclose(passwd);
}
void handle_query(int sock, const char *username) {
const char *response = "Lewd.\r\n";
if(writeall(sock, response, strlen(response)) < 0) {
log_perror("write");
errno = 0;
struct passwd *passwd_entry = getpwnam(username);
if(passwd_entry == NULL) {
if(errno == 0) {
if(writeall_str(sock, "Can't finger\r\n") < 0) {
log_perror("writeall_str");
return;
}
} else {
log_perror("getpwent");
}
return;
}
char *finger_response_file;
if(asprintf(&finger_response_file, "%s/.finger-response", passwd_entry->pw_dir) < 0) {
log_perror("asprintf");
return;
}
if(access(finger_response_file, F_OK) != 0) {
if(writeall_str(sock, "Can't finger\r\n") < 0) {
log_perror("writeall_str");
return;
}
} else {
const char *response = "Lewd.\r\n";
if(writeall(sock, response, strlen(response)) < 0) {
log_perror("write");
}
}
}