1 |
astrand |
96 |
/* |
2 |
|
|
rdesktop: A Remote Desktop Protocol client. |
3 |
|
|
User interface services - X keyboard mapping |
4 |
|
|
Copyright (C) Matthew Chapman 1999-2001 |
5 |
|
|
|
6 |
|
|
This program is free software; you can redistribute it and/or modify |
7 |
|
|
it under the terms of the GNU General Public License as published by |
8 |
|
|
the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
(at your option) any later version. |
10 |
|
|
|
11 |
|
|
This program is distributed in the hope that it will be useful, |
12 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
GNU General Public License for more details. |
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License |
17 |
|
|
along with this program; if not, write to the Free Software |
18 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
#include <sys/types.h> |
22 |
|
|
#include <signal.h> |
23 |
|
|
#include <stdio.h> |
24 |
|
|
#include <errno.h> |
25 |
|
|
#include "rdesktop.h" |
26 |
|
|
#include <assert.h> |
27 |
|
|
#include <unistd.h> |
28 |
|
|
#include <sys/wait.h> |
29 |
|
|
|
30 |
|
|
/* Execute specified askpass program and fetch password from standard |
31 |
|
|
output. Return NULL on failure, otherwise a pointer to the data |
32 |
|
|
read (which must be freed by caller) */ |
33 |
|
|
|
34 |
|
|
char * |
35 |
|
|
askpass(char *askpass, const char *msg) |
36 |
|
|
{ |
37 |
|
|
pid_t pid; |
38 |
|
|
size_t len; |
39 |
|
|
char *pass; |
40 |
|
|
int p[2], status, ret; |
41 |
|
|
char buf[1024]; |
42 |
|
|
|
43 |
|
|
if (fflush(stdout) != 0) |
44 |
|
|
error("askpass: fflush: %s", strerror(errno)); |
45 |
|
|
assert(askpass != NULL); |
46 |
|
|
if (pipe(p) < 0) |
47 |
|
|
{ |
48 |
|
|
error("askpass: pipe: %s", strerror(errno)); |
49 |
|
|
return NULL; |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
pid = fork(); |
53 |
|
|
switch (pid) |
54 |
|
|
{ |
55 |
|
|
case -1: |
56 |
|
|
error("askpass: fork: %s", strerror(errno)); |
57 |
|
|
return NULL; |
58 |
|
|
break; |
59 |
|
|
case 0: |
60 |
|
|
/* Child */ |
61 |
|
|
seteuid(getuid()); |
62 |
|
|
setuid(getuid()); |
63 |
|
|
/* Close read end */ |
64 |
|
|
close(p[0]); |
65 |
|
|
if (dup2(p[1], STDOUT_FILENO) < 0) |
66 |
|
|
{ |
67 |
|
|
error("askpass: dup2: %s", strerror(errno)); |
68 |
|
|
exit(1); |
69 |
|
|
} |
70 |
|
|
execlp(askpass, askpass, msg, (char *) 0); |
71 |
|
|
error("askpass: exec(%s): %s", askpass, strerror(errno)); |
72 |
|
|
exit(1); |
73 |
|
|
break; |
74 |
|
|
default: |
75 |
|
|
/* Parent */ |
76 |
|
|
break; |
77 |
|
|
} |
78 |
|
|
/* Close write end */ |
79 |
|
|
close(p[1]); |
80 |
|
|
|
81 |
|
|
len = ret = 0; |
82 |
|
|
do |
83 |
|
|
{ |
84 |
|
|
ret = read(p[0], buf + len, sizeof(buf) - 1 - len); |
85 |
|
|
|
86 |
|
|
if (ret == -1 && errno == EINTR) |
87 |
|
|
continue; |
88 |
|
|
if (ret <= 0) |
89 |
|
|
break; |
90 |
|
|
|
91 |
|
|
len += ret; |
92 |
|
|
} |
93 |
|
|
while (sizeof(buf) - 1 - len > 0); |
94 |
|
|
|
95 |
|
|
|
96 |
|
|
buf[len] = '\0'; |
97 |
|
|
|
98 |
|
|
close(p[0]); |
99 |
|
|
while (waitpid(pid, &status, 0) < 0) |
100 |
|
|
if (errno != EINTR) |
101 |
|
|
break; |
102 |
|
|
|
103 |
|
|
if (WIFEXITED(status)) |
104 |
|
|
{ |
105 |
|
|
if (WEXITSTATUS(status)) |
106 |
|
|
{ |
107 |
|
|
error("askpass program returned %d\n", WEXITSTATUS(status)); |
108 |
|
|
return NULL; |
109 |
|
|
} |
110 |
|
|
} |
111 |
|
|
else |
112 |
|
|
{ |
113 |
|
|
error("abnormal exit from askpass program"); |
114 |
|
|
return NULL; |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
buf[strcspn(buf, "\r\n")] = '\0'; |
118 |
|
|
pass = strdup(buf); |
119 |
|
|
memset(buf, 0, sizeof(buf)); |
120 |
|
|
return pass; |
121 |
|
|
} |