File: receive.c

package info (click to toggle)
sendfile 2.1-20.3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,304 kB
  • ctags: 695
  • sloc: ansic: 12,371; sh: 2,569; perl: 790; makefile: 136; java: 36
file content (1933 lines) | stat: -rw-r--r-- 54,129 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
/*
 * File:	receive.c
 *
 * Author:	Ulli Horlacher (framstag@rus.uni-stuttgart.de)
 *
 * Contrib.:	Christian Recktenwald (chris@yeti.faveve.uni-stuttgart.de)
 * 		Rainer Bawidamann (widi@sol.wohnheim.uni-ulm.de)
 * 		Beate Herrmann (beate@juhu.lake.de)
 * 		Stefan Zehl (sec@42.org)
 *
 * History:	
 * 
 *   11 Aug 95	Framstag	initial version
 *   14 Aug 95	Framstag	corrected bug when receiving archives
 *   10 Sep 95	Framstag	extracted functions to spool.c
 *   31 Oct 95	Framstag	fixed security problems
 *    5 Nov 95	Framstag	added NeXT support
 *    7 Nov 95	Framstag	corrected bug when receiving to
 *                              a non-writable directory
 *    2 Dec 95	Framstag	added rename option
 *   15 Jan 96	Framstag	better error handling
 *    6 Feb 96	Framstag	added ATTRIBUTE=EXE
 *    7 Feb 96	Chris		better Convex-OS support
 *   21 Feb 96	widi		better Solaris-2 support
 *   22 Feb 96	Framstag	added bounce (forward) option
 *    6 Mar 96	Framstag	moved time stamp setting to get_date
 *   17 Mar 96	Framstag	some bug fixes
 *   27 Mar 96	Framstag	added quiet mode: no questions asked
 *   28 Mar 96	Framstag	extended search for support programs
 *    1 Apr 96	Framstag	corrected forking bug
 *    2 Apr 96	Framstag	added verbose bouncing
 *    4 Apr 96	Framstag	added check for dangerous file names in archives
 *    5 Apr 96	Framstag	correction: 1 KB is now 1024 bytes
 *    8 Apr 96	Framstag	added question for renaming better dangerous 
 *				file name checking
 *   12 Apr 96	Framstag	added pgp support
 *   13 Apr 96	Framstag	added -f from option
 *   13 Apr 96	Beate		added -s list option
 *   18 Apr 96	Framstag	delete empty file when pgp fails
 *   20 Apr 96	Framstag	added preserve option for pgp and tar
 *				added -k keep option
 *   22 Apr 96	Framstag	some code cleanup
 *   23 Apr 96	Framstag	moved fcopy to io.c
 *   24 Apr 96	Framstag	changed bouncing option syntax
 *    2 May 96	Framstag	better tar error checking
 *   12 May 96	Framstag	better checking of not complete files
 *   23 May 96	Framstag	added check for writeable tmp-files
 *   26 May 96	Framstag	fixed bug with overwriting links
 * 		                fixed bug with returning from fork
 *   24 Jun 96	Framstag	added -P to stdout option
 *   13 Sep 96	Framstag	added rename option to "."-file checking
 *   26 Nov 96	Framstag	substituted all gets() with fgets()
 *   27 Nov 96	Framstag	corrected bug when receiving file is not 
 *                              writable
 *    1 Dec 96	Framstag	better file name questioning
 *   20 Dec 96	Framstag	don't ask for directory overwriting
 *   20 Jan 97	GNUish         modified to reflect the GNUs needs
 *   12 Feb 97	Framstag	fixed boolean bugs in receive_sf
 *   20 Feb 97	GNUish         renamed error to error_log to support glibc
 *   23 Feb 97	Framstag	modified str_* function names
 *   				extended with TYPE=MIME
 *   24 Feb 97	Framstag	sprintf() -> snprintf()
 *   23 Jun 97	Framstag	added readline support
 *    1 Aug 97	Framstag	added -S secure option (signature check)
 *    7 Aug 97	Framstag	no tar-file checking with quiet-mode
 *   27 Aug 97	Framstag	ignore symlink errors when extracting tar files
 *   21 Sep 97	Framstag	added multi-spool support and -Z option
 *   30 Sep 97	Framstag	corrected list bug
 *    6 Oct 97	Framstag	corrected bug with -n -b options together
 *   14 Nov 97	Framstag	better verbose mode for system() calls
 *    2 Dec 97	Framstag	fixed -b -a conjunction bug
 *   11 Dec 97	Framstag	added bzip2 support
 *   16 Dec 97	Sec		better xhoppel support for BSD
 *   11 Mar 98	Framstag	added -H option to display the headers
 *   10 May 98	Framstag	added -R option to renumber the spool files
 *  1999-02-15	Framstag	added check for dangerous symbolic links in 
 *				archive files
 *
 * The receive client of the sendfile package.
 * Receives, lists and deletes files from the sendfile spool.
 *
 * Copyright  1995-1999 Ulli Horlacher
 * This file is covered by the GNU General Public License
 */

#include "config.h"   /* various #defines */

#ifdef ULTRIX
  #define S_IFMT   0170000 /* type of file */
  #define S_IFDIR  0040000 /* directory */
#endif

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <pwd.h>
#include <time.h>
#include <utime.h>
#include <ctype.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>

#include "message.h"    /* information, warning and error messages */
#include "utf7.h"       /* UTF-7 coding */
#include "io.h"		/* read/write routines */
#include "string.h"     /* extended string functions */
#include "spool.h"	/* operations on files in the sendfile spool */
#include "getline.h"	/* get a line of text from stdin */
#include "lock.h"	/* file locking */

#if defined(HAVE_GETOPT_H)
  #include <getopt.h>
#else
  int getopt(int, char * const *, const char *);
  extern int opterr;
  extern int optind;
  extern int optopt;
  extern char *optarg;
#endif

#ifndef AIX
  #ifndef CONVEXOS
    FILE *popen(const char *, const char *);
  #endif
  int pclose(FILE *);
#endif


/* global variables */
char 
  *lll,			/* last list line */
  *prg,			/* name of the game */
  *pgpvm,		/* pgp verbose mode string */
  *verbose,		/* verbose mode on bouncing files */
  error_log[MAXLEN],	/* error log file */
  tartmp[MAXLEN],	/* tar temporary file */
  userspool[MAXLEN],	/* user spool directory */
  pgp_bin[MAXLEN],	/* the pgp binary */
  tar_bin[MAXLEN],	/* the tar binary */
  gzip_bin[MAXLEN],	/* the gzip binary */
  bzip2_bin[MAXLEN],	/* the bzip2 binary */
  recode_bin[MAXLEN],	/* the recode binary */
  metamail_bin[MAXLEN];	/* the metamail binary */
int 
  keep=0,		/* flag for keeping files in spool */
  pipeout=0,		/* flag for receiving to stdout (via pipe) */
  quiet=0,		/* quiet mode: no questions asked */
  client=1,		/* flag to determine client or server */
  ren=0,		/* flag for renaming files */
  pgppass=0,		/* flag if the pgp password is set as an env variable */
  preserve=0,		/* preserve pgp and tar attributes */
  nometamail=0;		/* metamail-usage flag */
mode_t 
  cmask;		/* umask value */


/* clean termination routine */
void cleanexit();

/* delete tmp-file */
void cleanup();

/* print short help usage text */
int usage(void);

/* list all spool files */
int list(struct senderlist *, int, char *, char *);

/* receive a spool file */
void receive_sf(struct filelist *, char *, int);

/* check what to do when file with same name already exists */
int checkfile(int, char *, char *, char *, char *);

/* create detached pgp signature file */
int create_sigfile(const char *, const char *, const char *, char *);

/* translate NVT format file to Unix text format file */
void crlf2lf(FILE *, FILE *, const char *, const char *);

/* spawn a subprocess and direct output to a file */
int spawn(char **, const char *, mode_t);

/* shameless plug from GNU fileutils (getdate.c) :-) */
time_t get_date(char *, void *);

/* check or print pgp signature */
int check_signature(struct filelist *, char *, int);

/* renumber spool files */
void renumber(struct senderlist *);


