comparison cli-main.c @ 1318:10e2a7727253 coverity

merge coverity
author Matt Johnston <matt@ucc.asn.au>
date Fri, 22 Jul 2016 00:08:02 +0800
parents 2c9dac2d6707
children 58a74cb829b8
comparison
equal deleted inserted replaced
1286:7d02b83c61fd 1318:10e2a7727253
33 #include "netio.h" 33 #include "netio.h"
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 #if DROPBEAR_CLI_PROXYCMD
39 static void cli_proxy_cmd(int *sock_in, int *sock_out, pid_t *pid_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 static void kill_proxy_sighandler(int signo);
41 #endif 41 #endif
42 42
43 #if defined(DBMULTI_dbclient) || !defined(DROPBEAR_MULTI) 43 #if defined(DBMULTI_dbclient) || !DROPBEAR_MULTI
44 #if defined(DBMULTI_dbclient) && defined(DROPBEAR_MULTI) 44 #if defined(DBMULTI_dbclient) && DROPBEAR_MULTI
45 int cli_main(int argc, char ** argv) { 45 int cli_main(int argc, char ** argv) {
46 #else 46 #else
47 int main(int argc, char ** argv) { 47 int main(int argc, char ** argv) {
48 #endif 48 #endif
49 49
72 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) { 72 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
73 dropbear_exit("signal() error"); 73 dropbear_exit("signal() error");
74 } 74 }
75 75
76 pid_t proxy_cmd_pid = 0; 76 pid_t proxy_cmd_pid = 0;
77 #ifdef ENABLE_CLI_PROXYCMD 77 #if DROPBEAR_CLI_PROXYCMD
78 if (cli_opts.proxycmd) { 78 if (cli_opts.proxycmd) {
79 cli_proxy_cmd(&sock_in, &sock_out, &proxy_cmd_pid); 79 cli_proxy_cmd(&sock_in, &sock_out, &proxy_cmd_pid);
80 m_free(cli_opts.proxycmd); 80 m_free(cli_opts.proxycmd);
81 if (signal(SIGINT, kill_proxy_sighandler) == SIG_ERR || 81 if (signal(SIGINT, kill_proxy_sighandler) == SIG_ERR ||
82 signal(SIGTERM, kill_proxy_sighandler) == SIG_ERR || 82 signal(SIGTERM, kill_proxy_sighandler) == SIG_ERR ||
96 return -1; 96 return -1;
97 } 97 }
98 #endif /* DBMULTI stuff */ 98 #endif /* DBMULTI stuff */
99 99
100 static void cli_dropbear_exit(int exitcode, const char* format, va_list param) { 100 static void cli_dropbear_exit(int exitcode, const char* format, va_list param) {
101 char exitmsg[150];
102 char fullmsg[300];
101 103
102 char fmtbuf[300]; 104 /* Note that exit message must be rendered before session cleanup */
103 char exitmsg[500];
104 105
106 /* Render the formatted exit message */
107 vsnprintf(exitmsg, sizeof(exitmsg), format, param);
108
109 /* Add the prefix depending on session/auth state */
105 if (!sessinitdone) { 110 if (!sessinitdone) {
106 snprintf(fmtbuf, sizeof(fmtbuf), "Exited: %s", 111 snprintf(fullmsg, sizeof(fullmsg), "Exited: %s", exitmsg);
107 format);
108 } else { 112 } else {
109 snprintf(fmtbuf, sizeof(fmtbuf), 113 snprintf(fullmsg, sizeof(fullmsg),
110 "Connection to %s@%s:%s exited: %s", 114 "Connection to %s@%s:%s exited: %s",
111 cli_opts.username, cli_opts.remotehost, 115 cli_opts.username, cli_opts.remotehost,
112 cli_opts.remoteport, format); 116 cli_opts.remoteport, exitmsg);
113 } 117 }
114
115 /* Arguments to the exit printout may be unsafe to use after session_cleanup() */
116 vsnprintf(exitmsg, sizeof(exitmsg), fmtbuf, param);
117 118
118 /* Do the cleanup first, since then the terminal will be reset */ 119 /* Do the cleanup first, since then the terminal will be reset */
119 session_cleanup(); 120 session_cleanup();
120 /* Avoid printing onwards from terminal cruft */ 121 /* Avoid printing onwards from terminal cruft */
121 fprintf(stderr, "\n"); 122 fprintf(stderr, "\n");
122 123
123 dropbear_log(LOG_INFO, "%s", exitmsg);; 124 dropbear_log(LOG_INFO, "%s", fullmsg);
124 exit(exitcode); 125 exit(exitcode);
125 } 126 }
126 127
127 static void cli_dropbear_log(int priority, 128 static void cli_dropbear_log(int priority,
128 const char* format, va_list param) { 129 const char* format, va_list param) {
148 usershell = m_strdup(get_user_shell()); 149 usershell = m_strdup(get_user_shell());
149 run_shell_command(cmd, ses.maxfd, usershell); 150 run_shell_command(cmd, ses.maxfd, usershell);
150 dropbear_exit("Failed to run '%s'\n", cmd); 151 dropbear_exit("Failed to run '%s'\n", cmd);
151 } 152 }
152 153
153 #ifdef ENABLE_CLI_PROXYCMD 154 #if DROPBEAR_CLI_PROXYCMD
154 static void cli_proxy_cmd(int *sock_in, int *sock_out, pid_t *pid_out) { 155 static void cli_proxy_cmd(int *sock_in, int *sock_out, pid_t *pid_out) {
155 char * ex_cmd = NULL; 156 char * ex_cmd = NULL;
156 size_t ex_cmdlen; 157 size_t ex_cmdlen;
157 int ret; 158 int ret;
158 159
173 174
174 static void kill_proxy_sighandler(int UNUSED(signo)) { 175 static void kill_proxy_sighandler(int UNUSED(signo)) {
175 kill_proxy_command(); 176 kill_proxy_command();
176 _exit(1); 177 _exit(1);
177 } 178 }
178 #endif /* ENABLE_CLI_PROXYCMD */ 179 #endif /* DROPBEAR_CLI_PROXYCMD */