void subshell_setup (char* shell, list_t* env) { int retcode = 0; int count; argv_t* argv; char* av[20]; list_t* next; if(shell == NULL) { return; } retcode = pipe(&subshell_pipe[PARENT_IN]); if(retcode == 0) { subshell_pid = fork(); if(subshell_pid == -1) { die_with_message(NULL, NULL, g_err_msg[E_SUBSHELL_FAIL]); } if(subshell_pid == 0) { dup2(subshell_pipe[PARENT_IN], STDIN_FILENO); close(subshell_pipe[PARENT_IN]); close(subshell_pipe[PARENT_OUT]); count = argc_argv(shell, &argv); if(count > 19) { av[19] = "\0"; count = 18; } while(count >= 0) { av[count] = argv[count].string; count--; } while(env) { next = env->next; putenv(env->buf); env = next; } execv(argv[0].string, av); free(argv); die_with_message(NULL, NULL, g_err_msg[E_SUBSHELL_FAIL]); } else { close(subshell_pipe[PARENT_IN]); } } }
main () { int argc, count; argv_t *argv; char string[2000]; strcpy (string, "\\This\\ string will be '' \"separated into\" \"'\\\"'\" ' 15 ' elements.\n" "' including a multi-line\n" "element' with a comment. # This should not be parsed\n" ";Nor should this\n" "The End."); printf ("%s\n", string); argc = argc_argv (string, &argv, "#;"); for (count = 0; count < argc; count++) { printf ("%03d: [%s] ", count, argv[count].string, ""); if (argv[count].quoted) { printf ("(it was quoted)"); } printf ("\n"); } if (argc != 15) { puts ("Test FAILED"); } else { puts ("Test PASSED"); } free (argv); }
void bash_setup (char *shell, list_t * env) { int retcode = 0; int count; argv_t *argv; char *av[20]; list_t *next; if (shell == NULL) return; retcode = pipe (&subshell_pipe[PARENT_IN]); if (retcode == 0) { #ifdef EMBED subshell_pid = vfork (); #else subshell_pid = fork (); #endif if (subshell_pid == -1) { die_with_message (NULL, NULL, g_err_msg[E_SUBSHELL_FAIL]); } if (subshell_pid == 0) { /* I'm the child, connect stdin to the parent */ dup2 (subshell_pipe[PARENT_IN], STDIN_FILENO); close (subshell_pipe[PARENT_IN]); close (subshell_pipe[PARENT_OUT]); count = argc_argv (shell, &argv, ""); if (count > 19) { /* over 20 command line args, silently truncate */ av[19] = "\0"; count = 18; } while (count >= 0) { av[count] = argv[count].string; count--; } /* populate the environment */ while (env) { next = env->next; putenv (env->buf); env = next; } execv (argv[0].string, av); free (argv); /* if we get here, we had a failure */ die_with_message (NULL, NULL, g_err_msg[E_SUBSHELL_FAIL]); } else { /* I'm parent, move along please */ close (subshell_pipe[PARENT_IN]); } } /* control should get to this point only in the parent. */ }
int main (int argc, char *argv[]) { #ifndef JUST_LUACSHELL token_t *tokenchain = NULL; buffer_t script_text; #endif script_t *scriptchain; int retval = 0; char *filename = NULL; argv_t *av = NULL; char **av2 = argv; int av2c = argc; int command; int count; list_t *env = NULL; assignGlobalStartupValues (); #ifndef JUST_LUACSHELL haserl_buffer_init (&script_text); #endif /* if more than argv[1] and argv[1] is not a file */ switch (argc) { case 1: /* we were run, instead of called as a shell script */ puts ("This is " PACKAGE_NAME " version " PACKAGE_VERSION "\n" "This program runs as a cgi interpeter, not interactively\n" "Please see: http://haserl.sourceforge.net\n" #ifdef USE_LUA "This version includes Lua (precompiled" #ifdef INCLUDE_LUASHELL " and interpreted" #endif ")\n" #endif #ifdef BASHEXTENSIONS "Unsupported bash extensions supplied by simnux enabled\n" #endif ); return (0); break; default: /* more than one */ /* split combined #! args - linux bundles them as one */ command = argc_argv (argv[1], &av, ""); if (command > 1) { /* rebuild argv into new av2 */ av2c = argc - 1 + command; av2 = xmalloc (sizeof (char *) * av2c); av2[0] = argv[0]; for (count = 1; count <= command; count++) { av2[count] = av[count - 1].string; } for (; count < av2c; count++) { av2[count] = argv[count - command + 1]; } } parseCommandLine (av2c, av2); free (av); if (av2 != argv) free (av2); if (optind < av2c) { filename = av2[optind]; } else { die_with_message (NULL, NULL, "No script file specified"); } break; } if (av2[0] != NULL && command == 1) { //prevents argument reconstruction (update.sh & reboot.sh) unsigned int prg_len=strlen(av2[0]); if (memcmp(av2[0]+prg_len-4, "i18n", 4) == 0) { if (global.translationKV_map == NULL) { buildTranslationMap (); } lookup_key (NULL, av2[optind], HASERL_SHELL_SYMBOLIC_LINK); buffer_destroy (&script_text); unsigned long num_destroyed; destroy_string_map(global.translationKV_map, DESTROY_MODE_IGNORE_VALUES, &num_destroyed); return (0); } } scriptchain = load_script (filename, NULL); /* drop permissions */ BecomeUser (scriptchain->uid, scriptchain->gid); /* populate the function pointers based on the shell selected */ if (strcmp (global.shell, "lua") && strcmp (global.shell, "luac")) /* default to "bash" */ { #ifdef INCLUDE_BASHSHELL shell_exec = &bash_exec; shell_echo = &bash_echo; shell_eval = &bash_eval; shell_setup = &bash_setup; shell_doscript = &bash_doscript; shell_destroy = &bash_destroy; #ifdef BASHEXTENSIONS shell_if = &bash_if; shell_elif = &bash_elif; shell_else = &bash_else; shell_endif = &bash_endif; shell_case = &bash_case; shell_when = &bash_when; shell_otherwise = &bash_otherwise; shell_endcase = &bash_endcase; shell_while = &bash_while; shell_endwhile = &bash_endwhile; shell_until = &bash_until; shell_enduntil = &bash_enduntil; shell_for = &bash_for; shell_endfor = &bash_endfor; shell_unless = &bash_unless; shell_elun = &bash_elun; shell_unelse = &bash_unelse; shell_endunless = &bash_endunless; #endif #else die_with_message (NULL, NULL, "Bash shell is not enabled."); #endif } else { #ifdef USE_LUA shell_setup = &lua_common_setup; shell_destroy = &lua_common_destroy; global.var_prefix = "FORM."; global.nul_prefix = "ENV."; if (global.shell[3] == 'c') /* luac only */ #ifdef INCLUDE_LUACSHELL shell_doscript = &luac_doscript; #else die_with_message (NULL, NULL, "Compiled Lua shell is not enabled."); #endif else { #ifdef INCLUDE_LUASHELL shell_exec = &lua_exec; shell_echo = &lua_echo; shell_eval = &lua_eval; shell_doscript = &lua_doscript; #else die_with_message (NULL, NULL, "Standard Lua shell is not enabled."); #endif } #else die_with_message (NULL, NULL, "Lua shells are not enabled."); #endif }
int main (int argc, char *argv[]) { #ifndef JUST_LUACSHELL token_t *tokenchain = NULL; buffer_t script_text; #endif script_t *scriptchain; int retval = 0; char *filename = NULL; argv_t *av = NULL; char **av2; int command; int count; list_t *env = NULL; assignGlobalStartupValues(); #ifndef JUST_LUACSHELL buffer_init (&script_text); #endif /* if more than argv[1] and argv[1] is not a file */ switch (argc) { case 1: /* we were run, instead of called as a shell script */ puts ("This is " PACKAGE_NAME " version " PACKAGE_VERSION "\n" "This program runs as a cgi interpeter, not interactively\n" "Please see: http://haserl.sourceforge.net"); return (0); break; case 2: filename = argv[1]; break; default: /* more than two */ command = argc_argv (argv[1], &av, ""); /* and we need to add the original argv[0] command for optarg to work */ av2 = xmalloc (sizeof (char *) * (command + 2)); av2[0] = argv[0]; for (count = 0; count < command; count++) { av2[count + 1] = av[count].string; } parseCommandLine (command + 1, av2); argv[1] = av[1].string; free (av); free (av2); filename = argv[2]; break; /* we silently ignore 3,4,etc. */ } scriptchain = load_script (filename, NULL); /* drop permissions */ BecomeUser (scriptchain->uid, scriptchain->gid); /* populate the function pointers based on the shell selected */ if (strcmp(global.shell, "lua") && strcmp(global.shell, "luac")) /* default to "bash" */ { #ifdef INCLUDE_BASHSHELL shell_exec = &bash_exec; shell_echo = &bash_echo; shell_eval = &bash_eval; shell_setup = &bash_setup; shell_doscript = &bash_doscript; shell_destroy = &bash_destroy; #else die_with_message (NULL, NULL, "Bash shell is not enabled."); #endif } else { #ifdef USE_LUA shell_setup = &lua_common_setup; shell_destroy = &lua_common_destroy; global.var_prefix = "FORM."; global.nul_prefix = "ENV."; if(global.shell[3] == 'c') /* compiled Lua specific function ptr... */ #ifdef INCLUDE_LUACSHELL shell_doscript = &luac_doscript; #else die_with_message (NULL, NULL, "Compiled Lua shell is not enabled."); #endif else { #ifdef INCLUDE_LUASHELL shell_exec = &lua_exec; shell_echo = &lua_echo; shell_eval = &lua_eval; shell_doscript = &lua_doscript; #else die_with_message (NULL, NULL, "Standard Lua shell is not enabled."); #endif } #else die_with_message (NULL, NULL, "Lua shells are not enabled."); #endif }