X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=client.c;h=9b39acd371e9efb36622c9b5b93a51169038a8be;hp=ce66ea7b7159c101d77896b09c77f433d1f05c54;hb=f5a29040feebefcec4472a67b3396b6bfae84f33;hpb=adbf9f09c9bf2a680c61b1ef3541c04f1cde97cf diff --git a/client.c b/client.c index ce66ea7b..9b39acd3 100644 --- a/client.c +++ b/client.c @@ -20,8 +20,6 @@ #include "para.h" #include "config.h" -#include -#include #include "client.cmdline.h" #include "crypt.h" #include "rc4.h" @@ -29,9 +27,6 @@ #include "net.h" #include "string.h" -/* A static variable for holding the line. */ -static char *line_read; - struct gengetopt_args_info args_info; /* @@ -54,29 +49,6 @@ void para_log(int ll, const char* fmt,...) va_end(argp); } -/* - * Read a string, and return a pointer to it. Returns NULL on EOF. - */ -static char *rl_gets(void) -{ - free(line_read); - /* Get a line from the user. */ - line_read = readline("para_client> "); - /* If the line has any text in it, save it on the history. */ - if (line_read && *line_read) - add_history(line_read); - return line_read; -} - -/* - * do several cleanups on sigint - */ -static void sigint_handler(__unused int i) -{ - rl_cleanup_after_signal(); - rl_reset_after_signal(); -} - void get_options(int argc, char *argv[], char **config_file, char **key_file) { @@ -152,7 +124,7 @@ static void append_str(char **data, const char* append) int main(int argc, char *argv[]) { - int sockfd, numbytes, i, interactive, received, ret; + int sockfd, numbytes, i, received, ret; struct hostent *he; struct sockaddr_in their_addr; char *command = NULL; @@ -160,7 +132,6 @@ int main(int argc, char *argv[]) char *auth_str; char *key_file, *config_file; long unsigned challenge_nr; - char *line; get_options(argc, argv, &config_file, &key_file); if (args_info.loglevel_arg <= NOTICE) @@ -176,38 +147,16 @@ int main(int argc, char *argv[]) args_info.hostname_arg, args_info.server_port_arg ); - interactive = args_info.inputs_num == 0? 1 : 0; - if (interactive) { - PARA_NOTICE_LOG("%s", "no command, entering interactive mode\n"); - signal(SIGINT, sigint_handler); - } else { - /* not interactive, concat args */ - for (i = 0; i < args_info.inputs_num; i++) - append_str(&command, args_info.inputs[i]); + if (!args_info.inputs_num) { + PARA_ERROR_LOG("%s", "syntax error\n"); + exit(EXIT_FAILURE); } -interactive_loop: + /* concat args */ + for (i = 0; i < args_info.inputs_num; i++) + append_str(&command, args_info.inputs[i]); + crypt_function_recv = NULL; crypt_function_send = NULL; - if (interactive) { - int i = 0; - char *p; - - rl_save_prompt(); - rl_message("\n"); - rl_kill_full_line(0, 0); - rl_free_line_state(); - /* read a line via readline */ - line = rl_gets(); - if (!line) - return 0; - if (!line[0]) - goto interactive_loop; - p = line; - while (sscanf(p, "%200s%n", buf, &i) == 1) { - append_str(&command, buf); - p += i; - } - } /* get the host info */ PARA_NOTICE_LOG("getting host info of %s\n", args_info.hostname_arg); @@ -240,7 +189,7 @@ interactive_loop: numbytes); buf[numbytes] = '\0'; PARA_ERROR_LOG("received the following instead: %s\n", buf); - goto write_out; + exit(EXIT_FAILURE); } PARA_INFO_LOG("<-- [challenge (%i bytes)]\n", numbytes); /* decrypt challenge number */ @@ -289,18 +238,16 @@ interactive_loop: if (send_buffer(sockfd, EOC_MSG "\n") < 0) exit(EXIT_FAILURE); PARA_NOTICE_LOG("%s", "command sent.\n"); -write_out: received = 0; - /* write server output to stdout */ - while ((numbytes = recv_bin_buffer(sockfd, buf, sizeof(buf))) > 0) { - int ret; - + while ((numbytes = recv_bin_buffer(sockfd, buf, sizeof(buf) - 1)) > 0) { + buf[numbytes] = '\0'; if (!received && strstr(buf, AWAITING_DATA_MSG)) { - PARA_NOTICE_LOG("%s", "<-- awaiting data\n"); - PARA_NOTICE_LOG("%s", "--> sending stdin\n"); + PARA_NOTICE_LOG("%s", "sending stdin\n"); while ((ret = read(STDIN_FILENO, buf, - sizeof(buf))) > 0) - send_bin_buffer(sockfd, buf, ret); + sizeof(buf))) > 0) { + if (send_bin_buffer(sockfd, buf, ret) < 0) + break; + } PARA_NOTICE_LOG("%s", "closing connection\n"); numbytes = 1; break; @@ -312,7 +259,5 @@ write_out: if (!numbytes) PARA_NOTICE_LOG("%s", "connection closed by peer\n"); close(sockfd); - if (interactive) - goto interactive_loop; - return 0; + return ret >= 0? 0: 1; }