vtysh_user.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* User authentication for vtysh.
  2. * Copyright (C) 2000 Kunihiro Ishiguro
  3. *
  4. * This file is part of GNU Zebra.
  5. *
  6. * GNU Zebra is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2, or (at your option) any
  9. * later version.
  10. *
  11. * GNU Zebra is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  18. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. * 02111-1307, USA.
  20. */
  21. #include <zebra.h>
  22. #include <lib/version.h>
  23. #include <pwd.h>
  24. #ifdef USE_PAM
  25. #include <security/pam_appl.h>
  26. #ifdef HAVE_PAM_MISC_H
  27. #include <security/pam_misc.h>
  28. #endif
  29. #ifdef HAVE_OPENPAM_H
  30. #include <security/openpam.h>
  31. #endif
  32. #endif /* USE_PAM */
  33. #include "memory.h"
  34. #include "linklist.h"
  35. #include "command.h"
  36. #include "vtysh_user.h"
  37. #ifdef USE_PAM
  38. static struct pam_conv conv =
  39. {
  40. PAM_CONV_FUNC,
  41. NULL
  42. };
  43. static int
  44. vtysh_pam (const char *user)
  45. {
  46. int ret;
  47. pam_handle_t *pamh = NULL;
  48. /* Start PAM. */
  49. ret = pam_start(QUAGGA_PROGNAME, user, &conv, &pamh);
  50. /* printf ("ret %d\n", ret); */
  51. /* Is user really user? */
  52. if (ret == PAM_SUCCESS)
  53. ret = pam_authenticate (pamh, 0);
  54. /* printf ("ret %d\n", ret); */
  55. #if 0
  56. /* Permitted access? */
  57. if (ret == PAM_SUCCESS)
  58. ret = pam_acct_mgmt (pamh, 0);
  59. printf ("ret %d\n", ret);
  60. if (ret == PAM_AUTHINFO_UNAVAIL)
  61. ret = PAM_SUCCESS;
  62. #endif /* 0 */
  63. /* This is where we have been authorized or not. */
  64. #ifdef DEBUG
  65. if (ret == PAM_SUCCESS)
  66. printf("Authenticated\n");
  67. else
  68. printf("Not Authenticated\n");
  69. #endif /* DEBUG */
  70. /* close Linux-PAM */
  71. if (pam_end (pamh, ret) != PAM_SUCCESS)
  72. {
  73. pamh = NULL;
  74. fprintf(stderr, "vtysh_pam: failed to release authenticator\n");
  75. exit(1);
  76. }
  77. return ret == PAM_SUCCESS ? 0 : 1;
  78. }
  79. #endif /* USE_PAM */
  80. struct vtysh_user
  81. {
  82. char *name;
  83. u_char nopassword;
  84. };
  85. struct list *userlist;
  86. static struct vtysh_user *
  87. user_new ()
  88. {
  89. return XCALLOC (0, sizeof (struct vtysh_user));
  90. }
  91. #if 0
  92. static void
  93. user_free (struct vtysh_user *user)
  94. {
  95. XFREE (0, user);
  96. }
  97. #endif
  98. static struct vtysh_user *
  99. user_lookup (const char *name)
  100. {
  101. struct listnode *node, *nnode;
  102. struct vtysh_user *user;
  103. for (ALL_LIST_ELEMENTS (userlist, node, nnode, user))
  104. {
  105. if (strcmp (user->name, name) == 0)
  106. return user;
  107. }
  108. return NULL;
  109. }
  110. #if 0
  111. static void
  112. user_config_write ()
  113. {
  114. struct listnode *node, *nnode;
  115. struct vtysh_user *user;
  116. for (ALL_LIST_ELEMENTS (userlist, node, nnode, user))
  117. {
  118. if (user->nopassword)
  119. printf (" username %s nopassword\n", user->name);
  120. }
  121. }
  122. #endif
  123. static struct vtysh_user *
  124. user_get (const char *name)
  125. {
  126. struct vtysh_user *user;
  127. user = user_lookup (name);
  128. if (user)
  129. return user;
  130. user = user_new ();
  131. user->name = strdup (name);
  132. listnode_add (userlist, user);
  133. return user;
  134. }
  135. DEFUN (username_nopassword,
  136. username_nopassword_cmd,
  137. "username WORD nopassword",
  138. "\n"
  139. "\n"
  140. "\n")
  141. {
  142. struct vtysh_user *user;
  143. user = user_get (argv[0]);
  144. user->nopassword = 1;
  145. return CMD_SUCCESS;
  146. }
  147. int
  148. vtysh_auth (void)
  149. {
  150. struct vtysh_user *user;
  151. struct passwd *passwd;
  152. passwd = getpwuid (geteuid ());
  153. user = user_lookup (passwd->pw_name);
  154. if (user && user->nopassword)
  155. /* Pass through */;
  156. else
  157. {
  158. #ifdef USE_PAM
  159. if (vtysh_pam (passwd->pw_name))
  160. exit (0);
  161. #endif /* USE_PAM */
  162. }
  163. return 0;
  164. }
  165. char *
  166. vtysh_get_home (void)
  167. {
  168. struct passwd *passwd;
  169. passwd = getpwuid (getuid ());
  170. return passwd ? passwd->pw_dir : NULL;
  171. }
  172. void
  173. vtysh_user_init (void)
  174. {
  175. userlist = list_new ();
  176. install_element (CONFIG_NODE, &username_nopassword_cmd);
  177. }