]> git.proxmox.com Git - mirror_frr.git/blame - vtysh/vtysh_user.c
debian: Remove some unnecesary files from debian directory.
[mirror_frr.git] / vtysh / vtysh_user.c
CommitLineData
718e3744 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
22#include <zebra.h>
bb6065a5 23#include <lib/version.h>
718e3744 24
25#include <pwd.h>
26
27#ifdef USE_PAM
28#include <security/pam_appl.h>
24cd435b 29#ifdef HAVE_PAM_MISC_H
718e3744 30#include <security/pam_misc.h>
24cd435b 31#endif
32#ifdef HAVE_OPENPAM_H
33#include <security/openpam.h>
34#endif
718e3744 35#endif /* USE_PAM */
36
37#include "memory.h"
38#include "linklist.h"
39#include "command.h"
88177fe3 40#include "vtysh/vtysh_user.h"
718e3744 41
c0e8c16f
DS
42/*
43 * Compiler is warning about prototypes not being declared.
44 * The DEFUNSH and DEFUN macro's are messing with the
45 * compiler I believe. This is just to make it happy.
46 */
47int vtysh_pam(const char *);
48struct vtysh_user *user_new(void);
49void user_free(struct vtysh_user *);
50struct vtysh_user *user_lookup(const char *);
c0e8c16f
DS
51struct vtysh_user *user_get(const char *);
52int vtysh_auth(void);
53void vtysh_user_init(void);
54
e4421165
DS
55extern struct list *config_top;
56extern void config_add_line(struct list *config, const char *line);
57
718e3744 58#ifdef USE_PAM
59static struct pam_conv conv =
60{
24cd435b 61 PAM_CONV_FUNC,
718e3744 62 NULL
63};
64
65int
5862ff52 66vtysh_pam (const char *user)
718e3744 67{
68 int ret;
69 pam_handle_t *pamh = NULL;
70
71 /* Start PAM. */
42053f4e 72 ret = pam_start(QUAGGA_PROGNAME, user, &conv, &pamh);
718e3744 73 /* printf ("ret %d\n", ret); */
74
75 /* Is user really user? */
76 if (ret == PAM_SUCCESS)
77 ret = pam_authenticate (pamh, 0);
78 /* printf ("ret %d\n", ret); */
79
80#if 0
81 /* Permitted access? */
82 if (ret == PAM_SUCCESS)
83 ret = pam_acct_mgmt (pamh, 0);
84 printf ("ret %d\n", ret);
85
86 if (ret == PAM_AUTHINFO_UNAVAIL)
87 ret = PAM_SUCCESS;
88#endif /* 0 */
89
90 /* This is where we have been authorized or not. */
91#ifdef DEBUG
92 if (ret == PAM_SUCCESS)
93 printf("Authenticated\n");
94 else
95 printf("Not Authenticated\n");
96#endif /* DEBUG */
97
98 /* close Linux-PAM */
99 if (pam_end (pamh, ret) != PAM_SUCCESS)
100 {
101 pamh = NULL;
102 fprintf(stderr, "vtysh_pam: failed to release authenticator\n");
103 exit(1);
104 }
105
106 return ret == PAM_SUCCESS ? 0 : 1;
107}
108#endif /* USE_PAM */
109
b8994085 110struct vtysh_user
718e3744 111{
112 char *name;
113 u_char nopassword;
114};
115
116struct list *userlist;
117
b8994085 118struct vtysh_user *
35dece84 119user_new (void)
718e3744 120{
393deb9b 121 return XCALLOC (0, sizeof (struct vtysh_user));
718e3744 122}
123
124void
b8994085 125user_free (struct vtysh_user *user)
718e3744 126{
127 XFREE (0, user);
128}
129
b8994085 130struct vtysh_user *
5862ff52 131user_lookup (const char *name)
718e3744 132{
1eb8ef25 133 struct listnode *node, *nnode;
b8994085 134 struct vtysh_user *user;
718e3744 135
1eb8ef25 136 for (ALL_LIST_ELEMENTS (userlist, node, nnode, user))
718e3744 137 {
138 if (strcmp (user->name, name) == 0)
139 return user;
140 }
141 return NULL;
142}
143
144void
145user_config_write ()
146{
1eb8ef25 147 struct listnode *node, *nnode;
b8994085 148 struct vtysh_user *user;
a7222276 149 char line[128];
718e3744 150
1eb8ef25 151 for (ALL_LIST_ELEMENTS (userlist, node, nnode, user))
718e3744 152 {
153 if (user->nopassword)
a7222276
DS
154 {
155 sprintf(line, "username %s nopassword", user->name);
156 config_add_line (config_top, line);
157 }
718e3744 158 }
159}
160
b8994085 161struct vtysh_user *
5862ff52 162user_get (const char *name)
718e3744 163{
b8994085 164 struct vtysh_user *user;
718e3744 165 user = user_lookup (name);
166 if (user)
167 return user;
168
169 user = user_new ();
170 user->name = strdup (name);
171 listnode_add (userlist, user);
172
173 return user;
174}
175
7cfc61d3
DS
176DEFUN (banner_motd_file,
177 banner_motd_file_cmd,
178 "banner motd file [FILE]",
179 "\n\n\n\n")
180{
181 return cmd_banner_motd_file (argv[0]);
182}
183
718e3744 184DEFUN (username_nopassword,
185 username_nopassword_cmd,
186 "username WORD nopassword",
187 "\n"
188 "\n"
189 "\n")
190{
b8994085 191 struct vtysh_user *user;
718e3744 192 user = user_get (argv[0]);
193 user->nopassword = 1;
194 return CMD_SUCCESS;
195}
196
197int
198vtysh_auth ()
199{
b8994085 200 struct vtysh_user *user;
718e3744 201 struct passwd *passwd;
202
203 passwd = getpwuid (geteuid ());
204
205 user = user_lookup (passwd->pw_name);
206 if (user && user->nopassword)
207 /* Pass through */;
208 else
209 {
210#ifdef USE_PAM
211 if (vtysh_pam (passwd->pw_name))
212 exit (0);
213#endif /* USE_PAM */
214 }
215 return 0;
216}
217
fba55c8a
DS
218char *
219vtysh_get_home (void)
220{
221 struct passwd *passwd;
222
223 passwd = getpwuid (getuid ());
224
225 return passwd ? passwd->pw_dir : NULL;
226}
227
718e3744 228void
fba55c8a 229vtysh_user_init (void)
718e3744 230{
231 userlist = list_new ();
232 install_element (CONFIG_NODE, &username_nopassword_cmd);
7cfc61d3 233 install_element (CONFIG_NODE, &banner_motd_file_cmd);
718e3744 234}