Imported Upstream version 1.7.0
[debian/sudo] / auth / sia.c
1 /*
2  * Copyright (c) 1999-2005, 2007 Todd C. Miller <Todd.Miller@courtesan.com>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
16  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  *
18  * Sponsored in part by the Defense Advanced Research Projects
19  * Agency (DARPA) and Air Force Research Laboratory, Air Force
20  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
21  */
22
23 #include <config.h>
24
25 #include <sys/types.h>
26 #include <sys/param.h>
27 #include <stdio.h>
28 #ifdef STDC_HEADERS
29 # include <stdlib.h>
30 # include <stddef.h>
31 #else
32 # ifdef HAVE_STDLIB_H
33 #  include <stdlib.h>
34 # endif
35 #endif /* STDC_HEADERS */
36 #ifdef HAVE_STRING_H
37 # include <string.h>
38 #else
39 # ifdef HAVE_STRINGS_H
40 #  include <strings.h>
41 # endif
42 #endif /* HAVE_STRING_H */
43 #ifdef HAVE_UNISTD_H
44 # include <unistd.h>
45 #endif /* HAVE_UNISTD_H */
46 #include <pwd.h>
47 #include <siad.h>
48
49 #include "sudo.h"
50 #include "sudo_auth.h"
51
52 #ifndef lint
53 __unused static const char rcsid[] = "$Sudo: sia.c,v 1.19 2008/11/09 14:13:13 millert Exp $";
54 #endif /* lint */
55
56 static int sudo_collect __P((int, int, uchar_t *, int, prompt_t *));
57
58 static char *def_prompt;
59
60 /*
61  * Collection routine (callback) for limiting the timeouts in SIA
62  * prompts and (possibly) setting a custom prompt.
63  */
64 static int
65 sudo_collect(timeout, rendition, title, nprompts, prompts)
66     int timeout;
67     int rendition;
68     uchar_t *title;
69     int nprompts;
70     prompt_t *prompts;
71 {
72     switch (rendition) {
73         case SIAFORM:
74         case SIAONELINER:
75             if (timeout <= 0 || timeout > def_passwd_timeout * 60)
76                 timeout = def_passwd_timeout * 60;
77             /*
78              * Substitute custom prompt if a) the sudo prompt is not "Password:"
79              * and b) the SIA prompt is "Password:" (so we know it is safe).
80              * This keeps us from overwriting things like S/Key challenges.
81              */
82             if (strcmp((char *)prompts[0].prompt, "Password:") == 0 &&
83                 strcmp(def_prompt, "Password:") != 0)
84                 prompts[0].prompt = (unsigned char *)def_prompt;
85             break;
86         default:
87             break;
88     }
89
90     return sia_collect_trm(timeout, rendition, title, nprompts, prompts);
91 }
92
93 int
94 sia_setup(pw, promptp, auth)
95     struct passwd *pw;
96     char **promptp;
97     sudo_auth *auth;
98 {
99     SIAENTITY *siah = NULL;
100     extern int Argc;
101     extern char **Argv;
102
103     if (sia_ses_init(&siah, Argc, Argv, NULL, pw->pw_name, ttyname(0), 1, NULL)
104         != SIASUCCESS) {
105
106         log_error(USE_ERRNO|NO_EXIT|NO_MAIL,
107             "unable to initialize SIA session");
108         return(AUTH_FATAL);
109     }
110
111     auth->data = (void *) siah;
112     return(AUTH_SUCCESS);
113 }
114
115 int
116 sia_verify(pw, prompt, auth)
117     struct passwd *pw;
118     char *prompt;
119     sudo_auth *auth;
120 {
121     SIAENTITY *siah = (SIAENTITY *) auth->data;
122
123     def_prompt = prompt;                /* for sudo_collect */
124
125     /* XXX - need a way to detect user hitting return or EOF at prompt */
126     if (sia_ses_reauthent(sudo_collect, siah) == SIASUCCESS)
127         return(AUTH_SUCCESS);
128     else
129         return(AUTH_FAILURE);
130 }
131
132 int
133 sia_cleanup(pw, auth)
134     struct passwd *pw;
135     sudo_auth *auth;
136 {
137     SIAENTITY *siah = (SIAENTITY *) auth->data;
138
139     (void) sia_ses_release(&siah);
140     return(AUTH_SUCCESS);
141 }