b957b7de84c525da747b4d4d69199eeb1b3d97b6
[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 static int connectsock(char *host, char *service, char *protocol, int defaultPort)
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 {
86                 sin.sin_port = htons(defaultPort);
87         }
88         phe = gethostbyname(host);
89         if (phe) {
90                 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
91         } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
92                 fprintf(stderr, "Can't get %s host entry: %s\n",
93                         host, strerror(errno));
94                 logoff(3);
95         }
96         if ((ppe = getprotobyname(protocol)) == 0) {
97                 fprintf(stderr, "Can't get %s protocol entry: %s\n",
98                         protocol, strerror(errno));
99                 logoff(3);
100         }
101         if (!strcmp(protocol, "udp")) {
102                 type = SOCK_DGRAM;
103         } else {
104                 type = SOCK_STREAM;
105         }
106
107         s = socket(PF_INET, type, ppe->p_proto);
108         if (s < 0) {
109                 fprintf(stderr, "Can't create socket: %s\n", strerror(errno));
110                 logoff(3);
111         }
112         signal(SIGALRM, timeout);
113         alarm(30);
114
115         if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
116                 fprintf(stderr, "can't connect to %s.%s: %s\n",
117                         host, service, strerror(errno));
118                 logoff(3);
119         }
120         alarm(0);
121         signal(SIGALRM, SIG_IGN);
122
123         return (s);
124 }
125
126 int uds_connectsock(char *sockpath)
127 {
128         struct sockaddr_un addr;
129         int s;
130
131
132         memset(&addr, 0, sizeof(addr));
133         addr.sun_family = AF_UNIX;
134         safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
135
136         s = socket(AF_UNIX, SOCK_STREAM, 0);
137         if (s < 0) {
138                 fprintf(stderr, "Can't create socket: %s\n",
139                         strerror(errno));
140                 logoff(3);
141         }
142
143         if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
144                 fprintf(stderr, "can't connect: %s\n",
145                         strerror(errno));
146                 logoff(3);
147         }
148
149         server_is_local = 1;
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 static void numericize(char *buf, char *host, char *service, char *protocol, int defaultPort)
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 {
170                 sin.sin_port = htons(defaultPort);
171         }
172         buf[1] = (((sin.sin_port) & 0xFF00) >> 8);
173         buf[0] = ((sin.sin_port) & 0x00FF);
174
175         phe = gethostbyname(host);
176         if (phe) {
177                 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
178         } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
179                 fprintf(stderr, "Can't get %s host entry: %s\n",
180                         host, strerror(errno));
181                 logoff(3);
182         }
183         buf[5] = ((sin.sin_addr.s_addr) & 0xFF000000) >> 24;
184         buf[4] = ((sin.sin_addr.s_addr) & 0x00FF0000) >> 16;
185         buf[3] = ((sin.sin_addr.s_addr) & 0x0000FF00) >> 8;
186         buf[2] = ((sin.sin_addr.s_addr) & 0x000000FF);
187 }
188
189 /*
190  * input binary data from socket
191  */
192 void serv_read(char *buf, int bytes)
193 {
194         int len, rlen;
195
196         len = 0;
197         while (len < bytes) {
198                 rlen = read(serv_sock, &buf[len], bytes - len);
199                 if (rlen < 1) {
200                         connection_died();
201                         return;
202                 }
203                 len = len + rlen;
204         }
205 }
206
207
208 /*
209  * send binary to server
210  */
211 void serv_write(char *buf, int nbytes)
212 {
213         int bytes_written = 0;
214         int retval;
215         while (bytes_written < nbytes) {
216                 retval = write(serv_sock, &buf[bytes_written],
217                                nbytes - bytes_written);
218                 if (retval < 1) {
219                         connection_died();
220                         return;
221                 }
222                 bytes_written = bytes_written + retval;
223         }
224 }
225
226
227
228 /*
229  * input string from socket - implemented in terms of serv_read()
230  */
231 void serv_gets(char *buf)
232 {
233         int i;
234
235         /* Read one character at a time.
236          */
237         for (i = 0;; i++) {
238                 serv_read(&buf[i], 1);
239                 if (buf[i] == '\n' || i == (SIZ-1))
240                         break;
241         }
242
243         /* If we got a long line, discard characters until the newline.
244          */
245         if (i == (SIZ-1))
246                 while (buf[i] != '\n')
247                         serv_read(&buf[i], 1);
248
249         /* Strip the trailing newline.
250          */
251         buf[i] = 0;
252 }
253
254
255 /*
256  * send line to server - implemented in terms of serv_write()
257  */
258 void serv_puts(char *buf)
259 {
260         /* printf("< %s\n", buf); */
261         serv_write(buf, strlen(buf));
262         serv_write("\n", 1);
263 }
264
265
266 /*
267  * attach to server
268  */
269 void attach_to_server(int argc, char **argv, char *hostbuf, char *portbuf)
270 {
271         int a;
272         char cithost[SIZ];
273         int host_copied = 0;
274         char citport[SIZ];
275         int port_copied = 0;
276         char socks4[SIZ];
277         char buf[SIZ];
278         struct passwd *p;
279         char sockpath[SIZ];
280
281         strcpy(cithost, DEFAULT_HOST);  /* default host */
282         strcpy(citport, DEFAULT_PORT);  /* default port */
283         strcpy(socks4, "");     /* SOCKS v4 server */
284
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         if ((!strcmp(cithost, "localhost"))
309             || (!strcmp(cithost, "127.0.0.1")))
310                 server_is_local = 1;
311
312         /* If we're using a unix domain socket we can do a bunch of stuff */
313         if (!strcmp(cithost, UDS)) {
314                 sprintf(sockpath, "citadel.socket");
315                 serv_sock = uds_connectsock(sockpath);
316                 if (hostbuf != NULL) strcpy(hostbuf, cithost);
317                 if (portbuf != NULL) strcpy(portbuf, sockpath);
318                 return;
319         }
320
321         /* if not using a SOCKS proxy server, make the connection directly */
322         if (strlen(socks4) == 0) {
323                 serv_sock = connectsock(cithost, citport, "tcp", 504);
324                 if (hostbuf != NULL) strcpy(hostbuf, cithost);
325                 if (portbuf != NULL) strcpy(portbuf, citport);
326                 return;
327         }
328         /* if using SOCKS, connect first to the proxy... */
329         serv_sock = connectsock(socks4, "1080", "tcp", 1080);
330         printf("Connected to SOCKS proxy at %s.\n", socks4);
331         printf("Attaching to server...\r");
332         fflush(stdout);
333
334         snprintf(buf, sizeof buf, "%c%c",
335                  4,             /* version 4 */
336                  1);            /* method = connect */
337         serv_write(buf, 2);
338
339         numericize(buf, cithost, citport, "tcp", 504);
340         serv_write(buf, 6);     /* port and address */
341
342         p = (struct passwd *) getpwuid(getuid());
343         serv_write(p->pw_name, strlen(p->pw_name) + 1);
344         /* user name */
345
346         serv_read(buf, 8);      /* get response */
347
348         if (buf[1] != 90) {
349                 printf("SOCKS server denied this proxy request.\n");
350                 logoff(3);
351         }
352 }
353
354 /*
355  * return the file descriptor of the server socket so we can select() on it.
356  */
357 int getsockfd(void)
358 {
359         return serv_sock;
360 }
361
362
363 /*
364  * return one character
365  */
366 char serv_getc(void)
367 {
368         char buf[2];
369         char ch;
370
371         serv_read(buf, 1);
372         ch = (int) buf[0];
373
374         return (ch);
375 }
376