Debian Codegraph @

ax25/axgetput/proto_bin.c

raw
1 /*
2 * (c) 2002 Thomas Osterried DL9SAU <[email protected]>
3 * License: GPL. See http://www.fsf.org/
4 * Sources: http://x-berg.in-berlin.de/cgi-bin/viewcvs.cgi/ampr/axgetput/
5 */
6
7 #include "includes.h"
8
9 #include "proto_bin.h"
10 #include "axgetput.h"
11 #include "util.h"
12
13 static int crctab[256];
14 static int crcbit[8] = {
15 0x9188, 0x48c4, 0x2462, 0x1231, 0x8108, 0x4084, 0x2042, 0x1021
16 };
17
18 /*---------------------------------------------------------------------------*/
19
20 static int init_crc(void)
21 {
22 int i,j;
23
24 for (i = 0; i < 256; i++) {
25 crctab[i] = 0;
26 for (j = 0; j < 8; j++) {
27 if ((i << j) & 0x80)
28 crctab[i] = crctab[i] ^ crcbit[j];
29 }
30 }
31 return 0;
32 }
33
34 /*---------------------------------------------------------------------------*/
35
36 static int do_crc(char b, unsigned int crc)
37 {
38 return (crctab[(crc >> 8)] ^ ((crc << 8) | (b & 0xff))) & 0xffff;
39 }
40
41 static unsigned int calc_crc(char *buf, int n, unsigned crc)
42 {
43 while (--n >= 0)
44 crc = do_crc(*buf++, crc);
45
46 return crc;
47 }
48
49 /*---------------------------------------------------------------------------*/
50
51 static long parse_sfbin_date_to_unix(const char *s)
52 {
53
54 unsigned long x;
55
56 sscanf(s, "%lX", &x);
57
58 return date_dos2unix(((x << 16) >> 16), (x >> 16));
59 }
60
61 /*---------------------------------------------------------------------------*/
62
63 static char * unix_to_sfbin_date_string(long gmt)
64 {
65
66 static char buf[9];
67 unsigned short s_time, s_date;
68
69 date_unix2dos(((gmt == -1) ? time(NULL) : gmt), &s_time, &s_date);
70 sprintf(buf, "%X", ((s_date << 16) + s_time));
71 return buf;
72 }
73
74 /*---------------------------------------------------------------------------*/
75
76 int bput(void)
77 {
78 struct stat statbuf;
79 char buf[1024]; /* < signed int */
80 char filename_given[PATH_MAX];
81 unsigned long len_read_expected = 0L;
82 unsigned long len_read_left;
83 time_t file_time = 0L;
84 unsigned int msg_crc = 0;
85 unsigned int crc = 0;
86 char *term_line = NULL;
87 int last_line_had_CR = 0;
88 int len_termline = 0;
89 int len = 0;
90 int fddata = fdout;
91 int is_eof;
92 int i;
93 char *p;
94 char *p_buf;
95
96 #define save_close(x) { \
97 if (!fdout_is_pipe) \
98 close(x); \
99 }
100
101
102 for (;;) {
103 len = my_read(fdin, buf, sizeof(buf), &is_eof, "\r\n");
104 if (is_eof || len < 1) {
105 sprintf(err_msg, "error: read failed (%s)\n", strerror(errno));
106 return 1;
107 }
108 if (buf[len-1] == '\n') {
109 #if 0
110 p = "warning: <LF> received. not 8bit clean?\r";
111 secure_write(fdout, p, strlen(p));
112 #endif
113 sprintf(err_msg, "bad EOL: <LF>\n");
114 return 1;
115 }
116 if (IS_BIN_ABORT(buf, len)) {
117 sprintf(err_msg, "Aborted by user request\n");
118 return 1;
119 }
120 if (buf[len-1] == '\r' && len > 5 && !memcmp(buf, "#BIN#", 5)) {
121 break;
122 }
123 if (len == sizeof(buf)) {
124 sprintf(err_msg, "line to long\n");
125 return 1;
126 }
127 }
128 buf[len-1] = 0; /* without trailing \r. and: string termination */
129
130 send_on_signal = bin_send_no_on_sig;
131
132 /* parse #BIN arguments */
133 *filename_given = 0;
134
135 p_buf = buf;
136 for (i = 0; (p = strsep(&p_buf, "#")); i++) {
137 switch (i) {
138 case 0:
139 case 1:
140 break;
141 case 2:
142 if (*p)
143 len_read_expected = (unsigned long ) atol(p);
144 break;
145 default:
146 if (*p == '|') {
147 msg_crc = (unsigned int ) atoi(p+1);
148 } else if (*p == '$') {
149 file_time = parse_sfbin_date_to_unix(p+1);
150 } else {
151 strncpy(filename_given, p, sizeof(filename_given)-1);
152 filename_given[sizeof(filename_given)-1] = 0;
153 }
154 }
155 }
156
157 if (!fdout_is_pipe) {
158 /* normal mode: store in given filename */
159 if (!*filename) {
160 p = get_fixed_filename(filename_given, len_read_expected, msg_crc, 1);
161 strncpy(filename, p, sizeof(filename)-1);
162 filename[sizeof(filename)-1] = 0;
163 }
164 if (!stat(filename, &statbuf)) {
165 /* file exist */
166 if (unlink(filename)) {
167 sprintf(err_msg, "error: cannot unlink %s (%s)\n", filename, strerror(errno));
168 goto abort;
169 }
170 }
171 fddata = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0640);
172 if (fddata < 0) {
173 sprintf(err_msg, "error: cannot open %s (%s)\n", filename, strerror(errno));
174 write(fderr, "\r#NO#\r", 6);
175 return 1;
176 }
177 }
178
179 if (!len_read_expected) {
180 term_line = "***END\r";
181 len_termline = strlen(term_line);
182 }
183
184 /* say helo */
185 send_on_signal = bin_send_abort_on_sig;
186 write(fderr, "\r#OK#\r", 6);
187
188 len_read_left = len_read_expected;
189
190 /* #bin# chechsum initialization */
191 init_crc();
192
193 for (;;) {
194
195 len = my_read(fdin, buf,
196 ((term_line || len_read_left > sizeof(buf)) ? sizeof(buf) : len_read_left),
197 &is_eof, "\r");
198 if (len < 1) {
199 save_close(fddata);
200 sprintf(err_msg, "error: read failed (%s)\n", strerror(errno));
201 goto abort;
202 }
203 if (!len) {
204 save_close(fddata);
205 if (!term_line) {
206 sprintf(err_msg, "error: unexpected end of file during read: %s\n", strerror(errno));
207 return 1;
208 }
209 return 0;
210 }
211
212 if (msg_crc)
213 crc = calc_crc(buf, len, crc);
214
215 if (buf[len-1] == '\r') {
216 if (last_line_had_CR) {
217 if (IS_BIN_ABORT(buf, len)) {
218 /* "\r#ABORT#\r" was sent */
219 if (!fdout_is_pipe) {
220 close(fddata);
221 /* clean up */
222 unlink(filename);
223 }
224 return 1;
225 }
226 if (term_line && len == len_termline && !memcmp(buf, term_line, len_termline)) {
227 /* successfully read until termination string */
228 break;
229 }
230 }
231 last_line_had_CR = 1;
232 } else {
233 last_line_had_CR = 0;
234 }
235 if (!term_line)
236 len_read_left -= len;
237
238 if (secure_write(fddata, buf, len) == -1) {
239 save_close(fddata);
240 sprintf(err_msg, "error: write failed (%s)\n", strerror(errno));
241 goto abort;
242 }
243
244 /* nothing left? */
245 if (!term_line && len_read_left == 0L)
246 break;
247 if (is_eof) {
248 if (!term_line && len_read_left) {
249 save_close(fddata);
250 goto abort;
251 }
252 break;
253 }
254 }
255 if (crc != msg_crc) {
256 sprintf(err_msg, "Invalid crc: computed %d, expected %d.\n", crc, msg_crc);
257 /* don't unlink */
258 save_close(fddata);
259 return 1;
260 }
261 if (!fdout_is_pipe) {
262 close(fddata);
263 if (file_time != 0L) {
264 struct utimbuf utb;
265 utb.modtime = file_time;
266 utb.actime = time(NULL);
267 utime(filename, &utb);
268 }
269 }
270
271 send_on_signal = NULL;
272 return 0;
273
274 abort:
275 sleep(1);
276 write(fderr, "\r#ABORT#\r", 9);
277 return 1;
278 #undef save_close
279 }
280
281 /*---------------------------------------------------------------------------*/
282
283 int bget(void) {
284 struct strlist {
285 struct strlist *next;
286 size_t len;
287 char data[1]; /* actually a the address of char * pointer */
288 };
289
290 struct strlist *stored_file = NULL;
291 struct strlist *sl_tail = NULL;
292 struct strlist *sl;
293 struct timeval timeout;
294 struct stat statbuf;
295
296 unsigned int crc = 0;
297
298 char buf[1024];
299 fd_set readfds;
300 int fddata = fdin;
301 int len;
302 unsigned long file_size = 0;
303 unsigned long len_remains;
304 int is_eof;
305 time_t file_time = 0L;
306
307 #define save_close(x) { \
308 if (!fdin_is_pipe) \
309 close(x); \
310 }
311
312 #define store_line(s, len) { \
313 if (!(sl = (struct strlist *) malloc(sizeof(struct strlist *) + sizeof(size_t) + len))) \
314 return 1; \
315 sl->next = NULL; \
316 sl->len = len; \
317 memcpy(sl->data, s, len); \
318 if (!stored_file) { \
319 stored_file = sl; \
320 } else { \
321 sl_tail->next = sl; \
322 } \
323 sl_tail = sl; \
324 }
325
326 if (BLOCKSIZ < 1 || BLOCKSIZ > sizeof(buf))
327 BLOCKSIZ = BLOCKSIZ_DEFAULT;
328
329 init_crc();
330
331 if (!fdin_is_pipe && *filename) {
332 fddata = open(filename, O_RDONLY);
333 if (fddata == -1) {
334 sprintf(err_msg, "error: cannot open %s (%s)\n", filename, strerror(errno));
335 return 1;
336 }
337 if (!fstat(fddata, &statbuf))
338 file_time = statbuf.st_mtime;
339 else
340 file_time = time(NULL);
341
342 /* compute crc */
343 while ((len = read(fddata, buf, BLOCKSIZ)) > 0) {
344 crc = calc_crc(buf, len, crc);
345 file_size += len;
346 }
347 if (len < 0) {
348 sprintf(err_msg, "error: read failed (%s)\n", strerror(errno));
349 close(fddata);
350 return 1;
351 }
352 /* rewind */
353 if (lseek(fddata, 0L, SEEK_SET) != 0L) {
354 sprintf(err_msg, "error: file io failed on lseek() (%s)\n", strerror(errno));
355 close(fddata);
356 return 1;
357 }
358 sprintf(buf, "\r#BIN#%ld#|%d#$%s#%s\r", file_size, crc, unix_to_sfbin_date_string(file_time), get_fixed_filename(filename, file_size, crc, 1));
359 } else {
360 file_time = time(NULL);
361 if (!is_stream || do_crc_only) {
362 sprintf(err_msg, "error: not enough memory\n");
363 while ((len = read(fddata, buf, sizeof(buf))) > 0) {
364 crc = calc_crc(buf, len, crc);
365 file_size += len;
366 if (!do_crc_only)
367 store_line(buf, len);
368 }
369 if (len < 0) {
370 sprintf(err_msg, "error: read failed (%s)\n", strerror(errno));
371 close(fddata);
372 return 1;
373 }
374 *err_msg = 0;
375 sprintf(buf, "\r#BIN#%ld#|%d#$%s#%s\r", file_size, crc, unix_to_sfbin_date_string(file_time), get_fixed_filename(filename, file_size, crc, 1));
376 } else {
377 sprintf(buf, "\r#BIN###$%s#%s\r", unix_to_sfbin_date_string(file_time), get_fixed_filename(filename, 0, 0, 1));
378 }
379 /*
380 * hack: check for #ABORT# from fdout (fd 1), because fddata (fd 0) is
381 * our pipe we read the data from, which we actually tx.
382 * believe me, it does work.
383 */
384 fdin = fdout;
385 }
386
387 if (do_crc_only) {
388 printf("File information for %s:\n", get_fixed_filename(filename, file_size, crc, 1));
389 printf(" size %ld bytes, crc %d, date %s (%ld)\n", file_size, crc, unix_to_sfbin_date_string(file_time), file_time);
390 return 0;
391 }
392
393 send_on_signal = bin_send_abort_on_sig;
394 if (secure_write(fdout, buf, strlen(buf)) == -1) {
395 sprintf(err_msg, "error: write failed (%s)\n", strerror(errno));
396 save_close(fddata);
397 return 1;
398 }
399
400 /* wait for answer */
401 for (;;) {
402 /*
403 * make sure we do not read from a pipe. fdout is also
404 * assigned to the tty
405 */
406 len = my_read(fdout, buf, sizeof(buf), &is_eof, "\r\n");
407 if (is_eof || len < 1) {
408 sprintf(err_msg, "error: read failed (%s)\n", strerror(errno));
409 save_close(fddata);
410 return 1;
411 }
412 if (buf[len-1] == '\n') {
413 #if 0
414 char *p = "warning: <LF> received. not 8bit clean?\r";
415 secure_write(fdout, p, strlen(p));
416 #endif
417 sprintf(err_msg, "bad EOL: <LF>\n");
418 goto abort;
419 } else if (buf[len-1] != '\r') {
420 sprintf(err_msg, "line to long\n");
421 continue;
422 }
423 if (IS_BIN_OK(buf, len))
424 break;
425 if (IS_BIN_NO(buf, len)) {
426 save_close(fddata);
427 return 0;
428 }
429 if (IS_BIN_ABORT(buf, len)) {
430 sprintf(err_msg, "Aborted by user request\n");
431 save_close(fddata);
432 return 1;
433 }
434 }
435
436 len_remains = file_size;
437 timeout.tv_sec = 0;
438 timeout.tv_usec = 0;
439
440 for (;;) {
441 char *p_buf;
442
443 /* check for user \r#ABORT#\r on tty stream */
444 FD_ZERO(&readfds);
445 FD_SET(fdin, &readfds);
446 if (select(fdin+1, &readfds, NULL, NULL, &timeout) &&
447 FD_ISSET(fdin, &readfds)) {
448 len = read(fdin, buf, sizeof(buf));
449 if (len < 0) {
450 sprintf(err_msg, "read from tty failed (%s)\n", strerror(errno));
451 save_close(fddata);
452 goto abort;
453 }
454 if (IS_BIN_ABORT(buf, len)) {
455 sprintf(err_msg, "Aborted by user request\n");
456 save_close(fddata);
457 return 1;
458 }
459 }
460 /* read data */
461 if (!fdin_is_pipe || is_stream) {
462 p_buf = buf;
463 len = my_read(fddata, buf,
464 ((len_remains > BLOCKSIZ || is_stream) ? BLOCKSIZ : len_remains),
465 &is_eof, NULL);
466 if (len < 1) {
467 save_close(fddata);
468 if (len < 0) {
469 sprintf(err_msg, "error: read failed (%s)\n", strerror(errno));
470 goto abort;
471 }
472 break;
473 }
474 len_remains -= len;
475 } else {
476 p_buf = stored_file->data;
477 len = stored_file->len;
478 }
479 /* write to client */
480 if (secure_write(fdout, p_buf, len) == -1) {
481 sprintf(err_msg, "error: write failed (%s)\n", strerror(errno));
482 save_close(fddata);
483 goto abort;
484 }
485 if (fdin_is_pipe && !is_stream) {
486 sl = stored_file;
487 stored_file = stored_file->next;
488 if (!stored_file)
489 is_eof = 1;
490 free(sl);
491 }
492 if (!fdin_is_pipe && !len_remains) {
493 if (read(fddata, buf, 1) == 1) {
494 sprintf(err_msg, "Warning: file has grown in the meantime\n");
495 }
496 is_eof = 1;
497 break;
498 }
499 /*
500 * need this because my_read may returned lenth != 0 (data to be written)
501 * but also has detected EOF.
502 */
503 if (is_eof)
504 break;
505 }
506
507 sleep(10);
508
509 return 0;
510
511 abort:
512 sleep(1);
513 write(fderr, "\r#ABORT#\r", 9);
514 return 1;
515 #undef save_close
516 }