]> arthur.barton.de Git - netatalk.git/blob - libatalk/dsi/dsi_stream.c
Fix a possible bug handling disconnected sessions, NetAFP Bug ID #16
[netatalk.git] / libatalk / dsi / dsi_stream.c
1 /*
2  * Copyright (c) 1998 Adrian Sun (asun@zoology.washington.edu)
3  * All rights reserved. See COPYRIGHT.
4  *
5  * this file provides the following functions:
6  * dsi_stream_write:    just write a bunch of bytes.
7  * dsi_stream_read:     just read a bunch of bytes.
8  * dsi_stream_send:     send a DSI header + data.
9  * dsi_stream_receive:  read a DSI header + data.
10  */
11
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif /* HAVE_CONFIG_H */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18
19 #ifdef HAVE_UNISTD_H
20 #include <unistd.h>
21 #endif
22
23 #include <string.h>
24 #include <errno.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <sys/uio.h>
28
29 #include <atalk/logger.h>
30 #include <atalk/dsi.h>
31 #include <netatalk/endian.h>
32 #include <atalk/util.h>
33
34 #define min(a,b)  ((a) < (b) ? (a) : (b))
35
36 #ifndef MSG_MORE
37 #define MSG_MORE 0x8000
38 #endif
39
40 #ifndef MSG_DONTWAIT
41 #define MSG_DONTWAIT 0x40
42 #endif
43
44 /*
45  * afpd is sleeping too much while trying to send something.
46  * May be there's no reader or the reader is also sleeping in write,
47  * look if there's some data for us to read, hopefully it will wake up
48  * the reader so we can write again.
49  *
50  * @returns 0 when is possible to send again, -1 on error
51  */
52 static int dsi_peek(DSI *dsi)
53 {
54     static int warned = 0;
55     fd_set readfds, writefds;
56     int    len;
57     int    maxfd;
58     int    ret;
59
60     LOG(log_debug, logtype_dsi, "dsi_peek");
61
62     maxfd = dsi->socket + 1;
63
64     while (1) {
65         if (dsi->socket == -1)
66             /* eg dsi_disconnect() might have disconnected us */
67             return -1;
68         FD_ZERO(&readfds);
69         FD_ZERO(&writefds);
70
71         if (dsi->eof < dsi->end) {
72             /* space in read buffer */
73             FD_SET( dsi->socket, &readfds);
74         } else {
75             if (!warned) {
76                 warned = 1;
77                 LOG(log_note, logtype_dsi, "dsi_peek: readahead buffer is full, possibly increase -dsireadbuf option");
78                 LOG(log_note, logtype_dsi, "dsi_peek: dsireadbuf: %d, DSI quantum: %d, effective buffer size: %d",
79                     dsi->dsireadbuf,
80                     dsi->server_quantum ? dsi->server_quantum : DSI_SERVQUANT_DEF,
81                     dsi->end - dsi->buffer);
82             }
83         }
84
85         FD_SET( dsi->socket, &writefds);
86
87         /* No timeout: if there's nothing to read nor nothing to write,
88          * we've got nothing to do at all */
89         if ((ret = select( maxfd, &readfds, &writefds, NULL, NULL)) <= 0) {
90             if (ret == -1 && errno == EINTR)
91                 /* we might have been interrupted by out timer, so restart select */
92                 continue;
93             /* give up */
94             LOG(log_error, logtype_dsi, "dsi_peek: unexpected select return: %d %s",
95                 ret, ret < 0 ? strerror(errno) : "");
96             return -1;
97         }
98
99         if (FD_ISSET(dsi->socket, &writefds)) {
100             /* we can write again */
101             LOG(log_debug, logtype_dsi, "dsi_peek: can write again");
102             break;
103         }
104
105         /* Check if there's sth to read, hopefully reading that will unblock the client */
106         if (FD_ISSET(dsi->socket, &readfds)) {
107             len = dsi->end - dsi->eof; /* it's ensured above that there's space */
108
109             if ((len = read(dsi->socket, dsi->eof, len)) <= 0) {
110                 if (len == 0) {
111                     LOG(log_error, logtype_dsi, "dsi_peek: EOF");
112                     return -1;
113                 }
114                 LOG(log_error, logtype_dsi, "dsi_peek: read: %s", strerror(errno));
115                 if (errno == EAGAIN)
116                     continue;
117                 return -1;
118             }
119             LOG(log_debug, logtype_dsi, "dsi_peek: read %d bytes", len);
120
121             dsi->eof += len;
122         }
123     }
124
125     return 0;
126 }
127
128 /* 
129  * Return all bytes up to count from dsi->buffer if there are any buffered there
130  */
131 static size_t from_buf(DSI *dsi, u_int8_t *buf, size_t count)
132 {
133     size_t nbe = 0;
134
135     if (dsi->buffer == NULL)
136         /* afpd master has no DSI buffering */
137         return 0;
138
139     LOG(log_maxdebug, logtype_dsi, "from_buf: %u bytes", count);
140     
141     nbe = dsi->eof - dsi->start;
142
143     if (nbe > 0) {
144         nbe = min((size_t)nbe, count);
145         memcpy(buf, dsi->start, nbe);
146         dsi->start += nbe;
147
148         if (dsi->eof == dsi->start)
149             dsi->start = dsi->eof = dsi->buffer;
150     }
151
152     LOG(log_debug, logtype_dsi, "from_buf(read: %u, unread:%u , space left: %u): returning %u",
153         dsi->start - dsi->buffer, dsi->eof - dsi->start, dsi->end - dsi->eof, nbe);
154
155     return nbe;
156 }
157
158 /*
159  * Get bytes from buffer dsi->buffer or read from socket
160  *
161  * 1. Check if there are bytes in the the dsi->buffer buffer.
162  * 2. Return bytes from (1) if yes.
163  *    Note: this may return fewer bytes then requested in count !!
164  * 3. If the buffer was empty, read from the socket.
165  */
166 static ssize_t buf_read(DSI *dsi, u_int8_t *buf, size_t count)
167 {
168     ssize_t len;
169
170     LOG(log_maxdebug, logtype_dsi, "buf_read(%u bytes)", count);
171
172     if (!count)
173         return 0;
174
175     len = from_buf(dsi, buf, count); /* 1. */
176     if (len)
177         return len;             /* 2. */
178   
179     len = readt(dsi->socket, buf, count, 0, 1); /* 3. */
180
181     LOG(log_maxdebug, logtype_dsi, "buf_read(%u bytes): got: %d", count, len);
182
183     return len;
184 }
185
186 /*
187  * Get "length" bytes from buffer and/or socket. In order to avoid frequent small reads
188  * this tries to read larger chunks (8192 bytes) into a buffer.
189  */
190 static size_t dsi_buffered_stream_read(DSI *dsi, u_int8_t *data, const size_t length)
191 {
192   size_t len;
193   size_t buflen;
194
195   LOG(log_maxdebug, logtype_dsi, "dsi_buffered_stream_read: %u bytes", length);
196   
197   len = from_buf(dsi, data, length); /* read from buffer dsi->buffer */
198   dsi->read_count += len;
199   if (len == length) {          /* got enough bytes from there ? */
200       return len;               /* yes */
201   }
202
203   /* fill the buffer with 8192 bytes or until buffer is full */
204   buflen = min(8192, dsi->end - dsi->eof);
205   if (buflen > 0) {
206       ssize_t ret;
207       ret = read(dsi->socket, dsi->eof, buflen);
208       if (ret > 0)
209           dsi->eof += ret;
210   }
211
212   /* now get the remaining data */
213   if ((buflen = dsi_stream_read(dsi, data + len, length - len)) != length - len)
214       return 0;
215   len += buflen;
216
217   return len;
218 }
219
220 /* ---------------------------------------
221 */
222 static void block_sig(DSI *dsi)
223 {
224   dsi->in_write++;
225 }
226
227 /* ---------------------------------------
228 */
229 static void unblock_sig(DSI *dsi)
230 {
231   dsi->in_write--;
232 }
233
234 /*********************************************************************************
235  * Public functions
236  *********************************************************************************/
237
238 /*!
239  * Communication error with the client, enter disconnected state
240  *
241  * 1. close the socket
242  * 2. set the DSI_DISCONNECTED flag, remove possible sleep flags
243  *
244  * @returns  0 if successfully entered disconnected state
245  *          -1 if ppid is 1 which means afpd master died
246  *             or euid == 0 ie where still running as root (unauthenticated session)
247  */
248 int dsi_disconnect(DSI *dsi)
249 {
250     LOG(log_note, logtype_dsi, "dsi_disconnect: entering disconnected state");
251     dsi->proto_close(dsi);          /* 1 */
252     dsi->flags &= ~(DSI_SLEEPING | DSI_EXTSLEEP); /* 2 */
253     dsi->flags |= DSI_DISCONNECTED;
254     if (geteuid() == 0)
255         return -1;
256     return 0;
257 }
258
259 /* ------------------------------
260  * write raw data. return actual bytes read. checks against EINTR
261  * aren't necessary if all of the signals have SA_RESTART
262  * specified. */
263 ssize_t dsi_stream_write(DSI *dsi, void *data, const size_t length, int mode)
264 {
265   size_t written;
266   ssize_t len;
267   unsigned int flags = 0;
268
269   dsi->in_write++;
270   written = 0;
271
272   LOG(log_maxdebug, logtype_dsi, "dsi_stream_write(send: %zd bytes): START", length);
273
274   if (dsi->flags & DSI_DISCONNECTED)
275       return -1;
276
277   while (written < length) {
278       len = send(dsi->socket, (u_int8_t *) data + written, length - written, flags);
279       if (len >= 0) {
280           written += len;
281           continue;
282       }
283
284       if (errno == EINTR)
285           continue;
286
287       if (errno == EAGAIN || errno == EWOULDBLOCK) {
288           LOG(log_debug, logtype_dsi, "dsi_stream_write: send: %s", strerror(errno));
289
290           if (mode == DSI_NOWAIT && written == 0) {
291               /* DSI_NOWAIT is used by attention give up in this case. */
292               written = -1;
293               goto exit;
294           }
295
296           /* Try to read sth. in order to break up possible deadlock */
297           if (dsi_peek(dsi) != 0) {
298               written = -1;
299               goto exit;
300           }
301           /* Now try writing again */
302           continue;
303       }
304
305       LOG(log_error, logtype_dsi, "dsi_stream_write: %s", strerror(errno));
306       written = -1;
307       goto exit;
308   }
309
310   dsi->write_count += written;
311   LOG(log_maxdebug, logtype_dsi, "dsi_stream_write(send: %zd bytes): END", length);
312
313 exit:
314   dsi->in_write--;
315   return written;
316 }
317
318
319 /* ---------------------------------
320 */
321 #ifdef WITH_SENDFILE
322 ssize_t dsi_stream_read_file(DSI *dsi, int fromfd, off_t offset, const size_t length)
323 {
324   int ret = 0;
325   size_t written;
326   ssize_t len;
327   off_t pos = offset;
328
329   LOG(log_maxdebug, logtype_dsi, "dsi_stream_read_file(send %zd bytes): START", length);
330
331   if (dsi->flags & DSI_DISCONNECTED)
332       return -1;
333
334   dsi->in_write++;
335   written = 0;
336
337   while (written < length) {
338     len = sys_sendfile(dsi->socket, fromfd, &pos, length - written);
339         
340     if (len < 0) {
341       if (errno == EINTR)
342           continue;
343       if (errno == EINVAL || errno == ENOSYS) {
344           ret = -1;
345           goto exit;
346       }          
347       if (errno == EAGAIN || errno == EWOULDBLOCK) {
348 #if defined(SOLARIS) || defined(FREEBSD)
349           if (pos > offset) {
350               /* we actually have sent sth., adjust counters and keep trying */
351               len = pos - offset;
352               written += len;
353               offset = pos;
354           }
355 #endif
356           if (dsi_peek(dsi)) {
357               /* can't go back to blocking mode, exit, the next read
358                  will return with an error and afpd will die.
359               */
360               break;
361           }
362           continue;
363       }
364       LOG(log_error, logtype_dsi, "dsi_stream_read_file: %s", strerror(errno));
365       break;
366     }
367     else if (!len) {
368         /* afpd is going to exit */
369           ret = -1;
370           goto exit;
371     }
372     else 
373         written += len;
374   }
375
376   dsi->write_count += written;
377
378 exit:
379   dsi->in_write--;
380   LOG(log_maxdebug, logtype_dsi, "dsi_stream_read_file: sent: %zd", written);
381   if (ret != 0)
382       return -1;
383   return written;
384 }
385 #endif
386
387
388 /*
389  * Essentially a loop around buf_read() to ensure "length" bytes are read
390  * from dsi->buffer and/or the socket.
391  *
392  * @returns length on success, some value smaller then length indicates an error
393  */
394 size_t dsi_stream_read(DSI *dsi, void *data, const size_t length)
395 {
396   size_t stored;
397   ssize_t len;
398
399   if (dsi->flags & DSI_DISCONNECTED)
400       return 0;
401
402   LOG(log_maxdebug, logtype_dsi, "dsi_stream_read(%u bytes)", length);
403
404   stored = 0;
405   while (stored < length) {
406       len = buf_read(dsi, (u_int8_t *) data + stored, length - stored);
407       if (len == -1 && (errno == EINTR || errno == EAGAIN)) {
408           LOG(log_maxdebug, logtype_dsi, "dsi_stream_read: select read loop");
409           continue;
410       } else if (len > 0) {
411           stored += len;
412       } else { /* eof or error */
413           /* don't log EOF error if it's just after connect (OSX 10.3 probe) */
414 #if 0
415           if (errno == ECONNRESET)
416               dsi->flags |= DSI_GOT_ECONNRESET;
417 #endif
418           if (len || stored || dsi->read_count) {
419               if (! (dsi->flags & DSI_DISCONNECTED)) {
420                   LOG(log_error, logtype_dsi, "dsi_stream_read: len:%d, %s",
421                       len, (len < 0) ? strerror(errno) : "unexpected EOF");
422               }
423               return 0;
424           }
425           break;
426       }
427   }
428
429   dsi->read_count += stored;
430
431   LOG(log_maxdebug, logtype_dsi, "dsi_stream_read(%u bytes): got: %u", length, stored);
432   return stored;
433 }
434
435 /* ---------------------------------------
436  * write data. 0 on failure. this assumes that dsi_len will never
437  * cause an overflow in the data buffer. 
438  */
439 int dsi_stream_send(DSI *dsi, void *buf, size_t length)
440 {
441   char block[DSI_BLOCKSIZ];
442   struct iovec iov[2];
443   size_t towrite;
444   ssize_t len;
445
446   LOG(log_maxdebug, logtype_dsi, "dsi_stream_send(%u bytes): START", length);
447
448   if (dsi->flags & DSI_DISCONNECTED)
449       return 0;
450
451   block[0] = dsi->header.dsi_flags;
452   block[1] = dsi->header.dsi_command;
453   memcpy(block + 2, &dsi->header.dsi_requestID, 
454          sizeof(dsi->header.dsi_requestID));
455   memcpy(block + 4, &dsi->header.dsi_code, sizeof(dsi->header.dsi_code));
456   memcpy(block + 8, &dsi->header.dsi_len, sizeof(dsi->header.dsi_len));
457   memcpy(block + 12, &dsi->header.dsi_reserved,
458          sizeof(dsi->header.dsi_reserved));
459
460   if (!length) { /* just write the header */
461       LOG(log_maxdebug, logtype_dsi, "dsi_stream_send(%u bytes): DSI header, no data", sizeof(block));
462     length = (dsi_stream_write(dsi, block, sizeof(block), 0) == sizeof(block));
463     return length; /* really 0 on failure, 1 on success */
464   }
465   
466   /* block signals */
467   block_sig(dsi);
468   iov[0].iov_base = block;
469   iov[0].iov_len = sizeof(block);
470   iov[1].iov_base = buf;
471   iov[1].iov_len = length;
472   
473   towrite = sizeof(block) + length;
474   dsi->write_count += towrite;
475   while (towrite > 0) {
476       if (((len = writev(dsi->socket, iov, 2)) == -1 && errno == EINTR) || (len == 0))
477           continue;
478     
479       if ((size_t)len == towrite) /* wrote everything out */
480           break;
481       else if (len < 0) { /* error */
482           if (errno == EAGAIN || errno == EWOULDBLOCK) {
483               if (!dsi_peek(dsi)) {
484                   continue;
485               }
486           }
487           LOG(log_error, logtype_dsi, "dsi_stream_send: %s", strerror(errno));
488           unblock_sig(dsi);
489           return 0;
490       }
491     
492       towrite -= len;
493       if (towrite > length) { /* skip part of header */
494           iov[0].iov_base = (char *) iov[0].iov_base + len;
495           iov[0].iov_len -= len;
496       } else { /* skip to data */
497           if (iov[0].iov_len) {
498               len -= iov[0].iov_len;
499               iov[0].iov_len = 0;
500           }
501           iov[1].iov_base = (char *) iov[1].iov_base + len;
502           iov[1].iov_len -= len;
503       }
504   }
505
506   LOG(log_maxdebug, logtype_dsi, "dsi_stream_send(%u bytes): END", length);
507   
508   unblock_sig(dsi);
509   return 1;
510 }
511
512
513 /*!
514  * Read DSI command and data
515  *
516  * @param  dsi   (rw) DSI handle
517  *
518  * @return    DSI function on success, 0 on failure
519  */
520 int dsi_stream_receive(DSI *dsi)
521 {
522   char block[DSI_BLOCKSIZ];
523
524   LOG(log_maxdebug, logtype_dsi, "dsi_stream_receive: START");
525
526   if (dsi->flags & DSI_DISCONNECTED)
527       return 0;
528
529   /* read in the header */
530   if (dsi_buffered_stream_read(dsi, (u_int8_t *)block, sizeof(block)) != sizeof(block)) 
531     return 0;
532
533   dsi->header.dsi_flags = block[0];
534   dsi->header.dsi_command = block[1];
535
536   if (dsi->header.dsi_command == 0)
537       return 0;
538
539   memcpy(&dsi->header.dsi_requestID, block + 2, sizeof(dsi->header.dsi_requestID));
540   memcpy(&dsi->header.dsi_code, block + 4, sizeof(dsi->header.dsi_code));
541   memcpy(&dsi->header.dsi_len, block + 8, sizeof(dsi->header.dsi_len));
542   memcpy(&dsi->header.dsi_reserved, block + 12, sizeof(dsi->header.dsi_reserved));
543   dsi->clientID = ntohs(dsi->header.dsi_requestID);
544   
545   /* make sure we don't over-write our buffers. */
546   dsi->cmdlen = min(ntohl(dsi->header.dsi_len), DSI_CMDSIZ);
547   if (dsi_stream_read(dsi, dsi->commands, dsi->cmdlen) != dsi->cmdlen) 
548     return 0;
549
550   LOG(log_debug, logtype_dsi, "dsi_stream_receive: DSI cmdlen: %zd", dsi->cmdlen);
551
552   return block[1];
553 }