script.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 = faux_str_c_esc_quote(value);
  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. whole_param = faux_str_sprintf("%s%s%s",
  101. whole_param ? whole_param : "",
  102. whole_param ? " " : "", qvalue);
  103. faux_str_free(qvalue);
  104. num++;
  105. }
  106. // All values of PARAM in one line (for multi-value parameters)
  107. whole_param_var = faux_str_sprintf("%sPARAM_%s",
  108. prefix, kentry_name(entry));
  109. setenv(whole_param_var, whole_param, OVERWRITE);
  110. faux_str_free(whole_param_var);
  111. faux_str_free(whole_param);
  112. }
  113. return BOOL_TRUE;
  114. }
  115. static bool_t populate_env(const kcontext_t *context)
  116. {
  117. kcontext_type_e type = KCONTEXT_TYPE_NONE;
  118. const kentry_t *entry = NULL;
  119. const ksession_t *session = NULL;
  120. const char *str = NULL;
  121. pid_t pid = -1;
  122. uid_t uid = -1;
  123. assert(context);
  124. session = kcontext_session(context);
  125. assert(session);
  126. // Type
  127. type = kcontext_type(context);
  128. if (type >= KCONTEXT_TYPE_MAX)
  129. type = KCONTEXT_TYPE_NONE;
  130. setenv(PREFIX"TYPE", kcontext_type_e_str[type], OVERWRITE);
  131. // Candidate
  132. entry = kcontext_candidate_entry(context);
  133. if (entry)
  134. setenv(PREFIX"CANDIDATE", kentry_name(entry), OVERWRITE);
  135. // Value
  136. str = kcontext_candidate_value(context);
  137. if (str)
  138. setenv(PREFIX"VALUE", str, OVERWRITE);
  139. // PID
  140. pid = ksession_pid(session);
  141. if (pid != -1) {
  142. char *t = faux_str_sprintf("%lld", (long long int)pid);
  143. setenv(PREFIX"PID", t, OVERWRITE);
  144. faux_str_free(t);
  145. }
  146. // UID
  147. uid = ksession_uid(session);
  148. if (uid != -1) {
  149. char *t = faux_str_sprintf("%lld", (long long int)uid);
  150. setenv(PREFIX"UID", t, OVERWRITE);
  151. faux_str_free(t);
  152. }
  153. // User
  154. str = ksession_user(session);
  155. if (str)
  156. setenv(PREFIX"USER", str, OVERWRITE);
  157. // Parameters
  158. populate_env_kpargv(context, PREFIX);
  159. // Parent parameters
  160. populate_env_kpargv(kcontext_parent_context(context), PREFIX"PARENT_");
  161. return BOOL_TRUE;
  162. }
  163. static char *find_out_shebang(const char *script)
  164. {
  165. char *default_shebang = "/bin/sh";
  166. char *shebang = NULL;
  167. char *line = NULL;
  168. line = faux_str_getline(script, NULL);
  169. if (
  170. !line ||
  171. (strlen(line) < 2) ||
  172. (line[0] != '#') ||
  173. (line[1] != '!')
  174. ) {
  175. faux_str_free(line);
  176. return faux_str_dup(default_shebang);
  177. }
  178. shebang = faux_str_dup(line + 2);
  179. faux_str_free(line);
  180. return shebang;
  181. }
  182. // Execute script
  183. int script_script(kcontext_t *context)
  184. {
  185. const char *script = NULL;
  186. pid_t cpid = -1;
  187. int res = -1;
  188. char *fifo_name = NULL;
  189. FILE *wpipe = NULL;
  190. char *command = NULL;
  191. char *shebang = NULL;
  192. script = kcontext_script(context);
  193. if (faux_str_is_empty(script))
  194. return 0;
  195. // Create FIFO
  196. if (!(fifo_name = script_mkfifo())) {
  197. fprintf(stderr, "Error: Can't create temporary FIFO.\n"
  198. "Error: The ACTION will be not executed.\n");
  199. return -1;
  200. }
  201. // Create process to write to FIFO
  202. cpid = fork();
  203. if (cpid == -1) {
  204. fprintf(stderr, "Error: Can't fork the write process.\n"
  205. "Error: The ACTION will be not executed.\n");
  206. unlink(fifo_name);
  207. faux_str_free(fifo_name);
  208. return -1;
  209. }
  210. // Child: write to FIFO
  211. if (cpid == 0) {
  212. wpipe = fopen(fifo_name, "w");
  213. faux_str_free(fifo_name);
  214. if (!wpipe)
  215. _exit(-1);
  216. fwrite(script, strlen(script), 1, wpipe);
  217. fflush(wpipe);
  218. fclose(wpipe);
  219. _exit(0);
  220. }
  221. // Parent
  222. // Populate environment. Put command parameters to env vars.
  223. populate_env(context);
  224. // Prepare command
  225. shebang = find_out_shebang(script);
  226. command = faux_str_sprintf("%s %s", shebang, fifo_name);
  227. res = system(command);
  228. // Wait for the writing process
  229. kill(cpid, SIGTERM);
  230. while (waitpid(cpid, NULL, 0) != cpid);
  231. // Clean up
  232. faux_str_free(command);
  233. unlink(fifo_name);
  234. faux_str_free(fifo_name);
  235. faux_str_free(shebang);
  236. return WEXITSTATUS(res);
  237. }