int main(int argc, char *argv[]) {
  int
    i,			/* simple loop count */
    number,		/* flag for specifying a spool file number */
    listformat,		/* format of listing */
    del,		/* flag for deleting */
    all,		/* flag for receiving all */
    header,		/* flag for receiving only the header (to stdout) */
    id,			/* spool id */
    found,		/* flag for found spool id */
    opt;		/* option to test for */
  char
    *cp,		/* a simple string pointer */
    bounce[FLEN],	/* bounce address */
    from[FLEN],		/* from which sender */
    pgpring[FLEN],	/* flag for receiveing only signed files */
    tmpdir[FLEN],	/* directory for temporary files */
    tmp[3*MAXLEN],	/* temporary string */
    fname[MAXLEN],	/* displayble file name */
    bouncelist[MAXLEN],	/* list of spool files to bounce */
    pattern[MAXLEN];	/* file name pattern to search for */
  struct stat finfo;	/* information about a file */
  struct passwd *pwe;	/* password entry */
  struct filelist *flp;	/* file list pointer */
  struct senderlist
    *sls,		/* sender list start */
    *slp;		/* sender list pointer */
  FILE *inf;

  del=0;
  all=0;
  found=0;
  number=0;
  header=0;
  listformat=0;
  lll="";
  verbose="";
  *bounce=0;
  *pgpring=0;
  *userspool=0;
  *bouncelist=0;
  strcpy(from,"*");
  cmask=umask(0);
  umask(cmask);

  /* get program name */
  prg=argv[0];
  if ((cp=strrchr(prg,'/'))) prg=cp+1;

  /* get pgp password environment variable */
  if (getenv("PGPPASS")) {
    pgppass=1;
    pgpvm="+verbose=0";
  } else {
    pgppass=0;
    pgpvm="+verbose=1";
  }

  /* get metamail usage environment variable */
  if (getenv("NOMETAMAIL")) nometamail=1;
    
  /* scan the command line */
  while ((opt=getopt(argc,argv,"h?lLsndarRVvqpkHPS:f:Z:b:")) > 0) {
    switch (opt) {
	case ':':
        case 'h':
        case '?': exit(usage());
        case 's': listformat=1; break;
        case 'l': listformat=2; break;
        case 'L': listformat=3; break;
        case 'n': number=1; break;
        case 'r': ren=1; break;
        case 'R': ren=2; break;
        case 'd': del=1; break;
        case 'a': all=1; break;
        case 'P': pipeout=1; break;
        case 'k': keep=1; break;
        case 'q': quiet=1; break;
        case 'p': preserve=1; break;
        case 'H': header=1; break;
        case 'S': snprintf(MAXS(pgpring),"%s",optarg); break;
        case 'v': verbose="-v"; break;
        case 'f': snprintf(MAXS(from),"%s",optarg); break;
        case 'b': snprintf(MAXS(bounce),"%s",optarg); break;
        case 'Z': snprintf(MAXS(userspool),"%s",optarg); break;
        case 'V': message(prg,'I',"version "VERSION" revision "REVISION"");
	          exit(0);
    }
  }

  /* no arguments? */
  if (optind==argc && !all && !listformat) {
    listformat=2;
    lll="Type \"receive -a\" to receive all files or \"receive -h\" "
        "for a short help.\n\n";
  }

  /* too few arguments? */
  if ((argc-optind==0 && !all && ren<2 &&
       (ren || del || (!str_eq(from,"*") && !listformat))) ||
      (argc-optind>0 && ((*bounce && all) || ren==2)) ||
      (argc-optind<1 && *bounce && !all))
    exit(usage());

  /* get the own user name */
  if ((pwe=getpwuid(getuid())) == NULL)
    message(prg,'F',"cannot determine own user name");

  /* determine the spool directory */
  if (!*userspool) {
    if ((cp=getenv("SF_SPOOL"))) {
      snprintf(MAXS(userspool),"%s",cp);
    } else {
      snprintf(MAXS(userspool),"%s/.sfspool",pwe->pw_dir);
      if (stat(userspool,&finfo)<0 || !(finfo.st_mode&S_IFDIR))
	snprintf(MAXS(userspool),SPOOL"/%s",pwe->pw_name);
    }
  }
  if (*userspool=='Z') snprintf(MAXS(userspool),SPOOL"/%s",pwe->pw_name);

  /* does the spool directory exist? */
  if (stat(userspool,&finfo)<0 || (finfo.st_mode&S_IFMT)!=S_IFDIR) {
    snprintf(MAXS(tmp),"spool directory %s does not exist",userspool);
    errno=0;
    message(prg,'E',tmp);
    exit(1);
  }

  /* correct permissions for the spool directory? */
  if (!(finfo.st_mode&S_IRWXU) || finfo.st_uid!=getuid()) {
    snprintf(MAXS(tmp),
	     "no access to spool directory %s (wrong permissions)",
	     userspool);
    errno=0;
    message(prg,'F',tmp);
    exit(1);
  }

  /* get tmpdir */
  if ((cp=getenv("TMPDIR")))
    strcpy(tmpdir,cp);
  else
    strcpy(tmpdir,"/tmp");
  if (stat(tmpdir,&finfo)<0 || access(tmpdir,R_OK|W_OK|X_OK)<0)
    strcpy(tmpdir,pwe->pw_dir);
  
  if (*verbose) {
    snprintf(MAXS(tmp),"directory for temporary files is: %s",tmpdir);
    message(prg,'I',tmp);
  }

  /* set file names */
  snprintf(MAXS(tartmp),"%s/.receive_%d.tar",tmpdir,(int)getpid());
  snprintf(MAXS(error_log),"%s/receive_%d.error",userspool,(int)getpid());

  /* check tmp files */
  unlink(tartmp);
  unlink(error_log);
  if (stat(tartmp,&finfo)==0) {
    snprintf(MAXS(tmp),
	     "tmp-file %s does already exist and cannot be deleted",tartmp);
    unlink(tartmp);
    message(prg,'F',tmp);
  }
  if (stat(error_log,&finfo)==0) {
    snprintf(MAXS(tmp),
	     "error-logfile %s does already exist and cannot be deleted",
	     error_log);
    unlink(error_log);
    message(prg,'F',tmp);
  }

  /* are there any files to receive? */
  sls=scanspool(from);
  if (sls==NULL) {
    snprintf(MAXS(tmp),"no files in spool directory %s",userspool);
    message(prg,'W',tmp);
    exit(1);
  }

  if (ren==2) {
    renumber(sls);
    sls=scanspool("");
  }

  /* set log file read status (st_atime) for xhoppel */
  snprintf(MAXS(tmp),"%s/log",userspool);
  inf=fopen(tmp,"r");
  if (inf) {
    fgetl(tmp,inf);
    fclose(inf);
  }

  /* incompatible options? */
  if (*bounce) {
    if (listformat||ren||del||preserve)
      message(prg,'W',"you cannot use any other option "
		      "when bouncing a file - ignored");
    listformat=ren=del=0;
  }

  if (!str_eq(from,"*") && number)
      message(prg,'W',"ignoring -f option when specifying a spool number");

  if (del&&keep)
      message(prg,'F',"you cannot delete and keep a file at the same time");

  /* support programs defaults */
  strcpy(pgp_bin,PGP);
  strcpy(tar_bin,TAR);
  strcpy(gzip_bin,GZIP);
  strcpy(bzip2_bin,BZIP2);
  strcpy(recode_bin,RECODE);
  strcpy(metamail_bin,RECODE);

  /* look for environment variables */
  if ((cp=getenv("SF_PGP")))	  strcpy(pgp_bin,cp);
  if ((cp=getenv("SF_TAR")))	  strcpy(tar_bin,cp);
  if ((cp=getenv("SF_GZIP")))	  strcpy(gzip_bin,cp);
  if ((cp=getenv("SF_BZIP2")))	  strcpy(bzip2_bin,cp);
  if ((cp=getenv("SF_RECODE")))   strcpy(recode_bin,cp);
  if ((cp=getenv("SF_METAMAIL"))) strcpy(metamail_bin,cp);

  /* do the support programs really exist? */
  if (access(pgp_bin,X_OK)<0)	   strcpy(pgp_bin,"pgp");
  if (access(tar_bin,X_OK)<0)	   strcpy(tar_bin,"tar");
  if (access(gzip_bin,X_OK)<0)	   strcpy(gzip_bin,"gzip");
  if (access(bzip2_bin,X_OK)<0)	   strcpy(bzip2_bin,"bzip2");
  if (access(recode_bin,X_OK)<0)   strcpy(recode_bin,"recode");
  if (access(metamail_bin,X_OK)<0) strcpy(metamail_bin,"metamail");

  /* enable simple interrupt handler */
  signal(SIGTERM,cleanexit);
  signal(SIGABRT,cleanexit);
  signal(SIGQUIT,cleanexit);
  signal(SIGHUP,cleanexit);
  signal(SIGINT,cleanexit);

  /* list files? */
  if (listformat) {
    if (list(sls,listformat,from,pgpring)<0) {
      snprintf(MAXS(tmp),"no files in spool directory %s",userspool);
      message(prg,'W',tmp);
    }
    exit(0);
  }

  /* number specified? */
  if (number) {
   
    /* loop over all args */
    for (i=optind; i<argc; i++) {
      id=atoi(argv[i]);

      /* loop over sender list */
      for (slp=sls, found=0; slp!=NULL && found==0; slp=slp->next) {
       
	/* loop over files list */
        for (flp=slp->flist; flp!=NULL && found==0; flp=flp->next) {
	 
          /* spool id found and spool file complete? */
	  if (flp->id==id && flp->csize==flp->tsize) {
	   
	    /* delete, bounce or receive spool file? */
	    if (del)
	      delete_sf(flp,1);
	    else if (*bounce) {
	      if (*bouncelist) {
		snprintf(MAXS(tmp),"%s %d",bouncelist,id);
		strcpy(bouncelist,tmp);
	      } else
		snprintf(MAXS(bouncelist),"%d",id);
	    } else 
	      receive_sf(flp,pgpring,header);

	    found=1;

	  }
	}
      }

      /* not found? */
      if (!found && id) {
        snprintf(MAXS(tmp),"spool file #%d not found",id);
	message(prg,'W',tmp);
      }

    }

  }  else  /* file name specified */ {
   
    /* loop over all args */
    for (i=optind; i<argc || all; i++) {
      if (all)
        strcpy(pattern,"*");
      else
      	strcpy(pattern,argv[i]);

      /* loop over sender list */
      for (slp=sls; slp!=NULL; slp=slp->next) {
       
	/* loop over files list */
	for (flp=slp->flist; flp!=NULL; flp=flp->next) {
	 
	  /* spool file incomplete? */
	  if (flp->csize!=flp->tsize) continue;

	  /* translate UTF-7 name to the displayable file name */
	  utf2iso(1,NULL,fname,NULL,flp->fname);

	  /* match? */
	  if (simplematch(fname,pattern,0)==1) {
	   
	    /* delete, bounce or receive spool file? */
	    if (del)
	      delete_sf(flp,1);
	    else if (*bounce) {
	      if (*bouncelist) {
		snprintf(MAXS(tmp),"%s %d",bouncelist,flp->id);
		strcpy(bouncelist,tmp);
	      } else
		snprintf(MAXS(bouncelist),"%d",flp->id);
	    } else
	      receive_sf(flp,pgpring,header);

	    found=1;

	  }
	}
      }

      /* not found? */
      if (!found && !all) {
        snprintf(MAXS(tmp),"file %s not found",pattern);
	message(prg,'W',tmp);
      }

      all=0;

    }
  }

  /* files to bounce? */
  if (*bounce && *bouncelist) {
    if (keep)
      snprintf(MAXS(tmp),
	       "sendfile -bk=y %s %s %s",verbose,bouncelist,bounce);
    else
      snprintf(MAXS(tmp),
	       "sendfile -bk=n %s %s %s",verbose,bouncelist,bounce);
    if (*verbose) printf("call: %s\n",tmp);
    system(tmp);
  }

  cleanup();
  exit(0);
}


