]>
Commit | Line | Data |
---|---|---|
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 | */ | |
47 | int vtysh_pam(const char *); | |
48 | struct vtysh_user *user_new(void); | |
49 | void user_free(struct vtysh_user *); | |
50 | struct vtysh_user *user_lookup(const char *); | |
c0e8c16f DS |
51 | struct vtysh_user *user_get(const char *); |
52 | int vtysh_auth(void); | |
53 | void vtysh_user_init(void); | |
54 | ||
e4421165 DS |
55 | extern struct list *config_top; |
56 | extern void config_add_line(struct list *config, const char *line); | |
57 | ||
718e3744 | 58 | #ifdef USE_PAM |
59 | static struct pam_conv conv = | |
60 | { | |
24cd435b | 61 | PAM_CONV_FUNC, |
718e3744 | 62 | NULL |
63 | }; | |
64 | ||
65 | int | |
5862ff52 | 66 | vtysh_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 | 110 | struct vtysh_user |
718e3744 | 111 | { |
112 | char *name; | |
113 | u_char nopassword; | |
114 | }; | |
115 | ||
116 | struct list *userlist; | |
117 | ||
b8994085 | 118 | struct vtysh_user * |
35dece84 | 119 | user_new (void) |
718e3744 | 120 | { |
393deb9b | 121 | return XCALLOC (0, sizeof (struct vtysh_user)); |
718e3744 | 122 | } |
123 | ||
124 | void | |
b8994085 | 125 | user_free (struct vtysh_user *user) |
718e3744 | 126 | { |
127 | XFREE (0, user); | |
128 | } | |
129 | ||
b8994085 | 130 | struct vtysh_user * |
5862ff52 | 131 | user_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 | ||
144 | void | |
145 | user_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 | 161 | struct vtysh_user * |
5862ff52 | 162 | user_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 |
176 | DEFUN (banner_motd_file, |
177 | banner_motd_file_cmd, | |
4d833e55 DS |
178 | "banner motd file FILE", |
179 | "Set banner\n" | |
180 | "Banner for motd\n" | |
181 | "Banner from a file\n" | |
182 | "Filename\n") | |
7cfc61d3 DS |
183 | { |
184 | return cmd_banner_motd_file (argv[0]); | |
185 | } | |
186 | ||
718e3744 | 187 | DEFUN (username_nopassword, |
188 | username_nopassword_cmd, | |
189 | "username WORD nopassword", | |
190 | "\n" | |
191 | "\n" | |
192 | "\n") | |
193 | { | |
b8994085 | 194 | struct vtysh_user *user; |
718e3744 | 195 | user = user_get (argv[0]); |
196 | user->nopassword = 1; | |
197 | return CMD_SUCCESS; | |
198 | } | |
199 | ||
200 | int | |
201 | vtysh_auth () | |
202 | { | |
b8994085 | 203 | struct vtysh_user *user; |
718e3744 | 204 | struct passwd *passwd; |
205 | ||
206 | passwd = getpwuid (geteuid ()); | |
207 | ||
208 | user = user_lookup (passwd->pw_name); | |
209 | if (user && user->nopassword) | |
210 | /* Pass through */; | |
211 | else | |
212 | { | |
213 | #ifdef USE_PAM | |
214 | if (vtysh_pam (passwd->pw_name)) | |
215 | exit (0); | |
216 | #endif /* USE_PAM */ | |
217 | } | |
218 | return 0; | |
219 | } | |
220 | ||
fba55c8a DS |
221 | char * |
222 | vtysh_get_home (void) | |
223 | { | |
224 | struct passwd *passwd; | |
225 | ||
226 | passwd = getpwuid (getuid ()); | |
227 | ||
228 | return passwd ? passwd->pw_dir : NULL; | |
229 | } | |
230 | ||
718e3744 | 231 | void |
fba55c8a | 232 | vtysh_user_init (void) |
718e3744 | 233 | { |
234 | userlist = list_new (); | |
235 | install_element (CONFIG_NODE, &username_nopassword_cmd); | |
7cfc61d3 | 236 | install_element (CONFIG_NODE, &banner_motd_file_cmd); |
718e3744 | 237 | } |