script.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 kcontext_t *context, const char *prefix)
  41. {
  42. const char *line = NULL;
  43. const kpargv_t *pargv = NULL;
  44. const kentry_t *cmd = NULL;
  45. faux_list_node_t *iter = NULL;
  46. faux_list_node_t *cur = NULL;
  47. if (!context)
  48. return BOOL_FALSE;
  49. pargv = kcontext_pargv(context);
  50. if (!pargv)
  51. return BOOL_FALSE;
  52. // Line
  53. line = kcontext_line(context);
  54. if (line) {
  55. char *var = faux_str_sprintf("%sLINE", prefix);
  56. setenv(var, line, OVERWRITE);
  57. faux_str_free(var);
  58. }
  59. // Command
  60. cmd = kpargv_command(pargv);
  61. if (cmd) {
  62. char *var = faux_str_sprintf("%sCOMMAND", prefix);
  63. setenv(var, kentry_name(cmd), OVERWRITE);
  64. faux_str_free(var);
  65. }
  66. // Parameters
  67. iter = faux_list_head(kpargv_pargs(pargv));
  68. while ((cur = faux_list_each_node(&iter))) {
  69. kparg_t *parg = (kparg_t *)faux_list_data(cur);
  70. kparg_t *tmp_parg = NULL;
  71. const kentry_t *entry = kparg_entry(parg);
  72. faux_list_node_t *iter_before = faux_list_prev_node(cur);
  73. faux_list_node_t *iter_after = cur;
  74. unsigned int num = 0;
  75. bool_t already_populated = BOOL_FALSE;
  76. char *whole_param = NULL;
  77. char *whole_param_var = NULL;
  78. if (!kparg_value(parg)) // PTYPE can contain parg with NULL value
  79. continue;
  80. // Search for such entry within arguments before current one
  81. while ((tmp_parg = (kparg_t *)faux_list_eachr(&iter_before))) {
  82. if (kparg_entry(tmp_parg) == entry) {
  83. already_populated = BOOL_TRUE;
  84. break;
  85. }
  86. }
  87. if (already_populated)
  88. continue;
  89. // Populate all next args with the current entry
  90. while ((tmp_parg = (kparg_t *)faux_list_each(&iter_after))) {
  91. char *var = NULL;
  92. const char *value = kparg_value(tmp_parg);
  93. char *qvalue = NULL;
  94. if (kparg_entry(tmp_parg) != entry)
  95. continue;
  96. var = faux_str_sprintf("%sPARAM_%s_%u",
  97. prefix, kentry_name(entry), num);
  98. setenv(var, value, OVERWRITE);
  99. faux_str_free(var);
  100. qvalue = faux_str_c_esc_quote(value);
  101. whole_param = faux_str_sprintf("%s%s%s",
  102. whole_param ? whole_param : "",
  103. whole_param ? " " : "", qvalue);
  104. faux_str_free(qvalue);
  105. num++;
  106. }
  107. // All values of PARAM in one line (for multi-value parameters)
  108. whole_param_var = faux_str_sprintf("%sPARAM_%s",
  109. prefix, kentry_name(entry));
  110. setenv(whole_param_var, whole_param, OVERWRITE);
  111. faux_str_free(whole_param_var);
  112. faux_str_free(whole_param);
  113. }
  114. return BOOL_TRUE;
  115. }
  116. static bool_t populate_env(const kcontext_t *context)
  117. {
  118. kcontext_type_e type = KCONTEXT_TYPE_NONE;
  119. const kentry_t *entry = NULL;
  120. const ksession_t *session = NULL;
  121. const char *str = NULL;
  122. pid_t pid = -1;
  123. uid_t uid = -1;
  124. assert(context);
  125. session = kcontext_session(context);
  126. assert(session);
  127. // Type
  128. type = kcontext_type(context);
  129. if (type >= KCONTEXT_TYPE_MAX)
  130. type = KCONTEXT_TYPE_NONE;
  131. setenv(PREFIX"TYPE", kcontext_type_e_str[type], OVERWRITE);
  132. // Candidate
  133. entry = kcontext_candidate_entry(context);
  134. if (entry)
  135. setenv(PREFIX"CANDIDATE", kentry_name(entry), OVERWRITE);
  136. // Value
  137. str = kcontext_candidate_value(context);
  138. if (str)
  139. setenv(PREFIX"VALUE", str, OVERWRITE);
  140. // PID
  141. pid = ksession_pid(session);
  142. if (pid != -1) {
  143. char *t = faux_str_sprintf("%lld", (long long int)pid);
  144. setenv(PREFIX"PID", t, OVERWRITE);
  145. faux_str_free(t);
  146. }
  147. // UID
  148. uid = ksession_uid(session);
  149. if (uid != -1) {
  150. char *t = faux_str_sprintf("%lld", (long long int)uid);
  151. setenv(PREFIX"UID", t, OVERWRITE);
  152. faux_str_free(t);
  153. }
  154. // User
  155. str = ksession_user(session);
  156. if (str)
  157. setenv(PREFIX"USER", str, OVERWRITE);
  158. // Parameters
  159. populate_env_kpargv(context, PREFIX);
  160. // Parent parameters
  161. populate_env_kpargv(kcontext_parent_context(context), PREFIX"PARENT_");
  162. return BOOL_TRUE;
  163. }
  164. static char *find_out_shebang(const char *script)
  165. {
  166. char *default_shebang = "/bin/sh";
  167. char *shebang = NULL;
  168. char *line = NULL;
  169. line = faux_str_getline(script, NULL);
  170. if (
  171. !line ||
  172. (strlen(line) < 2) ||
  173. (line[0] != '#') ||
  174. (line[1] != '!')
  175. ) {
  176. faux_str_free(line);
  177. return faux_str_dup(default_shebang);
  178. }
  179. shebang = faux_str_dup(line + 2);
  180. faux_str_free(line);
  181. return shebang;
  182. }
  183. // Execute script
  184. int script_script(kcontext_t *context)
  185. {
  186. const char *script = NULL;
  187. pid_t cpid = -1;
  188. int res = -1;
  189. char *fifo_name = NULL;
  190. FILE *wpipe = NULL;
  191. char *command = NULL;
  192. char *shebang = NULL;
  193. script = kcontext_script(context);
  194. if (faux_str_is_empty(script))
  195. return 0;
  196. // Create FIFO
  197. if (!(fifo_name = script_mkfifo())) {
  198. fprintf(stderr, "Error: Can't create temporary FIFO.\n"
  199. "Error: The ACTION will be not executed.\n");
  200. return -1;
  201. }
  202. // Create process to write to FIFO
  203. cpid = fork();
  204. if (cpid == -1) {
  205. fprintf(stderr, "Error: Can't fork the write process.\n"
  206. "Error: The ACTION will be not executed.\n");
  207. unlink(fifo_name);
  208. faux_str_free(fifo_name);
  209. return -1;
  210. }
  211. // Child: write to FIFO
  212. if (cpid == 0) {
  213. wpipe = fopen(fifo_name, "w");
  214. faux_str_free(fifo_name);
  215. if (!wpipe)
  216. _exit(-1);
  217. fwrite(script, strlen(script), 1, wpipe);
  218. fflush(wpipe);
  219. fclose(wpipe);
  220. _exit(0);
  221. }
  222. // Parent
  223. // Populate environment. Put command parameters to env vars.
  224. populate_env(context);
  225. // Prepare command
  226. shebang = find_out_shebang(script);
  227. command = faux_str_sprintf("%s %s", shebang, fifo_name);
  228. res = system(command);
  229. // Wait for the writing process
  230. kill(cpid, SIGTERM);
  231. while (waitpid(cpid, NULL, 0) != cpid);
  232. // Clean up
  233. faux_str_free(command);
  234. unlink(fifo_name);
  235. faux_str_free(fifo_name);
  236. faux_str_free(shebang);
  237. return WEXITSTATUS(res);
  238. }