New upstream version 1.2.3
[quagga-debian.git] / vtysh / vtysh_user.c
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>
23 #include <lib/version.h>
24
25 #include <pwd.h>
26
27 #ifdef USE_PAM
28 #include <security/pam_appl.h>
29 #ifdef HAVE_PAM_MISC_H
30 #include <security/pam_misc.h>
31 #endif
32 #ifdef HAVE_OPENPAM_H
33 #include <security/openpam.h>
34 #endif
35 #endif /* USE_PAM */
36
37 #include "memory.h"
38 #include "linklist.h"
39 #include "command.h"
40 #include "vtysh_user.h"
41
42 #ifdef USE_PAM
43 static struct pam_conv conv = 
44 {
45   PAM_CONV_FUNC,
46   NULL
47 };
48
49 static int
50 vtysh_pam (const char *user)
51 {
52   int ret;
53   pam_handle_t *pamh = NULL;
54
55   /* Start PAM. */
56   ret = pam_start(QUAGGA_PROGNAME, user, &conv, &pamh);
57   /* printf ("ret %d\n", ret); */
58
59   /* Is user really user? */
60   if (ret == PAM_SUCCESS)
61     ret = pam_authenticate (pamh, 0);
62   if (ret != PAM_SUCCESS)
63     printf("Not authenticated. Check /etc/pam.d/quagga.\n");
64   /* printf ("ret %d\n", ret); */
65   
66 #if 0
67   /* Permitted access? */
68   if (ret == PAM_SUCCESS)
69     ret = pam_acct_mgmt (pamh, 0);
70   printf ("ret %d\n", ret);
71
72   if (ret == PAM_AUTHINFO_UNAVAIL)
73     ret = PAM_SUCCESS;
74 #endif /* 0 */
75   
76   /* This is where we have been authorized or not. */
77 #ifdef DEBUG
78   if (ret == PAM_SUCCESS)
79     printf("Authenticated\n");
80   else
81     printf("Not Authenticated\n");
82 #endif /* DEBUG */
83
84   /* close Linux-PAM */
85   if (pam_end (pamh, ret) != PAM_SUCCESS) 
86     {
87       pamh = NULL;
88       fprintf(stderr, "vtysh_pam: failed to release authenticator\n");
89       exit(1);
90     }
91
92   return ret == PAM_SUCCESS ? 0 : 1;
93 }
94 #endif /* USE_PAM */
95
96 struct vtysh_user
97 {
98   char *name;
99   u_char nopassword;
100 };
101
102 struct list *userlist;
103
104 static struct vtysh_user *
105 user_new ()
106 {
107   return XCALLOC (MTYPE_TMP, sizeof (struct vtysh_user));
108 }
109
110 #if 0
111 static void
112 user_free (struct vtysh_user *user)
113 {
114   XFREE (0, user);
115 }
116 #endif
117
118 static struct vtysh_user *
119 user_lookup (const char *name)
120 {
121   struct listnode *node, *nnode;
122   struct vtysh_user *user;
123
124   for (ALL_LIST_ELEMENTS (userlist, node, nnode, user))
125     {
126       if (strcmp (user->name, name) == 0)
127         return user;
128     }
129   return NULL;
130 }
131
132 #if 0
133 static void
134 user_config_write ()
135 {
136   struct listnode *node, *nnode;
137   struct vtysh_user *user;
138
139   for (ALL_LIST_ELEMENTS (userlist, node, nnode, user))
140     {
141       if (user->nopassword)
142         printf (" username %s nopassword\n", user->name);
143     }
144 }
145 #endif
146
147 static struct vtysh_user *
148 user_get (const char *name)
149 {
150   struct vtysh_user *user;
151   user = user_lookup (name);
152   if (user)
153     return user;
154
155   user = user_new ();
156   user->name = strdup (name);
157   listnode_add (userlist, user);
158
159   return user;
160 }
161
162 DEFUN (username_nopassword,
163        username_nopassword_cmd,
164        "username WORD nopassword",
165        "\n"
166        "\n"
167        "\n")
168 {
169   struct vtysh_user *user;
170   user = user_get (argv[0]);
171   user->nopassword = 1;
172   return CMD_SUCCESS;
173 }
174
175 int
176 vtysh_auth (void)
177 {
178   struct vtysh_user *user;
179   struct passwd *passwd;
180
181   if ((passwd = getpwuid (geteuid ())) == NULL)
182   {
183     fprintf (stderr, "could not lookup user ID %d\n", (int) geteuid());
184     exit (1);
185   }
186
187   user = user_lookup (passwd->pw_name);
188   if (user && user->nopassword)
189     /* Pass through */;
190   else
191     {
192 #ifdef USE_PAM
193       if (vtysh_pam (passwd->pw_name))
194         exit (0);
195 #endif /* USE_PAM */
196     }
197   return 0;
198 }
199
200 char *
201 vtysh_get_home (void)
202 {
203   struct passwd *passwd;
204
205   passwd = getpwuid (getuid ());
206
207   return passwd ? passwd->pw_dir : NULL;
208 }
209
210 void
211 vtysh_user_init (void)
212 {
213   userlist = list_new ();
214   install_element (CONFIG_NODE, &username_nopassword_cmd);
215 }