paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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> |
paul | bb6065a | 2003-08-13 04:07:52 +0000 | [diff] [blame] | 23 | #include <lib/version.h> |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 24 | |
| 25 | #include <pwd.h> |
| 26 | |
| 27 | #ifdef USE_PAM |
| 28 | #include <security/pam_appl.h> |
paul | 24cd435 | 2003-05-06 12:16:27 +0000 | [diff] [blame] | 29 | #ifdef HAVE_PAM_MISC_H |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 30 | #include <security/pam_misc.h> |
paul | 24cd435 | 2003-05-06 12:16:27 +0000 | [diff] [blame] | 31 | #endif |
| 32 | #ifdef HAVE_OPENPAM_H |
| 33 | #include <security/openpam.h> |
| 34 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 35 | #endif /* USE_PAM */ |
| 36 | |
| 37 | #include "memory.h" |
| 38 | #include "linklist.h" |
| 39 | #include "command.h" |
David Lamparter | 388f885 | 2015-03-03 08:55:54 +0100 | [diff] [blame] | 40 | #include "vtysh_user.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 41 | |
| 42 | #ifdef USE_PAM |
| 43 | static struct pam_conv conv = |
| 44 | { |
paul | 24cd435 | 2003-05-06 12:16:27 +0000 | [diff] [blame] | 45 | PAM_CONV_FUNC, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 46 | NULL |
| 47 | }; |
| 48 | |
| 49 | int |
hasso | 5862ff5 | 2004-10-11 13:20:40 +0000 | [diff] [blame] | 50 | vtysh_pam (const char *user) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 51 | { |
| 52 | int ret; |
| 53 | pam_handle_t *pamh = NULL; |
| 54 | |
| 55 | /* Start PAM. */ |
paul | 42053f4 | 2003-08-13 02:54:44 +0000 | [diff] [blame] | 56 | ret = pam_start(QUAGGA_PROGNAME, user, &conv, &pamh); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 57 | /* 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 | |
paul | b899408 | 2005-07-12 15:15:02 +0000 | [diff] [blame] | 94 | struct vtysh_user |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 95 | { |
| 96 | char *name; |
| 97 | u_char nopassword; |
| 98 | }; |
| 99 | |
| 100 | struct list *userlist; |
| 101 | |
David Lamparter | a9eb906 | 2015-03-04 07:07:01 +0100 | [diff] [blame] | 102 | static struct vtysh_user * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 103 | user_new () |
| 104 | { |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 105 | return XCALLOC (0, sizeof (struct vtysh_user)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 106 | } |
| 107 | |
David Lamparter | a9eb906 | 2015-03-04 07:07:01 +0100 | [diff] [blame] | 108 | #if 0 |
| 109 | static void |
paul | b899408 | 2005-07-12 15:15:02 +0000 | [diff] [blame] | 110 | user_free (struct vtysh_user *user) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 111 | { |
| 112 | XFREE (0, user); |
| 113 | } |
David Lamparter | a9eb906 | 2015-03-04 07:07:01 +0100 | [diff] [blame] | 114 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 115 | |
David Lamparter | a9eb906 | 2015-03-04 07:07:01 +0100 | [diff] [blame] | 116 | static struct vtysh_user * |
hasso | 5862ff5 | 2004-10-11 13:20:40 +0000 | [diff] [blame] | 117 | user_lookup (const char *name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 118 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 119 | struct listnode *node, *nnode; |
paul | b899408 | 2005-07-12 15:15:02 +0000 | [diff] [blame] | 120 | struct vtysh_user *user; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 121 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 122 | for (ALL_LIST_ELEMENTS (userlist, node, nnode, user)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 123 | { |
| 124 | if (strcmp (user->name, name) == 0) |
| 125 | return user; |
| 126 | } |
| 127 | return NULL; |
| 128 | } |
| 129 | |
David Lamparter | a9eb906 | 2015-03-04 07:07:01 +0100 | [diff] [blame] | 130 | #if 0 |
| 131 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 132 | user_config_write () |
| 133 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 134 | struct listnode *node, *nnode; |
paul | b899408 | 2005-07-12 15:15:02 +0000 | [diff] [blame] | 135 | struct vtysh_user *user; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 136 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 137 | for (ALL_LIST_ELEMENTS (userlist, node, nnode, user)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 138 | { |
| 139 | if (user->nopassword) |
| 140 | printf (" username %s nopassword\n", user->name); |
| 141 | } |
| 142 | } |
David Lamparter | a9eb906 | 2015-03-04 07:07:01 +0100 | [diff] [blame] | 143 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 144 | |
David Lamparter | a9eb906 | 2015-03-04 07:07:01 +0100 | [diff] [blame] | 145 | static struct vtysh_user * |
hasso | 5862ff5 | 2004-10-11 13:20:40 +0000 | [diff] [blame] | 146 | user_get (const char *name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 147 | { |
paul | b899408 | 2005-07-12 15:15:02 +0000 | [diff] [blame] | 148 | struct vtysh_user *user; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 149 | 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 | |
| 160 | DEFUN (username_nopassword, |
| 161 | username_nopassword_cmd, |
| 162 | "username WORD nopassword", |
| 163 | "\n" |
| 164 | "\n" |
| 165 | "\n") |
| 166 | { |
paul | b899408 | 2005-07-12 15:15:02 +0000 | [diff] [blame] | 167 | struct vtysh_user *user; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 168 | user = user_get (argv[0]); |
| 169 | user->nopassword = 1; |
| 170 | return CMD_SUCCESS; |
| 171 | } |
| 172 | |
| 173 | int |
David Lamparter | a9eb906 | 2015-03-04 07:07:01 +0100 | [diff] [blame] | 174 | vtysh_auth (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 175 | { |
paul | b899408 | 2005-07-12 15:15:02 +0000 | [diff] [blame] | 176 | struct vtysh_user *user; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 177 | struct passwd *passwd; |
| 178 | |
| 179 | passwd = getpwuid (geteuid ()); |
| 180 | |
| 181 | user = user_lookup (passwd->pw_name); |
| 182 | if (user && user->nopassword) |
| 183 | /* Pass through */; |
| 184 | else |
| 185 | { |
| 186 | #ifdef USE_PAM |
| 187 | if (vtysh_pam (passwd->pw_name)) |
| 188 | exit (0); |
| 189 | #endif /* USE_PAM */ |
| 190 | } |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | void |
David Lamparter | a9eb906 | 2015-03-04 07:07:01 +0100 | [diff] [blame] | 195 | vtysh_user_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 196 | { |
| 197 | userlist = list_new (); |
| 198 | install_element (CONFIG_NODE, &username_nopassword_cmd); |
| 199 | } |