notify-pam-dead.patch 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. Index: src/init.sample
  2. ===================================================================
  3. --- src/init.sample (revision 0)
  4. +++ src/init.sample (revision 0)
  5. @@ -0,0 +1,9 @@
  6. +#%PAM-1.0
  7. +#
  8. +# The PAM configuration file for /sbin/init
  9. +# Used for updating the lastlog logging file
  10. +#
  11. +auth sufficient pam_rootok.so
  12. +account include common-account
  13. +session include common-session
  14. +session requisite pam_lastlog.so silent
  15. Index: src/init.c
  16. ===================================================================
  17. --- src/init.c (revision 56)
  18. +++ src/init.c (working copy)
  19. @@ -76,6 +76,10 @@
  20. #include "reboot.h"
  21. #include "set.h"
  22. +#ifdef USE_PAM
  23. +extern void notify_pam_dead_session(const char *id);
  24. +#endif
  25. +
  26. #ifndef SIGPWR
  27. # define SIGPWR SIGUSR2
  28. #endif
  29. @@ -1129,6 +1133,9 @@
  30. }
  31. dup(f);
  32. dup(f);
  33. +#ifdef USE_PAM
  34. + notify_pam_dead_session(ch->id);
  35. +#endif
  36. }
  37. /*
  38. @@ -1548,6 +1555,9 @@
  39. INITDBG(L_VB, "Updating utmp for pid %d [id %s]",
  40. ch->pid, ch->id);
  41. ch->flags &= ~RUNNING;
  42. +#ifdef USE_PAM
  43. + notify_pam_dead_session(ch->id);
  44. +#endif
  45. if (ch->process[0] != '+')
  46. write_utmp_wtmp("", ch->id, ch->pid, DEAD_PROCESS, NULL);
  47. }
  48. @@ -2009,6 +2019,9 @@
  49. if (ch->flags & ZOMBIE) {
  50. INITDBG(L_VB, "Child died, PID= %d", ch->pid);
  51. ch->flags &= ~(RUNNING|ZOMBIE|WAITING);
  52. +#ifdef USE_PAM
  53. + notify_pam_dead_session(ch->id);
  54. +#endif
  55. if (ch->process[0] != '+')
  56. write_utmp_wtmp("", ch->id, ch->pid, DEAD_PROCESS, NULL);
  57. }
  58. @@ -2453,6 +2466,9 @@
  59. if (ch->flags & ZOMBIE) {
  60. INITDBG(L_VB, "Child died, PID= %d", ch->pid);
  61. ch->flags &= ~(RUNNING|ZOMBIE|WAITING);
  62. +#ifdef USE_PAM
  63. + notify_pam_dead_session(ch->id);
  64. +#endif
  65. if (ch->process[0] != '+')
  66. write_utmp_wtmp("", ch->id, ch->pid, DEAD_PROCESS, NULL);
  67. }
  68. Index: src/utmp.c
  69. ===================================================================
  70. --- src/utmp.c (revision 51)
  71. +++ src/utmp.c (working copy)
  72. @@ -34,10 +34,18 @@
  73. #include <string.h>
  74. #include <utmp.h>
  75. +#if defined(USE_PAM) && defined(INIT_MAIN)
  76. +# include <security/pam_appl.h>
  77. +# include <security/pam_misc.h>
  78. +#endif
  79. +
  80. #include "init.h"
  81. #include "initreq.h"
  82. #include "paths.h"
  83. +#ifndef _PATH_DEV
  84. +# define _PATH_DEV "/dev/"
  85. +#endif
  86. #if defined(__GLIBC__)
  87. # if (__GLIBC__ == 2) && (__GLIBC_MINOR__ == 0) && defined(__powerpc__)
  88. @@ -127,9 +135,9 @@
  89. strncpy(utmp.ut_name, user, sizeof(utmp.ut_name));
  90. strncpy(utmp.ut_id , id , sizeof(utmp.ut_id ));
  91. strncpy(utmp.ut_line, line, sizeof(utmp.ut_line));
  92. -
  93. - /* Put the OS version in place of the hostname */
  94. - if (uname(&uname_buf) == 0)
  95. +
  96. + /* Put the OS version in place of the hostname */
  97. + if (uname(&uname_buf) == 0)
  98. strncpy(utmp.ut_host, uname_buf.release, sizeof(utmp.ut_host));
  99. #if HAVE_UPDWTMP
  100. @@ -262,3 +270,75 @@
  101. write_wtmp(user, id, pid, type, line && line[0] ? line : oldline);
  102. }
  103. +#if defined(USE_PAM) && defined(INIT_MAIN)
  104. +static pam_handle_t *pamh = NULL;
  105. +# ifdef __GNUC__
  106. +static int
  107. +null_conv(int num_msg, const struct pam_message **msgm,
  108. + struct pam_response **response __attribute__((unused)),
  109. + void *appdata_ptr __attribute__((unused)))
  110. +# else
  111. +static int
  112. +null_conv(int num_msg, const struct pam_message **msgm,
  113. + struct pam_response **response, void *appdata_ptr)
  114. +# endif
  115. +{
  116. + int i;
  117. + for (i = 0; i < num_msg; i++) {
  118. + const struct pam_message *msg = msgm[i];
  119. + if (msg == (const struct pam_message*)0)
  120. + continue;
  121. + if (msg->msg == (char*)0)
  122. + continue;
  123. + switch (msg->msg_style) {
  124. + case PAM_ERROR_MSG:
  125. + case PAM_TEXT_INFO:
  126. + initlog(L_VB, "pam_message %s", msg->msg);
  127. + default:
  128. + break;
  129. + }
  130. + }
  131. + return 0;
  132. +}
  133. +static const struct pam_conv conv = { null_conv, NULL };
  134. +# define PAM_FAIL_CHECK(func, args...) \
  135. + { \
  136. + if ((pam_ret = (func)(args)) != PAM_SUCCESS) { \
  137. + initlog(L_VB, "%s", pam_strerror(pamh, pam_ret)); \
  138. + goto pam_error; \
  139. + } \
  140. + }
  141. +
  142. +void notify_pam_dead_session(const char *id)
  143. +{
  144. + struct utmp *oldut, ut;
  145. +
  146. + setutent();
  147. +
  148. + memset(&ut, 0, sizeof(ut));
  149. + ut.ut_type = DEAD_PROCESS;
  150. + strncpy(ut.ut_id, id, sizeof(ut.ut_id));
  151. +
  152. + if ((oldut = getutid(&ut)) && (oldut->ut_type == USER_PROCESS)) {
  153. + int pam_ret;
  154. + char tty[UT_LINESIZE+ strlen(_PATH_DEV) + 1];
  155. +
  156. + if (strncmp(oldut->ut_line, _PATH_DEV, strlen(_PATH_DEV)))
  157. + snprintf(tty, sizeof(tty), _PATH_DEV "%.*s",
  158. + UT_LINESIZE, oldut->ut_line);
  159. + else
  160. + snprintf(tty, sizeof(tty), "%.*s",
  161. + UT_LINESIZE, oldut->ut_line);
  162. +
  163. + PAM_FAIL_CHECK(pam_start, "init", oldut->ut_user, &conv, &pamh);
  164. + PAM_FAIL_CHECK(pam_set_item, pamh, PAM_TTY, tty);
  165. + PAM_FAIL_CHECK(pam_set_item, pamh, PAM_RHOST, oldut->ut_host);
  166. + PAM_FAIL_CHECK(pam_close_session, pamh, PAM_SILENT);
  167. + pam_error:
  168. + pam_end(pamh, pam_ret);
  169. + }
  170. +
  171. + endutent();
  172. +}
  173. +#endif /* USE_PAM && INIT_MAIN */
  174. +
  175. Index: src/Makefile
  176. ===================================================================
  177. --- src/Makefile (revision 58)
  178. +++ src/Makefile (working copy)
  179. @@ -8,7 +8,7 @@
  180. # Version: @(#)Makefile 2.85-13 23-Mar-2004 miquels@cistron.nl
  181. #
  182. -CPPFLAGS =
  183. +CPPFLAGS = -DUSE_PAM
  184. CFLAGS ?= -ansi -O2 -fomit-frame-pointer
  185. override CFLAGS += -W -Wall -D_GNU_SOURCE
  186. STATIC =
  187. @@ -79,6 +79,13 @@
  188. endif
  189. # Additional libs for GNU libc.
  190. +ifneq ($(findstring -DUSE_PAM,$(CPPFLAGS)),)
  191. + INITLIBS += -lpam
  192. + PAMDOTD = /etc/pam.d
  193. + PAMINIT = $(PAMDOTD)/init
  194. +endif
  195. +
  196. +# Additional libs for GNU libc.
  197. ifneq ($(wildcard /usr/lib*/libcrypt.a),)
  198. SULOGINLIBS += -lcrypt
  199. endif
  200. @@ -153,6 +160,11 @@
  201. $(STRIP) $$i ; \
  202. $(INSTALL_EXEC) $$i $(ROOT)/usr/bin/ ; \
  203. done
  204. +ifneq ($(findstring -DUSE_PAM,$(CPPFLAGS)),)
  205. + $(INSTALL_DIR) $(ROOT)$(PAMDOTD)
  206. + test -s $(ROOT)$(PAMINIT) || \
  207. + $(INSTALL_DATA) init.sample $(ROOT)$(PAMINIT)
  208. +endif
  209. # $(INSTALL_DIR) $(ROOT)/etc/
  210. # $(INSTALL_EXEC) initscript.sample $(ROOT)/etc/
  211. ln -sf halt $(ROOT)/sbin/reboot