/*
 * list - list spool files with attributes
 *
 * INPUT:  sls		- sender list start
 *         format	- format of listing
 * 	   from		- select from user
 *         pgpring	- pgp ring file
 *
 * RETURN: 0 if files found, -1 if no files to list
 */
int list(struct senderlist *sls, int format, char *from, char *pgpring) {
  int
    i,				/* simple loop counter */
    found,			/* flag for files found */
    fff;			/* first file flag */
  char
    *cp1,*cp2,	    		/* simple character pointer */
    line[MAXLEN],		/* line to read in */
    showtar[MAXLEN],		/* shell command to look inside tar archive */
    show_fname[MAXLEN];		/* file name to display */
  struct senderlist *slp;	/* sender list pointer */
  struct filelist *flp;		/* file list pointer */
  FILE *pp;			/* pipe input stream */

  found=0;
  
  /* loop over sender list */
  for (slp=sls; slp!=NULL; slp=slp->next) {
   
    /* match this sender? */
    if (simplematch(slp->from,from,1)==1) {
     
      /* loop over files list */
      for (flp=slp->flist,fff=1; flp!=NULL; flp=flp->next) {
       
	/* not complete? */
	if (flp->csize!=flp->tsize) continue;

	if (!found && format>1) 
	  printf("\nFiles in spool directory %s :\n",userspool);
	
	found=1;
	
	/* first file from this sender? */
	if (fff) {
	  fff=0;

	  /* print from header */
	  printf("\nFrom %s\n",slp->from);
	  for (i=1; i<80 && i<=strlen(slp->from)+5; i++) printf("-");
	  printf("\n");

	}

	/* print spool file informations */
	utf2iso(1,NULL,show_fname,NULL,flp->fname);
	printf("%3d) %s  %8lu KB  %s",
	       flp->id,flp->rdate,(flp->osize+1023)/1024,show_fname);

	if (format>1) {
	 
	  /* tar archive, MIME file or encrypted? */
	  if (flp->flags&F_TAR && flp->flags&F_CRYPT)
	    printf(" (archive,encrypted)");
	  else if (flp->flags&F_MIME && flp->flags&F_CRYPT)
	    printf(" (MIME,encrypted)");
	  else if (flp->flags&F_MIME)
	    printf(" (MIME)");
	  else if (flp->flags&F_TAR)
	    printf(" (archive)");
	  else if (flp->flags&F_CRYPT)
	    printf(" (encrypted)");
	}

	printf("\n");

	/* check signature */
	if (*(flp->sign) && format>1) check_signature(flp,pgpring,1);
	
	/* comment available? */
	cp1=flp->comment;
	if (*cp1 && format>1) {
	  while ((cp2=strchr(cp1,'\n'))) {
	    *cp2=0;
	    printf("     \"%s\"\n",cp1);
	    cp1=cp2+1;
	  }
	  printf("     \"%s\"\n",cp1);
	}

	/* on verbose mode look inside tar */
	if (flp->flags&F_TAR && format==3 &&
	    !(flp->flags&F_CRYPT && (quiet || preserve) && !pgppass)) {
	 
	  /* encrypted, compressed or normal tar file? */
	  if (flp->flags&F_CRYPT)
	    snprintf(MAXS(showtar),"%s %s -f < %s/%d.d | %s tvf -",
		      pgp_bin,pgpvm,userspool,flp->id,tar_bin);
	  else if (flp->flags&F_COMPRESS) {
	    if (str_eq(flp->compress,S_BZIP2))
	      snprintf(MAXS(showtar),"%s -d < %s/%d.d | %s tvf -",
		       bzip2_bin,userspool,flp->id,tar_bin);
	    else
	      snprintf(MAXS(showtar),"%s -d < %s/%d.d | %s tvf -",
		       gzip_bin,userspool,flp->id,tar_bin);
	  } else
	    snprintf(MAXS(showtar),"%s tvf %s/%d.d",tar_bin,userspool,flp->id);

	  /* sneak inside... */
	  if (*verbose) printf("call: %s\n",showtar);
	  if ((pp=popen(showtar,"r")) == NULL)
	    message(prg,'E',"contents of archive is not accessible");
	  else {
	    if (flp->flags&F_CRYPT && !pgppass && fgets(line,MAXLEN-1,pp))
	      printf("\n\n     %s",line);
	    while (fgets(line,MAXLEN-1,pp)) printf("     %s",line);
	    if (flp->flags&F_CRYPT && !pgppass) printf("\n");
	  }
	  pclose(pp);

	}
      }
    }
  }

  if (found) {
    printf("\n%s",lll);
    return(0);
  }
  else
    return(-1);

}


