Setting tag nodemanager-1.8-39
[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  * 2007/05/02: [deisenst] Increased buffer space to 1MiB.
10  *                        Increased TIMEOUT_SECS to 2min.
11  * 2006/10/30: [deisenst] Changed location of Unix socket.
12  * 2006/09/14: [deisenst] Switched to PF_UNIX sockets so that SO_PEERCRED works
13  * 2006/09/08: [deisenst] First version.
14  */
15
16 static const int TIMEOUT_SECS = 120;
17 const char *API_addr = "/tmp/sliver_mgr.api";
18
19 static const char *Header =
20   "POST / HTTP/1.0\r\n"
21   "Content-Type: text/xml\r\n"
22   "Content-Length: %d\r\n"
23   "\r\n%n";
24
25 static const char *Error_template =
26   "<?xml version='1.0'?>\n"
27   "<methodResponse>\n"
28   "<fault>\n"
29   "<value><struct>\n"
30   "<member>\n"
31   "<name>faultCode</name>\n"
32   "<value><int>1</int></value>\n"
33   "</member>\n"
34   "<member>\n"
35   "<name>faultString</name>\n"
36   "<value><string>%s: %s</string></value>\n"
37   "</member>\n"
38   "</struct></value>\n"
39   "</fault>\n"
40   "</methodResponse>\n";
41
42 #include <ctype.h>
43 #include <errno.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <sys/socket.h>
48 #include <sys/types.h>
49 #include <sys/un.h>
50 #include <unistd.h>
51
52 static void ERROR(const char *s) {
53   printf(Error_template, s, strerror(errno));
54   exit(1);
55 }
56
57 int main(int argc, char **argv, char **envp) {
58   ssize_t len;
59   char header_buf[1<<20];
60   char content_buf[1<<20];
61   size_t content_len;
62   int sockfd;
63   struct sockaddr_un addr;
64   int consecutive_newlines;
65
66   alarm(TIMEOUT_SECS);
67
68   /* read xmlrpc request from stdin
69    * 4 KiB ought to be enough for anyone
70    * 2007/05/02: [deisenst] It wasn't.
71    */
72   content_len = 0;
73   while(content_len < sizeof content_buf) {
74     len = read(0,
75                content_buf + content_len,
76                sizeof content_buf - content_len);
77     if(len < 0) ERROR("read()");
78     else if(0 == len) break;
79     content_len += len;
80   }
81
82   /* connect to the API server */
83   sockfd = socket(PF_UNIX, SOCK_STREAM, 0);
84   if(sockfd < 0)
85     ERROR("socket()");
86   memset(&addr, 0, sizeof addr);
87   addr.sun_family = AF_UNIX;
88   strncpy(addr.sun_path, API_addr, sizeof addr.sun_path);
89   if(connect(sockfd, (struct sockaddr *)&addr, sizeof addr) < 0)
90     ERROR("connect()");
91
92   /* send the request */
93   snprintf(header_buf, sizeof header_buf, Header, content_len, &len);
94   write(sockfd, header_buf, len);
95   write(sockfd, content_buf, content_len);
96   shutdown(sockfd, SHUT_WR);
97
98   /* forward the response */
99   consecutive_newlines = 0;
100   while((len = read(sockfd, content_buf, sizeof content_buf)) != 0) {
101     size_t processed_len = 0;
102     if(len < 0) {
103       /* "Connection reset by peer" is not worth bothering the user. */
104       if(ECONNRESET == errno) break;
105       else ERROR("read()");
106     }
107     content_len = len;
108
109     while(consecutive_newlines < 2 && processed_len < content_len) {
110       char ch = content_buf[processed_len++];
111       if(ch == '\n') consecutive_newlines++;
112       else if(!isspace(ch)) consecutive_newlines = 0;
113     }
114
115     if(processed_len < content_len) {
116       len = fwrite(content_buf + processed_len, sizeof (char),
117                    content_len - processed_len, stdout);
118       /* make sure faults don't mess up previously sent xml */
119       if(len < content_len - processed_len) ERROR("fwrite()");
120     }
121   }
122
123   /* goodbye */
124   shutdown(sockfd, SHUT_RD);
125   close(sockfd);
126
127   return 0;
128 }