File: lurkftp.c

package info (click to toggle)
lurkftp 1.00-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 324 kB
  • ctags: 375
  • sloc: ansic: 4,551; sh: 186; makefile: 162
file content (542 lines) | stat: -rw-r--r-- 13,805 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
/*
 * lurkftp - lurk around FTP sites, possibly grabbing things
 * 
 * (C) 1997 Thomas J. Moore
 * questions/comments -> dark@mama.indstate.edu
 * 
 * This is free software.  No warranty for applicability or fitness is
 * expressed or implied.
 * 
 * This code may be modified and distributed freely so long as the original
 * author is credited and any changes are documented as such.
 * 
 */

#include "lurkftp.h"
#include <sys/wait.h>
#include <utime.h>
#include <sys/ipc.h>
#include <sys/sem.h>

int debug;

char *progname;
char *curstat;

char *reptpipe = NULL;

int bg = 0;

static int anyfork = 0;

FILE *of;
static int oflock = -1; /* semaphore */

char scratch[4096]; /* temp. buffer */

/* fork() support: output file locking */
void lockof(void)
{
    struct sembuf sop;

    if(oflock < 0)
      return;
    if(debug & DB_LOCK)
      fputs("Attempting to lock output file...\n",stderr);
    sop.sem_num = 0;
    sop.sem_op = -1;
    sop.sem_flg = 0; /* maybe SEM_UNDO */
    errno = 0;
    while(semop(oflock,&sop,1) < 0 && errno == EAGAIN) errno = 0;
    if(errno)
      perror("semop");
    if(debug & DB_LOCK)
      fputs("Output file locked\n",stderr);
}

void unlockof(void)
{
    struct sembuf sop;

    fflush(of);
    if(oflock < 0)
      return;
    sop.sem_num = 0;
    sop.sem_op = 1;
    sop.sem_flg = 0;
    semop(oflock,&sop,1);
    if(debug & DB_LOCK)
      fputs("Output file unlocked\n",stderr);
}

static void rmoflock(void)
{
#ifdef SEMCTL_NEEDS_ARG
    union semun arg;
#endif

    if(anyfork) {
#ifdef SEMCTL_NEEDS_ARG
	arg.val = 0;
	semctl(oflock,0,IPC_RMID,arg);
#else
	semctl(oflock,0,IPC_RMID);
#endif
    }
}

static int getoflock(void)
{
    if(oflock >= 0)
      return 1;
    oflock = semget(IPC_PRIVATE,1,IPC_CREAT|S_IRWXU);
    if(oflock < 0)
      return 0;
    atexit(rmoflock);
    unlockof();
    return 1;
}

/* fork() support: kill all children */
static void killchld(void)
{
    if(anyfork) {
	signal(SIGTERM,SIG_IGN);
	kill(-getpgrp(),SIGTERM);
    }
}

static int file_read(FILE *f, void *buf, int buflen)
{
    return fread(buf, buflen, 1, f);
}

static int read_dir(struct ftpdir *dir, char manrecurs, regex_t *xfilt, int nxfilt)
{
    FILE *lf;
    int err;

    switch(dir->type) {
      case ST_FTPD:
	return readftpdir(&dir->dirm, &dir->site, dir->dirs, dir->ndirs,
			  manrecurs, xfilt, nxfilt);
      case ST_LDIR:
	return readtree(&dir->dirm,dir->dir0);
      case ST_MFILE:
	headsubst(scratch,dir->lsfile,dir,NULL,NULL);
	if((lf = fopen(scratch, "r"))) {
	    err = readlsfile(&dir->dirm, lf, dir->dir0);
	    fclose(lf);
	    return err;
	} else {
	    lockof();
	    fprintf(of,"*** %s doesn't exist! ***\n",scratch);
	    unlockof();
	    return ERR_DIR;
	}
      case ST_LSLRF:
	headsubst(scratch,dir->lsfile,dir,NULL,NULL);
	if((lf = fopen(scratch, "r"))) {
	    err = parsels(&dir->dirm, (read_func)readf, lf, dir->dir0, dir->dir0);
	    fclose(lf);
	    return err;
	} else {
	    lockof();
	    fprintf(of,"*** %s doesn't exist! ***\n",scratch);
	    unlockof();
	    return ERR_DIR;
	}
      case ST_FTPLSLRF:
	return readftplsf(&dir->dirm, &dir->site, dir->dir0, dir->lsfile);
      case ST_CMD:
	headsubst(scratch,dir->lsfile,dir,NULL,NULL);
	if((lf = popen(scratch, "r"))) {
	    err = parsels(&dir->dirm, (read_func)file_read, lf, dir->dir0,
			  dir->dir0);
	    pclose(lf);
	    return err;
	} else {
	    lockof();
	    fprintf(of,"*** Can't execute %s! ***\n",scratch);
	    unlockof();
	    return ERR_OS;
	}
    }
    return 0;
}


