Imported Upstream version 1.8.7
[debian/sudo] / src / conversation.c
1 /*
2  * Copyright (c) 1999-2005, 2007-2012 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  *
16  * Sponsored in part by the Defense Advanced Research Projects
17  * Agency (DARPA) and Air Force Research Laboratory, Air Force
18  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
19  */
20
21 #include <config.h>
22
23 #include <sys/types.h>
24 #include <stdio.h>
25 #ifdef STDC_HEADERS
26 # include <stdlib.h>
27 # include <stddef.h>
28 #else
29 # ifdef HAVE_STDLIB_H
30 #  include <stdlib.h>
31 # endif
32 #endif /* STDC_HEADERS */
33 #ifdef HAVE_STRING_H
34 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
35 #  include <memory.h>
36 # endif
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 <errno.h>
46
47 #include "sudo.h"
48 #include "sudo_plugin.h"
49 #include "sudo_plugin_int.h"
50
51 extern int tgetpass_flags; /* XXX */
52
53 /*
54  * Sudo conversation function.
55  */
56 int
57 sudo_conversation(int num_msgs, const struct sudo_conv_message msgs[],
58     struct sudo_conv_reply replies[])
59 {
60     struct sudo_conv_reply *repl;
61     const struct sudo_conv_message *msg;
62     char *pass;
63     int n, flags = tgetpass_flags;
64
65     for (n = 0; n < num_msgs; n++) {
66         msg = &msgs[n];
67         repl = &replies[n];
68         switch (msg->msg_type & 0xff) {
69             case SUDO_CONV_PROMPT_ECHO_ON:
70             case SUDO_CONV_PROMPT_MASK:
71                 if (msg->msg_type == SUDO_CONV_PROMPT_ECHO_ON)
72                     SET(flags, TGP_ECHO);
73                 else
74                     SET(flags, TGP_MASK);
75                 /* FALLTHROUGH */
76             case SUDO_CONV_PROMPT_ECHO_OFF:
77                 if (ISSET(msg->msg_type, SUDO_CONV_PROMPT_ECHO_OK))
78                     SET(flags, TGP_NOECHO_TRY);
79                 /* Read the password unless interrupted. */
80                 pass = tgetpass(msg->msg, msg->timeout, flags);
81                 if (pass == NULL)
82                     goto err;
83                 repl->reply = estrdup(pass);
84                 zero_bytes(pass, strlen(pass));
85                 break;
86             case SUDO_CONV_INFO_MSG:
87                 if (msg->msg)
88                     (void) fputs(msg->msg, stdout);
89                 break;
90             case SUDO_CONV_ERROR_MSG:
91                 if (msg->msg)
92                     (void) fputs(msg->msg, stderr);
93                 break;
94             case SUDO_CONV_DEBUG_MSG:
95                 if (msg->msg)
96                     sudo_debug_write(msg->msg, strlen(msg->msg), 0);
97                 break;
98             default:
99                 goto err;
100         }
101     }
102
103     return 0;
104
105 err:
106     /* Zero and free allocated memory and return an error. */
107     do {
108         repl = &replies[n];
109         if (repl->reply != NULL) {
110             zero_bytes(repl->reply, strlen(repl->reply));
111             free(repl->reply);
112             repl->reply = NULL;
113         }
114     } while (n--);
115
116     return -1;
117 }