05_DCCP-Remove-Unused.diff
[paraslash.git] / command.c
index c1bbba9aef86d7fccc936d2230f591ce3eb734d4..379cc4d90044659fa893b22781b4f685f8534bac 100644 (file)
--- a/command.c
+++ b/command.c
@@ -654,8 +654,8 @@ out:
 /**
  * perform user authentication and execute a command
  *
- * \param fd the file descriptor to send output to
- * \param addr socket address info of peer
+ * \param fd       The file descriptor to send output to
+ * \param peername  Identifies the connecting peer.
  *
  * \return EXIT_SUCCESS or EXIT_FAILURE
  *
@@ -680,7 +680,7 @@ out:
  *
  * \sa alarm(2), rc4(3), crypt.c, crypt.h
  */
-int handle_connect(int fd, struct sockaddr_in *addr)
+int handle_connect(int fd, const char *peername)
 {
        int ret, argc, use_rc4 = 0;
        char buf[4096];
@@ -698,6 +698,10 @@ int handle_connect(int fd, struct sockaddr_in *addr)
        signal(SIGHUP, SIG_DFL);
        signal(SIGUSR1, SIG_IGN);
 
+       /* we need a blocking fd here as recv() might return EAGAIN otherwise. */
+       ret = mark_fd_blocking(fd);
+       if (ret < 0)
+               goto err_out;
        challenge_nr = random();
        /* send Welcome message */
        ret = send_va_buffer(fd, "This is para_server, version "
@@ -737,15 +741,15 @@ int handle_connect(int fd, struct sockaddr_in *addr)
        /* We can't use send_buffer here since buf may contain null bytes */
        ret = send_bin_buffer(fd,(char *) crypt_buf, numbytes);
        if (ret < 0)
-               goto err_out;
+               goto net_err;
        /* recv decrypted number */
        ret = recv_buffer(fd, buf, sizeof(buf));
        if (ret < 0)
-               goto err_out;
+               goto net_err;
        numbytes = ret;
        ret = -E_AUTH;
        if (!numbytes)
-               goto err_out;
+               goto net_err;
        if (sscanf(buf, CHALLENGE_RESPONSE_MSG "%lu", &chall_response) < 1
                        || chall_response != challenge_nr)
                goto err_out;
@@ -763,12 +767,14 @@ int handle_connect(int fd, struct sockaddr_in *addr)
                numbytes = strlen(buf);
        ret = send_bin_buffer(fd, buf, numbytes);
        if (ret < 0)
-               goto err_out;
+               goto net_err;
        if (use_rc4)
                enable_crypt(fd, rc4_recv, rc4_send, NULL);
        ret = read_command(fd, &command);
-       if (ret < 0)
+       if (ret == -E_COMMAND_SYNTAX)
                goto err_out;
+       if (ret < 0)
+               goto net_err;
        ret = -E_BAD_CMD;
        cmd = parse_cmd(command);
        if (!cmd)
@@ -784,16 +790,16 @@ int handle_connect(int fd, struct sockaddr_in *addr)
        mmd->num_commands++;
        mmd_unlock();
        PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u->name,
-               inet_ntoa(addr->sin_addr));
+                       peername);
        ret = cmd->handler(fd, argc, argv);
        if (ret >= 0) {
                ret = EXIT_SUCCESS;
                goto out;
        }
 err_out:
+       send_va_buffer(fd, "%s\n", PARA_STRERROR(-ret));
+net_err:
        PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-ret));
-       if (ret != -E_SEND && ret != -E_RECV)
-               send_va_buffer(fd, "%s\n", PARA_STRERROR(-ret));
        ret = EXIT_FAILURE;
 out:
        free(command);