Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

vncsession.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. /*
  2. * Copyright 2018 Pierre Ossman for Cendio AB
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This software is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this software; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. * USA.
  18. */
  19. #include <config.h>
  20. #include <dirent.h>
  21. #include <errno.h>
  22. #include <fcntl.h>
  23. #include <grp.h>
  24. #include <limits.h>
  25. #include <pwd.h>
  26. #include <signal.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <sysexits.h>
  31. #include <unistd.h>
  32. #include <syslog.h>
  33. #include <security/pam_appl.h>
  34. #include <sys/stat.h>
  35. #include <sys/types.h>
  36. #include <sys/wait.h>
  37. extern char **environ;
  38. // PAM service name
  39. const char *SERVICE_NAME = "tigervnc";
  40. // Main script PID
  41. volatile static pid_t script = -1;
  42. // Daemon completion pipe
  43. int daemon_pipe_fd = -1;
  44. static int
  45. begin_daemon(void)
  46. {
  47. int devnull, fds[2];
  48. pid_t pid;
  49. /* Pipe to report startup success */
  50. if (pipe(fds) < 0) {
  51. perror("pipe");
  52. return -1;
  53. }
  54. /* First fork */
  55. pid = fork();
  56. if (pid < 0) {
  57. perror("fork");
  58. return -1;
  59. }
  60. if (pid != 0) {
  61. ssize_t len;
  62. char buf[1];
  63. close(fds[1]);
  64. /* Wait for child to finish startup */
  65. len = read(fds[0], buf, 1);
  66. if (len != 1) {
  67. fprintf(stderr, "Failure daemonizing\n");
  68. _exit(EX_OSERR);
  69. }
  70. _exit(0);
  71. }
  72. close(fds[0]);
  73. daemon_pipe_fd = fds[1];
  74. /* Detach from terminal */
  75. if (setsid() < 0) {
  76. perror("setsid");
  77. return -1;
  78. }
  79. /* Another fork required to fully detach */
  80. pid = fork();
  81. if (pid < 0) {
  82. perror("fork");
  83. return -1;
  84. }
  85. if (pid == 0)
  86. _exit(0);
  87. /* Send all stdio to /dev/null */
  88. devnull = open("/dev/null", O_RDWR);
  89. if (devnull < 0) {
  90. fprintf(stderr, "Failed to open /dev/null: %s\n", strerror(errno));
  91. return -1;
  92. }
  93. if ((dup2(devnull, 0) < 0) ||
  94. (dup2(devnull, 1) < 0) ||
  95. (dup2(devnull, 2) < 0)) {
  96. perror("dup2");
  97. return -1;
  98. }
  99. if (devnull > 2)
  100. close(devnull);
  101. /* A safe working directory */
  102. if (chdir("/") < 0) {
  103. perror("chdir");
  104. return -1;
  105. }
  106. return 0;
  107. }
  108. static void
  109. finish_daemon(void)
  110. {
  111. write(daemon_pipe_fd, "+", 1);
  112. close(daemon_pipe_fd);
  113. daemon_pipe_fd = -1;
  114. }
  115. static void
  116. sighandler(int sig)
  117. {
  118. if (script > 0) {
  119. kill(script, SIGTERM);
  120. }
  121. }
  122. static void
  123. setup_signals(void)
  124. {
  125. struct sigaction act;
  126. memset(&act, 0, sizeof(act));
  127. act.sa_handler = sighandler;
  128. sigemptyset(&act.sa_mask);
  129. act.sa_flags = 0;
  130. sigaction(SIGHUP, &act, NULL);
  131. sigaction(SIGINT, &act, NULL);
  132. sigaction(SIGTERM, &act, NULL);
  133. sigaction(SIGQUIT, &act, NULL);
  134. sigaction(SIGPIPE, &act, NULL);
  135. }
  136. static int
  137. conv(int num_msg,
  138. const struct pam_message **msg,
  139. struct pam_response **resp, void *appdata_ptr)
  140. {
  141. /* Opening a session should not require a conversation */
  142. return PAM_CONV_ERR;
  143. }
  144. static pam_handle_t *
  145. run_pam(int *pamret, const char *username, const char *display)
  146. {
  147. pam_handle_t *pamh;
  148. /* Say hello to PAM */
  149. struct pam_conv pconv;
  150. pconv.conv = conv;
  151. pconv.appdata_ptr = NULL;
  152. *pamret = pam_start(SERVICE_NAME, username, &pconv, &pamh);
  153. if (*pamret != PAM_SUCCESS) {
  154. /* pam_strerror requires a pamh argument, but if pam_start
  155. fails, pamh is invalid. In practice, at least the Linux
  156. implementation of pam_strerror does not use the pamh
  157. argument, but let's take care - avoid pam_strerror here. */
  158. syslog(LOG_CRIT, "pam_start failed: %d", *pamret);
  159. return NULL;
  160. }
  161. /* ConsoleKit and systemd (and possibly others) uses this to
  162. determine if the session is local or not. It needs to be set to
  163. something that can't be interpreted as localhost. We don't know
  164. what the client's address is though, and that might change on
  165. reconnects. We also don't want to set it to some text string as
  166. that results in a DNS lookup with e.g. libaudit. Let's use a
  167. fake IPv4 address from the documentation range. */
  168. /* FIXME: This might throw an error on a IPv6-only host */
  169. *pamret = pam_set_item(pamh, PAM_RHOST, "203.0.113.20");
  170. if (*pamret != PAM_SUCCESS) {
  171. syslog(LOG_CRIT, "pam_set_item(PAM_RHOST) failed: %d (%s)",
  172. *pamret, pam_strerror(pamh, *pamret));
  173. return pamh;
  174. }
  175. #ifdef PAM_XDISPLAY
  176. /* Let PAM modules use this to tag the session as a graphical one */
  177. *pamret = pam_set_item(pamh, PAM_XDISPLAY, display);
  178. /* Note: PAM_XDISPLAY is only supported by modern versions of PAM */
  179. if (*pamret != PAM_BAD_ITEM && *pamret != PAM_SUCCESS) {
  180. syslog(LOG_CRIT, "pam_set_item(PAM_XDISPLAY) failed: %d (%s)",
  181. *pamret, pam_strerror(pamh, *pamret));
  182. return pamh;
  183. }
  184. #endif
  185. /* Open session */
  186. *pamret = pam_open_session(pamh, PAM_SILENT);
  187. if (*pamret != PAM_SUCCESS) {
  188. syslog(LOG_CRIT, "pam_open_session failed: %d (%s)",
  189. *pamret, pam_strerror(pamh, *pamret));
  190. return pamh;
  191. }
  192. return pamh;
  193. }
  194. static int
  195. stop_pam(pam_handle_t * pamh, int pamret)
  196. {
  197. /* Close session */
  198. if (pamret == PAM_SUCCESS) {
  199. pamret = pam_close_session(pamh, PAM_SILENT);
  200. if (pamret != PAM_SUCCESS) {
  201. syslog(LOG_ERR, "pam_close_session failed: %d (%s)",
  202. pamret, pam_strerror(pamh, pamret));
  203. }
  204. }
  205. /* If PAM was OK and we are running on a SELinux system, new
  206. processes images will be executed in the root context. */
  207. /* Say goodbye */
  208. pamret = pam_end(pamh, pamret);
  209. if (pamret != PAM_SUCCESS) {
  210. /* avoid pam_strerror - we have no pamh. */
  211. syslog(LOG_ERR, "pam_end failed: %d", pamret);
  212. return EX_OSERR;
  213. }
  214. return pamret;
  215. }
  216. static char **
  217. prepare_environ(pam_handle_t * pamh)
  218. {
  219. char **pam_env, **child_env, **entry;
  220. int orig_count, pam_count;
  221. /* This function merges the normal environment with PAM's changes */
  222. pam_env = pam_getenvlist(pamh);
  223. if (pam_env == NULL)
  224. return NULL;
  225. /*
  226. * Worst case scenario is that PAM only adds variables, so allocate
  227. * based on that assumption.
  228. */
  229. orig_count = 0;
  230. for (entry = environ; *entry != NULL; entry++)
  231. orig_count++;
  232. pam_count = 0;
  233. for (entry = pam_env; *entry != NULL; entry++)
  234. pam_count++;
  235. child_env = calloc(orig_count + pam_count + 1, sizeof(char *));
  236. if (child_env == NULL)
  237. return NULL;
  238. memcpy(child_env, environ, sizeof(char *) * orig_count);
  239. for (entry = child_env; *entry != NULL; entry++) {
  240. *entry = strdup(*entry);
  241. if (*entry == NULL) // FIXME: cleanup
  242. return NULL;
  243. }
  244. for (entry = pam_env; *entry != NULL; entry++) {
  245. size_t varlen;
  246. char **orig_entry;
  247. varlen = strcspn(*entry, "=") + 1;
  248. /* Check for overwrite */
  249. for (orig_entry = child_env; *orig_entry != NULL; orig_entry++) {
  250. if (strncmp(*entry, *orig_entry, varlen) != 0)
  251. continue;
  252. free(*orig_entry);
  253. *orig_entry = *entry;
  254. break;
  255. }
  256. /* New variable? */
  257. if (*orig_entry == NULL) {
  258. /*
  259. * orig_entry will be pointing at the terminating entry,
  260. * so we can just tack it on here. The new NULL was already
  261. * prepared by calloc().
  262. */
  263. *orig_entry = *entry;
  264. }
  265. }
  266. return child_env;
  267. }
  268. static void
  269. switch_user(const char *username, uid_t uid, gid_t gid)
  270. {
  271. // We must change group stuff first, because only root can do that.
  272. if (setgid(gid) < 0) {
  273. perror(": setgid");
  274. _exit(EX_OSERR);
  275. }
  276. // Supplementary groups.
  277. if (initgroups(username, gid) < 0) {
  278. perror("initgroups");
  279. _exit(EX_OSERR);
  280. }
  281. // Set euid, ruid and suid
  282. if (setuid(uid) < 0) {
  283. perror("setuid");
  284. _exit(EX_OSERR);
  285. }
  286. }
  287. static void
  288. redir_stdio(const char *homedir, const char *display)
  289. {
  290. int fd;
  291. char hostname[HOST_NAME_MAX+1];
  292. char logfile[PATH_MAX];
  293. fd = open("/dev/null", O_RDONLY);
  294. if (fd == -1) {
  295. perror("open");
  296. _exit(EX_OSERR);
  297. }
  298. if (dup2(fd, 0) == -1) {
  299. perror("dup2");
  300. _exit(EX_OSERR);
  301. }
  302. close(fd);
  303. snprintf(logfile, sizeof(logfile), "%s/.vnc", homedir);
  304. if (mkdir(logfile, 0755) == -1) {
  305. if (errno != EEXIST) {
  306. perror("mkdir");
  307. _exit(EX_OSERR);
  308. }
  309. }
  310. if (gethostname(hostname, sizeof(hostname)) == -1) {
  311. perror("gethostname");
  312. _exit(EX_OSERR);
  313. }
  314. snprintf(logfile, sizeof(logfile), "%s/.vnc/%s%s.log",
  315. homedir, hostname, display);
  316. fd = open(logfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
  317. if (fd == -1) {
  318. perror("open");
  319. _exit(EX_OSERR);
  320. }
  321. if ((dup2(fd, 1) == -1) || (dup2(fd, 2) == -1)) {
  322. perror("dup2");
  323. _exit(EX_OSERR);
  324. }
  325. close(fd);
  326. }
  327. static void
  328. close_fds(void)
  329. {
  330. DIR *dir;
  331. struct dirent *entry;
  332. dir = opendir("/proc/self/fd");
  333. if (dir == NULL) {
  334. perror("opendir");
  335. _exit(EX_OSERR);
  336. }
  337. while ((entry = readdir(dir)) != NULL) {
  338. int fd;
  339. fd = atoi(entry->d_name);
  340. if (fd < 3)
  341. continue;
  342. close(fd);
  343. }
  344. closedir(dir);
  345. }
  346. static pid_t
  347. run_script(const char *username, const char *display, char **envp)
  348. {
  349. struct passwd *pwent;
  350. pid_t pid;
  351. const char *child_argv[3];
  352. pwent = getpwnam(username);
  353. if (pwent == NULL) {
  354. syslog(LOG_CRIT, "getpwnam: %s", strerror(errno));
  355. return -1;
  356. }
  357. pid = fork();
  358. if (pid < 0) {
  359. syslog(LOG_CRIT, "fork: %s", strerror(errno));
  360. return pid;
  361. }
  362. /* two processes now */
  363. if (pid > 0)
  364. return pid;
  365. /* child */
  366. switch_user(pwent->pw_name, pwent->pw_uid, pwent->pw_gid);
  367. close_fds();
  368. redir_stdio(pwent->pw_dir, display);
  369. // execvpe() is not POSIX and is missing from older glibc
  370. // First clear out everything
  371. while ((environ != NULL) && (*environ != NULL)) {
  372. char *var, *eq;
  373. var = strdup(*environ);
  374. eq = strchr(var, '=');
  375. if (eq)
  376. *eq = '\0';
  377. unsetenv(var);
  378. free(var);
  379. }
  380. // Then copy over the desired environment
  381. for (; *envp != NULL; envp++)
  382. putenv(*envp);
  383. // Set up some basic environment for the script
  384. setenv("HOME", pwent->pw_dir, 1);
  385. setenv("SHELL", pwent->pw_shell, 1);
  386. setenv("LOGNAME", pwent->pw_name, 1);
  387. setenv("USER", pwent->pw_name, 1);
  388. setenv("USERNAME", pwent->pw_name, 1);
  389. child_argv[0] = CMAKE_INSTALL_FULL_LIBEXECDIR "/vncserver";
  390. child_argv[1] = display;
  391. child_argv[2] = NULL;
  392. execvp(child_argv[0], (char*const*)child_argv);
  393. // execvp failed
  394. perror("execvp");
  395. _exit(EX_OSERR);
  396. }
  397. int
  398. main(int argc, char **argv)
  399. {
  400. char pid_file[PATH_MAX];
  401. FILE *f;
  402. const char *username, *display;
  403. if ((argc != 3) || (argv[2][0] != ':')) {
  404. fprintf(stderr, "Syntax:\n");
  405. fprintf(stderr, " %s <username> <display>\n", argv[0]);
  406. return EX_USAGE;
  407. }
  408. username = argv[1];
  409. display = argv[2];
  410. if (geteuid() != 0) {
  411. fprintf(stderr, "This program needs to be run as root!\n");
  412. return EX_USAGE;
  413. }
  414. if (getpwnam(username) == NULL) {
  415. if (errno == 0)
  416. fprintf(stderr, "User \"%s\" does not exist\n", username);
  417. else
  418. fprintf(stderr, "Cannot look up user \"%s\": %s\n",
  419. username, strerror(errno));
  420. return EX_OSERR;
  421. }
  422. if (begin_daemon() == -1)
  423. return EX_OSERR;
  424. openlog("vncsession", LOG_PID, LOG_AUTH);
  425. /* Indicate that this is a graphical user session. We need to do
  426. this here before PAM as pam_systemd.so looks at these. */
  427. if ((putenv("XDG_SESSION_CLASS=user") < 0) ||
  428. (putenv("XDG_SESSION_TYPE=x11") < 0)) {
  429. syslog(LOG_CRIT, "putenv: %s", strerror(errno));
  430. return EX_OSERR;
  431. }
  432. /* Init PAM */
  433. int pamret;
  434. pam_handle_t *pamh = run_pam(&pamret, username, display);
  435. if (!pamh) {
  436. return EX_OSERR;
  437. }
  438. if (pamret != PAM_SUCCESS) {
  439. stop_pam(pamh, pamret);
  440. return EX_OSERR;
  441. }
  442. char **child_env;
  443. child_env = prepare_environ(pamh);
  444. if (child_env == NULL) {
  445. syslog(LOG_CRIT, "Failure creating child process environment");
  446. stop_pam(pamh, pamret);
  447. return EX_OSERR;
  448. }
  449. setup_signals();
  450. script = run_script(username, display, child_env);
  451. if (script == -1) {
  452. syslog(LOG_CRIT, "Failure starting vncserver script");
  453. stop_pam(pamh, pamret);
  454. return EX_OSERR;
  455. }
  456. snprintf(pid_file, sizeof(pid_file),
  457. "/var/run/vncsession-%s.pid", display);
  458. f = fopen(pid_file, "w");
  459. if (f == NULL) {
  460. syslog(LOG_ERR, "Failure creating pid file \"%s\": %s",
  461. pid_file, strerror(errno));
  462. } else {
  463. fprintf(f, "%ld\n", (long)getpid());
  464. fclose(f);
  465. }
  466. finish_daemon();
  467. while (1) {
  468. int status;
  469. pid_t gotpid = waitpid(script, &status, 0);
  470. if (gotpid < 0) {
  471. if (errno != EINTR) {
  472. syslog(LOG_CRIT, "waitpid: %s", strerror(errno));
  473. exit(EXIT_FAILURE);
  474. }
  475. continue;
  476. }
  477. if (WIFEXITED(status)) {
  478. if (WEXITSTATUS(status) != 0) {
  479. syslog(LOG_WARNING,
  480. "vncsession: vncserver exited with status=%d",
  481. WEXITSTATUS(status));
  482. }
  483. break;
  484. }
  485. else if (WIFSIGNALED(status)) {
  486. syslog(LOG_WARNING,
  487. "vncsession: vncserver was terminated by signal %d",
  488. WTERMSIG(status));
  489. break;
  490. }
  491. }
  492. unlink(pid_file);
  493. stop_pam(pamh, pamret);
  494. return 0;
  495. }