static int print_transfer_string(char *str, FILE *fp, transfer_info *ti, float bps, unsigned int secs, unsigned long long int eta) { int i; int len = 0; char *e = str; bool leftjust; int inescape = 0; int minlen; int number_of_dots; /* used by visual progress bar (%v) */ while(e && *e) { if(*e == '%') { leftjust = false; minlen = 0; e++; if(!*e) break; if(*e == '-') { leftjust = true; e++; } if(isdigit((int)*e)) { minlen = atoi(e); while(isdigit((int)*e)) e++; } if(leftjust) minlen = -minlen; char* sp = NULL; switch(*e) { case 'r': sp = shortpath(base_name_ptr(ti->remote_name), leftjust ? -minlen : minlen, ftp->homedir); len += max_printf(fp, minlen, "%s", sp); break; case 'R': sp = shortpath(ti->remote_name, leftjust ? -minlen : minlen, ftp->homedir); len += max_printf(fp, minlen, "%s", sp); break; case 'l': sp = shortpath(base_name_ptr(ti->local_name), leftjust ? -minlen : minlen, gvLocalHomeDir); len += max_printf(fp, minlen, "%s", sp); break; case 'L': sp = shortpath(ti->local_name, leftjust ? -minlen : minlen, gvLocalHomeDir); len += max_printf(fp, minlen, "%s", sp); break; case 's': len += max_printf(fp, minlen, "%sB", human_size(ti->size)); break; case 'S': len += max_printf(fp, minlen, "%sB", (ti->total_size == -1L ? "??" : human_size(ti->total_size))); break; case 'b': len += max_printf(fp, minlen < 2 ? minlen : minlen-2, "%sB/s", human_size(bps)); break; case 'B': if(ti->stalled >= 5) len += max_printf(fp, minlen, "%s", _("stalled")); else len += max_printf(fp, minlen < 2 ? minlen : minlen-2, "%sB/s", human_size(bps)); break; case 'e': if(eta != (unsigned) -1) len += max_printf(fp, minlen, "%s", human_time(eta)); else len += max_printf(fp, minlen, "%s", "--:--"); break; case 't': len += max_printf(fp, minlen, "%s", human_time(secs)); break; case '%': len += fprintf(fp, "%%"); break; case 'p': if(ti->total_size != -1L) len += max_printf(fp, minlen, "%.1f", (double)100*ti->size / (ti->total_size + (ti->total_size ? 0 : 1))); else len += fprintf(fp, "?"); break; case 'v': if(ti->total_size != -1L) { if(ti->total_size == ti->size) number_of_dots = minlen; else number_of_dots = (double)minlen * ti->size / (ti->total_size + 1); if(number_of_dots > minlen || number_of_dots < 0) /* just in case */ number_of_dots = minlen; i = minlen - number_of_dots; while(number_of_dots--) len += fprintf(fp, "#"); while(i--) len += fprintf(fp, " "); } else { number_of_dots = minlen / 2; i = minlen - number_of_dots; while(number_of_dots--) len += fprintf(fp, " "); if(i) { i--; len += fprintf(fp, "?"); while(i--) len += fprintf(fp, " "); } } break; case '{': inescape++; break; case '}': inescape--; break; default: len += fprintf(fp, "%%%c", *e); break; } free(sp); } else { fputc(*e, fp); if (inescape <= 0) len++; } e++; } return len; }
void cmd_exec (struct htlc_conn *htlc, u_int32_t cid, char *command) { int argc, pfds[2], fakepfds[2]; char *argv[32], myarg[32], *p, *pii, *thisarg, cmdpath[MAXPATHLEN]; struct exec_file *execp; char errstr[64]; int len, nolog=0, i; char *envp[6]; char rootdir[MAXPATHLEN + 16]; char accountdir[MAXPATHLEN + 16], account[32 + 16]; char uptime[32 + 16]; char version[6 + 16]; u_int16_t style; char abuf[HOSTLEN+1]; struct timeval now; time_t ts; char tstr[32]; if (htlc->nr_execs >= htlc->exec_limit) { style = htons(1); if (isclient(htlc->uid)) { len = snprintf(errstr, sizeof(errstr), "%u command%s at a time, please", htlc->exec_limit, htlc->exec_limit == 1 ? "" : "s"); hlwrite(htlc, HTLS_HDR_MSG, 0, 2, HTLS_DATA_STYLE, 2, &style, HTLS_DATA_MSG, len, errstr); } return; } else if (nr_execs >= (u_int32_t)hxd_cfg.limits.total_exec) { style = htons(1); if (isclient(htlc->uid)) { len = snprintf(errstr, sizeof(errstr), "server is too busy, limit is %u command%s at a time", hxd_cfg.limits.total_exec, hxd_cfg.limits.total_exec == 1 ? "" : "s"); hlwrite(htlc, HTLS_HDR_MSG, 0, 2, HTLS_DATA_STYLE, 2, &style, HTLS_DATA_MSG, len, errstr); } return; } while ((p = strstr(command, "../"))) { for (pii = p; *pii; pii++) *pii = *(pii + 3); } for (argc = 0, thisarg = p = command; *p && argc < 30; p++) { if (isspace(*p)) { *p = 0; argv[argc++] = thisarg; thisarg = p + 1; } } if (thisarg != p) argv[argc++] = thisarg; argv[argc] = 0; snprintf(cmdpath, sizeof(cmdpath), "%s/%s", hxd_cfg.paths.exec, argv[0]); snprintf(myarg, 30, thisarg); inaddr2str(abuf, &htlc->sockaddr); if (strlen(cmdpath) > 0 && strlen(myarg) > 0) { for (i = 0; hxd_cfg.options.exclude[i]; i++) { if (strstr(argv[0], hxd_cfg.options.exclude[i])) nolog = 1; } if (nolog == 0) { hxd_log("%s@%s:%u - %s:%s:%u - exec %s %s", htlc->userid, abuf, ntohs(htlc->sockaddr.SIN_PORT), htlc->name, htlc->login, htlc->uid, cmdpath, myarg); #if defined(CONFIG_SQL) if (strncmp(cmdpath, "./login", 7 )) sql_exec(htlc->name, htlc->login, abuf, cmdpath, myarg); #endif } else { hxd_log("%s@%s:%u - %s:%s:%u - exec %s", htlc->userid, abuf, ntohs(htlc->sockaddr.SIN_PORT), htlc->name,htlc->login,htlc->uid,cmdpath); #if defined(CONFIG_SQL) sql_exec(htlc->name, htlc->login, abuf, cmdpath, " "); #endif } } if (pipe(pfds)) { hxd_log("cmd_exec: pipe: %s", strerror(errno)); return; } nr_open_files += 2; if (nr_open_files >= hxd_open_max) { hxd_log("%s:%d: %d >= hxd_open_max (%d)", __FILE__, __LINE__, pfds[0], hxd_open_max); close(pfds[0]); close(pfds[1]); nr_open_files -= 2; return; } switch (fork()) { case -1: hxd_log("cmd_exec: fork: %s", strerror(errno)); close(pfds[0]); close(pfds[1]); nr_open_files -= 2; return; case 0: /* make sure fds 1 and 2 exist for dup2 */ fakepfds[0] = fakepfds[1] = 0; pipe(fakepfds); if (pfds[1] != 1) { if (dup2(pfds[1], 1) == -1) { hxd_log("cmd_exec: dup2(%d,%d): %s", pfds[1], 1, strerror(errno)); _exit(1); } } if (pfds[1] != 2) { if (dup2(pfds[1], 2) == -1) { hxd_log("cmd_exec: dup2(%d,%d): %s", pfds[1], 2, strerror(errno)); _exit(1); } } close(0); if (fakepfds[0] > 2) close(fakepfds[0]); if (fakepfds[1] > 2) close(fakepfds[1]); #if 0 { int i; fprintf(stderr, "executing"); for (i = 0; i < argc; i++) fprintf(stderr, " %s", argv[i]); } #endif snprintf(rootdir, sizeof(rootdir), "ROOTDIR=%s", htlc->rootdir); snprintf(account, sizeof(account), "ACCOUNT=%s", htlc->login); snprintf(accountdir, sizeof(accountdir), "ACCOUNTDIR=%s", hxd_cfg.paths.accounts); gettimeofday(&now, 0); ts = tv_secdiff(&server_start_time, &now); human_time(ts, tstr); snprintf(uptime, sizeof(uptime), "UPTIME=%s", tstr); snprintf(version, sizeof(version), "VERSION=%s", hxd_version); envp[0] = rootdir; envp[1] = accountdir; envp[2] = account; envp[3] = uptime; envp[4] = version; envp[5] = 0; execve(cmdpath, argv, envp); fprintf(stderr, "\r%s: %s", argv[0], strerror(errno)); _exit(1); default: close(pfds[1]); nr_open_files--; FD_SET(pfds[0], &exec_fds); execp = xmalloc(sizeof(struct exec_file)); execp->htlc = htlc; execp->cid = cid; hxd_files[pfds[0]].conn.ptr = (void *)execp; hxd_files[pfds[0]].ready_read = exec_ready_read; FD_SET(pfds[0], &hxd_rfds); if (high_fd < pfds[0]) high_fd = pfds[0]; htlc->nr_execs++; nr_execs++; break; } }