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