/*
 * receive_sf - receive a spool file
 *
 * INPUT:  flp     - file list element
 *         pgpring - pgp ring file (force pgp signature checking)
 * 	   header  - flag for receiving onnly the header to stdout
 */
void receive_sf(struct filelist *flp, char *pgpring, int header) {
  int
    utf,			/* return code from utf2iso */
    terr,			/* tar error flag */
    tom;			/* tar overwrite message flag */
  char
    *cp,			/* simple character pointer */
    *sad[10],			/* spawn argument descriptor */
    answer[FLEN],		/* answer string */
    line[MAXLEN],		/* one line of text */
    tmp[2*MAXLEN],		/* temporary string */
    tmpfile[MAXLEN],		/* temporary file name */
    cmd[2*MAXLEN],		/* command string for system() */
    sfile[MAXLEN],		/* spool data file */
    danger[OVERSIZE],		/* dangerous file names */
    fname[MAXLEN],		/* file name to write */
    nname[MAXLEN],		/* file name in display format */
    sname[MAXLEN];		/* file name in shell handling format */
  static char
    overwrite='n';		/* overwrite mode */
  struct stat finfo;		/* information about a file */
  struct utimbuf utb;		/* binary time */
  time_t dummy;			/* dummy arg for localtime() */
  FILE
    *pp,			/* pipe input stream */
    *inf,			/* spool data file to read */
    *outf;			/* file to create */

  tom=0;
  terr=0;
  *danger=0;
  *answer=0;

  /* oops? */
  if (flp==NULL) return;

  /* translate UTF-7 file name */
  utf=utf2iso(1,fname,nname,sname,flp->fname);
  fname[FLEN-1]=nname[FLEN-1]=sname[FLEN-1]=0;
  
  if (*pgpring) {
    switch (check_signature(flp,pgpring,0)) {
	case 1:  break;
	case 0:  snprintf(MAXS(tmp),"no signature found for '%s'",nname);
                 errno=0;
                 message(prg,'E',tmp);
                 return;
	case -1: snprintf(MAXS(tmp),"no public key found to check "
			  "signature for '%s'",nname);
                 errno=0;
                 message(prg,'E',tmp);
                 return;
	case -2: snprintf(MAXS(tmp),"bad signature for '%s'",nname);
                 errno=0;
                 message(prg,'E',tmp);
		 return;
        default: return;
    }
  }

  /* determine overwrite mode */
  if (quiet) overwrite='Y';
  if (!strchr("YNS",overwrite)) overwrite='n';

  (void) localtime(&dummy);

  /* show only the header? */
  if (header) {
    snprintf(MAXS(tmp),"%s/%d.h",userspool,flp->id);
    printf("%d) %s\n",flp->id,nname);
    inf=fopen(tmp,"r");
    while (fgets(line,MAXLEN-1,inf)) printf("%s",line);
    printf("\n");
    fclose(inf);
    return;
  }
  
  /* pipe to stdout? */
  if (pipeout) {
    
    /* encrypted spool file? */
    if (flp->flags&F_CRYPT) {
      snprintf(MAXS(cmd),"%s %s -f < %s/%d.d",pgp_bin,pgpvm,userspool,flp->id);
      if (*verbose) printf("call: %s\n",cmd);
      if (system(cmd)!=0) {
        errno=0;
	snprintf(MAXS(tmp),"cannot decrypt '%s' :",nname);
	message(prg,'E',tmp);
	return;
      }
    }
    /* compressed spool file? */
    else if (flp->flags&F_COMPRESS) {
      if (str_eq(flp->compress,S_BZIP2))
	snprintf(MAXS(cmd),"%s -d < %s/%d.d",bzip2_bin,userspool,flp->id);
      else
	snprintf(MAXS(cmd),"%s -d < %s/%d.d",gzip_bin,userspool,flp->id);
      if (*verbose) printf("call: %s\n",cmd);
      if (system(cmd)!=0 && !(flp->flags&F_TAR)) {
        errno=0;
	snprintf(MAXS(tmp),"cannot decompress '%s' :",nname);
	message(prg,'E',tmp);
	return;
      }
    }
    else /* copy spool file to stdout */ {
      
      /* copy file */
      snprintf(MAXS(tmp),"%s/%d.d",userspool,flp->id);
      if (fcopy(tmp,"",0)<0) {
        errno=0;
	snprintf(MAXS(tmp),"cannot read '%s'",nname);
        message(prg,'E',tmp);
	return;
      }

    }

    /* delete tar spool file if required */
    if (!keep) delete_sf(flp,0);

    return;
  }

  /* tar file to receive? */
  if (flp->flags&F_TAR) {
   
    /* save file in transfer shape? */
    if (preserve) {
     
      /* set new file name */
      fname[FLEN-12]=nname[FLEN-12]=sname[FLEN-12]=0;
      strcat(fname,".tar");
      strcat(nname,".tar");
      strcat(sname,".tar");
      if (flp->flags&F_COMPRESS) {
        strcat(fname,".gz");
	strcat(nname,".gz");
	strcat(sname,".gz");
      }
      if (flp->flags&F_CRYPT) {
        strcat(fname,".pgp");
	strcat(nname,".pgp");
	strcat(sname,".pgp");
      }

      /* if file with same name already exists check what to do */
      if (!quiet && checkfile(utf,fname,nname,sname,&overwrite)) return;

      /* copy file */
      snprintf(MAXS(tmp),"%s/%d.d",userspool,flp->id);
      if (fcopy(tmp,fname,0666&~cmask)<0) {
        snprintf(MAXS(tmp),"cannot receive '%s'",nname);
	errno=0;
        message(prg,'E',tmp);
	return;
      }

      /* pgp signature to save? */
      create_sigfile(flp->sign,fname,nname,&overwrite);

      if (!keep) delete_sf(flp,0);
      snprintf(MAXS(tmp),"'%s' received",nname);
      message(prg,'I',tmp);

      return;
    }

    /* encrypted tar spool file? */
    if (flp->flags&F_CRYPT) {
      
      snprintf(MAXS(cmd),"%s %s -f < %s/%d.d > %s",
	      pgp_bin,pgpvm,userspool,flp->id,tartmp);

      /* create temporary decrypted tar file */
      if (*verbose) printf("call: %s\n",cmd);
      system(cmd);
      if (stat(tartmp,&finfo)<0 || finfo.st_size==0) {
        errno=0;
	snprintf(MAXS(tmp),"cannot decrypt '%s' :",nname);
	message(prg,'E',tmp);
	return;
      }

      if (!pgppass) printf("\n\n");

    }

    /* test on (dangerous) symbolic links in tar file */

    /* compressed, encrypted or normal tar file? */
    if (flp->flags&F_COMPRESS) {
      if (str_eq(flp->compress,S_BZIP2))
	snprintf(MAXS(cmd),"%s -d < %s/%d.d | %s tvf -",
		 bzip2_bin,userspool,flp->id,tar_bin);
      else
	snprintf(MAXS(cmd),"%s -d < %s/%d.d | %s tvf -",
		 gzip_bin,userspool,flp->id,tar_bin);
    } else if (flp->flags&F_CRYPT) {
      snprintf(MAXS(cmd),"%s tvf %s",tar_bin,tartmp);
    } else {
      snprintf(MAXS(cmd),"%s tvf %s/%d.d",tar_bin,userspool,flp->id);
    }

    /* open pipe to read tar file-info */
    if (*verbose) printf("call: %s\n",cmd);
    if ((pp=popen(cmd,"r")) == NULL) {
      message(prg,'E',"cannot open spool file for reading");
      return;
    }

    /* loop over all files in tar archive */
    while (fgets(line,MAXLEN-1,pp)) {
      if ((cp=strchr(line,'\n'))) *cp=0;
     
      /* is it a (dangerous) symbolic link? */
      if (*line == 'l') {
	if (strlen(danger)+strlen(line)+4 > sizeof(danger)) {
	  snprintf(MAXS(tmp),
		   "archive %s has too many dangerous file names inside",
		   nname);
	  message(prg,'F',tmp);
	}
	strcat(danger,"\n  ");
	strcat(danger,line);
      }

    }

    pclose(pp);

    /* test on dangerous file namess in tar file */

    /* compressed, encrypted or normal tar file? */
    if (flp->flags&F_COMPRESS) {
      if (str_eq(flp->compress,S_BZIP2))
	snprintf(MAXS(cmd),"%s -d < %s/%d.d | %s tf -",
		 bzip2_bin,userspool,flp->id,tar_bin);
      else
	snprintf(MAXS(cmd),"%s -d < %s/%d.d | %s tf -",
		 gzip_bin,userspool,flp->id,tar_bin);
    } else if (flp->flags&F_CRYPT) {
      snprintf(MAXS(cmd),"%s tf %s",tar_bin,tartmp);
    } else {
      snprintf(MAXS(cmd),"%s tf %s/%d.d",tar_bin,userspool,flp->id);
    }

    /* open pipe to read tar file-info */
    if (*verbose) printf("call: %s\n",cmd);
    if ((pp=popen(cmd,"r")) == NULL) {
      message(prg,'E',"cannot open spool file for reading");
      return;
    }

    /* loop over all files in tar archive */
    while (fgets(fname,MAXLEN-1,pp)) {
     
      /* does the file already exist? */
      if ((cp=strchr(fname,'\n'))) *cp=0;
      if (overwrite!='Y' && stat(fname,&finfo)==0 && !S_ISDIR(finfo.st_mode)) {
        if (!tom) printf("Archive '%s' will overwrite:\n",nname);
        tom=1;
        printf("  %s\n",fname);
      }

      /* is it a dangerous file name? */
      if (strstr(fname,"../") || strstr(fname,"/.") || fname[0]=='/' ||
	  (fname[0]=='.' && fname[1]!='/')) {
	if (strlen(danger)+strlen(fname)+4 > sizeof(danger)) {
	  snprintf(MAXS(tmp),
		   "archive %s has too many dangerous file names inside",
		   nname);
	  message(prg,'F',tmp);
	}
        strcat(danger,"\n  ");
	strcat(danger,fname);
      }

    }

    pclose(pp);

    /* ask user for overwriting */
    if (tom && overwrite!='Y') {
      printf("Overwrite (yYnN)? ");
      fgets(answer,FLEN-1,stdin);
      overwrite=answer[0];
      if (toupper(overwrite)!='Y') return;
    }

    /* ask user for extracting dangerous files */
    if (*danger && overwrite!='Y') {
      printf("Archive contains files with dangerous names:%s\n",danger);
      printf("Continue with receiving (yYnN)? ");
      fgetl(tmp,stdin);
      overwrite=tmp[0];
      if (toupper(overwrite)!='Y') return;
    }

    /* receive from tar file */
    snprintf(MAXS(tmp),"receiving from archive '%s' :",nname);
    message(prg,'I',tmp);

    /* compressed, encrypted or normal tar file? */
    if (flp->flags&F_COMPRESS) {
      if (str_eq(flp->compress,S_BZIP2))
	snprintf(MAXS(cmd),"%s -d < %s/%d.d | %s xvf - 2>%s",
		 bzip2_bin,userspool,flp->id,tar_bin,error_log);
      else
	snprintf(MAXS(cmd),"%s -d < %s/%d.d | %s xvf - 2>%s",
		 gzip_bin,userspool,flp->id,tar_bin,error_log);
    } else if (flp->flags&F_CRYPT)
      snprintf(MAXS(cmd),"%s xvf %s 2>%s",tar_bin,tartmp,error_log);
    else
      snprintf(MAXS(cmd),"%s xvf %s/%d.d 2>%s",
	       tar_bin,userspool,flp->id,error_log);

    /* receive tar archive and check for errors */
    terr=0;
    if (*verbose) printf("call: %s\n",cmd);
    system(cmd);
    if (stat(error_log,&finfo)==0 && finfo.st_size>10) {
      errno=0;
      inf=fopen(error_log,"r");
      while (fgets(line,MAXLEN-1,inf)) {
	if (str_beq(TAR": ",line) && 
	    !simplematch(line,TAR": Could not create symlink*File exists*",1)) {
	  if (!terr) {
	    terr=1;
	    snprintf(MAXS(tmp),"errors while receive '%s' :",nname);
	    message(prg,'E',tmp);
	  }
	  printf("%s",line);
	}
      }
      fclose(inf);
    }

    /* was there an error with tar? */
    if (terr) {
      snprintf(MAXS(tmp),"leaving '%s' in spool intact",nname);
      message(prg,'I',tmp);
    } else {
     
      /* delete tar spool file */
      if (!keep) delete_sf(flp,0);
    }

    cleanup();
    return;
  }

  /* receive non-tar file */

  /* save file in transfer shape? */
  if (preserve && flp->flags&F_CRYPT) {
    fname[FLEN-5]=nname[FLEN-5]=sname[FLEN-5]=0;
    strcat(fname,".pgp");
    strcat(nname,".pgp");
    strcat(sname,".pgp");
  }

  /* if file with same name already exists check what to do */
  if (!quiet && checkfile(utf,fname,nname,sname,&overwrite)) return;

  /* leading . in file name? */
  if (!strchr("yYr",overwrite) && fname[0]=='.') {
   
    /* skipping? */
    if (overwrite=='S' || overwrite=='N') return;

    /* ask user what to do */
    printf("'%s' starts with a '.' which may be dangerous\n",nname);
    printf("Receive it anyway (yY), rename (r) or skip (sS) it? ");
    fgetl(answer,stdin);
    overwrite=answer[0];

    /* skipping this file? */
    if (toupper(overwrite)=='S') return;

    /* request for renaming? */
    if (overwrite=='r') {
      sprintf(fname,"Rename '%s' to? ",nname);
      if (getpromptline(fname,MAXLEN-1)==NULL || *fname=='\n' || *fname==0)
	strcpy(fname,nname);
      if ((cp=strrchr(fname,'\n'))) *cp=0;
      strcpy(nname,fname);
    }

  }

  /* safety fallback: try to delete an old file with the same name */
  unlink(fname);
  if (stat(fname,&finfo)==0) {
    snprintf(MAXS(tmp),"cannot create '%s' : "
	     "file does already exist and is not deletable",fname);
    errno=0;
    message(prg,'E',tmp);
    return;
  }

  /* save file encrypted? */
  if (preserve && flp->flags&F_CRYPT) {
   
    /* copy file */
    snprintf(MAXS(tmp),"%s/%d.d",userspool,flp->id);
    if (fcopy(tmp,fname,0666&~cmask)<0) {
      snprintf(MAXS(tmp),"cannot receive '%s'",nname);
      errno=0;
      message(prg,'E',tmp);
      return;
    }

    if ((flp->flags&F_SOURCE || flp->flags&F_TEXT) && !quiet) {
      snprintf(MAXS(tmp),
	       "'%s' has a SOURCE or TEXT attribute, you have to decode it "
	       "after pgp-decrypting with:  recode %s:"CHARSET" '%s'",
	       nname,flp->charset,nname);
      message(prg,'W',tmp);
    }

    if (flp->flags&F_MIME && !quiet) {
      snprintf(MAXS(tmp),
	       "'%s' has the MIME attribute, you have to run it through"
	       "metamail after pgp-decrypting",nname);
      message(prg,'W',tmp);
    }

    /* pgp signature to save? */
    create_sigfile(flp->sign,fname,nname,&overwrite);

    if (!keep) delete_sf(flp,0);
    snprintf(MAXS(tmp),"'%s' received",nname);
    message(prg,'I',tmp);

    return;
  }

  /* spool file compressed or encrypted? */
  if (flp->flags&F_COMPRESS || flp->flags&F_CRYPT) {
   
    /* source, text or MIME format? */
    if ((flp->flags&F_SOURCE) || (flp->flags&F_TEXT) || (flp->flags&F_MIME)) {
     
      /* open pipe to uncompress or decrypt spool file */
      if (flp->flags&F_COMPRESS) {
	if (str_eq(flp->compress,S_BZIP2))
	  snprintf(MAXS(cmd),"%s -d < %s/%d.d",bzip2_bin,userspool,flp->id);
	else
	  snprintf(MAXS(cmd),"%s -d < %s/%d.d",gzip_bin,userspool,flp->id);
      } else if (flp->flags&F_CRYPT)
	  snprintf(MAXS(cmd),
		   "%s %s -f < %s/%d.d",pgp_bin,pgpvm,userspool,flp->id);
      if (*verbose) printf("call: %s\n",cmd);
      if ((pp=popen(cmd,"r")) == NULL) {
        message(prg,'E',"cannot open spool file for reading");
        return;
      }

      /* open output file */
      if (!(outf=fopen(fname,"w"))) {
	pclose(pp);
	printf("\n"); 
	snprintf(MAXS(tmp),"cannot open '%s' for writing",nname);
        message(prg,'E',tmp);
        return;
      }

      /* translate CR LF to LF and write to output file */
      crlf2lf(pp,outf,fname,nname);

      /* ready */
      pclose(pp);
      fclose(outf);

      if (flp->flags&F_CRYPT && !pgppass) printf("\n\n");

    } else  /* binary format */ {
     
      snprintf(MAXS(sfile),"%s/%d.d",userspool,flp->id);

      /* try to create destination file */
      /* open output file */
      if (!(outf=fopen(fname,"w"))) {
        snprintf(MAXS(tmp),"cannot open '%s' for writing",nname);
        message(prg,'E',tmp);
        return;
      }
      fclose(outf);

      /* compressed spool file? */
      if (flp->flags&F_COMPRESS) {
       
	/* uncompress and receive binary file */
	
	/* stupid bzip2 NEEDS file from stdin */
	if (str_eq(flp->compress,S_BZIP2)) {
	  strcpy(tmp,fname);
	  if ((cp=strrchr(tmp,'/')))
	    *++cp=0;
	  else
	    *tmp=0;
	  snprintf(MAXS(tmpfile),"%sreceive-%d.tmp",tmp,(int)getpid());
	  snprintf(MAXS(tmp),"%s -d < %s > %s",bzip2_bin,sfile,tmpfile);
	  if (system(tmp)) {
	    snprintf(MAXS(tmp),"call to %s failed, cannot receive '%s'",
		     bzip2_bin,nname);
	    message(prg,'E',tmp); 
	    return;
	  }
	  if (rename(tmpfile,fname)<0) {
	    snprintf(MAXS(tmp),"cannot write to '%s'",nname);
	    message(prg,'E',tmp); 
	    unlink(tmpfile);
	    return;
	  }
	  
	} else { /* gzip format */
	  
	  sad[0]=gzip_bin;
	  sad[1]="-dc";
	  sad[2]=sfile;
	  sad[3]=NULL;

	  if (spawn(sad,fname,cmask)<0) {
	    errno=0;
	    snprintf(MAXS(tmp),
		     "call to %s failed, cannot receive '%s'",sad[0],nname);
	    message(prg,'E',tmp); 
	    return;
	  }
	  
	}

      }

      /* encrypted spool file? */
      if (flp->flags&F_CRYPT) {
       
	snprintf(MAXS(cmd),"%s %s -f < %s > '%s'",pgp_bin,pgpvm,sfile,fname);
	if (*verbose) printf("call: %s\n",cmd);
	if (system(cmd)!=0) {
	  errno=0;
	  snprintf(MAXS(tmp),"cannot receive '%s', pgp failed",nname);
	  message(prg,'E',tmp);
	  unlink(fname);
	  return;
	}
	if (!pgppass) printf("\n\n");
			    
      }
    }

  } else   /* not compressed and not encrypted or keep encryption */ {
   
    /* source, text or MIME format? */
    if (((flp->flags&F_SOURCE) || 
	 (flp->flags&F_TEXT)   ||
	 (flp->flags&F_MIME))  && 
       !(flp->flags&F_CRYPT)) {
     
      /* open input file */
      snprintf(MAXS(sfile),"%s/%d.d",userspool,flp->id);
      if ((inf=fopen(sfile,"r")) == NULL) {
        message(prg,'E',"cannot open spool file for reading");
        return;
      }

      /* open output file */
      if ((outf=fopen(fname,"w")) == NULL) {
        snprintf(MAXS(tmp),"cannot open '%s' for writing",nname);
        message(prg,'E',tmp);
        fclose(inf);
        return;
      }

      /* translate CR LF to LF and write to output file */
      crlf2lf(inf,outf,fname,nname);

      /* ready */
      fclose(inf);
      fclose(outf);

    } else   /* binary file */ {
     
      /* copy file */
      snprintf(MAXS(tmp),"%s/%d.d",userspool,flp->id);
      if (fcopy(tmp,fname,0666&~cmask)<0) {
        snprintf(MAXS(tmp),"cannot receive '%s'",nname);
	errno=0;
        message(prg,'E',tmp);
	return;
      }
    }
  }

  /* pgp signature to save? */
/* no more needed, because already saved in transfer shape!
  if (preserve) create_sigfile(flp->sign,fname,nname,&overwrite);
*/
  /* executable flag set? */
  if (flp->flags&F_EXE) chmod(fname,(S_IRWXU|S_IRWXG|S_IRWXO)&~cmask);

  snprintf(MAXS(tmp),"'%s' received",nname);
  message(prg,'I',tmp);

  /* foreign character set in text file? */
  if ((flp->flags&F_TEXT) && !str_eq(flp->charset,CHARSET)) {
   
    /* call GNU recode */
    snprintf(MAXS(tmp),"%s:"CHARSET,flp->charset);
    sad[0]=recode_bin;
    sad[1]=tmp;
    sad[2]=fname;
    sad[3]=NULL;
    if (spawn(sad,NULL,cmask)<0) {
      snprintf(MAXS(tmp),
	       "call to %s failed, cannot translate character set in '%s'",
	       recode_bin,nname);
      message(prg,'E',tmp);
    }

  }

  /* set the original date */
  if (*(flp->date)) {
    if (!strstr(flp->date,"UTC")) strcat(flp->date," UTC");
    utb.actime=utb.modtime=get_date(flp->date,NULL);
    utime(fname,&utb);
  }

  /* MIME? */
  if (flp->flags&F_MIME) {
    
    /* metamail call allowed? */
    if (nometamail) {
      snprintf(MAXS(tmp),
	       "'%s' is a MIME file, you have to run it through metamail",
	       nname);
      message(prg,'I',tmp);
      if (!keep) delete_sf(flp,0);
    } else {
      
      /* call metamail */
      sad[0]=metamail_bin;
      sad[1]=fname;
      sad[2]=NULL;
      if (spawn(sad,NULL,cmask)<0) {
	snprintf(MAXS(tmp),
		 "call to %s failed, keeping local file '%s'",
		 metamail_bin,nname);
	message(prg,'E',tmp);
      } else {
      
	/* delete spool file and received MIME-file*/
	if (!keep) delete_sf(flp,0);
	if (!nometamail) unlink(fname);

      }
    }

  } else
      
    /* delete spool file */
    if (!keep) delete_sf(flp,0);
    
}


