vtysh_user.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #ifdef USE_PAM
  37. static struct pam_conv conv =
  38. {
  39. PAM_CONV_FUNC,
  40. NULL
  41. };
  42. int
  43. vtysh_pam (const char *user)
  44. {
  45. int ret;
  46. pam_handle_t *pamh = NULL;
  47. /* Start PAM. */
  48. ret = pam_start(QUAGGA_PROGNAME, user, &conv, &pamh);
  49. /* printf ("ret %d\n", ret); */
  50. /* Is user really user? */
  51. if (ret == PAM_SUCCESS)
  52. ret = pam_authenticate (pamh, 0);
  53. /* printf ("ret %d\n", ret); */
  54. #if 0
  55. /* Permitted access? */
  56. if (ret == PAM_SUCCESS)
  57. ret = pam_acct_mgmt (pamh, 0);
  58. printf ("ret %d\n", ret);
  59. if (ret == PAM_AUTHINFO_UNAVAIL)
  60. ret = PAM_SUCCESS;
  61. #endif /* 0 */
  62. /* This is where we have been authorized or not. */
  63. #ifdef DEBUG
  64. if (ret == PAM_SUCCESS)
  65. printf("Authenticated\n");
  66. else
  67. printf("Not Authenticated\n");
  68. #endif /* DEBUG */
  69. /* close Linux-PAM */
  70. if (pam_end (pamh, ret) != PAM_SUCCESS)
  71. {
  72. pamh = NULL;
  73. fprintf(stderr, "vtysh_pam: failed to release authenticator\n");
  74. exit(1);
  75. }
  76. return ret == PAM_SUCCESS ? 0 : 1;
  77. }
  78. #endif /* USE_PAM */
  79. struct vtysh_user
  80. {
  81. char *name;
  82. u_char nopassword;
  83. };
  84. struct list *userlist;
  85. struct vtysh_user *
  86. user_new ()
  87. {
  88. struct vtysh_user *user;
  89. user = XMALLOC (0, sizeof (struct vtysh_user));
  90. memset (user, 0, sizeof (struct vtysh_user));
  91. return user;
  92. }
  93. void
  94. user_free (struct vtysh_user *user)
  95. {
  96. XFREE (0, user);
  97. }
  98. 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. void
  111. user_config_write ()
  112. {
  113. struct listnode *node, *nnode;
  114. struct vtysh_user *user;
  115. for (ALL_LIST_ELEMENTS (userlist, node, nnode, user))
  116. {
  117. if (user->nopassword)
  118. printf (" username %s nopassword\n", user->name);
  119. }
  120. }
  121. struct vtysh_user *
  122. user_get (const char *name)
  123. {
  124. struct vtysh_user *user;
  125. user = user_lookup (name);
  126. if (user)
  127. return user;
  128. user = user_new ();
  129. user->name = strdup (name);
  130. listnode_add (userlist, user);
  131. return user;
  132. }
  133. DEFUN (username_nopassword,
  134. username_nopassword_cmd,
  135. "username WORD nopassword",
  136. "\n"
  137. "\n"
  138. "\n")
  139. {
  140. struct vtysh_user *user;
  141. user = user_get (argv[0]);
  142. user->nopassword = 1;
  143. return CMD_SUCCESS;
  144. }
  145. int
  146. vtysh_auth ()
  147. {
  148. struct vtysh_user *user;
  149. struct passwd *passwd;
  150. passwd = getpwuid (geteuid ());
  151. user = user_lookup (passwd->pw_name);
  152. if (user && user->nopassword)
  153. /* Pass through */;
  154. else
  155. {
  156. #ifdef USE_PAM
  157. if (vtysh_pam (passwd->pw_name))
  158. exit (0);
  159. #endif /* USE_PAM */
  160. }
  161. return 0;
  162. }
  163. void
  164. vtysh_user_init ()
  165. {
  166. userlist = list_new ();
  167. install_element (CONFIG_NODE, &username_nopassword_cmd);
  168. }