Imported Upstream version 1.8.6p8
[debian/sudo] / src / conversation.c
1 /*
2  * Copyright (c) 1999-2005, 2007-2010 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 <sys/param.h>
25 #include <stdio.h>
26 #ifdef STDC_HEADERS
27 # include <stdlib.h>
28 # include <stddef.h>
29 #else
30 # ifdef HAVE_STDLIB_H
31 #  include <stdlib.h>
32 # endif
33 #endif /* STDC_HEADERS */
34 #ifdef HAVE_STRING_H
35 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
36 #  include <memory.h>
37 # endif
38 # include <string.h>
39 #endif /* HAVE_STRING_H */
40 #ifdef HAVE_STRINGS_H
41 # include <strings.h>
42 #endif /* HAVE_STRINGS_H */
43 #ifdef HAVE_UNISTD_H
44 # include <unistd.h>
45 #endif /* HAVE_UNISTD_H */
46 #include <errno.h>
47
48 #include "sudo.h"
49 #include "sudo_plugin.h"
50 #include "sudo_plugin_int.h"
51
52 extern int tgetpass_flags; /* XXX */
53
54 #if defined(HAVE_DLOPEN) || defined(HAVE_SHL_LOAD)
55 sudo_conv_t sudo_conv;  /* NULL in sudo front-end */
56 #endif
57
58 /*
59  * Sudo conversation function.
60  */
61 int
62 sudo_conversation(int num_msgs, const struct sudo_conv_message msgs[],
63     struct sudo_conv_reply replies[])
64 {
65     struct sudo_conv_reply *repl;
66     const struct sudo_conv_message *msg;
67     char *pass;
68     int n, flags = tgetpass_flags;
69
70     for (n = 0; n < num_msgs; n++) {
71         msg = &msgs[n];
72         repl = &replies[n];
73         switch (msg->msg_type & 0xff) {
74             case SUDO_CONV_PROMPT_ECHO_ON:
75             case SUDO_CONV_PROMPT_MASK:
76                 if (msg->msg_type == SUDO_CONV_PROMPT_ECHO_ON)
77                     SET(flags, TGP_ECHO);
78                 else
79                     SET(flags, TGP_MASK);
80                 /* FALLTHROUGH */
81             case SUDO_CONV_PROMPT_ECHO_OFF:
82                 if (ISSET(msg->msg_type, SUDO_CONV_PROMPT_ECHO_OK))
83                     SET(flags, TGP_NOECHO_TRY);
84                 /* Read the password unless interrupted. */
85                 pass = tgetpass(msg->msg, msg->timeout, flags);
86                 if (pass == NULL)
87                     goto err;
88                 repl->reply = estrdup(pass);
89                 zero_bytes(pass, strlen(pass));
90                 break;
91             case SUDO_CONV_INFO_MSG:
92                 if (msg->msg)
93                     (void) fputs(msg->msg, stdout);
94                 break;
95             case SUDO_CONV_ERROR_MSG:
96                 if (msg->msg)
97                     (void) fputs(msg->msg, stderr);
98                 break;
99             case SUDO_CONV_DEBUG_MSG:
100                 if (msg->msg)
101                     sudo_debug_write(msg->msg, strlen(msg->msg), 0);
102                 break;
103             default:
104                 goto err;
105         }
106     }
107
108     return 0;
109
110 err:
111     /* Zero and free allocated memory and return an error. */
112     do {
113         repl = &replies[n];
114         if (repl->reply != NULL) {
115             zero_bytes(repl->reply, strlen(repl->reply));
116             free(repl->reply);
117             repl->reply = NULL;
118         }
119     } while (n--);
120
121     return -1;
122 }
123
124 int
125 _sudo_printf(int msg_type, const char *fmt, ...)
126 {
127     va_list ap;
128     FILE *fp;
129     int len;
130
131     switch (msg_type) {
132     case SUDO_CONV_INFO_MSG:
133         fp = stdout;
134         break;
135     case SUDO_CONV_ERROR_MSG:
136         fp = stderr;
137         break;
138     default:
139         errno = EINVAL;
140         return -1;
141     }
142
143     va_start(ap, fmt);
144     len = vfprintf(fp, fmt, ap);
145     va_end(ap);
146
147     return len;
148 }