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