5 * forward_api_calls.c: forward XMLRPC calls to the Node Manager
6 * Used as a shell, this code works in tandem with sshd
7 * to allow authenticated remote access to a localhost-only service.
10 * Doesn't handle Unicode properly. UTF-8 is probably OK.
13 * 2007/05/02: [deisenst] Increased buffer space to 1MiB.
14 * Increased TIMEOUT_SECS to 2min.
15 * 2006/10/30: [deisenst] Changed location of Unix socket.
16 * 2006/09/14: [deisenst] Switched to PF_UNIX sockets so that SO_PEERCRED works
17 * 2006/09/08: [deisenst] First version.
20 static const int TIMEOUT_SECS = 120;
21 const char *API_addr = "/tmp/nodemanager.api";
23 static const char *Header =
25 "Content-Type: text/xml\r\n"
26 "Content-Length: %d\r\n"
29 static const char *Error_template =
30 "<?xml version='1.0'?>\n"
35 "<name>faultCode</name>\n"
36 "<value><int>1</int></value>\n"
39 "<name>faultString</name>\n"
40 "<value><string>%s: %s</string></value>\n"
44 "</methodResponse>\n";
51 #include <sys/socket.h>
52 #include <sys/types.h>
56 static void ERROR(const char *s) {
57 printf(Error_template, s, strerror(errno));
61 int main(int argc, char **argv, char **envp) {
63 char header_buf[1<<20];
64 char content_buf[1<<20];
67 struct sockaddr_un addr;
68 int consecutive_newlines;
72 /* read xmlrpc request from stdin
73 * 4 KiB ought to be enough for anyone
74 * 2007/05/02: [deisenst] It wasn't.
77 while(content_len < sizeof content_buf) {
79 content_buf + content_len,
80 sizeof content_buf - content_len);
81 if(len < 0) ERROR("read()");
82 else if(0 == len) break;
86 /* connect to the API server */
87 sockfd = socket(PF_UNIX, SOCK_STREAM, 0);
90 memset(&addr, 0, sizeof addr);
91 addr.sun_family = AF_UNIX;
92 strncpy(addr.sun_path, API_addr, sizeof addr.sun_path);
93 if(connect(sockfd, (struct sockaddr *)&addr, sizeof addr) < 0)
96 /* send the request */
97 snprintf(header_buf, sizeof header_buf, Header, content_len, &len);
98 write(sockfd, header_buf, len);
99 write(sockfd, content_buf, content_len);
100 shutdown(sockfd, SHUT_WR);
102 /* forward the response */
103 consecutive_newlines = 0;
104 while((len = read(sockfd, content_buf, sizeof content_buf)) != 0) {
105 size_t processed_len = 0;
107 /* "Connection reset by peer" is not worth bothering the user. */
108 if(ECONNRESET == errno) break;
109 else ERROR("read()");
113 while(consecutive_newlines < 2 && processed_len < content_len) {
114 char ch = content_buf[processed_len++];
115 if(ch == '\n') consecutive_newlines++;
116 else if(!isspace(ch)) consecutive_newlines = 0;
119 if(processed_len < content_len) {
120 len = fwrite(content_buf + processed_len, sizeof (char),
121 content_len - processed_len, stdout);
122 /* make sure faults don't mess up previously sent xml */
123 if(len < content_len - processed_len) ERROR("fwrite()");
128 shutdown(sockfd, SHUT_RD);