/*
 * crlf2lf - translate NVT format file to Unix text format file
 *
 * INPUT:  inf	  - file to read from
 *         outf   - file to write to
 *         fname  - file name to write
 */
void crlf2lf(FILE *inf, FILE *outf, const char *fname, const char *nname) {
  int c1,c2;		/* characters to read in */
  char tmp[MAXLEN];	/* temporary string */

  /* read first char */
  c1=fgetc(inf);

  /* loop until EOF */
  while ((c2=fgetc(inf)) != EOF) {
   
    /* crlf? */
    if (c1=='\r' && c2=='\n') {
     
      /* write lf */
      if(fputc(c2,outf)==EOF) {
        snprintf(MAXS(tmp),"cannot write to %s",nname);
        message(prg,'E',tmp);
        return;
      }

      /* read next char */
      if ((c2=fgetc(inf)) == EOF) return;

    } else {
     
      /* write char */
      if(fputc(c1,outf)==EOF) {
        snprintf(MAXS(tmp),"cannot write to %s",nname);
        message(prg,'E',tmp);
        return;
      }

    }
    c1=c2;
  }

  /* write last char */
  if(fputc(c1,outf)==EOF) {
    snprintf(MAXS(tmp),"cannot write to %s",nname);
    message(prg,'E',tmp);
    return;
  }

}


