Imported Upstream version 1.8.7
[debian/sudo] / plugins / sudoers / auth / sia.c
1 /*
2  * Copyright (c) 1999-2005, 2007, 2010-2013
3  *      Todd C. Miller <Todd.Miller@courtesan.com>
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  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
17  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
18  *
19  * Sponsored in part by the Defense Advanced Research Projects
20  * Agency (DARPA) and Air Force Research Laboratory, Air Force
21  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
22  */
23
24 #include <config.h>
25
26 #include <sys/types.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 #endif /* HAVE_STRING_H */
39 #ifdef HAVE_STRINGS_H
40 # include <strings.h>
41 #endif /* HAVE_STRINGS_H */
42 #ifdef HAVE_UNISTD_H
43 # include <unistd.h>
44 #endif /* HAVE_UNISTD_H */
45 #include <pwd.h>
46 #include <siad.h>
47
48 #include "sudoers.h"
49 #include "sudo_auth.h"
50
51 static int sudo_collect(int, int, uchar_t *, int, prompt_t *);
52
53 static char *def_prompt;
54 static char **sudo_argv;
55 static int sudo_argc;
56
57 /*
58  * Collection routine (callback) for limiting the timeouts in SIA
59  * prompts and (possibly) setting a custom prompt.
60  */
61 static int
62 sudo_collect(int timeout, int rendition, uchar_t *title, int nprompts,
63     prompt_t *prompts)
64 {
65     debug_decl(sudo_collect, SUDO_DEBUG_AUTH)
66
67     switch (rendition) {
68         case SIAFORM:
69         case SIAONELINER:
70             if (timeout <= 0 || timeout > def_passwd_timeout * 60)
71                 timeout = def_passwd_timeout * 60;
72             /*
73              * Substitute custom prompt if a) the sudo prompt is not "Password:"
74              * and b) the SIA prompt is "Password:" (so we know it is safe).
75              * This keeps us from overwriting things like S/Key challenges.
76              */
77             if (strcmp((char *)prompts[0].prompt, "Password:") == 0 &&
78                 strcmp(def_prompt, "Password:") != 0)
79                 prompts[0].prompt = (unsigned char *)def_prompt;
80             break;
81         default:
82             break;
83     }
84
85     debug_return_int(sia_collect_trm(timeout, rendition, title, nprompts, prompts));
86 }
87
88 int
89 sudo_sia_setup(struct passwd *pw, char **promptp, sudo_auth *auth)
90 {
91     SIAENTITY *siah = NULL;
92     int i;
93     extern int NewArgc;
94     extern char **NewArgv;
95     debug_decl(sudo_sia_setup, SUDO_DEBUG_AUTH)
96
97     /* Rebuild argv for sia_ses_init() */
98     sudo_argc = NewArgc + 1;
99     sudo_argv = emalloc2(sudo_argc + 1, sizeof(char *));
100     sudo_argv[0] = "sudo";
101     for (i = 0; i < NewArgc; i++)
102         sudo_argv[i + 1] = NewArgv[i];
103     sudo_argv[sudo_argc] = NULL;
104
105     if (sia_ses_init(&siah, sudo_argc, sudo_argv, NULL, pw->pw_name, user_ttypath, 1, NULL) != SIASUCCESS) {
106
107         log_warning(USE_ERRNO|NO_MAIL,
108             N_("unable to initialize SIA session"));
109         debug_return_int(AUTH_FATAL);
110     }
111
112     auth->data = (void *) siah;
113     debug_return_int(AUTH_SUCCESS);
114 }
115
116 int
117 sudo_sia_verify(struct passwd *pw, char *prompt, sudo_auth *auth)
118 {
119     SIAENTITY *siah = (SIAENTITY *) auth->data;
120     debug_decl(sudo_sia_verify, SUDO_DEBUG_AUTH)
121
122     def_prompt = prompt;                /* for sudo_collect */
123
124     /* XXX - need a way to detect user hitting return or EOF at prompt */
125     if (sia_ses_reauthent(sudo_collect, siah) == SIASUCCESS)
126         debug_return_int(AUTH_SUCCESS);
127     else
128         debug_return_int(AUTH_FAILURE);
129 }
130
131 int
132 sudo_sia_cleanup(struct passwd *pw, sudo_auth *auth)
133 {
134     SIAENTITY *siah = (SIAENTITY *) auth->data;
135     debug_decl(sudo_sia_cleanup, SUDO_DEBUG_AUTH)
136
137     (void) sia_ses_release(&siah);
138     efree(sudo_argv);
139     debug_return_int(AUTH_SUCCESS);
140 }