1 /* User authentication for vtysh.
2 * Copyright (C) 2000 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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
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.
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
23 #include <lib/version.h>
28 #include <security/pam_appl.h>
29 #ifdef HAVE_PAM_MISC_H
30 #include <security/pam_misc.h>
33 #include <security/openpam.h>
40 #include "vtysh_user.h"
43 static struct pam_conv conv =
50 vtysh_pam (const char *user)
53 pam_handle_t *pamh = NULL;
56 ret = pam_start(QUAGGA_PROGNAME, user, &conv, &pamh);
57 /* printf ("ret %d\n", ret); */
59 /* Is user really user? */
60 if (ret == PAM_SUCCESS)
61 ret = pam_authenticate (pamh, 0);
62 if (ret != PAM_SUCCESS)
63 printf("Not authenticated. Check /etc/pam.d/quagga.\n");
64 /* printf ("ret %d\n", ret); */
67 /* Permitted access? */
68 if (ret == PAM_SUCCESS)
69 ret = pam_acct_mgmt (pamh, 0);
70 printf ("ret %d\n", ret);
72 if (ret == PAM_AUTHINFO_UNAVAIL)
76 /* This is where we have been authorized or not. */
78 if (ret == PAM_SUCCESS)
79 printf("Authenticated\n");
81 printf("Not Authenticated\n");
85 if (pam_end (pamh, ret) != PAM_SUCCESS)
88 fprintf(stderr, "vtysh_pam: failed to release authenticator\n");
92 return ret == PAM_SUCCESS ? 0 : 1;
102 struct list *userlist;
104 static struct vtysh_user *
107 return XCALLOC (MTYPE_TMP, sizeof (struct vtysh_user));
112 user_free (struct vtysh_user *user)
118 static struct vtysh_user *
119 user_lookup (const char *name)
121 struct listnode *node, *nnode;
122 struct vtysh_user *user;
124 for (ALL_LIST_ELEMENTS (userlist, node, nnode, user))
126 if (strcmp (user->name, name) == 0)
136 struct listnode *node, *nnode;
137 struct vtysh_user *user;
139 for (ALL_LIST_ELEMENTS (userlist, node, nnode, user))
141 if (user->nopassword)
142 printf (" username %s nopassword\n", user->name);
147 static struct vtysh_user *
148 user_get (const char *name)
150 struct vtysh_user *user;
151 user = user_lookup (name);
156 user->name = strdup (name);
157 listnode_add (userlist, user);
162 DEFUN (username_nopassword,
163 username_nopassword_cmd,
164 "username WORD nopassword",
169 struct vtysh_user *user;
170 user = user_get (argv[0]);
171 user->nopassword = 1;
178 struct vtysh_user *user;
179 struct passwd *passwd;
181 if ((passwd = getpwuid (geteuid ())) == NULL)
183 fprintf (stderr, "could not lookup user ID %d\n", (int) geteuid());
187 user = user_lookup (passwd->pw_name);
188 if (user && user->nopassword)
193 if (vtysh_pam (passwd->pw_name))
201 vtysh_get_home (void)
203 struct passwd *passwd;
205 passwd = getpwuid (getuid ());
207 return passwd ? passwd->pw_dir : NULL;
211 vtysh_user_init (void)
213 userlist = list_new ();
214 install_element (CONFIG_NODE, &username_nopassword_cmd);