vtysh_user.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. return XCALLOC (0, sizeof (struct vtysh_user));
  89. }
  90. void
  91. user_free (struct vtysh_user *user)
  92. {
  93. XFREE (0, user);
  94. }
  95. struct vtysh_user *
  96. user_lookup (const char *name)
  97. {
  98. struct listnode *node, *nnode;
  99. struct vtysh_user *user;
  100. for (ALL_LIST_ELEMENTS (userlist, node, nnode, user))
  101. {
  102. if (strcmp (user->name, name) == 0)
  103. return user;
  104. }
  105. return NULL;
  106. }
  107. void
  108. user_config_write ()
  109. {
  110. struct listnode *node, *nnode;
  111. struct vtysh_user *user;
  112. for (ALL_LIST_ELEMENTS (userlist, node, nnode, user))
  113. {
  114. if (user->nopassword)
  115. printf (" username %s nopassword\n", user->name);
  116. }
  117. }
  118. struct vtysh_user *
  119. user_get (const char *name)
  120. {
  121. struct vtysh_user *user;
  122. user = user_lookup (name);
  123. if (user)
  124. return user;
  125. user = user_new ();
  126. user->name = strdup (name);
  127. listnode_add (userlist, user);
  128. return user;
  129. }
  130. DEFUN (username_nopassword,
  131. username_nopassword_cmd,
  132. "username WORD nopassword",
  133. "\n"
  134. "\n"
  135. "\n")
  136. {
  137. struct vtysh_user *user;
  138. user = user_get (argv[0]);
  139. user->nopassword = 1;
  140. return CMD_SUCCESS;
  141. }
  142. int
  143. vtysh_auth ()
  144. {
  145. struct vtysh_user *user;
  146. struct passwd *passwd;
  147. passwd = getpwuid (geteuid ());
  148. user = user_lookup (passwd->pw_name);
  149. if (user && user->nopassword)
  150. /* Pass through */;
  151. else
  152. {
  153. #ifdef USE_PAM
  154. if (vtysh_pam (passwd->pw_name))
  155. exit (0);
  156. #endif /* USE_PAM */
  157. }
  158. return 0;
  159. }
  160. void
  161. vtysh_user_init ()
  162. {
  163. userlist = list_new ();
  164. install_element (CONFIG_NODE, &username_nopassword_cmd);
  165. }