Imported Upstream version 1.7.0
[debian/sudo] / auth / securid5.c
1 /*
2  * Copyright (c) 1999-2005, 2007 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 #include <pwd.h>
49
50 /* Needed for SecurID v5.0 Authentication on UNIX */
51 #define UNIX 1
52 #include <acexport.h>
53 #include <sdacmvls.h>
54
55 #include "sudo.h"
56 #include "sudo_auth.h"
57
58 #ifndef lint
59 __unused static const char rcsid[] = "$Sudo: securid5.c,v 1.13 2008/11/09 14:13:13 millert Exp $";
60 #endif /* lint */
61
62 /*
63  * securid_init - Initialises communications with ACE server
64  * Arguments in:
65  *     pw - UNUSED
66  *     promptp - UNUSED
67  *     auth - sudo authentication structure
68  *
69  * Results out:
70  *     auth - auth->data contains pointer to new SecurID handle
71  *     return code - Fatal if initialization unsuccessful, otherwise
72  *                   success.
73  */
74 int
75 securid_init(pw, promptp, auth)
76     struct passwd *pw;
77     char **promptp;
78     sudo_auth *auth;
79 {
80     static SDI_HANDLE sd_dat;                   /* SecurID handle */
81
82     auth->data = (void *) &sd_dat;              /* For method-specific data */
83
84     /* Start communications */
85     if (AceInitialize() != SD_FALSE)
86         return(AUTH_SUCCESS);
87
88     warningx("failed to initialise the ACE API library");
89     return(AUTH_FATAL);
90 }
91
92 /*
93  * securid_setup - Initialises a SecurID transaction and locks out other
94  *     ACE servers
95  *
96  * Arguments in:
97  *     pw - struct passwd for username
98  *     promptp - UNUSED
99  *     auth - sudo authentication structure for SecurID handle
100  *
101  * Results out:
102  *     return code - Success if transaction started correctly, fatal
103  *                   otherwise
104  */
105 int
106 securid_setup(pw, promptp, auth)
107     struct passwd *pw;
108     char **promptp;
109     sudo_auth *auth;
110 {
111     SDI_HANDLE *sd = (SDI_HANDLE *) auth->data;
112     int retval;
113
114     /* Re-initialize SecurID every time. */
115     if (SD_Init(sd) != ACM_OK) {
116         warningx("unable to contact the SecurID server");
117         return(AUTH_FATAL);
118     }
119
120     /* Lock new PIN code */
121     retval = SD_Lock(*sd, pw->pw_name);
122
123     switch (retval) {
124         case ACM_OK:
125                 warningx("User ID locked for SecurID Authentication");
126                 return(AUTH_SUCCESS);
127
128         case ACE_UNDEFINED_USERNAME:
129                 warningx("invalid username length for SecurID");
130                 return(AUTH_FATAL);
131
132         case ACE_ERR_INVALID_HANDLE:
133                 warningx("invalid Authentication Handle for SecurID");
134                 return(AUTH_FATAL);
135
136         case ACM_ACCESS_DENIED:
137                 warningx("SecurID communication failed");
138                 return(AUTH_FATAL);
139
140         default:
141                 warningx("unknown SecurID error");
142                 return(AUTH_FATAL);
143         }
144 }
145
146 /*
147  * securid_verify - Authenticates user and handles ACE responses
148  *
149  * Arguments in:
150  *     pw - struct passwd for username
151  *     pass - UNUSED
152  *     auth - sudo authentication structure for SecurID handle
153  *
154  * Results out:
155  *     return code - Success on successful authentication, failure on
156  *                   incorrect authentication, fatal on errors
157  */
158 int
159 securid_verify(pw, pass, auth)
160     struct passwd *pw;
161     char *pass;
162     sudo_auth *auth;
163 {
164     SDI_HANDLE *sd = (SDI_HANDLE *) auth->data;
165     int rval;
166
167     pass = (char *) tgetpass("Enter your PASSCODE: ",
168         def_passwd_timeout * 60, tgetpass_flags);
169
170     /* Have ACE verify password */
171     switch (SD_Check(*sd, pass, pw->pw_name)) {
172         case ACM_OK:
173                 rval = AUTH_SUCESS;
174                 break;
175
176         case ACE_UNDEFINED_PASSCODE:
177                 warningx("invalid passcode length for SecurID");
178                 rval = AUTH_FATAL;
179                 break;
180
181         case ACE_UNDEFINED_USERNAME:
182                 warningx("invalid username length for SecurID");
183                 rval = AUTH_FATAL;
184                 break;
185
186         case ACE_ERR_INVALID_HANDLE:
187                 warningx("invalid Authentication Handle for SecurID");
188                 rval = AUTH_FATAL;
189                 break;
190
191         case ACM_ACCESS_DENIED:
192                 rval = AUTH_FAILURE;
193                 break;
194
195         case ACM_NEXT_CODE_REQUIRED:
196                 /* Sometimes (when current token close to expire?)
197                    ACE challenges for the next token displayed
198                    (entered without the PIN) */
199                 pass = (char *) tgetpass("\
200 !!! ATTENTION !!!\n\
201 Wait for the token code to change, \n\
202 then enter the new token code.\n", \
203                 def_passwd_timeout * 60, tgetpass_flags);
204
205                 if (SD_Next(*sd, pass) == ACM_OK) {
206                         rval = AUTH_SUCCESS;
207                         break;
208                 }
209
210                 rval = AUTH_FAILURE;
211                 break;
212
213         case ACM_NEW_PIN_REQUIRED:
214                 /*
215                  * This user's SecurID has not been activated yet,
216                  * or the pin has been reset
217                  */
218                 /* XXX - Is setting up a new PIN within sudo's scope? */
219                 SD_Pin(*sd, "");
220                 fprintf(stderr, "Your SecurID access has not yet been set up.\n");
221                 fprintf(stderr, "Please set up a PIN before you try to authenticate.\n");
222                 rval = AUTH_FATAL;
223                 break;
224
225         default:
226                 warningx("unknown SecurID error");
227                 rval = AUTH_FATAL;
228                 break;
229     }
230
231     /* Free resources */
232     SD_Close(*sd);
233
234     /* Return stored state to calling process */
235     return(rval);
236 }