void filterdir(struct dirmem *dir, const regex_t *filter, int nfilter, int cut)
{
    struct fname **p, **q;
    int i, cnt = dir->count;

    for(p=q=dir->array,i=dir->count;i--;p++) {
	strcpy(scratch,(*p)->dir);
	strcat(scratch,(*p)->name);
	if(S_ISLNK((*p)->mode)) {
	    strcat(scratch," -> ");
	    strcat(scratch,(*p)->lnk);
	}
	if(!match_lp(scratch, filter, nfilter) == !cut)
	  *q++ = *p;
	else {
	    if(debug & DB_OTHER)
	      fprintf(stderr,"Filtering out %s\n",scratch);
	    cnt--;
	}
    }
    dir->count = cnt;
}

static void filt_dir(struct dirmem *dir, const char *ifilter, regex_t *iregex,
		     int niregex, const char *xfilter, regex_t *xregex,
		     int nxregex, char filtdir, char filtspec)
{
    struct fname **o, **n;
    int i, j;

    if(ifilter) {
	if(debug & DB_TRACE)
	  fprintf(stderr,"Filtering out all but '%s'\n",ifilter);
	filterdir(dir, iregex, niregex, 0);
    }
    if(xfilter) {
	if(debug & DB_TRACE)
	  fprintf(stderr,"Filtering out '%s'\n",xfilter);
	filterdir(dir, xregex, nxregex, 1);
    }
    if(filtdir) {
	for(o=n=dir->array,j=0,i=dir->count;i;i--,o++)
	  if(!S_ISDIR((*o)->mode)) {
	      *n++ = *o;
	      j++;
	  }
	dir->count = j;
	}
	if(filtspec) {
	    for(o=n=dir->array,j=0,i=dir->count;i;i--,o++)
	      if(!S_ISSOCK((*o)->mode) && !S_ISCHR((*n)->mode) &&
		 !S_ISBLK((*o)->mode) && !S_ISFIFO((*n)->mode)) {
		  *n++ = *o;
		  j++;
	      }
	    dir->count = j;
	}
}

#if TIMECORRECT
void timecorrect(struct ftpdir *dir)
{
    int i;
    struct fname **n;
    struct tm tm;

    /* time-correct */
    dir->timecorrect = 0;
    if(dir->type == ST_FTPD && dir->dirm.count) {
	/* since this list sorted in date-first ascending order, get last */
	/* (i.e. most recent) entry & see if it matches file's MDTM date */
	for(i=dir->dirm.count,n=dir->dirm.array+i-1;i;i--,n--)
	  if(S_ISREG((*n)->mode)) /* only files have important dates */
	    break;
	if(i) { /* if there was any file at all, n points to most recent */
	    strcpy(scratch,(*n)->dir);
	    strcat(scratch,(*n)->name);
	    if(!ftp_filetm(&dir->site,scratch,&tm) && /* mdtm command successful */
	       (tm.tm_year != (*n)->year-1900 || tm.tm_mon != (*n)->month-1 ||
		tm.tm_mday != (*n)->day ||
		((*n)->hr >= 0 && tm.tm_hour != (*n)->hr) ||
		((*n)->min >= 0 && tm.tm_min != (*n)->min))) {
		time_t t1, t2;
		int j;

		/* ugh! remote ls returns inaccurate times! */
		/* see how inaccurate it is.. */
		t1 = mktime(&tm);
		tm.tm_year = (*n)->year-1900;
		tm.tm_mon = (*n)->month-1;
		tm.tm_mday = (*n)->day;
		if((*n)->hr >= 0)
		  tm.tm_hour = (*n)->hr;
		else
		  tm.tm_hour = 12; /* why not? */
		if((*n)->min >= 0)
		  tm.tm_min = (*n)->min;
		else
		  tm.tm_min = 0;
		tm.tm_sec = 0;
		t2 = mktime(&tm);
		t2 = (time_t)difftime(t1,t2);
		j = (int)(t2<0?-t2:t2);
		if(j%(60*60))
		    dir->timecorrect = t2 < 0 ? -1 : 1;
		if(debug & DB_TRACE)
		    fprintf(stderr,"Correcting time by %s%d:%02d:%02d:%02d\n",
			    t2<0?"-":"",j/(24*60*60),j/(60*60)%24,j/60%60,j%60);
		for(i=dir->dirm.count,n=dir->dirm.array;i;i--,n++) {
		    tm.tm_year = (*n)->year-1900;
		    tm.tm_mon = (*n)->month-1;
		    tm.tm_mday = (*n)->day;
		    if((*n)->hr >= 0)
			tm.tm_hour = (*n)->hr;
		    else
			tm.tm_hour = t2 < 0 ? 23 : 0;
		    if((*n)->min >= 0)
		      tm.tm_min = (*n)->min;
		    else
			tm.tm_min = t2 < 0 ? 59 : 00;
		    tm.tm_sec = t2 < 0 ? 59 : 0;
		    t1 = mktime(&tm) + t2;
		    if(debug & DB_TRACE)
			fprintf(stderr, "Correcting %d-%02d-%02d %02d:%02d to ",
				tm.tm_year + 1900, tm.tm_mon + 1,
				tm.tm_mday, tm.tm_hour, tm.tm_min);
		    tm = *localtime(&t1);
		    if(debug & DB_TRACE)
			fprintf(stderr, "%d-%02d-%02d %02d:%02d\n",
				tm.tm_year + 1900, tm.tm_mon + 1,
				tm.tm_mday, tm.tm_hour, tm.tm_min);
		    (*n)->year = tm.tm_year+1900;
		    (*n)->month = tm.tm_mon+1;
		    (*n)->day = tm.tm_mday;
		    if((*n)->hr >= 0)
		      (*n)->hr = tm.tm_hour;
		    if((*n)->min >= 0)
		      (*n)->min = tm.tm_min;
		}
	    }
	}
    }
}
#endif

