--- ../bacula-trunk/bacula/src/console/console.c 2010-05-27 16:56:07.429041610 +0400 +++ src/console/console.c 2010-05-31 17:41:47.181923077 +0400 @@ -134,6 +134,7 @@ " -s no signals\n" " -u set command execution timeout to seconds\n" " -t test - read configuration and exit\n" +" -e exec - execute one command and exit\n" " -? print this message.\n" "\n"), 2000, HOST_OS, DISTNAME, DISTVER); } @@ -873,6 +874,7 @@ rl_completion_entry_function = dummy_completion_function; rl_attempted_completion_function = readline_completion; rl_filename_completion_desired = 0; + stifle_history(100); #endif return ret; @@ -888,6 +890,8 @@ int ch, i, item; bool no_signals = false; bool test_config = false; + bool one_command = false; + char *exec_command = NULL; JCR jcr; utime_t heart_beat; @@ -902,7 +906,7 @@ working_directory = "/tmp"; args = get_pool_memory(PM_FNAME); - while ((ch = getopt(argc, argv, "bc:d:nstu:?")) != -1) { + while ((ch = getopt(argc, argv, "bc:d:e:nstu:?")) != -1) { switch (ch) { case 'c': /* configuration file */ if (configfile != NULL) { @@ -938,6 +942,18 @@ timeout = atoi(optarg); break; +#if defined(HAVE_WIN32) + case 'e': + fprintf(stderr, "Not implemented on WIN32 platform.\n"); // see below for mkstemp() + exit(1); + break; +#else + case 'e': + exec_command = bstrdup(optarg); + one_command = true; + break; +#endif + case '?': default: usage(); @@ -1163,7 +1179,39 @@ console_init_history(history_file.c_str()); } +#if defined(HAVE_WIN32) +/* win32 doesn't have mkstemp() file function, and I don't have win32 skills, + * so this code is skipped, but it does have insecure GetTempFileName()... + * page 66 of "Secure programming cookbook for C and C++" by John Viega, Matt Messier + */ read_and_process_input(stdin, UA_sock); +#else + /* run one command from command line with use of a tempfile */ + if (one_command) { + FILE *fdtemp; + char fname[]="/tmp/bacula.XXXXXX"; + int fd; + fd = mkstemp(fname); + if (fd == -1) { + fprintf(stderr,"unable to create temporary file: mkstemp()\n"); + } else { + /* can unlink() here to increase file security */ + fdtemp = fdopen(fd,"w+"); + if (fdtemp == NULL) { + fprintf(stderr,"unable to create temporary file: fdopen()\n"); + } else { + fprintf(fdtemp, "%s\n", exec_command); + // fclose(fdtemp); fdtemp = fopen(fname, "rb"); + /* fflush(fdtemp); */ rewind(fdtemp); + read_and_process_input(fdtemp, UA_sock); + fclose(fdtemp); + } + unlink(fname); + } + } else { + read_and_process_input(stdin, UA_sock); + } +#endif if (UA_sock) { UA_sock->signal(BNET_TERMINATE); /* send EOF */