comparison cli-main.c @ 1219:84cf9062718d coverity

merge
author Matt Johnston <matt@ucc.asn.au>
date Tue, 15 Dec 2015 22:24:34 +0800
parents 3daff2072bd0
children f7d565054e5f
comparison
equal deleted inserted replaced
1196:a29559086628 1219:84cf9062718d
34 34
35 static void cli_dropbear_exit(int exitcode, const char* format, va_list param) ATTRIB_NORETURN; 35 static void cli_dropbear_exit(int exitcode, const char* format, va_list param) ATTRIB_NORETURN;
36 static void cli_dropbear_log(int priority, const char* format, va_list param); 36 static void cli_dropbear_log(int priority, const char* format, va_list param);
37 37
38 #ifdef ENABLE_CLI_PROXYCMD 38 #ifdef ENABLE_CLI_PROXYCMD
39 static void cli_proxy_cmd(int *sock_in, int *sock_out); 39 static void cli_proxy_cmd(int *sock_in, int *sock_out, pid_t *pid_out);
40 static void kill_proxy_sighandler(int signo);
40 #endif 41 #endif
41 42
42 #if defined(DBMULTI_dbclient) || !defined(DROPBEAR_MULTI) 43 #if defined(DBMULTI_dbclient) || !defined(DROPBEAR_MULTI)
43 #if defined(DBMULTI_dbclient) && defined(DROPBEAR_MULTI) 44 #if defined(DBMULTI_dbclient) && defined(DROPBEAR_MULTI)
44 int cli_main(int argc, char ** argv) { 45 int cli_main(int argc, char ** argv) {
57 seedrandom(); 58 seedrandom();
58 crypto_init(); 59 crypto_init();
59 60
60 cli_getopts(argc, argv); 61 cli_getopts(argc, argv);
61 62
63 #ifndef DISABLE_SYSLOG
64 if (opts.usingsyslog) {
65 startsyslog("dbclient");
66 }
67 #endif
68
62 TRACE(("user='%s' host='%s' port='%s'", cli_opts.username, 69 TRACE(("user='%s' host='%s' port='%s'", cli_opts.username,
63 cli_opts.remotehost, cli_opts.remoteport)) 70 cli_opts.remotehost, cli_opts.remoteport))
64 71
65 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) { 72 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
66 dropbear_exit("signal() error"); 73 dropbear_exit("signal() error");
67 } 74 }
68 75
76 pid_t proxy_cmd_pid = 0;
69 #ifdef ENABLE_CLI_PROXYCMD 77 #ifdef ENABLE_CLI_PROXYCMD
70 if (cli_opts.proxycmd) { 78 if (cli_opts.proxycmd) {
71 cli_proxy_cmd(&sock_in, &sock_out); 79 cli_proxy_cmd(&sock_in, &sock_out, &proxy_cmd_pid);
72 m_free(cli_opts.proxycmd); 80 m_free(cli_opts.proxycmd);
81 if (signal(SIGINT, kill_proxy_sighandler) == SIG_ERR ||
82 signal(SIGTERM, kill_proxy_sighandler) == SIG_ERR ||
83 signal(SIGHUP, kill_proxy_sighandler) == SIG_ERR) {
84 dropbear_exit("signal() error");
85 }
73 } else 86 } else
74 #endif 87 #endif
75 { 88 {
76 progress = connect_remote(cli_opts.remotehost, cli_opts.remoteport, cli_connected, &ses); 89 progress = connect_remote(cli_opts.remotehost, cli_opts.remoteport, cli_connected, &ses);
77 sock_in = sock_out = -1; 90 sock_in = sock_out = -1;
78 } 91 }
79 92
80 cli_session(sock_in, sock_out, progress); 93 cli_session(sock_in, sock_out, progress, proxy_cmd_pid);
81 94
82 /* not reached */ 95 /* not reached */
83 return -1; 96 return -1;
84 } 97 }
85 #endif /* DBMULTI stuff */ 98 #endif /* DBMULTI stuff */
109 122
110 dropbear_log(LOG_INFO, "%s", exitmsg);; 123 dropbear_log(LOG_INFO, "%s", exitmsg);;
111 exit(exitcode); 124 exit(exitcode);
112 } 125 }
113 126
114 static void cli_dropbear_log(int UNUSED(priority), 127 static void cli_dropbear_log(int priority,
115 const char* format, va_list param) { 128 const char* format, va_list param) {
116 129
117 char printbuf[1024]; 130 char printbuf[1024];
118 131
119 vsnprintf(printbuf, sizeof(printbuf), format, param); 132 vsnprintf(printbuf, sizeof(printbuf), format, param);
133
134 #ifndef DISABLE_SYSLOG
135 if (opts.usingsyslog) {
136 syslog(priority, "%s", printbuf);
137 }
138 #endif
120 139
121 fprintf(stderr, "%s: %s\n", cli_opts.progname, printbuf); 140 fprintf(stderr, "%s: %s\n", cli_opts.progname, printbuf);
122 fflush(stderr); 141 fflush(stderr);
123 } 142 }
124 143
130 run_shell_command(cmd, ses.maxfd, usershell); 149 run_shell_command(cmd, ses.maxfd, usershell);
131 dropbear_exit("Failed to run '%s'\n", cmd); 150 dropbear_exit("Failed to run '%s'\n", cmd);
132 } 151 }
133 152
134 #ifdef ENABLE_CLI_PROXYCMD 153 #ifdef ENABLE_CLI_PROXYCMD
135 static void cli_proxy_cmd(int *sock_in, int *sock_out) { 154 static void cli_proxy_cmd(int *sock_in, int *sock_out, pid_t *pid_out) {
136 int ret; 155 int ret;
137 156
138 fill_passwd(cli_opts.own_user); 157 fill_passwd(cli_opts.own_user);
139 158
140 ret = spawn_command(exec_proxy_cmd, cli_opts.proxycmd, 159 ret = spawn_command(exec_proxy_cmd, cli_opts.proxycmd,
141 sock_out, sock_in, NULL, NULL); 160 sock_out, sock_in, NULL, pid_out);
142 if (ret == DROPBEAR_FAILURE) { 161 if (ret == DROPBEAR_FAILURE) {
143 dropbear_exit("Failed running proxy command"); 162 dropbear_exit("Failed running proxy command");
144 *sock_in = *sock_out = -1; 163 *sock_in = *sock_out = -1;
145 } 164 }
146 } 165 }
166
167 static void kill_proxy_sighandler(int UNUSED(signo)) {
168 kill_proxy_command();
169 _exit(1);
170 }
147 #endif /* ENABLE_CLI_PROXYCMD */ 171 #endif /* ENABLE_CLI_PROXYCMD */