static void process_all(void)
{
    struct op *op;
    int i;
    pid_t pid = -1;
    struct fname **mirarray;
    int ndel, nadd, malen;
    int domir;
    char *s;

    if(ops)
      ops->dep = 0; /* can't depend on nothing */
    for(op = ops;op;op = op->next) {
	/* in case of continue when forked, quit */
	if(!pid && !op->dep)
	  exit(0);

	/* set up main listing file */
	if(!of && (!reptpipe || !(of = tmpfile())))
	  of = stdout;

	/* clean up zombie processes */
	if(pid && oflock >= 0)
	  while((i = waitpid(-1,NULL,WNOHANG)) > 0 || (i && errno != ECHILD));
	errno = 0;

	/* prepare for fork */
	if(op->dofork && !op->dep && getoflock()) {
	    if((pid = fork()) > 0) { /* parent */
		anyfork = 1;
		while(op->next && op->next->dep)
		  op = op->next;
		continue;
	    }
	    if(!pid) { /* child */
		anyfork = 0; /* no children */
		if(debug & DB_TRACE)
		  fprintf(stderr,"Forked pid %ld\n",(unsigned long)getpid());
		if(debug & DB_OTHER) /* give gdb time to attach */
		  sleep(5);
	    }
	    /* here, it's either the child or no child was created */
	}

	/* perform actual processing: */
	strcpy(curstat, op->srcls.site.host);
	for(s = curstat+strlen(curstat); *s || s[1]; s++)
	  *s = 0;
	
	domir = op->trymir && op->domirop;

	/* read "remote" dir */
	if(debug & DB_TRACE)
	  fputs("Reading remote directory\n",stderr);
	i = read_dir(&op->srcls, op->manrecurs, op->xfilter?&op->xregex:NULL,
		     op->xnre);
	if(i) {
	    if(!op->quiet) {
		lockof();
		fprintf(of,"*** %s %s: %s ***\n",op->srcls.site.host,
			op->srcls.dir0, ftperr(i));
		unlockof();
	    }
	    continue;
	}
	/* run filters on it */
	filt_dir(&op->srcls.dirm, op->ifilter, &op->iregex, op->inre,
		 op->xfilter, &op->xregex, op->xnre, op->filtdir, op->filtspec);
	/* check if anything left after filtering */
	if(!op->srcls.dirm.count) {
	    lockof();
	    fprintf(of,"*** %s %s: Nothing left after filter ***\n",
		    op->srcls.site.host,op->srcls.dir0);
	    unlockof();
	    continue;
	}
	/* sort; ready for diffing. */
	sort((const struct fname **)op->srcls.dirm.array, op->srcls.dirm.count,
	     op->detmove?MVCMP:DIFFCMP);
#if TIMECORRECT
	if(op->srcls.type != ST_LDIR && op->srcls.type != ST_MFILE)
	  timecorrect(&op->srcls);
#endif
	/* read "local" dir */
	if(debug & DB_TRACE)
	  fputs("Reading local directory\n",stderr);
	i = read_dir(&op->dstls,op->manrecurs,
		     op->xfilter?&op->xregex:NULL, op->xnre);
	if(i && i != ERR_DIR) {
	    fprintf(stderr,"Internal error: %s\n", ftperr(i));
	    continue;
	}
	/* sort; ready for diffing. */
	sort((const struct fname **)op->dstls.dirm.array, op->dstls.dirm.count,
	     op->detmove?MVCMP:DIFFCMP);
#if TIMECORRECT
	if(op->dstls.type != ST_LDIR && op->dstls.type != ST_MFILE)
	  timecorrect(&op->dstls);
#endif
	/* diff listings */
	mirarray = diff_dir(&op->srcls, &op->dstls, &malen, &nadd, &ndel,
			    op->detmove, op->mirmodes && domir,
			    op->mirappend && !op->dstls.pipe);
	/* report differences */
	if(debug & DB_TRACE)
	  fputs("Printing report\n",stderr);
	if(!op->quiet && (nadd || ndel))
	  pr_rept(mirarray, malen, nadd, ndel, op->rhead?op->rhead:DEFHEAD,
		  op->rtail?op->rtail:DEFTAIL, &op->srcls, &op->dstls,
		  op->rfmt?op->rfmt:(op->trymir? /* or maybe domir? */
				     (op->filtdir?"%[d:" DEFFMT "]":DEFFMT):
				     (op->filtdir?
				      "%[dT<T>TM:" DEFFMT "]":
				      "%[T<T>TM:" DEFFMT "]")),
		  op->rsort?op->rsort:REPTCMP, op->rsort || op->detmove);
	/* now perform mirroring functions */
	if(domir) {
	    if(op->mirmodes && nadd)
	      mir_chmod(&op->dstls, mirarray, nadd);
	    if(!op->mirstmp && ndel)
	      mir_del(&op->dstls, mirarray+malen-ndel,ndel, op->filtdir);
	    if(op->detmove && ndel && nadd)
	      mir_move(&op->dstls, mirarray, malen, &nadd, &ndel);
	    if(nadd)
 	      mir_transfer(mirarray, nadd, &op->srcls, &op->dstls,
			   op->mirstmp, op->mirmodes, op->mirappend);
	    mir_errrpt(mirarray, mirarray+malen-ndel, nadd, ndel,
		       op->ehead?op->ehead:DEFEHEAD,
		       op->etail?op->etail:DEFETAIL, &op->dstls, &op->srcls,
		       op->filtdir?"%[d:" DEFFMT "]":DEFFMT);
	}
	/* write out new list file */
	/* deferred until here so that failed transfers aren't added */
	if((ndel || nadd) && op->dstls.type == ST_MFILE) {
	    headsubst(scratch,op->dstls.lsfile,&op->dstls,NULL,NULL);
	    if((i = writelsfile(scratch,&op->srcls.dirm,domir)))
	      if(!op->quiet) {
		  lockof();
		  fprintf(of,"*** %s %s: %s ***\n",op->srcls.site.host,
			  op->srcls.dir0, ftperr(i));
		  unlockof();
	      }
	}
	/* reset variables between site/dir lists */
	ftp_closeconn(&op->srcls.site); /* in case file or dir retrieval was in progress */
	ftp_closeconn(&op->dstls.site); /* in case file or dir retrieval was in progress */
	/* in case we got here when forked, quit */
	if(!pid && (!op->next || !op->next->dep))
	  exit(0);
	freedir(&op->srcls.dirm); /* dstls on same dirm */
	if(mirarray) {
	    free(mirarray);
	    mirarray = NULL;
	}
    }
    if(!pid)
      exit(0);
}

