script.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. *
  3. */
  4. #include <assert.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <errno.h>
  10. #include <sys/types.h>
  11. #include <sys/wait.h>
  12. #include <sys/stat.h>
  13. #include <fcntl.h>
  14. #include <limits.h>
  15. #include <syslog.h>
  16. #include <faux/str.h>
  17. #include <faux/list.h>
  18. #include <klish/kcontext.h>
  19. #include <klish/ksession.h>
  20. static char *script_mkfifo(void)
  21. {
  22. char *name = NULL;
  23. name = faux_str_sprintf("/tmp/klish.fifo.%u.XXXXXX", getpid());
  24. mktemp(name);
  25. if (mkfifo(name, 0600) < 0) {
  26. faux_str_free(name);
  27. return NULL;
  28. }
  29. return name;
  30. }
  31. const char *kcontext_type_e_str[] = {
  32. "none",
  33. "plugin_init",
  34. "plugin_fini",
  35. "action",
  36. "service_action"
  37. };
  38. #define PREFIX "KLISH_"
  39. #define OVERWRITE 1
  40. static bool_t populate_env_kpargv(const kpargv_t *pargv, const char *prefix)
  41. {
  42. const kentry_t *entry = NULL;
  43. kpargv_pargs_node_t *iter = NULL;
  44. kparg_t *parg = NULL;
  45. const kentry_t *saved_entry = NULL;
  46. size_t num = 0;
  47. if (!pargv)
  48. return BOOL_FALSE;
  49. // Command
  50. entry = kpargv_command(pargv);
  51. if (entry) {
  52. char *var = faux_str_sprintf("%sCOMMAND", prefix);
  53. setenv(var, kentry_name(entry), OVERWRITE);
  54. faux_str_free(var);
  55. }
  56. // Parameters
  57. iter = kpargv_pargs_iter(pargv);
  58. while ((parg = kpargv_pargs_each(&iter))) {
  59. char *var = NULL;
  60. const char *value = kparg_value(parg);
  61. if (!value) // PTYPE contains "__ptype" parg with NULL value
  62. continue;
  63. entry = kparg_entry(parg);
  64. if (kentry_max(entry) > 1) { // Multi
  65. if (entry == saved_entry)
  66. num++;
  67. else
  68. num = 0;
  69. var = faux_str_sprintf("%sPARAM_%s_%u",
  70. prefix, kentry_name(entry), num);
  71. saved_entry = entry;
  72. } else { // Single
  73. var = faux_str_sprintf("%sPARAM_%s",
  74. prefix, kentry_name(entry));
  75. saved_entry = NULL;
  76. num = 0;
  77. }
  78. setenv(var, value, OVERWRITE);
  79. faux_str_free(var);
  80. }
  81. return BOOL_TRUE;
  82. }
  83. static bool_t populate_env(kcontext_t *context)
  84. {
  85. kcontext_type_e type = KCONTEXT_TYPE_NONE;
  86. const kentry_t *entry = NULL;
  87. const char *str = NULL;
  88. assert(context);
  89. // Type
  90. type = kcontext_type(context);
  91. if (type >= KCONTEXT_TYPE_MAX)
  92. type = KCONTEXT_TYPE_NONE;
  93. setenv(PREFIX"TYPE", kcontext_type_e_str[type], OVERWRITE);
  94. // Candidate
  95. entry = kcontext_candidate_entry(context);
  96. if (entry)
  97. setenv(PREFIX"CANDIDATE", kentry_name(entry), OVERWRITE);
  98. // Value
  99. str = kcontext_candidate_value(context);
  100. if (str)
  101. setenv(PREFIX"VALUE", str, OVERWRITE);
  102. // Parameters
  103. populate_env_kpargv(kcontext_pargv(context), PREFIX);
  104. // Parent parameters
  105. populate_env_kpargv(kcontext_parent_pargv(context), PREFIX"PARENT_");
  106. return BOOL_TRUE;
  107. }
  108. static char *find_out_shebang(const char *script)
  109. {
  110. char *default_shebang = "/bin/sh";
  111. char *shebang = NULL;
  112. char *line = NULL;
  113. line = faux_str_getline(script, NULL);
  114. if (
  115. !line ||
  116. (strlen(line) < 2) ||
  117. (line[0] != '#') ||
  118. (line[1] != '!')
  119. ) {
  120. faux_str_free(line);
  121. return faux_str_dup(default_shebang);
  122. }
  123. shebang = faux_str_dup(line + 2);
  124. faux_str_free(line);
  125. return shebang;
  126. }
  127. // Execute script
  128. int script_script(kcontext_t *context)
  129. {
  130. const char *script = NULL;
  131. pid_t cpid = -1;
  132. int res = -1;
  133. char *fifo_name = NULL;
  134. FILE *wpipe = NULL;
  135. char *command = NULL;
  136. char *shebang = NULL;
  137. script = kcontext_script(context);
  138. if (faux_str_is_empty(script))
  139. return 0;
  140. // Create FIFO
  141. if (!(fifo_name = script_mkfifo())) {
  142. fprintf(stderr, "Error: Can't create temporary FIFO.\n"
  143. "Error: The ACTION will be not executed.\n");
  144. return -1;
  145. }
  146. // Create process to write to FIFO
  147. cpid = fork();
  148. if (cpid == -1) {
  149. fprintf(stderr, "Error: Can't fork the write process.\n"
  150. "Error: The ACTION will be not executed.\n");
  151. unlink(fifo_name);
  152. faux_str_free(fifo_name);
  153. return -1;
  154. }
  155. // Child: write to FIFO
  156. if (cpid == 0) {
  157. wpipe = fopen(fifo_name, "w");
  158. faux_str_free(fifo_name);
  159. if (!wpipe)
  160. _exit(-1);
  161. fwrite(script, strlen(script), 1, wpipe);
  162. fflush(wpipe);
  163. fclose(wpipe);
  164. _exit(0);
  165. }
  166. // Parent
  167. // Populate environment. Put command parameters to env vars.
  168. populate_env(context);
  169. // Prepare command
  170. shebang = find_out_shebang(script);
  171. command = faux_str_sprintf("%s %s", shebang, fifo_name);
  172. res = system(command);
  173. // Wait for the writing process
  174. kill(cpid, SIGTERM);
  175. while (waitpid(cpid, NULL, 0) != cpid);
  176. // Clean up
  177. faux_str_free(command);
  178. unlink(fifo_name);
  179. faux_str_free(fifo_name);
  180. faux_str_free(shebang);
  181. return WEXITSTATUS(res);
  182. }