diff --git a/lewdfingerd.c b/lewdfingerd.c index 6fd4ae9..8296634 100644 --- a/lewdfingerd.c +++ b/lewdfingerd.c @@ -1,7 +1,9 @@ #define _GNU_SOURCE +#define _BSD_SOURCE #include #include #include +#include #include #include #include @@ -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"); + } + } }