script.c 5.5 KB

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