/*
 * spawn  - spawn a subprocess and direct output to a file
 *
 * INPUT:  sad     - spawn argument descriptor
 *         output  - output file
 *         cmask   - protection mask
 *
 * RETURN: 0 on success, -1 on failure
 *
 */
int spawn(char **sad, const char *output, mode_t cmask) {
  int status,		/* fork status */
      fd;		/* output file descriptor */
  pid_t pid;		/* process id */

  /* spawn subprocess */
  pid=fork();

  /* failed? */
  if (pid<0) {
    message(prg,'E',"cannot fork subprocess");
    return(-1);
  }

  /* is this the subprocess? */
  if (pid==0) {
   
    /* redirect stdout? */
    if (output) {
     
      /* close stdout */
      close(1);

      /* open output file as stdout */
      fd=creat(output,0666&~cmask);
      if (fd!=1) {
        errno=0;
	message(prg,'E',"file descriptor mismatch");
	exit(1);
      }

    }

    /* execute program */
    execvp(sad[0],sad);

    /* oops - failed */
    message(prg,'F',"execvp() failed!");
    cleanup();
    exit(2);
  }

  /* wait for termination of subprocess */
#ifdef NEXT
  wait(&status);
#else
  waitpid(pid,&status,0);
#endif

  /* error in subprocess? */
  if (status) return(-1);

  return(0);
}


