4 * Session proxy for Citadel PHP bindings
6 * This is an unfinished session proxy ... it is a C version of the
7 * session proxy implemented in sessionproxy.php ... that version is pure PHP
8 * so we should probably stick with it as long as it works.
10 * Copyright (c) 2003 by Art Cancro <ajc@uncensored.citadel.org>
11 * This program is released under the terms of the GNU General Public License.
22 #include <sys/types.h>
24 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
40 #define INADDR_NONE 0xffffffff
44 void timeout(int signum)
46 fprintf(stderr, "Connection timed out.\n");
52 * Connect a unix domain socket
54 int uds_connectsock(char *sockpath)
56 struct sockaddr_un addr;
59 memset(&addr, 0, sizeof(addr));
60 addr.sun_family = AF_UNIX;
61 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
63 s = socket(AF_UNIX, SOCK_STREAM, 0);
65 fprintf(stderr, "Can't create socket: %s\n",
70 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
71 fprintf(stderr, "Can't connect: %s\n",
82 * Connect a TCP/IP socket
84 int tcp_connectsock(char *host, char *service)
89 struct sockaddr_in sin;
92 memset(&sin, 0, sizeof(sin));
93 sin.sin_family = AF_INET;
95 pse = getservbyname(service, "tcp");
97 sin.sin_port = pse->s_port;
98 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
99 fprintf(stderr, "Can't get %s service entry\n", service);
102 phe = gethostbyname(host);
104 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
105 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
106 fprintf(stderr, "Can't get %s host entry: %s\n",
107 host, strerror(errno));
110 if ((ppe = getprotobyname("tcp")) == 0) {
111 fprintf(stderr, "Can't get TCP protocol entry: %s\n",
116 s = socket(PF_INET, SOCK_STREAM, ppe->p_proto);
118 fprintf(stderr, "Can't create socket: %s\n", strerror(errno));
121 signal(SIGALRM, timeout);
124 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
125 fprintf(stderr, "Can't connect to %s.%s: %s\n",
126 host, service, strerror(errno));
131 signal(SIGALRM, SIG_IGN);
140 * Input binary data from socket
142 int sock_read(int sock, char *buf, int bytes)
147 while (len < bytes) {
148 rlen = read(sock, &buf[len], bytes - len);
150 fprintf(stderr, "Server connection broken: %s\n",
161 * input string from pipe
163 int sock_gets(int sock, char *strbuf)
171 if (sock_read(sock, &buf[0], 1) < 0) return(-1);
174 } while ((ch != 10) && (ch != 0) && (len < (SIZ-1)));
175 if (strbuf[len-1] == 10) strbuf[--len] = 0;
176 if (strbuf[len-1] == 13) strbuf[--len] = 0;
183 * send binary to server
185 int sock_write(int sock, char *buf, int nbytes)
187 int bytes_written = 0;
189 while (bytes_written < nbytes) {
190 retval = write(sock, &buf[bytes_written],
191 nbytes - bytes_written);
193 fprintf(stderr, "Server connection broken: %s\n",
197 bytes_written = bytes_written + retval;
199 return(bytes_written);
204 * send line to server
206 int sock_puts(int sock, char *string)
210 sprintf(buf, "%s\n", string);
211 return sock_write(sock, buf, strlen(buf));
216 * Create a Unix domain socket and listen on it
218 int ig_uds_server(char *sockpath, int queue_len)
220 struct sockaddr_un addr;
223 int actual_queue_len;
225 actual_queue_len = queue_len;
226 if (actual_queue_len < 5) actual_queue_len = 5;
228 i = unlink(sockpath);
229 if (i != 0) if (errno != ENOENT) {
230 fprintf(stderr, "can't unlink %s: %s\n",
231 sockpath, strerror(errno));
235 memset(&addr, 0, sizeof(addr));
236 addr.sun_family = AF_UNIX;
237 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
239 s = socket(AF_UNIX, SOCK_STREAM, 0);
241 fprintf(stderr, "Can't create a socket: %s\n",
246 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
247 fprintf(stderr, "Can't bind: %s\n",
252 if (listen(s, actual_queue_len) < 0) {
253 fprintf(stderr, "Can't listen: %s\n", strerror(errno));
257 chmod(sockpath, 0700); /* only me me me can talk to this */
266 int main(int argc, char **argv) {
275 /* Fail if we weren't supplied with the right number of arguments
281 /* Fail if we can't connect to Citadel
283 ctdl_sock = uds_connectsock("/appl/citadel/citadel.socket");
288 /* Fail if we can't read the server greeting message
290 if (sock_gets(ctdl_sock, buf) < 0) {
294 /* Fail if the server isn't giving us an error-free startup
300 /* Now we're solid with the Citadel server. Nice. It's time to
301 * open our proxy socket so PHP can talk to us. Fail if we can't
304 listen_sock = ig_uds_server(argv[1], 5);
305 if (listen_sock < 0) {
310 /* The socket is ready to listen for connections, so it's time for
311 * this program to go into the background. Fork, then close all file
312 * descriptors so that the PHP script that called us can continue
327 for (i=0; i<256; ++i) {
328 /* Close fd's so PHP doesn't get all, like, whatever */
329 if ( (i != ctdl_sock) && (i != listen_sock) ) {
335 /* Listen for connections. */
337 signal(SIGPIPE, SIG_IGN);
338 while (cmd_sock = accept(listen_sock, NULL, 0), cmd_sock >= 0) {
340 while (sock_gets(cmd_sock, buf) >= 0) {
341 if (sock_puts(ctdl_sock, buf) < 0) goto CTDL_BAIL;
342 if (sock_gets(ctdl_sock, buf) < 0) goto CTDL_BAIL;
343 sock_puts(cmd_sock, buf);
345 if (buf[0] == '1') do {
346 if (sock_gets(ctdl_sock, dbuf) < 0) {
349 sock_puts(cmd_sock, dbuf);
350 } while (strcmp(dbuf, "000"));
352 else if (buf[0] == '4') do {
353 sock_gets(cmd_sock, dbuf);
354 if (sock_puts(ctdl_sock, dbuf) < 0) {
357 } while (strcmp(dbuf, "000"));
364 /* Clean up and go away.