f927a084805f0342cfbae0a49195b09888169293
[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 #if defined(HAVE_OPENSSL) && defined(CIT_CLIENT)
34 #include "client_crypto.h"
35 #endif
36 #ifndef HAVE_SNPRINTF
37 #include "snprintf.h"
38 #endif
39 #ifdef CIT_CLIENT
40 #include "screen.h"
41 #else
42 extern int err_printf(char *fmt, ...);
43 #endif
44
45 /*
46  * If server_is_local is set to nonzero, the client assumes that it is running
47  * on the same computer as the server.  Several things happen when this is
48  * the case, including the ability to map a specific tty to a particular login
49  * session in the "<W>ho is online" listing, the ability to run external
50  * programs, and the ability to download files directly off the disk without
51  * having to first fetch them from the server.
52  * Set the flag to 1 if this IPC is local (as is the case with pipes, or a
53  * network session to the local machine) or 0 if the server is executing on
54  * a remote computer.
55  */
56 int server_is_local = 0;
57
58 #ifndef INADDR_NONE
59 #define INADDR_NONE 0xffffffff
60 #endif
61
62 int serv_sock;
63
64 #if defined(HAVE_OPENSSL) && defined(CIT_CLIENT)
65 extern int ssl_is_connected;
66 #endif
67
68
69 void connection_died(void) {
70 #ifdef CIT_CLIENT
71         screen_delete();
72 #endif
73         err_printf("\rYour connection to this Citadel server is broken.\n"
74                         "Last error: %s\n"
75                         "Please re-connect and log in again.\n",
76                         strerror(errno));
77         logoff(3);
78 }
79
80
81 static void ipc_timeout(int signum)
82 {
83         err_printf("\rConnection timed out.\n");
84         logoff(3);
85 }
86
87
88 static int connectsock(char *host, char *service, char *protocol, int defaultPort)
89 {
90         struct hostent *phe;
91         struct servent *pse;
92         struct protoent *ppe;
93         struct sockaddr_in sin;
94         int s, type;
95
96         memset(&sin, 0, sizeof(sin));
97         sin.sin_family = AF_INET;
98
99         pse = getservbyname(service, protocol);
100         if (pse != NULL) {
101                 sin.sin_port = pse->s_port;
102         }
103         else if (atoi(service) > 0) {
104                 sin.sin_port = htons(atoi(service));
105         }
106         else {
107                 sin.sin_port = htons(defaultPort);
108         }
109         phe = gethostbyname(host);
110         if (phe) {
111                 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
112         } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
113                 err_printf("Can't get %s host entry: %s\n",
114                         host, strerror(errno));
115                 logoff(3);
116         }
117         if ((ppe = getprotobyname(protocol)) == 0) {
118                 err_printf("Can't get %s protocol entry: %s\n",
119                         protocol, strerror(errno));
120                 logoff(3);
121         }
122         if (!strcmp(protocol, "udp")) {
123                 type = SOCK_DGRAM;
124         } else {
125                 type = SOCK_STREAM;
126         }
127
128         s = socket(PF_INET, type, ppe->p_proto);
129         if (s < 0) {
130                 err_printf("Can't create socket: %s\n", strerror(errno));
131                 logoff(3);
132         }
133         signal(SIGALRM, ipc_timeout);
134         alarm(30);
135
136         if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
137                 err_printf("can't connect to %s:%s: %s\n",
138                         host, service, strerror(errno));
139                 logoff(3);
140         }
141         alarm(0);
142         signal(SIGALRM, SIG_IGN);
143
144         return (s);
145 }
146
147 int uds_connectsock(char *sockpath)
148 {
149         struct sockaddr_un addr;
150         int s;
151
152         memset(&addr, 0, sizeof(addr));
153         addr.sun_family = AF_UNIX;
154         safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
155
156         s = socket(AF_UNIX, SOCK_STREAM, 0);
157         if (s < 0) {
158                 err_printf("Can't create socket: %s\n", strerror(errno));
159                 logoff(3);
160         }
161
162         if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
163                 err_printf("can't connect: %s\n", strerror(errno));
164                 logoff(3);
165         }
166
167         server_is_local = 1;
168         return s;
169 }
170
171
172 /*
173  * input binary data from socket
174  */
175 void serv_read(char *buf, int bytes)
176 {
177         int len, rlen;
178
179 #if defined(HAVE_OPENSSL) && defined(CIT_CLIENT)
180         if (ssl_is_connected) {
181                 serv_read_ssl(buf, bytes);
182                 return;
183         }
184 #endif
185         len = 0;
186         while (len < bytes) {
187                 rlen = read(serv_sock, &buf[len], bytes - len);
188                 if (rlen < 1) {
189                         connection_died();
190                         return;
191                 }
192                 len += rlen;
193         }
194 }
195
196
197 /*
198  * send binary to server
199  */
200 void serv_write(char *buf, int nbytes)
201 {
202         int bytes_written = 0;
203         int retval;
204
205 #if defined(HAVE_OPENSSL) && defined(CIT_CLIENT)
206         if (ssl_is_connected) {
207                 serv_write_ssl(buf, nbytes);
208                 return;
209         }
210 #endif
211         while (bytes_written < nbytes) {
212                 retval = write(serv_sock, &buf[bytes_written],
213                                nbytes - bytes_written);
214                 if (retval < 1) {
215                         connection_died();
216                         return;
217                 }
218                 bytes_written += retval;
219         }
220 }
221
222
223
224 /*
225  * input string from socket - implemented in terms of serv_read()
226  */
227 void serv_gets(char *buf)
228 {
229         int i;
230
231         /* Read one character at a time.
232          */
233         for (i = 0;; i++) {
234                 serv_read(&buf[i], 1);
235                 if (buf[i] == '\n' || i == (SIZ-1))
236                         break;
237         }
238
239         /* If we got a long line, discard characters until the newline.
240          */
241         if (i == (SIZ-1))
242                 while (buf[i] != '\n')
243                         serv_read(&buf[i], 1);
244
245         /* Strip the trailing newline.
246          */
247         buf[i] = 0;
248 }
249
250
251 /*
252  * send line to server - implemented in terms of serv_write()
253  */
254 void serv_puts(char *buf)
255 {
256         /* err_printf("< %s\n", buf); */
257         serv_write(buf, strlen(buf));
258         serv_write("\n", 1);
259 }
260
261
262 /*
263  * attach to server
264  */
265 void attach_to_server(int argc, char **argv, char *hostbuf, char *portbuf)
266 {
267         int a;
268         char cithost[SIZ];
269         char citport[SIZ];
270         char sockpath[SIZ];
271
272         strcpy(cithost, DEFAULT_HOST);  /* default host */
273         strcpy(citport, DEFAULT_PORT);  /* default port */
274
275         for (a = 0; a < argc; ++a) {
276                 if (a == 0) {
277                         /* do nothing */
278                 } else if (a == 1) {
279                         strcpy(cithost, argv[a]);
280                 } else if (a == 2) {
281                         strcpy(citport, argv[a]);
282                 }
283                 else {
284                         err_printf("%s: usage: ",argv[0]);
285                         err_printf("%s [host] [port] ",argv[0]);
286                         logoff(2);
287                 }
288         }
289
290         if ((!strcmp(cithost, "localhost"))
291            || (!strcmp(cithost, "127.0.0.1"))) {
292                 server_is_local = 1;
293         }
294
295         /* If we're using a unix domain socket we can do a bunch of stuff */
296         if (!strcmp(cithost, UDS)) {
297                 snprintf(sockpath, sizeof sockpath, "citadel.socket");
298                 serv_sock = uds_connectsock(sockpath);
299                 if (hostbuf != NULL) strcpy(hostbuf, cithost);
300                 if (portbuf != NULL) strcpy(portbuf, sockpath);
301                 return;
302         }
303
304         serv_sock = connectsock(cithost, citport, "tcp", 504);
305         if (hostbuf != NULL) strcpy(hostbuf, cithost);
306         if (portbuf != NULL) strcpy(portbuf, citport);
307         return;
308 }
309
310 /*
311  * return the file descriptor of the server socket so we can select() on it.
312  */
313 int getsockfd(void)
314 {
315         return serv_sock;
316 }
317
318
319 /*
320  * return one character
321  */
322 char serv_getc(void)
323 {
324         char buf[2];
325         char ch;
326
327         serv_read(buf, 1);
328         ch = (int) buf[0];
329
330         return (ch);
331 }