X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=command.c;h=77d3ef7f0ddd223b0408285f1df18e61b45ddd6f;hp=7bde617e1a5406108c17a5f6e821dff5dbfb1c24;hb=47fedd501291bf9b0ca2204e2af3d1617b6f18a8;hpb=3a0018aba38f2d3c073a659b9b443245ffe349cb diff --git a/command.c b/command.c index 7bde617e..77d3ef7f 100644 --- a/command.c +++ b/command.c @@ -20,7 +20,7 @@ #include /* gettimeofday */ #include "server.cmdline.h" -#include "db.h" +#include "afs.h" #include "server.h" #include "vss.h" #include "send.h" @@ -34,6 +34,9 @@ #include "user_list.h" #include "server_command_list.h" +/** commands including options must be shorter than this */ +#define MAX_COMMAND_LEN 4096 + static RC4_KEY rc4_recv_key; static RC4_KEY rc4_send_key; static unsigned char rc4_buf[2 * RC4_KEY_LEN]; @@ -43,7 +46,6 @@ extern struct misc_meta_data *mmd; extern struct audio_file_selector selectors[]; extern struct sender senders[]; extern char *user_list; -struct sockaddr_in *in_addr; static void dummy(__a_unused int s) {} @@ -76,8 +78,8 @@ static char *cmd_perms_itohuman(unsigned int perms) { char *msg = para_malloc(5 * sizeof(char)); - msg[0] = perms & DB_READ? 'a' : '-'; - msg[1] = perms & DB_WRITE? 'A' : '-'; + msg[0] = perms & AFS_READ? 'a' : '-'; + msg[1] = perms & AFS_WRITE? 'A' : '-'; msg[2] = perms & VSS_READ? 'v' : '-'; msg[3] = perms & VSS_WRITE? 'V' : '-'; msg[4] = '\0'; @@ -342,20 +344,18 @@ int com_si(int fd, int argc, __a_unused char **argv) } /* version */ -int com_version(int socket_fd, int argc, __a_unused char **argv) +int com_version(int fd, int argc, __a_unused char **argv) { if (argc != 1) return -E_COMMAND_SYNTAX; - return send_buffer(socket_fd, "para_server-" PACKAGE_VERSION ", \"" - CODENAME "\"\n" - COPYRIGHT "\n" - "built: " BUILD_DATE "\n" - SYSTEM ", " CC_VERSION "\n" - ); + return send_buffer(fd, VERSION_TEXT("server") + "built: " BUILD_DATE "\n" + SYSTEM ", " CC_VERSION "\n" + ); } /* sc */ -int com_sc(int socket_fd, int argc, char **argv) +int com_sc(int fd, int argc, char **argv) { char *name = NULL; int ret, old = 0, count = -1; /* print af change forever */ @@ -370,7 +370,7 @@ repeat: } mmd_unlock(); if (name) { - ret = send_va_buffer(socket_fd, "%s\n", name); + ret = send_va_buffer(fd, "%s\n", name); free(name); name = NULL; if (ret < 0) @@ -383,7 +383,7 @@ repeat: } /* sb */ -int com_sb(int socket_fd, int argc, char **argv) +int com_sb(int fd, int argc, char **argv) { char *sb; int ret, nr = -1; /* status bar will be printed that many @@ -396,7 +396,7 @@ int com_sb(int socket_fd, int argc, char **argv) mmd_lock(); sb = get_sb_string(mmd); mmd_unlock(); - ret = send_va_buffer(socket_fd, "%s\n", sb); + ret = send_va_buffer(fd, "%s\n", sb); free(sb); if (ret < 0) return ret; @@ -410,7 +410,7 @@ int com_sb(int socket_fd, int argc, char **argv) } /* stat */ -int com_stat(int socket_fd, int argc, char **argv) +int com_stat(int fd, int argc, char **argv) { int ret, num = 0;/* status will be printed that many * times. num <= 0 means: print forever @@ -426,7 +426,7 @@ int com_stat(int socket_fd, int argc, char **argv) mmd_dup(nmmd); s = get_status(nmmd); - ret = send_buffer(socket_fd, s); + ret = send_buffer(fd, s); free(s); if (ret < 0) goto out; @@ -528,7 +528,7 @@ int com_help(int fd, int argc, char **argv) } /* hup */ -int com_hup(__a_unused int socket_fd, int argc, __a_unused char **argv) +int com_hup(__a_unused int fd, int argc, __a_unused char **argv) { if (argc != 1) return -E_COMMAND_SYNTAX; @@ -537,7 +537,7 @@ int com_hup(__a_unused int socket_fd, int argc, __a_unused char **argv) } /* term */ -int com_term(__a_unused int socket_fd, int argc, __a_unused char **argv) +int com_term(__a_unused int fd, int argc, __a_unused char **argv) { if (argc != 1) return -E_COMMAND_SYNTAX; @@ -545,7 +545,7 @@ int com_term(__a_unused int socket_fd, int argc, __a_unused char **argv) return 1; } -int com_play(__a_unused int socket_fd, int argc, __a_unused char **argv) +int com_play(__a_unused int fd, int argc, __a_unused char **argv) { if (argc != 1) return -E_COMMAND_SYNTAX; @@ -558,7 +558,7 @@ int com_play(__a_unused int socket_fd, int argc, __a_unused char **argv) } /* stop */ -int com_stop(__a_unused int socket_fd, int argc, __a_unused char **argv) +int com_stop(__a_unused int fd, int argc, __a_unused char **argv) { if (argc != 1) return -E_COMMAND_SYNTAX; @@ -571,7 +571,7 @@ int com_stop(__a_unused int socket_fd, int argc, __a_unused char **argv) } /* pause */ -int com_pause(__a_unused int socket_fd, int argc, __a_unused char **argv) +int com_pause(__a_unused int fd, int argc, __a_unused char **argv) { if (argc != 1) return -E_COMMAND_SYNTAX; @@ -610,7 +610,7 @@ int com_chs(int fd, int argc, char **argv) } /* next */ -int com_next(__a_unused int socket_fd, int argc, __a_unused char **argv) +int com_next(__a_unused int fd, int argc, __a_unused char **argv) { if (argc != 1) return -E_COMMAND_SYNTAX; @@ -622,7 +622,7 @@ int com_next(__a_unused int socket_fd, int argc, __a_unused char **argv) } /* nomore */ -int com_nomore(__a_unused int socket_fd, int argc, __a_unused char **argv) +int com_nomore(__a_unused int fd, int argc, __a_unused char **argv) { if (argc != 1) return -E_COMMAND_SYNTAX; @@ -634,7 +634,7 @@ int com_nomore(__a_unused int socket_fd, int argc, __a_unused char **argv) } /* ff */ -int com_ff(__a_unused int socket_fd, int argc, char **argv) +int com_ff(__a_unused int fd, int argc, char **argv) { long promille; int ret, backwards = 0; @@ -673,7 +673,7 @@ out: } /* jmp */ -int com_jmp(__a_unused int socket_fd, int argc, char **argv) +int com_jmp(__a_unused int fd, int argc, char **argv) { long unsigned int i; int ret; @@ -757,12 +757,43 @@ static void rc4_send(unsigned long len, const unsigned char *indata, RC4(&rc4_send_key, len, indata, outdata); } +/** + * perform user authentication and execute a command + * + * \param fd the file descriptor to send output to + * \param addr socket address info of peer + * + * \return EXIT_SUCCESS or EXIT_FAILURE + * + * Whenever para_server accepts an incoming tcp connection on + * the port it listens on, it forks and the resulting child + * calls this function. + * + * An RSA-based challenge/response is used to authenticate + * the peer. It that authentication succeeds, a random RC4 + * session key is generated and sent back to the peer, + * encrypted with its RSA public key. From this point on, + * all transfers are crypted with this session key. + * + * Next it is checked if the peer supplied a valid server + * command or a command for the audio file selector currently + * in use. If yes, and if the user has sufficient + * permissions to execute that command, the function calls + * the corresponding command handler which does argument + * checking and further processing. + * + * In order to cope with a DOS attacks, a timeout is set up + * which terminates the function if the connection was not + * authenticated when the timeout expires. + * + * \sa alarm(2), rc4(3), crypt.c, crypt.h + */ int handle_connect(int fd, struct sockaddr_in *addr) { int numbytes, ret, argc, use_rc4 = 0; - char buf[STRINGSIZE]; + char buf[4096]; unsigned char crypt_buf[MAXLINE]; - struct user u; + struct user *u; struct server_command *cmd = NULL; long unsigned challenge_nr, chall_response; char **argv = NULL; @@ -774,7 +805,6 @@ int handle_connect(int fd, struct sockaddr_in *addr) signal(SIGHUP, SIG_DFL); signal(SIGUSR1, SIG_IGN); - in_addr = addr; challenge_nr = random(); /* send Welcome message */ ret = send_va_buffer(fd, "This is para_server, version " @@ -795,16 +825,18 @@ int handle_connect(int fd, struct sockaddr_in *addr) goto err_out; if (numbytes < 9 || strncmp(buf, "auth rc4 ", 9)) - u.name = para_strdup(buf + 5); /* client version < 0.2.6 */ + p = buf + 5; /* client version < 0.2.6 */ else { - u.name = para_strdup(buf + 9); /* client version >= 0.2.6 */ + p = buf + 9; /* client version >= 0.2.6 */ use_rc4 = 1; } PARA_DEBUG_LOG("received %s request for user %s\n", - use_rc4? "rc4" : "auth", u.name); - if ((ret = lookup_user(&u)) < 0) + use_rc4? "rc4" : "auth", p); + ret = -E_BAD_USER; + u = lookup_user(p); + if (!u) goto err_out; - ret = para_encrypt_challenge(u.rsa, challenge_nr, crypt_buf); + ret = para_encrypt_challenge(u->rsa, challenge_nr, crypt_buf); if (ret <= 0) goto err_out; numbytes = ret; @@ -825,11 +857,11 @@ int handle_connect(int fd, struct sockaddr_in *addr) || chall_response != challenge_nr) goto err_out; /* auth successful. Send 'Proceed' message */ - PARA_INFO_LOG("good auth for %s (%lu)\n", u.name, challenge_nr); + PARA_INFO_LOG("good auth for %s (%lu)\n", u->name, challenge_nr); sprintf(buf, "%s", PROCEED_MSG); if (use_rc4) { init_rc4_keys(); - ret = para_encrypt_buffer(u.rsa, rc4_buf, 2 * RC4_KEY_LEN, + ret = para_encrypt_buffer(u->rsa, rc4_buf, 2 * RC4_KEY_LEN, (unsigned char *)buf + PROCEED_MSG_LEN + 1); if (ret <= 0) goto err_out; @@ -845,7 +877,7 @@ int handle_connect(int fd, struct sockaddr_in *addr) while ((numbytes = recv_buffer(fd, buf, sizeof(buf))) > 0) { // PARA_INFO_LOG("recvd: %s (%d)\n", buf, numbytes); ret = -E_COMMAND_SYNTAX; - if (command && numbytes + strlen(command) > STRINGSIZE) /* DOS */ + if (command && numbytes + strlen(command) > MAX_COMMAND_LEN) /* DOS */ goto err_out; command = para_strcat(command, buf); if ((p = strstr(command, EOC_MSG))) { @@ -861,7 +893,7 @@ int handle_connect(int fd, struct sockaddr_in *addr) if (!(cmd = parse_cmd(command))) goto err_out; /* valid command, check permissions */ - ret = check_perms(u.perms, cmd); + ret = check_perms(u->perms, cmd); if (ret < 0) goto err_out; /* valid command and sufficient perms */ @@ -870,7 +902,7 @@ int handle_connect(int fd, struct sockaddr_in *addr) mmd_lock(); mmd->num_commands++; mmd_unlock(); - PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u.name, + PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u->name, inet_ntoa(addr->sin_addr)); ret = cmd->handler(fd, argc, argv); if (ret >= 0) { @@ -887,7 +919,7 @@ out: free(command); free(argv); mmd_lock(); - if (cmd && (cmd->perms & DB_WRITE) && ret >= 0) + if (cmd && (cmd->perms & AFS_WRITE) && ret >= 0) mmd->events++; mmd->active_connections--; mmd_unlock();