You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vncsession.c 16KB

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