blob: 584b61f51d1335ce3b4403a4a5e6bab4734e4f08 [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
Donald Sharp8965be42015-09-04 14:21:23 -040049static int
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
David Lampartera9eb9062015-03-04 07:07:01 +0100102static struct vtysh_user *
paul718e3742002-12-13 20:15:29 +0000103user_new ()
104{
David Lamparter23757db2016-02-24 06:26:02 +0100105 return XCALLOC (MTYPE_TMP, sizeof (struct vtysh_user));
paul718e3742002-12-13 20:15:29 +0000106}
107
David Lampartera9eb9062015-03-04 07:07:01 +0100108#if 0
109static void
paulb8994082005-07-12 15:15:02 +0000110user_free (struct vtysh_user *user)
paul718e3742002-12-13 20:15:29 +0000111{
112 XFREE (0, user);
113}
David Lampartera9eb9062015-03-04 07:07:01 +0100114#endif
paul718e3742002-12-13 20:15:29 +0000115
David Lampartera9eb9062015-03-04 07:07:01 +0100116static struct vtysh_user *
hasso5862ff52004-10-11 13:20:40 +0000117user_lookup (const char *name)
paul718e3742002-12-13 20:15:29 +0000118{
paul1eb8ef22005-04-07 07:30:20 +0000119 struct listnode *node, *nnode;
paulb8994082005-07-12 15:15:02 +0000120 struct vtysh_user *user;
paul718e3742002-12-13 20:15:29 +0000121
paul1eb8ef22005-04-07 07:30:20 +0000122 for (ALL_LIST_ELEMENTS (userlist, node, nnode, user))
paul718e3742002-12-13 20:15:29 +0000123 {
124 if (strcmp (user->name, name) == 0)
125 return user;
126 }
127 return NULL;
128}
129
David Lampartera9eb9062015-03-04 07:07:01 +0100130#if 0
131static void
paul718e3742002-12-13 20:15:29 +0000132user_config_write ()
133{
paul1eb8ef22005-04-07 07:30:20 +0000134 struct listnode *node, *nnode;
paulb8994082005-07-12 15:15:02 +0000135 struct vtysh_user *user;
paul718e3742002-12-13 20:15:29 +0000136
paul1eb8ef22005-04-07 07:30:20 +0000137 for (ALL_LIST_ELEMENTS (userlist, node, nnode, user))
paul718e3742002-12-13 20:15:29 +0000138 {
139 if (user->nopassword)
140 printf (" username %s nopassword\n", user->name);
141 }
142}
David Lampartera9eb9062015-03-04 07:07:01 +0100143#endif
paul718e3742002-12-13 20:15:29 +0000144
David Lampartera9eb9062015-03-04 07:07:01 +0100145static struct vtysh_user *
hasso5862ff52004-10-11 13:20:40 +0000146user_get (const char *name)
paul718e3742002-12-13 20:15:29 +0000147{
paulb8994082005-07-12 15:15:02 +0000148 struct vtysh_user *user;
paul718e3742002-12-13 20:15:29 +0000149 user = user_lookup (name);
150 if (user)
151 return user;
152
153 user = user_new ();
154 user->name = strdup (name);
155 listnode_add (userlist, user);
156
157 return user;
158}
159
160DEFUN (username_nopassword,
161 username_nopassword_cmd,
162 "username WORD nopassword",
163 "\n"
164 "\n"
165 "\n")
166{
paulb8994082005-07-12 15:15:02 +0000167 struct vtysh_user *user;
paul718e3742002-12-13 20:15:29 +0000168 user = user_get (argv[0]);
169 user->nopassword = 1;
170 return CMD_SUCCESS;
171}
172
173int
David Lampartera9eb9062015-03-04 07:07:01 +0100174vtysh_auth (void)
paul718e3742002-12-13 20:15:29 +0000175{
paulb8994082005-07-12 15:15:02 +0000176 struct vtysh_user *user;
paul718e3742002-12-13 20:15:29 +0000177 struct passwd *passwd;
178
Jafar Al-Gharaibehf5a44882016-08-01 18:14:38 -0500179 if ((passwd = getpwuid (geteuid ())) == NULL)
180 {
181 fprintf (stderr, "could not lookup user ID %d\n", (int) geteuid());
182 exit (1);
183 }
paul718e3742002-12-13 20:15:29 +0000184
185 user = user_lookup (passwd->pw_name);
186 if (user && user->nopassword)
187 /* Pass through */;
188 else
189 {
190#ifdef USE_PAM
191 if (vtysh_pam (passwd->pw_name))
192 exit (0);
193#endif /* USE_PAM */
194 }
195 return 0;
196}
197
Donald Sharp2e320422016-01-13 10:49:50 -0800198char *
199vtysh_get_home (void)
200{
201 struct passwd *passwd;
202
203 passwd = getpwuid (getuid ());
204
205 return passwd ? passwd->pw_dir : NULL;
206}
207
paul718e3742002-12-13 20:15:29 +0000208void
David Lampartera9eb9062015-03-04 07:07:01 +0100209vtysh_user_init (void)
paul718e3742002-12-13 20:15:29 +0000210{
211 userlist = list_new ();
212 install_element (CONFIG_NODE, &username_nopassword_cmd);
213}