Imported Upstream version 1.6.8p5
[debian/sudo] / auth / securid5.c
1 /*
2  * Copyright (c) 1999-2003 Todd C. Miller <Todd.Miller@courtesan.com>
3  * Copyright (c) 2002 Michael Stroucken <michael@stroucken.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
17  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
18  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19  *
20  * Sponsored in part by the Defense Advanced Research Projects
21  * Agency (DARPA) and Air Force Research Laboratory, Air Force
22  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
23  */
24
25 #include "config.h"
26
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <stdio.h>
30 #ifdef STDC_HEADERS
31 # include <stdlib.h>
32 # include <stddef.h>
33 #else
34 # ifdef HAVE_STDLIB_H
35 #  include <stdlib.h>
36 # endif
37 #endif /* STDC_HEADERS */
38 #ifdef HAVE_STRING_H
39 # include <string.h>
40 #else
41 # ifdef HAVE_STRINGS_H
42 #  include <strings.h>
43 # endif
44 #endif /* HAVE_STRING_H */
45 #ifdef HAVE_UNISTD_H
46 # include <unistd.h>
47 #endif /* HAVE_UNISTD_H */
48 #ifdef HAVE_ERR_H
49 # include <err.h>
50 #else
51 # include "emul/err.h"
52 #endif /* HAVE_ERR_H */
53 #include <pwd.h>
54
55 /* Needed for SecurID v5.0 Authentication on UNIX */
56 #define UNIX 1
57 #include <acexport.h>
58 #include <sdacmvls.h>
59
60 #include "sudo.h"
61 #include "sudo_auth.h"
62
63 #ifndef lint
64 static const char rcsid[] = "$Sudo: securid5.c,v 1.6 2004/02/13 21:36:47 millert Exp $";
65 #endif /* lint */
66
67 /*
68  * securid_init - Initialises communications with ACE server
69  * Arguments in:
70  *     pw - UNUSED
71  *     promptp - UNUSED
72  *     auth - sudo authentication structure
73  *
74  * Results out:
75  *     auth - auth->data contains pointer to new SecurID handle
76  *     return code - Fatal if initialization unsuccessful, otherwise
77  *                   success.
78  */
79 int
80 securid_init(pw, promptp, auth)
81     struct passwd *pw;
82     char **promptp;
83     sudo_auth *auth;
84 {
85     static SDI_HANDLE sd_dat;                   /* SecurID handle */
86
87     auth->data = (VOID *) &sd_dat;              /* For method-specific data */
88
89     /* Start communications */
90     if (AceInitialize() != SD_FALSE)
91         return(AUTH_SUCCESS);
92
93     warnx("failed to initialise the ACE API library");
94     return(AUTH_FATAL);
95 }
96
97 /*
98  * securid_setup - Initialises a SecurID transaction and locks out other
99  *     ACE servers
100  *
101  * Arguments in:
102  *     pw - struct passwd for username
103  *     promptp - UNUSED
104  *     auth - sudo authentication structure for SecurID handle
105  *
106  * Results out:
107  *     return code - Success if transaction started correctly, fatal
108  *                   otherwise
109  */
110 int
111 securid_setup(pw, promptp, auth)
112     struct passwd *pw;
113     char **promptp;
114     sudo_auth *auth;
115 {
116     SDI_HANDLE *sd = (SDI_HANDLE *) auth->data;
117     int retval;
118
119     /* Re-initialize SecurID every time. */
120     if (SD_Init(sd) != ACM_OK) {
121         warnx("unable to contact the SecurID server");
122         return(AUTH_FATAL);
123     }
124
125     /* Lock new PIN code */
126     retval = SD_Lock(*sd, pw->pw_name);
127
128     switch (retval) {
129         case ACE_UNDEFINED_USERNAME:
130                 warnx("invalid username length for SecurID");
131                 return(AUTH_FATAL);
132
133         case ACE_ERR_INVALID_HANDLE:
134                 warnx("invalid Authentication Handle for SecurID");
135                 return(AUTH_FATAL);
136
137         case ACM_ACCESS_DENIED:
138                 warnx("SecurID communication failed");
139                 return(AUTH_FATAL);
140
141         case ACM_OK:
142                 warnx("User ID locked for SecurID Authentication");
143                 return(AUTH_SUCCESS);
144         }
145 }
146
147 /*
148  * securid_verify - Authenticates user and handles ACE responses
149  *
150  * Arguments in:
151  *     pw - struct passwd for username
152  *     pass - UNUSED
153  *     auth - sudo authentication structure for SecurID handle
154  *
155  * Results out:
156  *     return code - Success on successful authentication, failure on
157  *                   incorrect authentication, fatal on errors
158  */
159 int
160 securid_verify(pw, pass, auth)
161     struct passwd *pw;
162     char *pass;
163     sudo_auth *auth;
164 {
165     SDI_HANDLE *sd = (SDI_HANDLE *) auth->data;
166     int rval;
167
168     pass = (char *) tgetpass("Enter your PASSCODE: ",
169         def_passwd_timeout * 60, tgetpass_flags);
170
171     /* Have ACE verify password */
172     switch (SD_Check(*sd, pass, pw->pw_name)) {
173         case ACE_UNDEFINED_PASSCODE:
174                 warnx("invalid passcode length for SecurID");
175                 rval = AUTH_FATAL;
176                 break;
177
178         case ACE_UNDEFINED_USERNAME:
179                 warnx("invalid username length for SecurID");
180                 rval = AUTH_FATAL;
181                 break;
182
183         case ACE_ERR_INVALID_HANDLE:
184                 warnx("invalid Authentication Handle for SecurID");
185                 rval = AUTH_FATAL;
186
187         case ACM_ACCESS_DENIED:
188                 rval = AUTH_FAILURE;
189                 break;
190
191         case ACM_NEXT_CODE_REQUIRED:
192                 /* Sometimes (when current token close to expire?)
193                    ACE challenges for the next token displayed
194                    (entered without the PIN) */
195                 pass = (char *) tgetpass("\
196 !!! ATTENTION !!!\n\
197 Wait for the token code to change, \n\
198 then enter the new token code.\n", \
199                 def_passwd_timeout * 60, tgetpass_flags);
200
201                 if (SD_Next(*sd, pass) == ACM_OK) {
202                         rval = AUTH_SUCCESS;
203                         break;
204                 }
205
206                 rval = AUTH_FAILURE;
207                 break;
208
209         case ACM_NEW_PIN_REQUIRED:
210                 /*
211                  * This user's SecurID has not been activated yet,
212                  * or the pin has been reset
213                  */
214                 /* XXX - Is setting up a new PIN within sudo's scope? */
215                 SD_Pin(*sd, "");
216                 fprintf(stderr, "Your SecurID access has not yet been set up.\n");
217                 fprintf(stderr, "Please set up a PIN before you try to authenticate.\n");
218                 rval = AUTH_FATAL;
219                 break;
220     }
221
222     /* Free resources */
223     SD_Close(*sd);
224
225     /* Return stored state to calling process */
226     return(rval);
227 }