-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathforward_api_calls.c
More file actions
132 lines (117 loc) · 3.46 KB
/
forward_api_calls.c
File metadata and controls
132 lines (117 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* $Id$
* $URL$
*
* forward_api_calls.c: forward XMLRPC calls to the Node Manager
* Used as a shell, this code works in tandem with sshd
* to allow authenticated remote access to a localhost-only service.
*
* Bugs:
* Doesn't handle Unicode properly. UTF-8 is probably OK.
*
* Change History:
* 2007/05/02: [deisenst] Increased buffer space to 1MiB.
* Increased TIMEOUT_SECS to 2min.
* 2006/10/30: [deisenst] Changed location of Unix socket.
* 2006/09/14: [deisenst] Switched to PF_UNIX sockets so that SO_PEERCRED works
* 2006/09/08: [deisenst] First version.
*/
static const int TIMEOUT_SECS = 120;
const char *API_addr = "/tmp/nodemanager.api";
static const char *Header =
"POST / HTTP/1.0\r\n"
"Content-Type: text/xml\r\n"
"Content-Length: %d\r\n"
"\r\n%n";
static const char *Error_template =
"<?xml version='1.0'?>\n"
"<methodResponse>\n"
"<fault>\n"
"<value><struct>\n"
"<member>\n"
"<name>faultCode</name>\n"
"<value><int>1</int></value>\n"
"</member>\n"
"<member>\n"
"<name>faultString</name>\n"
"<value><string>%s: %s</string></value>\n"
"</member>\n"
"</struct></value>\n"
"</fault>\n"
"</methodResponse>\n";
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
static void ERROR(const char *s) {
printf(Error_template, s, strerror(errno));
exit(1);
}
int main(int argc, char **argv, char **envp) {
ssize_t len;
char header_buf[1<<20];
char content_buf[1<<20];
size_t content_len;
int sockfd;
struct sockaddr_un addr;
int consecutive_newlines;
alarm(TIMEOUT_SECS);
/* read xmlrpc request from stdin
* 4 KiB ought to be enough for anyone
* 2007/05/02: [deisenst] It wasn't.
*/
content_len = 0;
while(content_len < sizeof content_buf) {
len = read(0,
content_buf + content_len,
sizeof content_buf - content_len);
if(len < 0) ERROR("read()");
else if(0 == len) break;
content_len += len;
}
/* connect to the API server */
sockfd = socket(PF_UNIX, SOCK_STREAM, 0);
if(sockfd < 0)
ERROR("socket()");
memset(&addr, 0, sizeof addr);
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, API_addr, sizeof addr.sun_path);
if(connect(sockfd, (struct sockaddr *)&addr, sizeof addr) < 0)
ERROR("connect()");
/* send the request */
snprintf(header_buf, sizeof header_buf, Header, content_len, &len);
write(sockfd, header_buf, len);
write(sockfd, content_buf, content_len);
shutdown(sockfd, SHUT_WR);
/* forward the response */
consecutive_newlines = 0;
while((len = read(sockfd, content_buf, sizeof content_buf)) != 0) {
size_t processed_len = 0;
if(len < 0) {
/* "Connection reset by peer" is not worth bothering the user. */
if(ECONNRESET == errno) break;
else ERROR("read()");
}
content_len = len;
while(consecutive_newlines < 2 && processed_len < content_len) {
char ch = content_buf[processed_len++];
if(ch == '\n') consecutive_newlines++;
else if(!isspace(ch)) consecutive_newlines = 0;
}
if(processed_len < content_len) {
len = fwrite(content_buf + processed_len, sizeof (char),
content_len - processed_len, stdout);
/* make sure faults don't mess up previously sent xml */
if(len < content_len - processed_len) ERROR("fwrite()");
}
}
/* goodbye */
shutdown(sockfd, SHUT_RD);
close(sockfd);
return 0;
}