script.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 can contain 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 ksession_t *session = NULL;
  88. const char *str = NULL;
  89. pid_t pid = -1;
  90. uid_t uid = -1;
  91. assert(context);
  92. session = kcontext_session(context);
  93. assert(session);
  94. // Type
  95. type = kcontext_type(context);
  96. if (type >= KCONTEXT_TYPE_MAX)
  97. type = KCONTEXT_TYPE_NONE;
  98. setenv(PREFIX"TYPE", kcontext_type_e_str[type], OVERWRITE);
  99. // Candidate
  100. entry = kcontext_candidate_entry(context);
  101. if (entry)
  102. setenv(PREFIX"CANDIDATE", kentry_name(entry), OVERWRITE);
  103. // Value
  104. str = kcontext_candidate_value(context);
  105. if (str)
  106. setenv(PREFIX"VALUE", str, OVERWRITE);
  107. // PID
  108. pid = ksession_pid(session);
  109. if (pid != -1) {
  110. char *t = faux_str_sprintf("%lld", (long long int)pid);
  111. setenv(PREFIX"PID", t, OVERWRITE);
  112. faux_str_free(t);
  113. }
  114. // UID
  115. uid = ksession_uid(session);
  116. if (uid != -1) {
  117. char *t = faux_str_sprintf("%lld", (long long int)uid);
  118. setenv(PREFIX"UID", t, OVERWRITE);
  119. faux_str_free(t);
  120. }
  121. // User
  122. str = ksession_user(session);
  123. if (str)
  124. setenv(PREFIX"USER", str, OVERWRITE);
  125. // Parameters
  126. populate_env_kpargv(kcontext_pargv(context), PREFIX);
  127. // Parent parameters
  128. populate_env_kpargv(kcontext_parent_pargv(context), PREFIX"PARENT_");
  129. return BOOL_TRUE;
  130. }
  131. static char *find_out_shebang(const char *script)
  132. {
  133. char *default_shebang = "/bin/sh";
  134. char *shebang = NULL;
  135. char *line = NULL;
  136. line = faux_str_getline(script, NULL);
  137. if (
  138. !line ||
  139. (strlen(line) < 2) ||
  140. (line[0] != '#') ||
  141. (line[1] != '!')
  142. ) {
  143. faux_str_free(line);
  144. return faux_str_dup(default_shebang);
  145. }
  146. shebang = faux_str_dup(line + 2);
  147. faux_str_free(line);
  148. return shebang;
  149. }
  150. // Execute script
  151. int script_script(kcontext_t *context)
  152. {
  153. const char *script = NULL;
  154. pid_t cpid = -1;
  155. int res = -1;
  156. char *fifo_name = NULL;
  157. FILE *wpipe = NULL;
  158. char *command = NULL;
  159. char *shebang = NULL;
  160. script = kcontext_script(context);
  161. if (faux_str_is_empty(script))
  162. return 0;
  163. // Create FIFO
  164. if (!(fifo_name = script_mkfifo())) {
  165. fprintf(stderr, "Error: Can't create temporary FIFO.\n"
  166. "Error: The ACTION will be not executed.\n");
  167. return -1;
  168. }
  169. // Create process to write to FIFO
  170. cpid = fork();
  171. if (cpid == -1) {
  172. fprintf(stderr, "Error: Can't fork the write process.\n"
  173. "Error: The ACTION will be not executed.\n");
  174. unlink(fifo_name);
  175. faux_str_free(fifo_name);
  176. return -1;
  177. }
  178. // Child: write to FIFO
  179. if (cpid == 0) {
  180. wpipe = fopen(fifo_name, "w");
  181. faux_str_free(fifo_name);
  182. if (!wpipe)
  183. _exit(-1);
  184. fwrite(script, strlen(script), 1, wpipe);
  185. fflush(wpipe);
  186. fclose(wpipe);
  187. _exit(0);
  188. }
  189. // Parent
  190. // Populate environment. Put command parameters to env vars.
  191. populate_env(context);
  192. // Prepare command
  193. shebang = find_out_shebang(script);
  194. command = faux_str_sprintf("%s %s", shebang, fifo_name);
  195. res = system(command);
  196. // Wait for the writing process
  197. kill(cpid, SIGTERM);
  198. while (waitpid(cpid, NULL, 0) != cpid);
  199. // Clean up
  200. faux_str_free(command);
  201. unlink(fifo_name);
  202. faux_str_free(fifo_name);
  203. faux_str_free(shebang);
  204. return WEXITSTATUS(res);
  205. }