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 | |
paul | b899408 | 2005-07-12 15:15:02 +0000 | [diff] [blame] | 102 | 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 | |
| 108 | void |
paul | b899408 | 2005-07-12 15:15:02 +0000 | [diff] [blame] | 109 | user_free (struct vtysh_user *user) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 110 | { |
| 111 | XFREE (0, user); |
| 112 | } |
| 113 | |
paul | b899408 | 2005-07-12 15:15:02 +0000 | [diff] [blame] | 114 | struct vtysh_user * |
hasso | 5862ff5 | 2004-10-11 13:20:40 +0000 | [diff] [blame] | 115 | user_lookup (const char *name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 116 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 117 | struct listnode *node, *nnode; |
paul | b899408 | 2005-07-12 15:15:02 +0000 | [diff] [blame] | 118 | struct vtysh_user *user; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 119 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 120 | for (ALL_LIST_ELEMENTS (userlist, node, nnode, user)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 121 | { |
| 122 | if (strcmp (user->name, name) == 0) |
| 123 | return user; |
| 124 | } |
| 125 | return NULL; |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | user_config_write () |
| 130 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 131 | struct listnode *node, *nnode; |
paul | b899408 | 2005-07-12 15:15:02 +0000 | [diff] [blame] | 132 | struct vtysh_user *user; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 133 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 134 | for (ALL_LIST_ELEMENTS (userlist, node, nnode, user)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 135 | { |
| 136 | if (user->nopassword) |
| 137 | printf (" username %s nopassword\n", user->name); |
| 138 | } |
| 139 | } |
| 140 | |
paul | b899408 | 2005-07-12 15:15:02 +0000 | [diff] [blame] | 141 | struct vtysh_user * |
hasso | 5862ff5 | 2004-10-11 13:20:40 +0000 | [diff] [blame] | 142 | user_get (const char *name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 143 | { |
paul | b899408 | 2005-07-12 15:15:02 +0000 | [diff] [blame] | 144 | struct vtysh_user *user; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 145 | 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 | |
| 156 | DEFUN (username_nopassword, |
| 157 | username_nopassword_cmd, |
| 158 | "username WORD nopassword", |
| 159 | "\n" |
| 160 | "\n" |
| 161 | "\n") |
| 162 | { |
paul | b899408 | 2005-07-12 15:15:02 +0000 | [diff] [blame] | 163 | struct vtysh_user *user; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 164 | user = user_get (argv[0]); |
| 165 | user->nopassword = 1; |
| 166 | return CMD_SUCCESS; |
| 167 | } |
| 168 | |
| 169 | int |
| 170 | vtysh_auth () |
| 171 | { |
paul | b899408 | 2005-07-12 15:15:02 +0000 | [diff] [blame] | 172 | struct vtysh_user *user; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 173 | 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 | |
| 190 | void |
| 191 | vtysh_user_init () |
| 192 | { |
| 193 | userlist = list_new (); |
| 194 | install_element (CONFIG_NODE, &username_nopassword_cmd); |
| 195 | } |