Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
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.

182 lines
5.1 KiB

27 years ago
  1. /* $OpenBSD: login_fbtab.c,v 1.16 2015/11/27 01:57:59 mmcc Exp $ */
  2. /************************************************************************
  3. * Copyright 1995 by Wietse Venema. All rights reserved. Some individual
  4. * files may be covered by other copyrights.
  5. *
  6. * This material was originally written and compiled by Wietse Venema at
  7. * Eindhoven University of Technology, The Netherlands, in 1990, 1991,
  8. * 1992, 1993, 1994 and 1995.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that this entire copyright notice
  12. * is duplicated in all such copies.
  13. *
  14. * This software is provided "as is" and without any expressed or implied
  15. * warranties, including, without limitation, the implied warranties of
  16. * merchantibility and fitness for any particular purpose.
  17. ************************************************************************/
  18. /*
  19. SYNOPSIS
  20. void login_fbtab(tty, uid, gid)
  21. char *tty;
  22. uid_t uid;
  23. gid_t gid;
  24. DESCRIPTION
  25. This module implements device security as described in the
  26. SunOS 4.1.x fbtab(5) and SunOS 5.x logindevperm(4) manual
  27. pages. The program first looks for /etc/fbtab. If that file
  28. cannot be opened it attempts to process /etc/logindevperm.
  29. We expect entries with the folowing format:
  30. Comments start with a # and extend to the end of the line.
  31. Blank lines or lines with only a comment are ignored.
  32. All other lines consist of three fields delimited by
  33. whitespace: a login device (/dev/console), an octal
  34. permission number (0600), and a ":"-delimited list of
  35. devices (/dev/kbd:/dev/mouse). All device names are
  36. absolute paths. A path that ends in "*" refers to all
  37. directory entries except "." and "..".
  38. If the tty argument (relative path) matches a login device
  39. name (absolute path), the permissions of the devices in the
  40. ":"-delimited list are set as specified in the second
  41. field, and their ownership is changed to that of the uid
  42. and gid arguments.
  43. DIAGNOSTICS
  44. Problems are reported via the syslog daemon with severity
  45. LOG_ERR.
  46. AUTHOR
  47. Wietse Venema (wietse@wzv.win.tue.nl)
  48. Eindhoven University of Technology
  49. The Netherlands
  50. */
  51. #include <sys/types.h>
  52. #include <sys/stat.h>
  53. #include <errno.h>
  54. #include <dirent.h>
  55. #include <limits.h>
  56. #include <paths.h>
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59. #include <string.h>
  60. #include <syslog.h>
  61. #include <unistd.h>
  62. #include <util.h>
  63. #define _PATH_FBTAB "/etc/fbtab"
  64. static void login_protect(const char *, mode_t, uid_t, gid_t);
  65. #define WSPACE " \t\n"
  66. /*
  67. * login_fbtab - apply protections specified in /etc/fbtab or logindevperm
  68. */
  69. void
  70. login_fbtab(const char *tty, uid_t uid, gid_t gid)
  71. {
  72. FILE *fp;
  73. char *buf, *toklast, *tbuf, *devnam, *cp;
  74. mode_t prot;
  75. size_t len;
  76. if ((fp = fopen(_PATH_FBTAB, "r")) == NULL)
  77. return;
  78. tbuf = NULL;
  79. while ((buf = fgetln(fp, &len)) != NULL) {
  80. if (buf[len - 1] == '\n')
  81. buf[len - 1] = '\0';
  82. else {
  83. if ((tbuf = malloc(len + 1)) == NULL)
  84. break;
  85. memcpy(tbuf, buf, len);
  86. tbuf[len] = '\0';
  87. buf = tbuf;
  88. }
  89. if ((cp = strchr(buf, '#')))
  90. *cp = '\0'; /* strip comment */
  91. if (buf[0] == '\0' ||
  92. (cp = devnam = strtok_r(buf, WSPACE, &toklast)) == NULL)
  93. continue; /* empty or comment */
  94. if (strncmp(devnam, _PATH_DEV, sizeof(_PATH_DEV) - 1) != 0 ||
  95. (cp = strtok_r(NULL, WSPACE, &toklast)) == NULL ||
  96. *cp != '0' ||
  97. sscanf(cp, "%o", &prot) == 0 ||
  98. prot == 0 ||
  99. (prot & 0777) != prot ||
  100. (cp = strtok_r(NULL, WSPACE, &toklast)) == NULL) {
  101. syslog(LOG_ERR, "%s: bad entry: %s", _PATH_FBTAB,
  102. cp ? cp : "(null)");
  103. continue;
  104. }
  105. if (strcmp(devnam + sizeof(_PATH_DEV) - 1, tty) == 0) {
  106. for (cp = strtok_r(cp, ":", &toklast); cp != NULL;
  107. cp = strtok_r(NULL, ":", &toklast))
  108. login_protect(cp, prot, uid, gid);
  109. }
  110. }
  111. free(tbuf);
  112. fclose(fp);
  113. }
  114. /*
  115. * login_protect - protect one device entry
  116. */
  117. static void
  118. login_protect(const char *path, mode_t mask, uid_t uid, gid_t gid)
  119. {
  120. char buf[PATH_MAX];
  121. size_t pathlen = strlen(path);
  122. DIR *dir;
  123. struct dirent *ent;
  124. if (pathlen >= sizeof(buf)) {
  125. errno = ENAMETOOLONG;
  126. syslog(LOG_ERR, "%s: %s: %m", _PATH_FBTAB, path);
  127. return;
  128. }
  129. if (strcmp("/*", path + pathlen - 2) != 0) {
  130. if (chmod(path, mask) && errno != ENOENT)
  131. syslog(LOG_ERR, "%s: chmod(%s): %m", _PATH_FBTAB, path);
  132. if (chown(path, uid, gid) && errno != ENOENT)
  133. syslog(LOG_ERR, "%s: chown(%s): %m", _PATH_FBTAB, path);
  134. } else {
  135. /*
  136. * This is a wildcard directory (/path/to/whatever/ * ).
  137. * Make a copy of path without the trailing '*' (but leave
  138. * the trailing '/' so we can append directory entries.)
  139. */
  140. memcpy(buf, path, pathlen - 1);
  141. buf[pathlen - 1] = '\0';
  142. if ((dir = opendir(buf)) == NULL) {
  143. syslog(LOG_ERR, "%s: opendir(%s): %m", _PATH_FBTAB,
  144. path);
  145. return;
  146. }
  147. while ((ent = readdir(dir)) != NULL) {
  148. if (strcmp(ent->d_name, ".") != 0 &&
  149. strcmp(ent->d_name, "..") != 0) {
  150. buf[pathlen - 1] = '\0';
  151. if (strlcat(buf, ent->d_name, sizeof(buf))
  152. >= sizeof(buf)) {
  153. errno = ENAMETOOLONG;
  154. syslog(LOG_ERR, "%s: %s: %m",
  155. _PATH_FBTAB, path);
  156. } else
  157. login_protect(buf, mask, uid, gid);
  158. }
  159. }
  160. closedir(dir);
  161. }
  162. }