* Added support for protocols over Unix domain sockets.
[citadel.git] / citadel / ipc_c_tcp.c
1 /*
2  * ipc_c_tcp.c
3  * 
4  * Citadel/UX client/server IPC - client module using TCP/IP
5  *
6  * version 1.3 $Id$
7  *
8  */
9
10 #define DEFAULT_HOST    "127.0.0.1"
11 #define DEFAULT_PORT    "citadel"
12
13
14 #include "sysdep.h"
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <stdio.h>
18 #include <signal.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <sys/un.h>
24 #include <netdb.h>
25 #include <string.h>
26 #include <pwd.h>
27 #include <errno.h>
28 #include <stdarg.h>
29 #include "citadel.h"
30 #include "citadel_decls.h"
31 #include "ipc.h"
32 #ifndef HAVE_SNPRINTF
33 #include "snprintf.h"
34 #endif
35
36 /*
37  * If server_is_local is set to nonzero, the client assumes that it is running
38  * on the same computer as the server.  Several things happen when this is
39  * the case, including the ability to map a specific tty to a particular login
40  * session in the "<W>ho is online" listing, the ability to run external
41  * programs, and the ability to download files directly off the disk without
42  * having to first fetch them from the server.
43  * Set the flag to 1 if this IPC is local (as is the case with pipes, or a
44  * network session to the local machine) or 0 if the server is executing on
45  * a remote computer.
46  */
47 char server_is_local = 0;
48
49 #ifndef INADDR_NONE
50 #define INADDR_NONE 0xffffffff
51 #endif
52
53 int serv_sock;
54
55 void connection_died(void) {
56         fprintf(stderr, "\r"
57                         "Your connection to this Citadel server is broken.\n"
58                         "Please re-connect and log in again.\n");
59         logoff(3);
60 }
61
62
63 void timeout(int signum)
64 {
65         printf("\rConnection timed out.\n");
66         logoff(3);
67 }
68
69
70 int connectsock(char *host, char *service, char *protocol)
71 {
72         struct hostent *phe;
73         struct servent *pse;
74         struct protoent *ppe;
75         struct sockaddr_in sin;
76         struct sockaddr_un sun;
77         int s, type;
78
79
80         if ( (!strcmp(protocol, "unix")) || (atoi(service)<0) ) {
81                 memset(&sun, 0, sizeof(sun));
82                 sun.sun_family = AF_UNIX;
83                 sprintf(sun.sun_path, USOCKPATH, 0-atoi(service) );
84
85                 s = socket(AF_UNIX, SOCK_STREAM, 0);
86                 if (s < 0) {
87                         fprintf(stderr, "Can't create socket: %s\n",
88                                 strerror(errno));
89                         logoff(3);
90                 }
91
92                 if (connect(s, (struct sockaddr *) &sun, sizeof(sun)) < 0) {
93                         fprintf(stderr, "can't connect: %s\n",
94                                 strerror(errno));
95                         logoff(3);
96                 }
97
98                 return s;
99         }
100
101
102         /* otherwise it's a network connection */
103
104         memset(&sin, 0, sizeof(sin));
105         sin.sin_family = AF_INET;
106
107         pse = getservbyname(service, protocol);
108         if (pse) {
109                 sin.sin_port = pse->s_port;
110         } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
111                 fprintf(stderr, "Can't get %s service entry: %s\n",
112                         service, strerror(errno));
113                 logoff(3);
114         }
115         phe = gethostbyname(host);
116         if (phe) {
117                 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
118         } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
119                 fprintf(stderr, "Can't get %s host entry: %s\n",
120                         host, strerror(errno));
121                 logoff(3);
122         }
123         if ((ppe = getprotobyname(protocol)) == 0) {
124                 fprintf(stderr, "Can't get %s protocol entry: %s\n",
125                         protocol, strerror(errno));
126                 logoff(3);
127         }
128         if (!strcmp(protocol, "udp")) {
129                 type = SOCK_DGRAM;
130         } else {
131                 type = SOCK_STREAM;
132         }
133
134         s = socket(PF_INET, type, ppe->p_proto);
135         if (s < 0) {
136                 fprintf(stderr, "Can't create socket: %s\n", strerror(errno));
137                 logoff(3);
138         }
139         signal(SIGALRM, timeout);
140         alarm(30);
141
142         if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
143                 fprintf(stderr, "can't connect to %s.%s: %s\n",
144                         host, service, strerror(errno));
145                 logoff(3);
146         }
147         alarm(0);
148         signal(SIGALRM, SIG_IGN);
149
150         return (s);
151 }
152
153 /*
154  * convert service and host entries into a six-byte numeric in the format
155  * expected by a SOCKS v4 server
156  */
157 void numericize(char *buf, char *host, char *service, char *protocol)
158 {
159         struct hostent *phe;
160         struct servent *pse;
161         struct sockaddr_in sin;
162
163         memset(&sin, 0, sizeof(sin));
164         sin.sin_family = AF_INET;
165
166         pse = getservbyname(service, protocol);
167         if (pse) {
168                 sin.sin_port = pse->s_port;
169         } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
170                 fprintf(stderr, "Can't get %s service entry: %s\n",
171                         service, strerror(errno));
172                 logoff(3);
173         }
174         buf[1] = (((sin.sin_port) & 0xFF00) >> 8);
175         buf[0] = ((sin.sin_port) & 0x00FF);
176
177         phe = gethostbyname(host);
178         if (phe) {
179                 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
180         } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
181                 fprintf(stderr, "Can't get %s host entry: %s\n",
182                         host, strerror(errno));
183                 logoff(3);
184         }
185         buf[5] = ((sin.sin_addr.s_addr) & 0xFF000000) >> 24;
186         buf[4] = ((sin.sin_addr.s_addr) & 0x00FF0000) >> 16;
187         buf[3] = ((sin.sin_addr.s_addr) & 0x0000FF00) >> 8;
188         buf[2] = ((sin.sin_addr.s_addr) & 0x000000FF);
189 }
190
191 /*
192  * input binary data from socket
193  */
194 void serv_read(char *buf, int bytes)
195 {
196         int len, rlen;
197
198         len = 0;
199         while (len < bytes) {
200                 rlen = read(serv_sock, &buf[len], bytes - len);
201                 if (rlen < 1) {
202                         connection_died();
203                         return;
204                 }
205                 len = len + rlen;
206         }
207 }
208
209
210 /*
211  * send binary to server
212  */
213 void serv_write(char *buf, int nbytes)
214 {
215         int bytes_written = 0;
216         int retval;
217         while (bytes_written < nbytes) {
218                 retval = write(serv_sock, &buf[bytes_written],
219                                nbytes - bytes_written);
220                 if (retval < 1) {
221                         connection_died();
222                         return;
223                 }
224                 bytes_written = bytes_written + retval;
225         }
226 }
227
228
229
230 /*
231  * input string from socket - implemented in terms of serv_read()
232  */
233 void serv_gets(char *buf)
234 {
235         int i;
236
237         /* Read one character at a time.
238          */
239         for (i = 0;; i++) {
240                 serv_read(&buf[i], 1);
241                 if (buf[i] == '\n' || i == 255)
242                         break;
243         }
244
245         /* If we got a long line, discard characters until the newline.
246          */
247         if (i == 255)
248                 while (buf[i] != '\n')
249                         serv_read(&buf[i], 1);
250
251         /* Strip the trailing newline.
252          */
253         buf[i] = 0;
254 }
255
256
257 /*
258  * send line to server - implemented in terms of serv_write()
259  */
260 void serv_puts(char *buf)
261 {
262         /* printf("< %s\n", buf); */
263         serv_write(buf, strlen(buf));
264         serv_write("\n", 1);
265 }
266
267
268 /*
269  * attach to server
270  */
271 void attach_to_server(int argc, char **argv)
272 {
273         int a;
274         char cithost[256];
275         int host_copied = 0;
276         char citport[256];
277         int port_copied = 0;
278         char socks4[256];
279         char buf[256];
280         struct passwd *p;
281
282         strcpy(cithost, DEFAULT_HOST);  /* default host */
283         strcpy(citport, DEFAULT_PORT);  /* default port */
284         strcpy(socks4, "");     /* SOCKS v4 server */
285
286         for (a = 0; a < argc; ++a) {
287                 if (a == 0) {
288                         /* do nothing */
289                 } else if (!strcmp(argv[a], "-s")) {
290                         strcpy(socks4, argv[++a]);
291                 } else if (host_copied == 0) {
292                         host_copied = 1;
293                         strcpy(cithost, argv[a]);
294                 } else if (port_copied == 0) {
295                         port_copied = 1;
296                         strcpy(citport, argv[a]);
297                 }
298 /*
299    else {
300    fprintf(stderr,"%s: usage: ",argv[0]);
301    fprintf(stderr,"%s [host] [port] ",argv[0]);
302    fprintf(stderr,"[-s socks_server]\n");
303    logoff(2);
304    }
305  */
306         }
307
308         server_is_local = 0;
309         if ((!strcmp(cithost, "localhost"))
310             || (!strcmp(cithost, "127.0.0.1")))
311                 server_is_local = 1;
312
313         /* if not using a SOCKS proxy server, make the connection directly */
314         if (strlen(socks4) == 0) {
315                 serv_sock = connectsock(cithost, citport, "tcp");
316                 return;
317         }
318         /* if using SOCKS, connect first to the proxy... */
319         serv_sock = connectsock(socks4, "1080", "tcp");
320         printf("Connected to SOCKS proxy at %s.\n", socks4);
321         printf("Attaching to server...\r");
322         fflush(stdout);
323
324         snprintf(buf, sizeof buf, "%c%c",
325                  4,             /* version 4 */
326                  1);            /* method = connect */
327         serv_write(buf, 2);
328
329         numericize(buf, cithost, citport, "tcp");
330         serv_write(buf, 6);     /* port and address */
331
332         p = (struct passwd *) getpwuid(getuid());
333         serv_write(p->pw_name, strlen(p->pw_name) + 1);
334         /* user name */
335
336         serv_read(buf, 8);      /* get response */
337
338         if (buf[1] != 90) {
339                 printf("SOCKS server denied this proxy request.\n");
340                 logoff(3);
341         }
342 }
343
344 /*
345  * return the file descriptor of the server socket so we can select() on it.
346  */
347 int getsockfd(void)
348 {
349         return serv_sock;
350 }
351
352
353 /*
354  * return one character
355  */
356 char serv_getc(void)
357 {
358         char buf[2];
359         char ch;
360
361         serv_read(buf, 1);
362         ch = (int) buf[0];
363
364         return (ch);
365 }
366