/*
 * cleanexit  - clean termination routine
 */
void cleanexit() {
 
  /* ignore all relevant signals */
  signal(SIGTERM,SIG_IGN);
  signal(SIGABRT,SIG_IGN);
  signal(SIGQUIT,SIG_IGN);
  signal(SIGHUP,SIG_IGN);
  signal(SIGINT,SIG_IGN);

  printf("\r\n");
  cleanup();
  exit(0);
}


/*
 * cleanup  - delete tmp files
 */
void cleanup() {
 
#ifndef DEBUG
  unlink(tartmp);
  unlink(error_log);
#endif
}


/*
 * checkfile  - check file name before writing
 *
 * INPUT:  fname	- real file name
 * 	   nname	- normal file name to display
 * 	   sname	- file name for shell usage
 * 	   overwrite	- overwriting, renaming or skipping
 *
 * OUTPUT: fname	- new real file name
 * 	   nname	- new normal name to display
 * 	   sname	- file name for shell usage
 * 	   overwrite	- overwriting, renaming or skipping
 *
 * RETURN: 0 if renaming or overwriting
 *         1 if skipping
 */
int checkfile(int utf, char *fname, char *nname, char *sname, char *overwrite) {
  char
    *cp,			/* a simple string pointer */
    tmp[MAXLEN],		/* temporary string */
    answer[FLEN];		/* answer string */
  static char storeformat='c';	/* file name storing format */
  struct stat finfo;		/* information about a file */

  if (quiet) storeformat='C';

  /* rename files before receiving? */
  if (ren) {
     sprintf(fname,"Rename '%s' to? ",nname);
    if (getpromptline(fname,MAXLEN-1)==NULL || *fname=='\n' || *fname==0) 
      strcpy(fname,nname);
    if ((cp=strrchr(fname,'\n'))) *cp=0;
    strcpy(nname,fname);
  } else {
   
    /* need user request? */
    if (!strchr("CNS",storeformat)) {
     
      /* found Unicode or 0x0? */
      if (utf==1) {
        printf("The next file name contains characters which are not allowed "
	       "in Unix.\nThese characters have been substituted with '_'.\n");
      }

      /* found control or meta characters? */
      if (utf&2) {
          printf("The next file name contains characters which may cause "
		 "problems with your shell\n or may do strange things with "
		 "your terminal.\n"
	         "These characters have been substituted with '_'.\n");
	if (utf&1)
	  printf("Non-valid characters for Unix file names have been substituted"
		 ", too.\n");

	/* let user choose file name format */
	/*if (strcmp(nname,sname)!=0) */
	printf("Save the next file as:\n");
	printf("   (c)omplete file name with control code characters "
	       "(not displayable)\n");
	printf("   (n)ormal file name: '%s'\n",nname);
	printf("   (u)ntainted file name:  '%s'\n",sname);
	printf("   (s)kip it\n");
	printf("   (r)ename it\n");
	do {
	  printf("c or n may cause severe security problems "
		 "(Kids, don't try this at home!) !\n");
	  printf("Select one of c n u s r (or C N U S R for no more questions): ");
	  fgetl(tmp,stdin);
	  storeformat=tmp[0];
	} while (strchr("cnusrCNUSR",storeformat)==NULL);

      }
    }

    /* set file name */
    switch (toupper(storeformat)) {
      case 'S': return(1);
      case 'N': strcpy(fname,nname); break;
      case 'U': strcpy(fname,sname);
                strcpy(nname,sname); break;
      case 'R': sprintf(fname,"Rename '%s' to? ",nname);
                if (getpromptline(fname,MAXLEN-1)==NULL 
		    || *fname=='\n' || *fname==0) strcpy(fname,nname);
                if ((cp=strrchr(fname,'\n'))) *cp=0;
                strcpy(nname,fname);
    }

  }

  /* does the file already exist? */
  while (stat(fname,&finfo)==0 && (*overwrite!='Y')) {
   
    /* skipping? */
    if (*overwrite=='S' || *overwrite=='N') return(1);

    /* ask user what to do */
    printf("'%s' already exists.\n"
           "Overwrite (yY), rename (r) or skip (sS) it? ",nname);
    fgetl(answer,stdin);
    *overwrite=answer[0];

    /* request for renaming? */
    if (*overwrite=='r') {
      sprintf(fname,"Rename '%s' to? ",nname);
      if (getpromptline(fname,MAXLEN-1)==NULL || *fname=='\n' || *fname==0)
	strcpy(fname,nname);
      if ((cp=strrchr(fname,'\n'))) *cp=0;
      strcpy(nname,fname);
      continue;
    }

    /* overwriting or leaving? */
    if (toupper(*overwrite)=='Y') break; else return(1);

  }

  return(0);
}


/*
 * create_sigfile  - create detached pgp signature file
 *
 * INPUT:  sign		- signature
 * 	   fname	- original file name
 * 	   overwrite	- overwriting, renaming or skipping
 *
 * RETURN: 0 if ok, -1 if failed
 */
int create_sigfile(const char *sign, const char *fname, const char *nname,
		   char *overwrite) {
  char
    *cp,			/* a simple string pointer */
    tmp[MAXLEN],		/* temporary string */
    sigfile[MAXLEN],		/* signature file */
    nsigfile[MAXLEN],		/* normal displayable signature file name */
    answer[FLEN];		/* answer string */
  struct stat finfo;		/* information about a file */

  FILE *outf;			/* file to create */

  /* no pgp signature to save? */
  if (!*sign) return(0);

  snprintf(MAXS(sigfile),"%s.sig",fname);
  snprintf(MAXS(nsigfile),"%s.sig",nname);

  /* signature file does already exist? */
  while (stat(sigfile,&finfo)==0 && (*overwrite!='Y')) {
   
    /* skipping? */
    if (*overwrite=='S' || *overwrite=='N') return(-1);

    /* ask user what to do */
    printf("'%s' already exists.\n"
           "Overwrite (yY), rename (r) or skip (sS) it? ",nsigfile);
    fgetl(answer,stdin);
    *overwrite=answer[0];

    /* request for renaming? */
    if (*overwrite=='r') {
      sprintf(nsigfile,"Rename to? ");
      if (getpromptline(nsigfile,MAXLEN-1)==NULL || 
	  *nsigfile=='\n' || *nsigfile==0)
	strcpy(nsigfile,sigfile);
      if ((cp=strrchr(nsigfile,'\n'))) *cp=0;
      strcpy(sigfile,nsigfile);
      continue;
    }

    /* overwriting or leaving? */
    if (toupper(*overwrite)=='Y') break; else return(-1);

  }

  /* safety fallback: try to delete an old file with the same name */
  unlink(fname);
  if (stat(sigfile,&finfo)==0) {
    snprintf(MAXS(tmp),"cannot create '%s' : "
	     "file does already exist and is not deletable",sigfile);
    errno=0;
    message(prg,'E',tmp);
    return(-1);
  }

  if (!(outf=fopen(sigfile,"w"))) {
    snprintf(MAXS(tmp),"cannot create signature file '%s' ",nsigfile);
    message(prg,'E',tmp);
    return(-1);
  }

  fprintf(outf,"%s",sign);
  fclose(outf);
  snprintf(MAXS(tmp),"signature file '%s' created",nsigfile);
  message(prg,'I',tmp);
  return(0);

}


