From ba4b7e126dd402c40287e981cb316d1d1415bce0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Sun, 22 Jul 2018 11:10:32 +0000 Subject: [PATCH] Read the .finger-response file if not executable --- lewdfingerd.c | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/lewdfingerd.c b/lewdfingerd.c index ae7a55c..77a89df 100644 --- a/lewdfingerd.c +++ b/lewdfingerd.c @@ -1,5 +1,6 @@ #define _GNU_SOURCE #define _BSD_SOURCE +#define _POSIX_C_SOURCE 200809L #include #include #include @@ -142,17 +143,57 @@ ssize_t writeall_str(int fd, const char *s) { return writeall(fd, s, strlen(s)); } -bool handle_finger_response(int sock, const char *finger_response_file) { +bool execute_finger_response(int sock, const char *finger_response_file) { + // TODO: Execute the file + (void)sock; (void)finger_response_file; - // TODO: Handle .finger-response - if(writeall_str(sock, "Lewd.\r\n") < 0) { - log_perror("writeall_str (lewd)"); + return true; +} + +bool read_finger_response(int sock, const char *finger_response_file) { + FILE *f = fopen(finger_response_file, "r"); + if(f == NULL) { + log_perror("fopen"); return false; } + size_t line_size = 0; + char *line = NULL; + ssize_t line_len; + while((line_len = getline(&line, &line_size, f)) > 0) { + // Strip the newline + if(line[line_len - 1] == '\n') { + line[line_len - 1] = '\0'; + line_len--; + } + + // Send the line along with the correct newline + if(writeall(sock, line, line_len) < 0 || writeall_str(sock, "\r\n") < 0) { + log_perror("writeall / writeall_str"); + free(line); + return false; + } + } + + if(!feof(f)) { + log_perror("getline"); + free(line); + return false; + } + + free(line); + fclose(f); return true; } +bool handle_finger_response(int sock, const char *finger_response_file) { + if(access(finger_response_file, X_OK) == 0) { + return execute_finger_response(sock, finger_response_file); + } else { + return read_finger_response(sock, finger_response_file); + } +} + void handle_userlist(int sock, bool force_multiline) { // Go over users' home dirs and see if .finger-response is present bool first_line = true;