cosmetics
[paraslash.git] / command.c
index 7df0108..ad3f504 100644 (file)
--- a/command.c
+++ b/command.c
@@ -18,7 +18,6 @@
 
 /** \file command.c does client authentication and executes server commands */
 
-#include <malloc.h> /* mallinfo */
 #include <sys/time.h> /* gettimeofday */
 #include "crypt.h"
 #include "server.cmdline.h"
 #include "net.h"
 #include "daemon.h"
 #include "string.h"
+#include "fd.h"
 
-void (*crypt_function_recv)(unsigned long len, const unsigned char *indata,
-       unsigned char *outdata) = NULL;
-void (*crypt_function_send)(unsigned long len, const unsigned char *indata,
-       unsigned char *outdata) = NULL;
 static RC4_KEY rc4_recv_key;
 static RC4_KEY rc4_send_key;
 static unsigned char rc4_buf[2 * RC4_KEY_LEN];
 
 extern const char *status_item_list[NUM_STAT_ITEMS];
 extern struct misc_meta_data *mmd;
-extern struct gengetopt_args_info conf;
 extern struct audio_file_selector selectors[];
-extern struct audio_format afl[];
 extern struct sender senders[];
 extern char *user_list;
 struct sockaddr_in *in_addr;
@@ -309,7 +303,7 @@ static struct server_command cmd_struct[] = {
 }
 };
 
-static void dummy(__unused int s)
+static void dummy(__a_unused int s)
 {}
 
 static void mmd_dup(struct misc_meta_data *new_mmd)
@@ -434,9 +428,12 @@ static char *get_status(struct misc_meta_data *nmmd)
                nmmd->audio_file_info,
 
                status_item_list[SI_UPTIME], ut,
-               status_item_list[SI_STREAM_START], nmmd->stream_start.tv_sec,
-                       nmmd->stream_start.tv_usec,
-               status_item_list[SI_CURRENT_TIME], now.tv_sec, now.tv_usec
+               status_item_list[SI_STREAM_START],
+                       (long unsigned)nmmd->stream_start.tv_sec,
+                       (long unsigned)nmmd->stream_start.tv_usec,
+               status_item_list[SI_CURRENT_TIME],
+                       (long unsigned)now.tv_sec,
+                       (long unsigned)now.tv_usec
 
        );
        free(bar);
@@ -554,12 +551,11 @@ static int com_sender(int fd, int argc, char **argv)
 }
 
 /* server info */
-static int com_si(int fd, int argc, __unused char **argv)
+static int com_si(int fd, int argc, __a_unused char **argv)
 {
        int i, ret;
        char *ut;
        char *selector_string = NULL, *sender_info = NULL, *sender_list = NULL;
-       struct mallinfo mi = mallinfo();
 
        if (argc != 1)
                return -E_COMMAND_SYNTAX;
@@ -578,7 +574,6 @@ static int com_si(int fd, int argc, __unused char **argv)
        ut = uptime_str();
        ret = send_va_buffer(fd, "up: %s\nplayed: %u\n"
                "pid: %d\n"
-               "mallinfo: %d\n"
                "connections (active/accepted/total): %u/%u/%u\n"
                "current loglevel: %i\n"
                "supported audio file selectors: %s\n"
@@ -587,13 +582,12 @@ static int com_si(int fd, int argc, __unused char **argv)
                "%s",
                ut, mmd->num_played,
                getppid(),
-               mi.arena / 1024,
                mmd->active_connections,
                mmd->num_commands,
                mmd->num_connects,
                conf.loglevel_arg,
                selector_string,
-               SUPPORTED_AUDIO_FORMATS,
+               supported_audio_formats(),
                sender_list,
                sender_info
        );
@@ -606,11 +600,12 @@ static int com_si(int fd, int argc, __unused char **argv)
 }
 
 /* version */
