blob: 8f2cf422039aadcc559f8879e6e1226ef1d9271d [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* 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
22#include <zebra.h>
paulbb6065a2003-08-13 04:07:52 +000023#include <lib/version.h>
paul718e3742002-12-13 20:15:29 +000024
25#include <pwd.h>
26
27#ifdef USE_PAM
28#include <security/pam_appl.h>
paul24cd4352003-05-06 12:16:27 +000029#ifdef HAVE_PAM_MISC_H
paul718e3742002-12-13 20:15:29 +000030#include <security/pam_misc.h>
paul24cd4352003-05-06 12:16:27 +000031#endif
32#ifdef HAVE_OPENPAM_H
33#include <security/openpam.h>
34#endif
paul718e3742002-12-13 20:15:29 +000035#endif /* USE_PAM */
36
37#include "memory.h"
38#include "linklist.h"
39#include "command.h"
David Lamparter388f8852015-03-03 08:55:54 +010040#include "vtysh_user.h"
paul718e3742002-12-13 20:15:29 +000041
42#ifdef USE_PAM
43static struct pam_conv conv =
44{
paul24cd4352003-05-06 12:16:27 +000045 PAM_CONV_FUNC,
paul718e3742002-12-13 20:15:29 +000046 NULL
47};
48
49int
hasso5862ff52004-10-11 13:20:40 +000050vtysh_pam (const char *user)
paul718e3742002-12-13 20:15:29 +000051{
52 int ret;
53 pam_handle_t *pamh = NULL;
54
55 /* Start PAM. */
paul42053f42003-08-13 02:54:44 +000056 ret = pam_start(QUAGGA_PROGNAME, user, &conv, &pamh);
paul718e3742002-12-13 20:15:29 +000057 /* printf ("ret %d\n", ret); */
58
59 /* Is user really user? */
60 if (ret == PAM_SUCCESS)
61 ret = pam_authenticate (pamh, 0);
62 /* printf ("ret %d\n", ret); */
63
64#if 0
65 /* Permitted access? */
66 if (ret == PAM_SUCCESS)
67 ret = pam_acct_mgmt (pamh, 0);
68 printf ("ret %d\n", ret);
69
70 if (ret == PAM_AUTHINFO_UNAVAIL)
71 ret = PAM_SUCCESS;
72#endif /* 0 */
73
74 /* This is where we have been authorized or not. */
75#ifdef DEBUG
76 if (ret == PAM_SUCCESS)
77 printf("Authenticated\n");
78 else
79 printf("Not Authenticated\n");
80#endif /* DEBUG */
81
82 /* close Linux-PAM */
83 if (pam_end (pamh, ret) != PAM_SUCCESS)
84 {
85 pamh = NULL;
86 fprintf(stderr, "vtysh_pam: failed to release authenticator\n");
87 exit(1);
88 }
89
90 return ret == PAM_SUCCESS ? 0 : 1;
91}
92#endif /* USE_PAM */
93
paulb8994082005-07-12 15:15:02 +000094struct vtysh_user
paul718e3742002-12-13 20:15:29 +000095{
96 char *name;
97 u_char nopassword;
98};
99
100struct list *userlist;
101
paulb8994082005-07-12 15:15:02 +0000102struct vtysh_user *
paul718e3742002-12-13 20:15:29 +0000103user_new ()
104{
Stephen Hemminger393deb92008-08-18 14:13:29 -0700105 return XCALLOC (0, sizeof (struct vtysh_user));
paul718e3742002-12-13 20:15:29 +0000106}
107
108void
paulb8994082005-07-12 15:15:02 +0000109user_free (struct vtysh_user *user)
paul718e3742002-12-13 20:15:29 +0000110{
111 XFREE (0, user);
112}
113
paulb8994082005-07-12 15:15:02 +0000114struct vtysh_user *
hasso5862ff52004-10-11 13:20:40 +0000115user_lookup (const char *name)
paul718e3742002-12-13 20:15:29 +0000116{
paul1eb8ef22005-04-07 07:30:20 +0000117 struct listnode *node, *nnode;
paulb8994082005-07-12 15:15:02 +0000118 struct vtysh_user *user;
paul718e3742002-12-13 20:15:29 +0000119
paul1eb8ef22005-04-07 07:30:20 +0000120 for (ALL_LIST_ELEMENTS (userlist, node, nnode, user))
paul718e3742002-12-13 20:15:29 +0000121 {
122 if (strcmp (user->name, name) == 0)
123 return user;
124 }
125 return NULL;
126}
127
128void
129user_config_write ()
130{
paul1eb8ef22005-04-07 07:30:20 +0000131 struct listnode *node, *nnode;
paulb8994082005-07-12 15:15:02 +0000132 struct vtysh_user *user;
paul718e3742002-12-13 20:15:29 +0000133
paul1eb8ef22005-04-07 07:30:20 +0000134 for (ALL_LIST_ELEMENTS (userlist, node, nnode, user))
paul718e3742002-12-13 20:15:29 +0000135 {
136 if (user->nopassword)
137 printf (" username %s nopassword\n", user->name);
138 }
139}
140
paulb8994082005-07-12 15:15:02 +0000141struct vtysh_user *
hasso5862ff52004-10-11 13:20:40 +0000142user_get (const char *name)
paul718e3742002-12-13 20:15:29 +0000143{
paulb8994082005-07-12 15:15:02 +0000144 struct vtysh_user *user;
paul718e3742002-12-13 20:15:29 +0000145 user = user_lookup (name);
146 if (user)
147 return user;
148
149 user = user_new ();
150 user->name = strdup (name);
151 listnode_add (userlist, user);
152
153 return user;
154}
155
156DEFUN (username_nopassword,
157 username_nopassword_cmd,
158 "username WORD nopassword",
159 "\n"
160 "\n"
161 "\n")
162{
paulb8994082005-07-12 15:15:02 +0000163 struct vtysh_user *user;
paul718e3742002-12-13 20:15:29 +0000164 user = user_get (argv[0]);
165 user->nopassword = 1;
166 return CMD_SUCCESS;
167}
168
169int
170vtysh_auth ()
171{
paulb8994082005-07-12 15:15:02 +0000172 struct vtysh_user *user;
paul718e3742002-12-13 20:15:29 +0000173 struct passwd *passwd;
174
175 passwd = getpwuid (geteuid ());
176
177 user = user_lookup (passwd->pw_name);
178 if (user && user->nopassword)
179 /* Pass through */;
180 else
181 {
182#ifdef USE_PAM
183 if (vtysh_pam (passwd->pw_name))
184 exit (0);
185#endif /* USE_PAM */
186 }
187 return 0;
188}
189
190void
191vtysh_user_init ()
192{
193 userlist = list_new ();
194 install_element (CONFIG_NODE, &username_nopassword_cmd);
195}