static void cleanexit(int ret)
{
    FILE *f;
    int i;

    /* wait for all children */
    if(oflock >= 0)
      while(waitpid(-1,NULL,0) > 0 || errno != ECHILD);
    anyfork = 0;

    /* pipe out the report */
    if(reptpipe && of != stdout && ftell(of) > 0) {
	f = popen(reptpipe,"w");
	rewind(of);
	while((i = nosig_fread(scratch, 1, 2048, of)) > 0)
	  nosig_fwrite(scratch,i,1,f);
	pclose(f);
    }
    if(of != stdout)
      fclose(of);

    exit(ret);
}

void ignsig(int ign)
{
}

int main(int argc, char **argv)
{
    time_t tm;
    pid_t pid;
    struct sigaction sa;

    ftp_init(argv[0]);

    progname = argv[0];
    curstat = argv[1];

    /* for dir listings -- mainly current year/month */
    time(&tm);
    memcpy(&curtm,localtime(&tm),sizeof(curtm));

    atexit(killchld);
    sigaction(SIGCHLD, NULL, &sa);
    sa.sa_handler = ignsig;
    sa.sa_flags |= SA_RESTART|SA_NOCLDSTOP;
    sa.sa_flags &= ~SA_RESETHAND;
    sigaction(SIGCHLD, &sa, NULL);

    parseargs(argc, argv); /* parse all args */

    if(bg) { /* dissociate if requested */
	freopen("/dev/null","w",stdout);
	fclose(stdin);
	freopen("/dev/null","w",stderr);
	pid = fork();
	if(pid > 0) {
	    anyfork = 0;
	    exit(0);
	} else if(!pid)
	    setsid();
    }

    process_all();

    cleanexit(0);

    return 0;
}