Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/src/conversation.c
1532 views
1
/*
2
* SPDX-License-Identifier: ISC
3
*
4
* Copyright (c) 1999-2005, 2007-2012 Todd C. Miller <[email protected]>
5
*
6
* Permission to use, copy, modify, and distribute this software for any
7
* purpose with or without fee is hereby granted, provided that the above
8
* copyright notice and this permission notice appear in all copies.
9
*
10
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
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 <stdio.h>
26
#include <stdlib.h>
27
#include <string.h>
28
#include <errno.h>
29
#include <fcntl.h>
30
#include <unistd.h>
31
32
#include <sudo.h>
33
#include <sudo_plugin.h>
34
#include <sudo_plugin_int.h>
35
36
/*
37
* Sudo conversation function.
38
*/
39
int
40
sudo_conversation(int num_msgs, const struct sudo_conv_message msgs[],
41
struct sudo_conv_reply replies[], struct sudo_conv_callback *callback)
42
{
43
const int conv_debug_instance = sudo_debug_get_active_instance();
44
char *pass;
45
int n;
46
47
sudo_debug_set_active_instance(sudo_debug_instance);
48
49
for (n = 0; n < num_msgs; n++) {
50
const struct sudo_conv_message *msg = &msgs[n];
51
unsigned int flags = tgetpass_flags;
52
FILE *fp = stdout;
53
54
if (replies != NULL)
55
replies[n].reply = NULL;
56
57
switch (msg->msg_type & 0xff) {
58
case SUDO_CONV_PROMPT_ECHO_ON:
59
SET(flags, TGP_ECHO);
60
goto read_pass;
61
case SUDO_CONV_PROMPT_MASK:
62
SET(flags, TGP_MASK);
63
FALLTHROUGH;
64
case SUDO_CONV_PROMPT_ECHO_OFF:
65
if (ISSET(msg->msg_type, SUDO_CONV_PROMPT_ECHO_OK))
66
SET(flags, TGP_NOECHO_TRY);
67
read_pass:
68
/* Read the password unless interrupted. */
69
if (replies == NULL)
70
goto err;
71
pass = tgetpass(msg->msg, msg->timeout, flags, callback);
72
if (pass == NULL)
73
goto err;
74
replies[n].reply = strdup(pass);
75
if (replies[n].reply == NULL) {
76
sudo_fatalx_nodebug(U_("%s: %s"), "sudo_conversation",
77
U_("unable to allocate memory"));
78
}
79
explicit_bzero(pass, strlen(pass));
80
break;
81
case SUDO_CONV_ERROR_MSG:
82
fp = stderr;
83
FALLTHROUGH;
84
case SUDO_CONV_INFO_MSG:
85
if (msg->msg != NULL) {
86
size_t len = strlen(msg->msg);
87
const char *crnl = NULL;
88
bool written = false;
89
int ttyfd = -1;
90
bool raw_tty = false;
91
92
if (ISSET(msg->msg_type, SUDO_CONV_PREFER_TTY) &&
93
!ISSET(flags, TGP_STDIN)) {
94
ttyfd = open(_PATH_TTY, O_WRONLY);
95
if (ttyfd != -1)
96
raw_tty = sudo_term_is_raw(ttyfd);
97
} else {
98
raw_tty = sudo_term_is_raw(fileno(fp));
99
}
100
if (len != 0 && raw_tty) {
101
/* Convert nl -> cr nl in case tty is in raw mode. */
102
if (msg->msg[len - 1] == '\n') {
103
if (len == 1 || msg->msg[len - 2] != '\r') {
104
len--;
105
crnl = "\r\n";
106
}
107
}
108
}
109
if (ttyfd != -1) {
110
/* Try writing to tty but fall back to fp on error. */
111
if ((len == 0 || write(ttyfd, msg->msg, len) > 0) &&
112
(crnl == NULL || write(ttyfd, crnl, 2) > 0)) {
113
written = true;
114
}
115
close(ttyfd);
116
}
117
if (!written) {
118
if (len != 0 && fwrite(msg->msg, 1, len, fp) != len)
119
goto err;
120
if (crnl != NULL && fwrite(crnl, 1, 2, fp) != 2)
121
goto err;
122
}
123
}
124
break;
125
default:
126
goto err;
127
}
128
}
129
130
sudo_debug_set_active_instance(conv_debug_instance);
131
return 0;
132
133
err:
134
/* Zero and free allocated memory and return an error. */
135
if (replies != NULL) {
136
do {
137
struct sudo_conv_reply *repl = &replies[n];
138
if (repl->reply == NULL)
139
continue;
140
freezero(repl->reply, strlen(repl->reply));
141
repl->reply = NULL;
142
} while (n-- > 0);
143
}
144
145
sudo_debug_set_active_instance(conv_debug_instance);
146
return -1;
147
}
148
149
int
150
sudo_conversation_1_7(int num_msgs, const struct sudo_conv_message msgs[],
151
struct sudo_conv_reply replies[])
152
{
153
return sudo_conversation(num_msgs, msgs, replies, NULL);
154
}
155
156
int
157
sudo_conversation_printf(int msg_type, const char * restrict fmt, ...)
158
{
159
FILE *ttyfp = NULL;
160
FILE *fp = stdout;
161
char fmt2[1024];
162
char sbuf[8192];
163
char *buf = sbuf;
164
va_list ap;
165
int len;
166
const int conv_debug_instance = sudo_debug_get_active_instance();
167
168
sudo_debug_set_active_instance(sudo_debug_instance);
169
170
if (ISSET(msg_type, SUDO_CONV_PREFER_TTY) &&
171
!ISSET(tgetpass_flags, TGP_STDIN)) {
172
/* Try writing to /dev/tty first. */
173
ttyfp = fopen(_PATH_TTY, "w");
174
}
175
176
switch (msg_type & 0xff) {
177
case SUDO_CONV_ERROR_MSG:
178
fp = stderr;
179
FALLTHROUGH;
180
case SUDO_CONV_INFO_MSG:
181
/* Convert nl -> cr nl in case tty is in raw mode. */
182
if (sudo_term_is_raw(fileno(ttyfp ? ttyfp : fp))) {
183
size_t fmtlen = strlen(fmt);
184
if (fmtlen < sizeof(fmt2) - 1 && fmtlen && fmt[fmtlen - 1] == '\n') {
185
if (fmtlen == 1) {
186
/* Convert bare newline -> \r\n. */
187
len = (int)fwrite("\r\n", 1, 2, ttyfp ? ttyfp : fp);
188
if (len != 2)
189
len = -1;
190
break;
191
}
192
if (fmt[fmtlen - 2] != '\r') {
193
/* Convert trailing \n -> \r\n. */
194
memcpy(fmt2, fmt, fmtlen - 1);
195
fmt2[fmtlen - 1] = '\r';
196
fmt2[fmtlen ] = '\n';
197
fmt2[fmtlen + 1] = '\0';
198
fmt = fmt2;
199
}
200
}
201
}
202
/*
203
* We use vsnprintf() instead of vfprintf() here to avoid
204
* problems on systems where the system printf(3) is not
205
* C99-compliant. We use our own snprintf() on such systems.
206
*/
207
va_start(ap, fmt);
208
len = vsnprintf(sbuf, sizeof(sbuf), fmt, ap);
209
va_end(ap);
210
if (len < 0 || len >= ssizeof(sbuf)) {
211
/* Try again with a dynamically-sized buffer. */
212
va_start(ap, fmt);
213
len = vasprintf(&buf, fmt, ap);
214
va_end(ap);
215
}
216
if (len >= 0) {
217
if (fwrite(buf, 1, (size_t)len, ttyfp ? ttyfp : fp) != (size_t)len)
218
len = -1;
219
if (buf != sbuf)
220
free(buf);
221
}
222
break;
223
default:
224
len = -1;
225
errno = EINVAL;
226
break;
227
}
228
229
if (ttyfp != NULL)
230
fclose(ttyfp);
231
232
sudo_debug_set_active_instance(conv_debug_instance);
233
return len;
234
}
235
236