-static int com_version(int socket_fd, int argc, __unused char **argv)
+static int com_version(int socket_fd, int argc, __a_unused char **argv)
 {
        if (argc != 1)
                return -E_COMMAND_SYNTAX;
-       return send_buffer(socket_fd, "para_server-" VERSION ", \"" CODENAME "\"\n"
+       return send_buffer(socket_fd, "para_server-" PACKAGE_VERSION ", \""
+                       CODENAME "\"\n"
                        COPYRIGHT "\n"
                        "built: " BUILD_DATE "\n"
                        SYSTEM ", " CC_VERSION "\n"
@@ -696,7 +691,9 @@ static int com_stat(int socket_fd, int argc, char **argv)
                ret = 1;
                if (num == 1)
                        goto out;
-               usleep(500000 * 100);
+               sleep(50);
+               if (getppid() == 1)
+                       return -E_SERVER_CRASH;
        }
 out:
        return ret;
@@ -791,7 +788,7 @@ static int com_help(int fd, int argc, char **argv)
 }
 
 /* hup */
-static int com_hup(__unused int socket_fd, int argc, __unused char **argv)
+static int com_hup(__a_unused int socket_fd, int argc, __a_unused char **argv)
 {
        if (argc != 1)
                return -E_COMMAND_SYNTAX;
@@ -800,7 +797,7 @@ static int com_hup(__unused int socket_fd, int argc, __unused char **argv)
 }
 
 /* term */
-static int com_term(__unused int socket_fd, int argc, __unused char **argv)
+static int com_term(__a_unused int socket_fd, int argc, __a_unused char **argv)
 {
        if (argc != 1)
                return -E_COMMAND_SYNTAX;
@@ -808,7 +805,7 @@ static int com_term(__unused int socket_fd, int argc, __unused char **argv)
        return 1;
 }
 
-static int com_play(__unused int socket_fd, int argc, __unused char **argv)
+static int com_play(__a_unused int socket_fd, int argc, __a_unused char **argv)
 {
        if (argc != 1)
                return -E_COMMAND_SYNTAX;
@@ -821,7 +818,7 @@ static int com_play(__unused int socket_fd, int argc, __unused char **argv)
 }
 
 /* stop */
-static int com_stop(__unused int socket_fd, int argc, __unused char **argv)
+static int com_stop(__a_unused int socket_fd, int argc, __a_unused char **argv)
 {
        if (argc != 1)
                return -E_COMMAND_SYNTAX;
@@ -834,7 +831,7 @@ static int com_stop(__unused int socket_fd, int argc, __unused char **argv)
 }
 
 /* pause */
-static int com_pause(__unused int socket_fd, int argc, __unused char **argv)
+static int com_pause(__a_unused int socket_fd, int argc, __a_unused char **argv)
 {
        if (argc != 1)
                return -E_COMMAND_SYNTAX;
@@ -873,7 +870,7 @@ static int com_chs(int fd, int argc, char **argv)
 }
 
 /* next */
-static int com_next(__unused int socket_fd, int argc, __unused char **argv)
+static int com_next(__a_unused int socket_fd, int argc, __a_unused char **argv)
 {
        if (argc != 1)
                return -E_COMMAND_SYNTAX;
@@ -885,7 +882,7 @@ static int com_next(__unused int socket_fd, int argc, __unused char **argv)
 }
 
 /* nomore */
-static int com_nomore(__unused int socket_fd, int argc, __unused char **argv)
+static int com_nomore(__a_unused int socket_fd, int argc, __a_unused char **argv)
 {
        if (argc != 1)
                return -E_COMMAND_SYNTAX;
@@ -897,7 +894,7 @@ static int com_nomore(__unused int socket_fd, int argc, __unused char **argv)
 }
 
 /* ff */
-static int com_ff(__unused int socket_fd, int argc, char **argv)
+static int com_ff(__a_unused int socket_fd, int argc, char **argv)
 {
        long promille;
        int ret, backwards = 0;
@@ -936,7 +933,7 @@ out:
 }
 
 /* jmp */
-static int com_jmp(__unused int socket_fd, int argc, char **argv)
+static int com_jmp(__a_unused int socket_fd, int argc, char **argv)
 {
        long unsigned int i;
        int ret;
@@ -1015,51 +1012,48 @@ static int get_user(struct user *user) {
        char line[MAXLINE];
        /* keyword, user, key, perms */
        char w[MAXLINE], n[MAXLINE], k[MAXLINE], p[MAXLINE], tmp[4][MAXLINE];
-       int num;
+       int num, ret;
 
        file_ptr = open_user_list(user_list);
        if (!file_ptr)
                return -E_USERLIST;
-       while (fgets(line, MAXLINE, file_ptr)) {
-//             PARA_DEBUG_LOG("%s: Read line (%i bytes) "
-//                     "from config file\n", __func__, strlen(line));
+       for (;;) {
+               ret = para_fgets(line, MAXLINE, file_ptr);
+               if (ret < 0)
+                       PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
+               if (ret <= 0)
+                       break;
                if (sscanf(line,"%200s %200s %200s %200s", w, n, k, p) < 3)
                        continue;
-               if (!strcmp(w, "user") && !strcmp(user->name, n)) {
-                       PARA_DEBUG_LOG("found entry for %s\n", n);
-                       strcpy(user->name, n);
-                       strcpy(user->pubkey_file, k);
-                       user->perms = 0;
-                       char_ptr = p;
-                       num = sscanf(char_ptr, "%200[A-Z_],%200[A-Z_],%200[A-Z_],%200[A-Z_]",
-                               tmp[0], tmp[1], tmp[2], tmp[3]);
-                       PARA_DEBUG_LOG("found %i perm entries\n",
-                               num);
-                       user->perms = 0;
-                       while (num > 0) {
-                               num--;
-                               //PARA_DEBUG_LOG("%s: tmp[%i]=%s\n", __func__,
-                               //      num, tmp[num]);
-                               if (!strcmp(tmp[num], "AFS_READ"))
-                                       user->perms =
-                                               user->perms | AFS_READ;
-                               else if (!strcmp(tmp[num], "AFS_WRITE"))
-                                       user->perms =
-                                               user->perms | AFS_WRITE;
-                               else if (!strcmp(tmp[num], "DB_READ"))
-                                       user->perms = user->perms | DB_READ;
-                               else if (!strcmp(tmp[num], "DB_WRITE"))
-                                       user->perms = user->perms | DB_WRITE;
-                               else /* unknown permission */
-                                       PARA_WARNING_LOG("unknown permission:"
-                                               "%s\n", tmp[num]);
-                       }
-                       fclose(file_ptr);
-                       return 1;
+               if (strcmp(w, "user") || strcmp(user->name, n))
+                       continue;
+               PARA_DEBUG_LOG("found entry for %s\n", n);
+               strcpy(user->name, n);
+               strcpy(user->pubkey_file, k);
+               user->perms = 0;
+               char_ptr = p;
+               num = sscanf(char_ptr, "%200[A-Z_],%200[A-Z_],%200[A-Z_],%200[A-Z_]",
+                       tmp[0], tmp[1], tmp[2], tmp[3]);
+               PARA_DEBUG_LOG("found %i perm entries\n", num);
+               user->perms = 0;
+               while (num > 0) {
+                       num--;
+                       if (!strcmp(tmp[num], "AFS_READ"))
+                               user->perms = user->perms | AFS_READ;
+                       else if (!strcmp(tmp[num], "AFS_WRITE"))
+                               user->perms = user->perms | AFS_WRITE;
+                       else if (!strcmp(tmp[num], "DB_READ"))
+                               user->perms = user->perms | DB_READ;
+                       else if (!strcmp(tmp[num], "DB_WRITE"))
+                               user->perms = user->perms | DB_WRITE;
+                       else /* unknown permission */
+                               PARA_WARNING_LOG("unknown permission: %s\n",
+                                       tmp[num]);
                }
+               break;
        }
        fclose(file_ptr);
-       return 0;
+       return ret;
 }
 
 static void init_rc4_keys(void)
@@ -1075,12 +1069,14 @@ static void init_rc4_keys(void)
        RC4_set_key(&rc4_send_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
 }
 
-static void rc4_recv(unsigned long len, const unsigned char *indata, unsigned char *outdata)
+static void rc4_recv(unsigned long len, const unsigned char *indata,
+               unsigned char *outdata, __a_unused void *private_data)
 {
        RC4(&rc4_recv_key, len, indata, outdata);
 }
 
-static void rc4_send(unsigned long len, const unsigned char *indata, unsigned char *outdata)
+static void rc4_send(unsigned long len, const unsigned char *indata,
+               unsigned char *outdata, __a_unused void *private_data)
 {
        RC4(&rc4_send_key, len, indata, outdata);
 }
@@ -1105,7 +1101,8 @@ int handle_connect(int fd, struct sockaddr_in *addr)
        in_addr = addr;
        challenge_nr = random();
        /* send Welcome message */
-       ret = send_va_buffer(fd, "This is para_server, version " VERSION  ".\n" );
+       ret = send_va_buffer(fd, "This is para_server, version "
+               PACKAGE_VERSION  ".\n" );
        if (ret < 0)
                goto err_out;
        /* recv auth request line */
@@ -1173,11 +1170,8 @@ int handle_connect(int fd, struct sockaddr_in *addr)
        ret = send_bin_buffer(fd, buf, numbytes);
        if (ret < 0)
                goto err_out;
-       if (use_rc4) {
-               crypt_function_recv = rc4_recv;
-               crypt_function_send = rc4_send;
-               PARA_INFO_LOG("%s", "rc4 encrytion activated\n");
-       }
+       if (use_rc4)
+               enable_crypt(fd, rc4_recv, rc4_send, NULL);
        /* read command */
        while ((numbytes = recv_buffer(fd, buf, sizeof(buf))) > 0) {
 //             PARA_INFO_LOG("recvd: %s (%d)\n", buf, numbytes);