-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathckofio.c
8552 lines (7610 loc) · 266 KB
/
ckofio.c
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
#ifdef NT
char *ckzv = "Win32 File support, 8.0.187, 18 July 2004";
#else /* NT */
char *ckzv = "OS/2 File support, 8.0.187, 18 July 2004";
#endif /* NT */
/* C K O F I O -- Kermit file system support for OS/2 */
/*
NOTE TO CONTRIBUTORS: This file, and all the other C-Kermit files, must be
compatible with C preprocessors that support only #ifdef, #else, #endif,
#define, and #undef. Please do not use #if, logical operators, or other
preprocessor features in any of the portable C-Kermit modules. You can,
of course, use these constructions in system-specific modules when you they
are supported.
*/
/*
Authors: Frank da Cruz (fdc@columbia.edu),
Columbia University Academic Information Systems, New York City,
Jeffrey E Altman <jaltman@secure-endpoints.com>
Secure Endpoints Inc., New York City
and others noted below. Note: CUCCA = Previous name of Columbia University
Academic Information Systems.
Copyright (C) 1985, 2004, Trustees of Columbia University in the City of New
York. All rights reserved.
*/
/* Include Files */
#include "ckcsym.h"
#include "ckcdeb.h"
#include "ckcasc.h"
#ifndef NOCSETS
#include "ckcxla.h"
#endif /* NOCSETS */
#include "ckuusr.h"
#ifdef CK_LOGIN /* For AUTH_VALID, AUTHTYPE_xxx, ... */
#include "ckctel.h"
#ifdef CK_SSL
#include "ck_ssl.h"
#endif /* CK_SSL */
#endif /* CK_LOGIN */
#include <stdio.h>
#include <errno.h>
_PROTOTYP( int os2settitle, (char *, int) );
extern int priority;
#include <signal.h>
/*
C-Kermit's OS/2 support originally by Chris Adie <C.Adie@uk.ac.edinburgh>
Edinburgh University Computing Service, Scotland, for C-Kermit 4F. Adapted
to C-Kermit 5A and integrated into the UNIX support module by Kai Uwe Rommel
<rommel@informatik.tu-muenchen.de>, Muenchen, Germany, December 1991.
And then split away from the Unix modules for C-Kermit 6.1 because the
amount of disparate code was just too great.
*/
/*
Directory Separator macros, to allow this module to work with both UNIX and
OS/2: Because of ambiguity with the command line editor escape \ character,
the directory separator is currently left as / for OS/2 too, because the
OS/2 kernel also accepts / as directory separator. But this is subject to
change in future versions to conform to the normal OS/2 style.
Win32 also allows the Directory Separator to be / when it is used for
local disk operations. However, if a UNC is in use, then \ must be used.
So we have two choices, we can return UNC names with \ quoted which might
break if the command quoting is turned off, or we can performed the reverse
translation in all file i/o functions if the first two characters are "//"
indicating a UNC.
*/
#ifndef DIRSEP
#define DIRSEP '/'
#endif /* DIRSEP */
#ifndef ISDIRSEP
#define ISDIRSEP(c) ((c)=='/'||(c)=='\\')
#endif /* ISDIRSEP */
#ifdef SDIRENT
#define DIRENT
#endif /* SDIRENT */
#include "ckodir.h"
extern int binary; /* We need to know this for open() */
#ifdef CK_CTRLZ
extern int eofmethod;
#endif /* CK_CTRLZ */
extern int k95stdin,k95stdout;
#include <sys/utime.h>
#include <stdlib.h>
#include <process.h>
#include <share.h>
extern int fncact; /* Need this for zchkspa() */
extern int tlevel, cmdlvl; /* Need this for external commands */
#ifdef __IBMC__
extern FILE *popen(char *, char *);
extern int pclose(FILE *);
#else
#define popen _popen
#define pclose _pclose
#define fopen(n, m) _fsopen(n, m, _SH_DENYWR)
#endif /* __IBMC__ */
#ifdef NT
#include <io.h>
#endif /* NT */
#define TIMESTAMP /* Can do file dates */
#include <time.h> /* Need this */
#include <sys/timeb.h> /* Need this too */
#include <sys/stat.h>
#include <sys/types.h>
#ifdef __IBMC__
#ifdef system
#undef system
#endif
#ifdef COMMENT
#ifdef stat
#undef stat
#endif
#define stat _stat
#endif /* COMMENT */
#endif
#ifdef NT
#define timezone _timezone
#define write _write
#define fileno _fileno
#define stricmp _stricmp
#define setmode _setmode
#define access _access
#define unlink _unlink
#define chdir _chdir
#define getcwd _getcwd
#define utime _utime
#define rmdir _rmdir
#define utimbuf _utimbuf
#define stat _stat
#ifndef SEM_INDEFINITE_WAIT
#define SEM_INDEFINITE_WAIT INFINITE
#endif /* SEM_INDEFINITE_WAIT */
#endif /* NT */
/* Because standard stat has trouble with trailing /'s we have to wrap it */
int os2stat(char *, struct stat *);
/* Is `y' a leap year? */
#define leap(y) (((y) % 4 == 0 && (y) % 100 != 0) || (y) % 400 == 0)
/* Number of leap years from 1970 to `y' (not including `y' itself). */
#define nleap(y) (((y) - 1969) / 4 - ((y) - 1901) / 100 + ((y) - 1601) / 400)
#ifdef DEBUG
extern int deblog;
#else
#define deblog 0
#endif /* DEBUG */
extern int xferlog;
extern char * xferfile;
int iklogopen = 0;
static time_t timenow;
static int logged_in = 0;
static char iksdmsg[CKMAXPATH+512];
extern char exedir[CKMAXPATH];
#ifndef XFERFILE
#define XFERFILE "iksd.log"
#endif /* XFERFILE */
/*
Functions (n is one of the predefined file numbers from ckcker.h):
zopeni(n,name) -- Opens an existing file for input.
zopeno(n,name,attr,fcb) -- Opens a new file for output.
zclose(n) -- Closes a file.
zchin(n,&c) -- Gets the next character from an input file.
zsinl(n,&s,x) -- Read a line from file n, max len x, into address s.
zsout(n,s) -- Write a null-terminated string to output file, buffered.
zsoutl(n,s) -- Like zsout, but appends a line terminator.
zsoutx(n,s,x) -- Write x characters to output file, unbuffered.
zchout(n,c) -- Add a character to an output file, unbuffered.
zchki(name) -- Check if named file exists and is readable, return size.
zchko(name) -- Check if named file can be created.
zchkspa(name,n) -- Check if n bytes available to create new file, name.
znewn(name,s) -- Make a new unique file name based on the given name.
zdelet(name) -- Delete the named file.
nzxpand(string,flags) -- Expands the given wildcard string into a list of files.
znext(string) -- Returns the next file from the list in "string".
zxcmd(n,cmd) -- Execute the command in a lower fork on file number n.
zclosf() -- Close input file associated with zxcmd()'s lower fork.
zrtol(n1,n2) -- Convert remote filename into local form.
zltor(n1,n2) -- Convert local filename into remote form.
zchdir(dirnam) -- Change working directory.
zhome() -- Return pointer to home directory name string.
zkself() -- Kill self, log out own job.
zsattr(struct zattr *) -- Return attributes for file which is being sent.
zstime(f, struct zattr *, x) - Set file creation date from attribute packet.
zrename(old, new) -- Rename a file.
zlink(source,destination) -- Link a file.
zcopy(source,destination) -- Copy a file.
zmkdir(path) -- Create the directory path if possible
zfnqfp(fname,len,fullpath) - Determine full path for file name.
*/
/* Kermit-specific includes */
/*
Definitions here supersede those from system include files.
ckcdeb.h is included above.
*/
#include "ckcker.h" /* Kermit definitions */
#include "ckucmd.h" /* For sys-dependent keyword tables */
#include "ckuver.h" /* Version herald */
char *ckzsys = HERALD;
#include <fcntl.h>
/* Define macros for getting file type */
#ifdef ISDIRBUG /* Also allow this from command line */
#ifdef S_ISREG
#undef S_ISREG
#endif /* S_ISREG */
#ifdef S_ISDIR
#undef S_ISDIR
#endif /* S_ISDIR */
#endif /* ISDIRBUG */
#ifndef S_IFREG
#ifdef _S_IFREG
#define S_IFREG _S_IFREG
#endif
#endif
#ifndef S_IFDIR
#ifdef _S_IFDIR
#define S_IFDIR _S_IFDIR
#endif
#endif
#ifndef S_IFMT
#ifdef _S_IFMT
#define S_IFMT _S_IFMT
#endif
#endif
#ifndef S_ISREG
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
#endif /* S_ISREG */
#ifndef S_ISDIR
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#endif /* S_ISDIR */
/* More macros we might need */
#ifndef _O_APPEND
#define _O_APPEND O_APPEND
#endif
#ifndef _O_WRONLY
#define _O_WRONLY O_WRONLY
#endif
#ifndef _O_CREAT
#define _O_CREAT O_CREAT
#endif
#ifndef _O_TEXT
#define _O_TEXT O_TEXT
#endif
#ifndef _SH_DENYNO
#define _SH_DENYNO SH_DENYNO
#endif
#ifndef _O_BINARY
#define _O_BINARY O_BINARY
#endif
/* Longest pathname ... */
#ifdef MAXPATHLEN
#ifdef MAXPATH
#undef MAXPATH
#endif /* MAXPATH */
#define MAXPATH MAXPATHLEN
#else
#ifdef PATH_MAX
#define MAXPATH PATH_MAX
#else
#define MAXPATH 255
#endif /* PATH_MAX */
#endif /* MAXPATHLEN */
#ifndef NOPUSH
extern int nopush;
#endif /* NOPUSH */
_PROTOTYP( char * zdtstr, (time_t) );
_PROTOTYP( time_t zstrdt, (char *, int) );
/* Some systems define these symbols in include files, others don't... */
#ifndef R_OK
#define R_OK 4 /* For access */
#endif /* R_OK */
#ifndef W_OK
#define W_OK 2
#endif /* W_OK */
#ifndef O_RDONLY
#define O_RDONLY 000
#endif /* O_RDONLY */
#ifdef CKROOT
static char ckroot[CKMAXPATH+1] = { NUL, NUL };
static int ckrootset = 0;
int ckrooterr = 0;
#endif /* CKROOT */
/* syslog and wtmp items for Internet Kermit Service */
extern char * clienthost; /* From ckcmai.c. */
extern int ckxlogging;
static char fullname[CKMAXPATH+1];
static char tmp2[CKMAXPATH+1];
#ifdef CK_LOGIN
int ckxanon = 0; /* Anonymous login not ok */
char * anonacct = NULL;
#ifdef NT
char * iks_domain = NULL; /* Default domain to use for logins */
#endif /* NT */
#ifdef CK_PERMS
int ckxperms = 0040;
#endif /* CK_PERMS */
#ifdef UNIX
int ckxpriv = 1; /* Allow Root logins? */
#endif /* UNIX */
#endif /* CK_LOGIN */
#ifndef CKWTMP
int ckxwtmp = 0;
#else
int ckxwtmp = 1;
#include <utmp.h>
#define WTMPFILE "/usr/adm/wtmp" /* I hope this is portable */
char * wtmpfile = NULL;
static int wtmpfd = 0;
static char cksysline[32] = { NUL, NUL };
VOID
logwtmp(line, name, host) char *line, *name, *host; {
struct utmp ut;
struct stat buf;
time_t time();
if (!ckxwtmp)
return;
if (!wtmpfile)
makestr(&wtmpfile,WTMPFILE);
if (!line) line = "";
if (!name) name = "";
if (!host) host = "";
if (!wtmpfd && (wtmpfd = open(wtmpfile, O_WRONLY|O_APPEND, 0)) < 0) {
ckxwtmp = 0;
debug(F110,"WTMP open failed",line,0);
return;
}
if (!fstat(wtmpfd, &buf)) {
ckstrncpy(ut.ut_line, line, sizeof(ut.ut_line));
ckstrncpy(ut.ut_name, name, sizeof(ut.ut_name));
ckstrncpy(ut.ut_host, host, sizeof(ut.ut_host));
time(&ut.ut_time);
if (write(wtmpfd, (char *)&ut, sizeof(struct utmp)) !=
sizeof(struct utmp)) {
ftruncate(wtmpfd, buf.st_size); /* Error, undo any partial write */
debug(F110,"WTMP write error",line,0);
} else {
debug(F110,"WTMP record OK",line,0);
return;
}
}
}
#endif /* CKWTMP */
/* Declarations */
int mskrename = 0; /* MS-Kermit file collision renaming off */
int maxnam = MAXNAMLEN; /* Available to the outside */
int maxpath = MAXPATH;
int ck_znewn = -1;
int pexitstat = -2; /* Process exit status */
FILE *fp[ZNFILS] = { /* File pointers */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
/* Flags for each file indicating whether it was opened with popen() */
int ispipe[ZNFILS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
/* Buffers and pointers used in buffered file input and output. */
#ifdef DYNAMIC
extern char *zinbuffer, *zoutbuffer;
#else
extern char zinbuffer[], zoutbuffer[];
#endif /* DYNAMIC */
extern char *zinptr, *zoutptr;
extern int zincnt, zoutcnt;
static long iflen = -1L; /* Input file length */
static PID_T pid = 0; /* pid of child fork */
static int fcount[2] = {0,0}; /* Number of files left in search */
static int fcntsav[2] = {0,0};
static int fcountstream[2] = {0,0};
static int fcntstreamsav[2] = {0,0};
static char nambuf[MAXNAMLEN+4]; /* Buffer for a filename */
static char UNCnam[MAXNAMLEN+1]; /* Scratch buffer for UNC names */
#ifndef NOFRILLS
static char zmbuf[200]; /* For mail, remote print strings */
#endif /* NOFRILLS */
#ifdef OS2ONLY
#define INCL_KBD
#define INCL_DOSERRORS
#define INCL_DOSFILEMGR
#define INCL_DOSPROCESS
#define INCL_DOSSEMAPHORES
#include <os2.h>
typedef unsigned short WORD;
#undef COMMENT
/* Get/Set All Extended Attributes support */
#define FreeMem(p) DosFreeMem(p)
#define MAX_GEA 500L /* Max size for a GEA List */
#define MAXEACOUNT 128 /* Max number of EA's supported */
#define Ref_ASCIIZ 1 /* Reference type for DosEnumAttribute */
/* Definition of level specifiers, required for File Info */
#define GetInfoLevel1 1 /* Get info from SFT */
#define GetInfoLevel2 2 /* Get size of FEAlist */
#define GetInfoLevel3 3 /* Get FEAlist given the GEAlist */
#define GetInfoLevel4 4 /* Get whole FEAlist */
#define GetInfoLevel5 5 /* Get FSDname */
#define SetInfoLevel1 1 /* Set info in SFT */
#define SetInfoLevel2 2 /* Set FEAlist */
FEA2LIST *pFEAList = 0; /* Pointer to buffer containing all EA information */
ULONG os2attrs = FILE_NORMAL;
extern unsigned int lf_opts;
#endif /* OS2ONLY */
#ifdef NT
#include <windows.h>
#ifdef CK_LOGIN
#define SECURITY_WIN32
#include <security.h>
#include <ntsecapi.h>
#include <lmaccess.h>
#include <lmapibuf.h>
#include <lmerr.h>
#endif /* CK_LOGIN */
#endif /* NT */
char os2filename[MAXPATH];
#ifdef IKSD
extern int inserver, local;
#endif /* IKSD */
extern int server, en_mkd, en_cwd, en_del;
extern char uidbuf[]; /* from ckcmai.c... */
#ifdef CK_LOGIN
#ifdef CKROOT
extern char * anonroot;
#endif /* CKROOT */
extern int isguest;
#define GUESTPASS_LEN 256
static char guestpass[GUESTPASS_LEN] = "";
#endif /* CK_LOGIN */
_PROTOTYP(char * whoami, (void));
/* Z K S E L F -- Kill Self: log out own job, if possible. */
int
zkself() { /* For "bye", but no guarantee! */
_exit(3);
return(0);
}
/* Z C H K P I D -- Check to see if a PID is valid */
/* returns 1 if pid is valid and active, 0 if not */
int
zchkpid(unsigned long pid) {
#ifdef NT
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS,0,pid);
if ( hProcess ) {
CloseHandle(hProcess);
return(1);
}
return(0);
#else /* NT */
#endif /* NT */
return(1);
}
/* D O I K L O G -- Open Kermit-specific ftp-like transfer log. */
VOID /* called in ckcmai.c */
doiklog() {
if (iklogopen) /* Already open? */
return;
debug(F111,"doiklog",xferfile,xferlog);
if (xferlog) { /* Open iksd log if requested */
if (!xferfile) { /* If no pathname given */
char buf[MAXPATH];
ckstrncpy(buf,exedir,MAXPATH);
ckstrncat(buf,XFERFILE,MAXPATH);
makestr(&xferfile,buf); /* use this default */
}
if (*xferfile) {
xferlog = sopen(xferfile, _O_WRONLY | _O_APPEND | _O_CREAT | _O_TEXT,
_SH_DENYNO, 666 );
debug(F111,"doiklog open()",xferfile,xferlog);
if (xferlog < 0) {
#ifdef CKSYSLOG
cksyslog(SYSLG_FC, 0, "xferlog open failure",xferfile,NULL);
#endif /* CKSYSLOG */
debug(F101,"doiklog open errno","",errno);
xferlog = 0;
} else
iklogopen = 1;
} else
xferlog = 0;
#ifdef CKSYSLOG
if (ckxlogging) {
if ( xferlog )
cksyslog(SYSLG_FC, 1, "xferlog: open ok", xferfile, NULL);
else
cksyslog(SYSLG_FC, 0, "xferlog: open failed", xferfile, NULL);
}
#endif /* CKSYSLOG */
}
}
static VOID
getfullname(name) char * name; {
char *p = (char *)fullname;
int len = 0;
fullname[0] = '\0';
/* If necessary we could also chase down symlinks here... */
#ifdef COMMENT
/* This works but is incompatible with wuftpd */
if (isguest && anonroot) {
ckstrncpy(fullname,anonroot,CKMAXPATH);
len = strlen(fullname);
if (len > 0)
if (fullname[len-1] == '/')
len--;
}
p += len;
#endif /* COMMENT */
zfnqfp(name, CKMAXPATH - len, p);
while (*p) {
if (*p < '!') *p = '_';
p++;
}
}
/* Z O P E N I -- Open an existing file for input. */
/* Returns 1 on success, 0 on failure */
int
zopeni(n,name) int n; char *name; {
int x, y;
debug(F111,"zopeni name",name,n);
debug(F101,"zopeni fp","", (unsigned) fp[n]);
if (chkfn(n) != 0) {
debug(F110,"zopeni chkfn()","file is open",0);
return(0);
}
zincnt = 0; /* Reset input buffer */
if (n == ZSYSFN) { /* Input from a system function? */
/*** Note, this function should not be called with ZSYSFN ***/
/*** Always call zxcmd() directly, and give it the real file number ***/
/*** you want to use. ***/
debug(F110,"zopeni called with ZSYSFN, failing!",name,0);
*nambuf = '\0'; /* No filename. */
return(0); /* fail. */
#ifdef COMMENT
return(zxcmd(n,name)); /* Try to fork the command */
#endif
}
if (n == ZSTDIO) { /* Standard input? */
if (is_a_tty(0)) {
fprintf(stderr,"Terminal input not allowed");
debug(F110,"zopeni: attempts input from unredirected stdin","",0);
return(0);
}
fp[ZIFILE] = stdin;
ispipe[ZIFILE] = 0;
setmode(fileno(stdin),_O_BINARY);
return(1);
}
#ifdef CKROOT
debug(F111,"zopeni setroot",ckroot,ckrootset);
if (ckrootset)
if (!zinroot(name)) {
debug(F110,"zopeni setroot violation",name,0);
return(0);
}
#endif /* CKROOT */
if (n == ZIFILE || n == ZRFILE) {
ckstrncpy( os2filename, name, MAXPATH ) ;
errno = 0;
#ifdef NT
fp[n] = _fsopen(name,"rb",_SH_DENYWR); /* Binary mode */
if (fp[n])
_setmode(_fileno(fp[n]),_O_SEQUENTIAL);
else {
debug(F111,"zopeni ZI/ZR _fsopen failed","GetLastError",GetLastError());
}
#else
fp[n] = fopen(name,"rb");/* Binary mode */
#endif /* NT */
ispipe[ZIFILE] = 0;
if (fp[ZIFILE]) {
x = ferror(fp[ZIFILE]);
#ifdef ZDEBUG
printf("ZOPENI errno=%d\n",errno);
printf("ZOPENI ferror=%d\n",x);
#endif /* ZDEBUG */
}
#ifdef CK_LABELED
if (binary == XYFT_L) {
os2getattr(name);
os2geteas(name);
}
#endif /* CK_LABELED */
} else {
#ifdef NT
fp[n] = _fsopen(name,"rb",_SH_DENYWR); /* Real file, open it. */
if (fp[n])
_setmode(_fileno(fp[n]),_O_SEQUENTIAL);
else {
debug(F111,"zopeni _fsopen failed","GetLastError",GetLastError());
}
#else
fp[n] = fopen(name,"rb");/* Real file, open it. */
#endif /* NT */
}
debug(F111,"zopeni fopen", name, fp[n]);
#ifdef ZDEBUG
printf("ZOPENI fp[%d]=%ld\n",n,fp[n]);
#endif /* ZDEBUG */
ispipe[n] = 0;
if (xferlog
#ifdef CKSYSLOG
|| ckxsyslog >= SYSLG_FA && ckxlogging
#endif /* CKSYSLOG */
) {
getfullname(name);
debug(F110,"zopeni fullname",fullname,0);
}
if (fp[n] == NULL) {
#ifdef CKSYSLOG
if (ckxsyslog >= SYSLG_FA && ckxlogging) {
char buf[1024];
sprintf(buf,"file[%d] %s: open failed",n,fullname);
cksyslog(SYSLG_FA, 0, buf,NULL,NULL);
}
perror(fullname);
#else
perror(name);
#endif /* CKSYSLOG */
return(0);
} else {
#ifdef CKSYSLOG
if (ckxsyslog >= SYSLG_FA && ckxlogging) {
char buf[1024];
sprintf(buf,"file[%d] %s: open read ok", n, fullname);
cksyslog(SYSLG_FA, 1, buf, NULL, NULL);
}
#endif /* CKSYSLOG */
clearerr(fp[n]);
return(1);
}
}
/* Z O P E N O -- Open a new file for output. */
int
zopeno(n,name,zz,fcb)
/* zopeno */ int n; char *name; struct zattr *zz; struct filinfo *fcb; {
char p[8]; /* (===OS2 change===) */
int append = 0;
/* As of Version 5A, the attribute structure and the file information */
/* structure are included in the arglist. */
#ifdef DEBUG
debug(F111,"zopeno",name,n);
if (fcb) {
debug(F101,"zopeno fcb disp","",fcb->dsp);
debug(F101,"zopeno fcb type","",fcb->typ);
debug(F101,"zopeno fcb char","",fcb->cs);
} else {
debug(F100,"zopeno fcb is NULL","",0);
}
#endif /* DEBUG */
if (chkfn(n) != 0) /* Already open? */
return(0); /* Nothing to do. */
if ((n == ZCTERM) || (n == ZSTDIO)) { /* Terminal or standard output */
fp[ZOFILE] = stdout;
ispipe[ZOFILE] = 0;
#ifdef DEBUG
if (n != ZDFILE)
debug(F101,"zopeno fp[n]=stdout","",fp[n]);
#endif /* DEBUG */
zoutcnt = 0;
zoutptr = zoutbuffer;
return(1);
}
/* A real file. Open it in desired mode (create or append). */
#ifdef CKROOT
debug(F111,"zopeno setroot",ckroot,ckrootset);
if (ckrootset)
if (!zinroot(name)) {
debug(F110,"zopeno setroot violation",name,0);
return(0);
}
#endif /* CKROOT */
ckstrncpy(p,"w",8); /* Assume write/create mode */
if (fcb) { /* If called with an FCB... */
if (fcb->dsp == XYFZ_A) { /* Does it say Append? */
ckstrncpy(p,"a",8); /* Yes. */
debug(F100,"zopeno append","",0);
append = 1;
}
}
if (n == ZOFILE || n == ZSFILE) { /* OS/2 binary mode */
ckstrncpy( os2filename, name, MAXPATH ) ;
ckstrncat(p,"b",8);
}
if (xferlog
#ifdef CKSYSLOG
|| ckxsyslog >= SYSLG_FC && ckxlogging
#endif /* CKSYSLOG */
) {
getfullname(name);
debug(F110,"zopeno fullname",fullname,0);
}
debug(F110,"zopeno fopen arg",p,0);
#ifdef NT_COMMENT
/*
There is a good reason I never did this before. And that is
because Windows 95 does not support Overlapped I/O for disk
files nor does the C Run Time Library.
*/
if ( !isWin95() )
{
SECURITY_ATTRIBUTES security ;
HANDLE hFile = NULL;
memset(&security, 0, sizeof(SECURITY_ATTRIBUTES));
security.nLength = sizeof(SECURITY_ATTRIBUTES);
security.lpSecurityDescriptor = NULL ;
security.bInheritHandle = FALSE ;
hFile = CreateFile(name,
GENERIC_WRITE | GENERIC_READ,
FALSE, /* do not share */
&security,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL |
/* FILE_FLAG_OVERLAPPED |*/
FILE_FLAG_SEQUENTIAL_SCAN,
NULL);
if (hFile == INVALID_HANDLE_VALUE) {
fp[n] = NULL;
}
else {
int fd = _open_osfhandle( (int) hFile,
(n == ZOFILE || n == ZSFILE) ?
_O_BINARY : _O_TEXT );
if ( fd == -1 )
fp[n] = NULL;
else
fp[n] = _fdopen(fd, p);
}
} else
#endif /* NT_COMMENT */
{
fp[n] = fopen(name,p); /* Try to open the file */
}
ispipe[ZIFILE] = 0;
#ifdef ZDEBUG
printf("ZOPENO fp[%d]=%ld\n",n,fp[n]);
#endif /* ZDEBUG */
if (fp[n] == NULL) { /* Failed */
debug(F101,"zopeno failed errno","",errno);
#ifdef CKSYSLOG
if (ckxsyslog >= SYSLG_FC && ckxlogging) {
char buf[1024];
sprintf(buf, "file[%d] %s: %s failed (%m)",
n,
fullname,
append ? "append" : "create"
);
cksyslog(SYSLG_FC, 0, buf, NULL, NULL);
}
#endif /* CKSYSLOG */
} else { /* Succeeded */
#ifdef NT
if ( n == ZOFILE )
_setmode(_fileno(fp[n]),_O_SEQUENTIAL);
#endif /* NT */
if (n == ZDFILE || /* If it's the debug log */
n == ZTFILE ) /* or the transaction log */
setbuf(fp[n],NULL); /* make it unbuffered */
#ifdef CKSYSLOG
if (ckxsyslog >= SYSLG_FC && ckxlogging) {
char buf[1024];
sprintf(buf, "file[%d] %s: %s ok",
n, fullname, append ? "append" : "create");
cksyslog(SYSLG_FC, 1, buf, NULL, NULL);
}
#endif /* CKSYSLOG */
debug(F100, "zopeno ok", "", 0);
}
zoutcnt = 0; /* (PWP) reset output buffer */
zoutptr = zoutbuffer;
return((fp[n] != NULL) ? 1 : 0);
}
/* Z C L O S E -- Close the given file. */
/* Returns 0 if arg out of range, 1 if successful, -1 if close failed. */
int
zclose(n) int n; {
int x, x2;
extern long ffc;
debug(F101,"zclose","",n);
if (chkfn(n) < 1) return(0); /* Check range of n */
if ((n == ZOFILE) && (zoutcnt > 0)) /* (PWP) output leftovers */
x2 = zoutdump();
else
x2 = 0;
x = 0; /* Initialize return code */
if (fp[ZSYSFN] || ispipe[n]) { /* If file is really pipe */
x = zclosf(n); /* do it specially */
debug(F101,"zclose zclosf","",x);
debug(F101,"zclose zclosf fp[n]","",fp[n]);
} else {
if ((fp[n] != stdout) && (fp[n] != stdin))
x = fclose(fp[n]);
fp[n] = NULL;
#ifdef CK_LABELED
if (binary == XYFT_L) {
debug(F111,"zclose LABELED","file number",n);
if (n == ZOFILE) {
debug(F111,"zclose LABELED",
"lf_opts && LBL_EXT",
lf_opts && LBL_EXT
);
if (lf_opts && LBL_EXT)
os2seteas(os2filename);
os2setattr(os2filename);
} else if (n == ZIFILE && pFEAList) {
FreeMem(pFEAList);
pFEAList = 0;
}
}
#endif /* CK_LABELED */
}
iflen = -1L; /* Invalidate file length */
if (x == EOF) { /* if we got a close error */
debug(F101,"zclose fclose fails","",x);
return(-1);
} else if (x2 < 0) { /* or error flushing last buffer */
debug(F101,"zclose error flushing last buffer","",x2);
return(-1); /* then return an error */
} else {
/* Print log record compatible with wu-ftpd */
if (xferlog && (n == ZIFILE || n == ZOFILE)) {
char * s, *p;
extern char ttname[];
if (!iklogopen) (VOID) doiklog(); /* Open log if necessary */
debug(F101,"zclose iklogopen","",iklogopen);
if (iklogopen) {
int len;
timenow = time(NULL);
#ifdef CK_LOGIN
if (logged_in)
s = clienthost;
else
#endif /* CK_LOGIN */
s = (char *)ttname;
if (!s) s = "";
if (!*s) s = "*";
#ifdef CK_LOGIN
if (logged_in) {
p = guestpass;
if (!*p) p = "*";
} else
#endif /* CK_LOGIN */
p = whoami();
len = 24 + 12 + strlen(s) + 16
+ strlen(fullname) + 1 + 1 + 1 + 1
+ strlen(p) + 6 + 2 + 12;
if ( len > sizeof(iksdmsg) )
sprintf(iksdmsg, /* SAFE */
"%.24s [BUFFER WOULD OVERFLOW]\n",
ctime(&timenow));
else
sprintf(iksdmsg, /* SAFE */
"%.24s %d %s %ld %s %c %s %c %c %s %s %d %s\n",
ctime(&timenow), /* date/time */
gtimer(), /* elapsed secs */
s, /* peer name */
ffc, /* byte count */
fullname, /* full pathname of file */
(binary ? 'b' : 'a'), /* binary or ascii */
"_", /* options = none */
n == ZIFILE ? 'o' : 'i', /* in/out */
#ifdef CK_LOGIN
(isguest ? 'a' : 'r'), /* User type */
#else
'r',
#endif /* CK_LOGIN */
p, /* Username or guest passwd */
#ifdef CK_LOGIN
logged_in ? "iks" : "kermit", /* Record ID */
#else
"kermit",
#endif /* CK_LOGIN */
0, /* User ID on client system unknown */
"*" /* Ditto */
);
debug(F110,"zclose iksdmsg",iksdmsg,0);
write(xferlog, iksdmsg, (int)strlen(iksdmsg));
}
}
debug(F101,"zclose returns","",1);
return(1);
}
}
/* Z C H I N -- Get a character from the input file. */
/* Returns -1 if EOF, 0 otherwise with character returned in argument */
int
zchin(n,c) int n; int *c; {
int a;
#ifdef IKSD
if (inserver && !local && (n == ZCTERM || n == ZSTDIO)) {
a = coninc(0);
if (a < 0)
return(-1);
} else
#endif /* IKSD */
/* (PWP) Just in case this gets called when it shouldn't. */
if (n == ZIFILE) {
a = zminchar();
if (a < 0)
return(-1);
} else {
a = getc(fp[n]);
if (a == EOF)
return(-1);
}
#ifdef CK_CTRLZ