/*
 * check_signature  - check or print pgp signature
 *
 * INPUT:  flp		- file list pointer to spool file
 *         pgpring	- pgp ring file name
 * 	   print	- flag for printing signature status
 *
 * RETURN:  1 if good signature
 *          0 if no signature
 *         -1 if signature not found in pgp key ring
 *         -2 if bad signature
 *         -3 if other error
 *         
 */					  
int check_signature(struct filelist *flp, char *pgpring, int print) {  
  int status;			/* return status */
  char *cp;	    		/* simple character pointer */
  char sigfile[FLEN],		/* pgp signature file */
       pgpopt[FLEN],		/* pgp options */
       tmp[MAXLEN],		/* temp-string */
       line[MAXLEN];		/* line to read in */
  FILE
    *outf,			/* output file */
    *pp;			/* pipe input stream */

  status=0;
  
  if (!(*(flp->sign))) return(0);
  
  if (str_eq(pgpring,".")) *pgpring=0;
  
  /* write signature file */
  snprintf(MAXS(sigfile),"%s/%d.d.sig",userspool,flp->id);
  if (!(outf=fopen(sigfile,"w"))) {
    snprintf(MAXS(tmp),"cannot write signature file %s",sigfile);
    message(prg,'E',tmp);
    return(-2);
  }
  fprintf(outf,"%s",flp->sign);
  fclose(outf);

  /* build pgp options */
  if (*pgpring) {
    if (access(pgpring,R_OK)<0) {
      snprintf(MAXS(tmp),"cannot read pgp pub ring file %s",pgpring);
      message(prg,'F',tmp);
    }
    snprintf(MAXS(pgpopt),"+batchmode=on +language=en +pubring=%s",pgpring);
  } else
    snprintf(MAXS(pgpopt),"+batchmode=on +language=en");
  
  /* check signature file with pgp */
  snprintf(MAXS(tmp),"%s %s %s 2>/dev/null",pgp_bin,pgpopt,sigfile);
  if (*verbose) printf("call: %s\n",tmp);
  if (!(pp=popen(tmp,"r"))) {
    message(prg,'E',"cannot call pgp");
    unlink(sigfile);
    return(-2);
  }

  /* print or check only result */
  if (print) {
    while (fgets(line,MAXLEN-1,pp)) {
      if ((cp=strchr(line,'\n'))) *cp=0;
      if ((cp=strrchr(line,'.'))) *cp=0;
      if (strstr(line,"Good signature from user") || 
	  strstr(line,"Bad signature from user")) {
	printf("     (%s)\n",line);
	break;
      }
      if (strstr(line,"Key matching expected")) {
	printf("     (No matching signature found in pgp key file)\n");
	break;
      }
    }
  } else {  /* check only result */
    while (fgets(line,MAXLEN-1,pp)) {
      if (strstr(line,"Good signature from user")) { 
	status=1;
	break;
      }
      if (strstr(line,"Key matching expected")) { 
	status=-1;
	break;
      }
      if (strstr(line,"Could not read key")) { 
	status=-1;
	break;
      }
      if (strstr(line,"Bad signature from user")) { 
	status=-2;
	break;
      }
    }
  }
    
  /* close pipe and delete signature file */
  pclose(pp);
  unlink(sigfile);
  
  return(status);    
}


/* 
 * renumber  - renumber spool files
 */
void renumber (struct senderlist *sls) {
  int 
    i,				/* actual spool file number */
    nextfree,			/* next free spool file number */
    lastused,			/* last used spool file number */
    min,			/* smallest spool file number */
    max;			/* biggest spool file number */
  char 
    tmp[MAXLEN],		/* temporary string */
    ofile[FLEN],		/* old filename */
    nfile[FLEN];		/* new filename */
  struct filelist *flp;		/* file list pointer */
  struct senderlist *slp;	/* sender list pointer */
  struct stat finfo;            /* information about a file */
  FILE *lockf;			/* lock file */

  min = lastused = 0;
  nextfree = max = nextfree = 1;
  
  if (chdir(userspool)<0) {
    snprintf(MAXS(tmp),"cannot change to %s",userspool);
    message(prg,'F',tmp);
  }

  /* create lock file */
  lockf=fopen("renumber","a+");
  if (!lockf) message(prg,'F',"cannot open renumber file");
  if (wlock_file(fileno(lockf))<0) {
    message(prg,'E',"spool is locked - try again later");
    return;
  }
  
  /* loop over all spool files to find max spool file number */
  for (slp=sls; slp; slp=slp->next) {
    for (flp=slp->flist; flp; flp=flp->next) {
      fprintf(lockf,"%d\n",flp->id);
      if (flp->id > max) max=flp->id;
    }
  }

  /* renumber loop */
  while (min<max) {
    rewind(lockf);
    min=max;
    while (fscanf(lockf,"%d\n",&i)!=EOF) {
      if (i<min && i>lastused && i>nextfree) min=i;
    }
    snprintf(MAXS(ofile),"%d.h",min);
    for (i=nextfree; i<min; i++) {
      snprintf(MAXS(nfile),"%d.h",i);
      if (stat(nfile,&finfo)<0 || finfo.st_size==0) {
	unlink(nfile);
	if (rename(ofile,nfile)<0) {
	  snprintf(MAXS(tmp),"cannot rename %s to %s",ofile,nfile);
	  message(prg,'F',tmp);
	}
	snprintf(MAXS(nfile),"%d.d",i);
	snprintf(MAXS(ofile),"%d.d",min);
	unlink(nfile);
	if (rename(ofile,nfile)<0) {
	  snprintf(MAXS(tmp),"cannot rename %s to %s",ofile,nfile);
	  message(prg,'F',tmp);
	}
	nextfree=i+1;
	break;
      }
    }
    lastused=min;
  }
  
  unlink("renumber");
}


/*
 * reply  - dummy function, only needed for linking
 */
void reply(int x) { }


/*
 * usage - print short help usage text
 */
int usage() {
  fprintf(stderr,"usage:  %s [-dqrpkP] [-f from] [-S keyring] file-name [...]\n",prg);
  fprintf(stderr,"   or:  %s -n [-dqrpkP] [-S keyring] file-number [...]\n",prg);
  fprintf(stderr,"   or:  %s [-slLR] [-f from]\n",prg);
  fprintf(stderr,"   or:  %s -a [-dqrpkP] [-S keyring] [-f from]\n",prg);
  fprintf(stderr,"   or:  %s -b user[@host] [-k] [-f from] file-name [...]\n",prg);
  fprintf(stderr,"   or:  %s -b user[@host] -n [-k] file-number [...]\n",prg);
  fprintf(stderr,"   or:  %s -b user[@host] -a [-k]\n",prg);
  fprintf(stderr,"options:  -n  specify a file number\n");
  fprintf(stderr,"          -a  specify all files\n");
  fprintf(stderr,"          -f  specify only files from this user\n");
  fprintf(stderr,"          -d  delete file (do not receive)\n");
  fprintf(stderr,"          -q  quiet mode: no questions asked\n");
  fprintf(stderr,"          -r  rename file when receiving\n");
  fprintf(stderr,"          -R  renumber files in spool\n");
  fprintf(stderr,"          -p  preserve tar or pgp file formats\n");
  fprintf(stderr,"          -k  keep file in spool after receiving\n");
  fprintf(stderr,"          -P  receive file to stdout (decrypt but don't untar)\n");
  fprintf(stderr,"          -b  bounce (forward) a file\n");
  fprintf(stderr,"          -S  force validating pgp signature from keyring (\".\" is default ring)\n");
  fprintf(stderr,"          -l/-L/-s  normal/full/short list of files in spool\n");
  fprintf(stderr,"examples:  receive -af frams  ! receive all files from user frams\n");
  fprintf(stderr,"           receive -n 1       ! receive file number 1\n");
  fprintf(stderr,"           receive blubb      ! receive file blubb\n");
  return(2);
}