File: rcfile.c

package info (click to toggle)
sitecopy 1:0.16.6-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 5,668 kB
  • ctags: 5,783
  • sloc: ansic: 48,856; sh: 3,820; makefile: 774; yacc: 291; awk: 58; sed: 27
file content (885 lines) | stat: -rw-r--r-- 25,083 bytes parent folder | download | duplicates (2)
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
/* 
   sitecopy, for managing remote web sites.
   Copyright (C) 1998-2006, Joe Orton <joe@manyfish.co.uk>
                                                                     
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
  
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
  
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include <config.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <ctype.h>
#include <errno.h>
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif 
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif /* HAVE_STDLIB_H */
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */

#include <ne_string.h>
#include <ne_alloc.h>

#include "common.h"
#include "netrc.h"
#include "rcfile.h"
#include "sites.h"

/** Global variables **/
char *copypath;
char *rcfile;
char *netrcfile;
char *home;
int havenetrc;

/* These are used for reporting errors back to the calling procedures. */
int rcfile_linenum; 
char *rcfile_err;

/** Not quite so global variables **/

/* These are appended to $HOME */
#define RCNAME "/.sitecopyrc"
#define COPYNAME "/.sitecopy/"
#define NETRCNAME "/.netrc"

/* Stores the list of entries in the ~/.netrc */
netrc_entry *netrc_list;

const char *rc_get_netrc_password(const char *server, const char *username);

/* The driver definitions */
#ifdef USE_FTP
extern const struct proto_driver ftp_driver;
#endif /* USE_FTP */
#ifdef USE_DAV
extern const struct proto_driver dav_driver;
#endif /* USE_DAV */
#ifdef USE_RSH
extern const struct proto_driver rsh_driver;
#endif /* USE_RSH */
#ifdef USE_SFTP
extern const struct proto_driver sftp_driver;
#endif /* USE_SFTP */

/* rcfile_read will read the rcfile and fill given sites list.
 * This returns 0 on success, RC_OPENFILE if the rcfile could not
 * be read, or RC_CORRUPT if the rcfile was corrupt.
 * If it is corrupt, rcfile_linenum and rcfile_line are set to the
 * the corrupt line.
 */
#define LINESIZE 128
int rcfile_read(struct site **sites) 
{
    FILE *fp;
    int state, last_state=8, ret=0;
    int alpha, hash;
    char buf[LINESIZE];
    char *ch;
    char *ptr, key[LINESIZE], val[LINESIZE], val2[LINESIZE];
    /* Holders for the site info, and default site settings */
    struct site *this_site, *last_site, default_site = {0};
    
    if ((fp = fopen(rcfile, "r")) == NULL) {
	rcfile_err = strerror(errno);
	return RC_OPENFILE;
    } 
    
    default_site.perms = sitep_ignore;
    default_site.symlinks = sitesym_follow;
    default_site.protocol = siteproto_ftp;
    default_site.proto_string = ne_strdup("ftp");

    default_site.ftp_pasv_mode = true;
    default_site.ftp_use_cwd = false;
    
    last_site = this_site = NULL;
    rcfile_linenum = 0;
    rcfile_err = NULL;

    while ((ret==0) && (fgets(buf, sizeof(buf), fp) != NULL)) {
	rcfile_linenum++;
	/* Put the line without the LF into the error buffer */
	if (rcfile_err != NULL) free(rcfile_err);
	rcfile_err = ne_strdup(buf);
	ptr = strchr(rcfile_err, '\n');
	if (ptr != NULL) *ptr = '\0';
	state = 0;
	ptr = key;
	memset(key, 0, LINESIZE);
	memset(val, 0, LINESIZE);
	memset(val2, 0, LINESIZE);
	for (ch=buf; *ch!='\0'; ch++) {
	    alpha = !isspace((unsigned)*ch); /* well, alphaish */
	    hash = (*ch == '#');
	    switch (state) {
	    case 0: /* whitespace at beginning of line */
		if (hash) {
		    state = 8;
		} else if (alpha) {
		    *(ptr++) = *ch;
		    state = 1;
		}
		break;
	    case 1: /* key */
		if (hash) {
		    state = 8;
		} else if (!alpha) {
		    ptr = val;
		    state = 2;
		} else {
		    *(ptr++) = *ch;
		}
		break;
	    case 2: /* whitespace after key */
		if (hash) {
		    state = 8;
		} else if (*ch == '"') {
		    state = 4; /* begin quoted value */
		} else if (alpha) {
		    *(ptr++) = *ch;
		    state = 3;
		} 
		break;
	    case 3: /* unquoted value 1 */
		if (hash) {
		    state = 8;
		} else if (!alpha) {
		    ptr = val2;
		    state = 5;
		} else {
		    *(ptr++) = *ch;
		}
		break;
	    case 4: /* quoted value 1 */
		if (*ch == '"') {
		    ptr = val2;
		    state = 5;
		} else if (*ch == '\\') {
		    last_state = 4;
		    state = 9;
		} else {
		    *(ptr++) = *ch;
		}
		break;
	    case 5: /* whitespace after value 1 */
		if (hash) {
		    state = 8;
		} else if (*ch == '"') {
		    state = 6; /* begin quoted value 2 */
		} else if (alpha) {
		    *(ptr++) = *ch;
		    state = 7; /* begin unquoted value 2 */
		} 
		break;
	    case 6: /* quoted value 2 */
		if (*ch == '"') {
		    state = 8;
		} else if (*ch == '\\') {
		    last_state = 4;
		    state = 9;
		} else {
		    *(ptr++) = *ch;
		}
		break;
	    case 7: /* unquoted value 2 */
		if (hash) {
		    state = 8;
		} else if (!alpha) {
		    state = 8;
		} else {
		    *(ptr++) = *ch;
		}
		break;
	    case 8: /* ignore till end of line */
		break;
	    case 9: /* a literal (\-slashed) in a value */
		*(ptr++) = *ch;
		state = last_state;
		break;
	    }
	}
	
	NE_DEBUG(DEBUG_RCFILE, "Key [%s] Value: [%s] Value2: [%s]\n", key, val, val2);
	
	if (strlen(key) == 0) {
	    continue;
	}
	if (strlen(val) == 0) {
	    /* A key with no value. */
	    if (this_site == NULL) {
		if (strcmp(key, "default") == 0) {
		    /* Setting up the default site */
		    NE_DEBUG(DEBUG_RCFILE, "Default site entry:\n");
		    this_site = &default_site;
		} else {
		    /* Need to be in a site! */
		    ret = RC_CORRUPT;
		}
	    } else if (strcmp(key, "nodelete") == 0) {
		this_site->nodelete = true;
	    } else if (strcmp(key, "checkmoved") == 0) {
		this_site->checkmoved = true;
	    } else if (strcmp(key, "nooverwrite") == 0) {
		this_site->nooverwrite = true;
	    } else if (strcmp(key, "lowercase") == 0) {
		this_site->lowercase = true;
	    } else if (strcmp(key, "safe") == 0) {
		this_site->safemode = true;
	    } else if (strcmp(key, "tempupload") == 0) {
		this_site->tempupload = true;
	    } else {
		ret = RC_CORRUPT;
	    }
	} else if (strlen(val2) == 0) {
	    /* A key with a single value. */
	    if (strcmp(key, "site") == 0) {
		/* Beginning of a new Site */
		if (this_site != &default_site)
		    last_site = this_site;
		/* Allocate new item */
		this_site = ne_calloc(sizeof(struct site));
		/* Copy over the defaults */
		memcpy(this_site, &default_site, sizeof(struct site));
		/* Deep-copy the string lists */
		this_site->excludes = fnlist_deep_copy(default_site.excludes);
		this_site->ignores = fnlist_deep_copy(default_site.ignores);
		this_site->asciis = fnlist_deep_copy(default_site.asciis);
		this_site->prev = last_site;
		if (last_site != NULL) { /* next site */
		    last_site->next = this_site;
		} else { /* First site */
		    *sites = this_site;
		}		
		this_site->name = ne_strdup(val);
		this_site->files = NULL;
		this_site->proto_string = ne_strdup(default_site.proto_string);
		/* Now work out the info filename */
		this_site->infofile = ne_concat(copypath, val, NULL);
                this_site->certfile = ne_concat(copypath, val, ".crt", NULL);
	    } else if (this_site == NULL) {
		ret = RC_CORRUPT;
	    } else if (strcmp(key, "username") == 0) {
		/* username */
		this_site->server.username = ne_strdup(val);
	    } else if (strcmp(key, "server") == 0) {
		this_site->server.hostname = ne_strdup(val);
	    } else if (strcmp(key, "port") == 0) {
		this_site->server.port = atoi(val);
	    } else if (strcmp(key, "proxy-server") == 0) {
		this_site->proxy.hostname = ne_strdup(val);
	    } else if (strcmp(key, "proxy-port") == 0) {
		this_site->proxy.port = atoi(val);
	    } else if (strcmp(key, "proxy-password") == 0) {
		this_site->proxy.password = ne_strdup(val);
	    } else if (strcmp(key, "proxy-username") == 0) {
		this_site->proxy.username = ne_strdup(val);
	    } else if (strcmp(key, "password") == 0) {
		this_site->server.password = ne_strdup(val);
	    } else if (strcmp(key, "url") == 0) {
	        this_site->url = ne_strdup(val);
	    } else if (strcmp(key, "remote") == 0) {
		/* Relative filenames must start with "~/" */
		if (val[0] == '~') {
		    if (val[1] == '/') {
			this_site->remote_isrel = true;
		    } else {
			ret = RC_CORRUPT;
		    }
		} else {
		    /* Dirname doesn't begin with "~/" */
		    this_site->remote_isrel = false;
		}
		if (val[strlen(val)-1] != '/')
		    strcat(val, "/");
		this_site->remote_root_user = ne_strdup(val);
	    } else if (strcmp(key, "local") == 0) {
		/* Relative filenames must start with "~/" */
		if (val[0] == '~') {
		    if (val[1] == '/') {
			this_site->local_isrel = true;
		    } else {
			ret = RC_CORRUPT;
		    }
		} else { 
		    /* Dirname doesn't begin with a "~/" */
		    this_site->local_isrel = false;
		}
		if (val[strlen(val)-1] != '/')
		    strcat(val, "/");
		this_site->local_root_user = ne_strdup(val);
	    } else if (strcmp(key, "permissions") == 0) {
		if (strcmp(val, "ignore") == 0) {
		    this_site->perms = sitep_ignore;
                    this_site->dirperms = 0;
		} else if (strcmp(val, "exec") == 0) {
		    this_site->perms = sitep_exec;
		} else if (strcmp(val, "all") == 0) {
		    this_site->perms = sitep_all;
                } else if (strcmp(val, "dir") == 0) {
                    this_site->dirperms = 1;
		} else {
		    ret = RC_CORRUPT;
		}
	    } else if (strcmp(key, "symlinks") == 0) {
		if (strcmp(val, "follow") == 0) {
		    this_site->symlinks = sitesym_follow;
		} else if (strcmp(val, "maintain") == 0) {
		    this_site->symlinks = sitesym_maintain;
		} else if (strcmp(val, "ignore") == 0) {
		    this_site->symlinks = sitesym_ignore;
		} else {
		    ret = RC_CORRUPT;
		}
	    } else if (strcmp(key, "exclude") == 0) {
		struct fnlist *f = fnlist_prepend(&this_site->excludes);
		if (val[0] == '/') {
		    f->pattern = ne_strdup(val+1);
		    f->haspath = true;
		} else {
		    f->pattern = ne_strdup(val);
		    f->haspath = false;
		}
	    } else if (strcmp(key, "ignore") == 0) {
		struct fnlist *f = fnlist_prepend(&this_site->ignores);
		if (val[0] == '/') {
		    f->pattern = ne_strdup(val+1);
		    f->haspath = true;
		} else {
		    f->pattern = ne_strdup(val);
		    f->haspath = false;
		}
	    } else if (strcmp(key, "ascii") == 0) {
		struct fnlist *f = fnlist_prepend(&this_site->asciis);
		if (val[0] == '/') {
		    f->pattern = ne_strdup(val+1);
		    f->haspath = true;
		} else {
		    f->pattern = ne_strdup(val);
		    f->haspath = false;
		}
	    } else if (strcmp(key, "protocol") == 0) {
		if (strcasecmp(val, "ftp") == 0) {
		    this_site->protocol = siteproto_ftp;
		} else if (strcasecmp(val, "http") == 0 || 
			   strcasecmp(val, "dav") == 0 ||
			   strcasecmp(val, "webdav") == 0) {
		    this_site->protocol = siteproto_dav;
		} else if (strcasecmp(val, "rsh") == 0) {
		    this_site->protocol = siteproto_rsh;
		} else if (strcasecmp(val, "ssh") == 0) {
                    this_site->protocol = siteproto_rsh;
                    if (this_site->rsh_cmd == NULL) 
                        this_site->rsh_cmd = ne_strdup("ssh");
                    if (this_site->rcp_cmd == NULL) 
                        this_site->rcp_cmd = ne_strdup("scp");
		} else if (strcasecmp(val, "sftp") == 0) {
		    this_site->protocol = siteproto_sftp;
                } else {
		    this_site->protocol = siteproto_unknown;
		}
		free(this_site->proto_string);
		this_site->proto_string = ne_strdup(val);
	    } else if (strcmp(key, "ftp") == 0) {
		if (strcmp(val, "nopasv") == 0) {
		    this_site->ftp_pasv_mode = false;
		} else if (strcmp(val, "showquit") == 0) {
		    this_site->ftp_echo_quit = true;		    
		} else if (strcmp(val, "usecwd") == 0) {
		    this_site->ftp_use_cwd = true;		    
		} else if (strcmp(val, "nousecwd") == 0) {
		    this_site->ftp_use_cwd = false;		    
		} else {
		    ret = RC_CORRUPT;
		}
	    } else if (strcmp(key, "http") == 0) {
		if (strcmp(val, "expect") == 0) {
		    this_site->http_use_expect = true;
		} else if (strcmp(val, "limit") == 0) {
		    this_site->http_limit = true;
		} else if (strcmp(val, "secure") == 0) {
		    this_site->http_secure = true;
		} else if (strcmp(val, "tolerant") == 0) {
		    this_site->http_tolerant = true;
		} else {		    
		    ret = RC_CORRUPT;
		}
            } else if (strcmp(key, "client-cert") == 0) {
                this_site->client_cert = ne_strdup(val);
	    } else if (strcmp(key, "rsh") == 0) {
		this_site->rsh_cmd = ne_strdup(val);
	    } else if (strcmp(key, "rcp") == 0) {
		this_site->rcp_cmd = ne_strdup(val);
	    } else if (strcmp(key, "state") == 0) {
		if (strcmp(val, "checksum") == 0) {
		    this_site->state_method = state_checksum;
		} else if (strcmp(val, "timesize") == 0) {
		    this_site->state_method = state_timesize;
		} else {
		    ret = RC_CORRUPT;
		}
	    } else if (strcmp(key, "checkmoved") == 0) {
		if (strcmp(val, "renames") == 0) {
		    this_site->checkrenames = true;
		    this_site->checkmoved = true;
		} else {
		    ret = RC_CORRUPT;
		}
	    } else if (strcmp(key, "charset") == 0) {
                NE_DEBUG(DEBUG_RCFILE, "Ignored key %s\n", key);
            } else {
		/* Unknown key! */
		ret = RC_CORRUPT;
	    }
	} else {
	    {
		ret = RC_CORRUPT;
	    }
	}
    }

    fclose(fp);
    return ret;
}
#undef LINESIZE

const char *rc_get_netrc_password(const char *server, const char *username) {
    netrc_entry *found;
    found = search_netrc(netrc_list, server);
    if (found == NULL) {
	return NULL;
    }
    if (strcmp(found->account, username) == 0) {
	return found->password;
    } else {
	return NULL;
    }
}

/* Returns zero if site is properly defined, else non-zero */
int rcfile_verify(struct site *any_site) 
{
    struct stat localst;
    char *temp;
    int ret;

    /* Protocol-specific checks first, since if a new protocol driver is used,
     * any of the other checks may be irrelevant. */
    switch (any_site->protocol) {
    case siteproto_ftp:
#ifdef USE_FTP
	any_site->driver = &ftp_driver;
	/* FTP checks */
	if (any_site->symlinks == sitesym_maintain) {
	    return SITE_NOMAINTAIN;
	}
	break;
#else /* !USE_FTP */
	return SITE_UNSUPPORTED;
#endif /* USE_FTP */
    case siteproto_dav:
#ifdef USE_DAV
	any_site->driver = &dav_driver;
	/* HTTP checks */
	if (any_site->remote_isrel) { 
	    return SITE_NOREMOTEREL;
	}
	if (any_site->perms == sitep_all || any_site->dirperms) {
	    return SITE_NOPERMS;
	}
	if (any_site->symlinks == sitesym_maintain) {
	    return SITE_NOMAINTAIN;
	}
	break;
#else /* !USE_DAV */
	return SITE_UNSUPPORTED;
#endif /* USE_DAV */
    case siteproto_rsh:
#ifdef USE_RSH
	any_site->driver = &rsh_driver;
	/* FIXME: rsh checks? */
	break;
#else /* !USE_RSH */
	return SITE_UNSUPPORTED;
#endif /* USE_RSH */
    case siteproto_sftp:
#ifdef USE_SFTP
	any_site->driver = &sftp_driver;
	/* FIXME: sftp checks? */
	break;
#else /* !USE_SFTP */
	return SITE_UNSUPPORTED;
#endif /* USE_SFTP */
    case siteproto_unknown:
	return SITE_UNSUPPORTED;
    }

    /* Valid options check */
    if (any_site->checkrenames && (any_site->state_method != state_checksum)) {
	return SITE_NORENAMES;
    }

    /* Check they specified everything in the rcfile */
    if (any_site->server.hostname == NULL) {
	return SITE_NOSERVER;
    } 

    if (any_site->server.username != NULL && any_site->server.password == NULL) {
	if (havenetrc) {
	    const char *pass;
	    NE_DEBUG(DEBUG_RCFILE, "Checking netrc for password for %s@%s...",
		   any_site->server.username, any_site->server.hostname);
	    pass = rc_get_netrc_password(any_site->server.hostname, 
					  any_site->server.username);
	    if (pass != NULL) {
		NE_DEBUG(DEBUG_RCFILE, "found!\n");
		any_site->server.password = (char *) pass;
	    } else {
		NE_DEBUG(DEBUG_RCFILE, "none found.\n");
	    }
	}
    }
    /* TODO: lookup proxy username/password in netrc too */

    if (any_site->remote_root_user == NULL) {
	return SITE_NOREMOTEDIR;
    } else if (any_site->local_root_user == NULL) {
	return SITE_NOLOCALDIR;
    }
    
    /* Need a home directory if we're using relative local root */
    if (home == NULL && any_site->local_root)
	return SITE_NOLOCALREL;

    /* Can't use safe mode and nooverwrite mode */
    if (any_site->safemode && any_site->nooverwrite)
	return SITE_NOSAFEOVER;

    if (any_site->safemode && any_site->tempupload)
	return SITE_NOSAFETEMPUP;

    if (any_site->remote_isrel) {
	any_site->remote_root = ne_strdup(any_site->remote_root_user + 2);
    } else {
	any_site->remote_root = ne_strdup(any_site->remote_root_user);
    }
    if (any_site->local_isrel) {
	/* We skip the first char ('~') of l_r_u */
	any_site->local_root = ne_concat(home, any_site->local_root_user + 1,
					 NULL);
    } else {
	any_site->local_root = any_site->local_root_user;
    }

    /* Now check the local directory actually exists.
     * To do this, stat `/the/local/root/.', which will fail if the
     * can't read the directory or if it's a file not a directory */
    temp = ne_concat(any_site->local_root, ".", NULL);
    ret = stat(temp, &localst);
    free(temp);
    if (ret != 0) {
	return SITE_ACCESSLOCALDIR;
    }

    if (any_site->client_cert && strncmp(any_site->client_cert, "~/", 2) == 0) {
        temp = ne_concat(home, any_site->client_cert + 1, NULL);
        ne_free(any_site->client_cert);
        any_site->client_cert = temp;
    }

    /* Assign default ports if they didn't bother to */
    if (any_site->server.port == 0) {
	NE_DEBUG(DEBUG_RCFILE, "Lookup up default port:\n");
	any_site->server.port = (*any_site->driver->get_server_port)(any_site);
	NE_DEBUG(DEBUG_RCFILE, "Using port: %d\n", any_site->server.port);
    }

    if (any_site->proxy.port == 0) {
	NE_DEBUG(DEBUG_RCFILE, "Lookup default proxy port...\n");
	any_site->proxy.port = (*any_site->driver->get_proxy_port)(any_site);
	NE_DEBUG(DEBUG_RCFILE, "Using port %d\n", any_site->proxy.port);
    }

    /* TODO: ditto for proxy server */
    return 0;
}

int init_netrc() {
    if (!havenetrc) return 0;
    netrc_list = parse_netrc(netrcfile);
    if (netrc_list == NULL) {
	/* Couldn't parse it */
	return 1;
    } else {
	/* Could parse it */
	return 0;
    }
}

/* Checks the perms of the rcfile and site storage directory. */
int init_paths()
{
    struct stat st;
    if (stat(rcfile, &st) < 0) {
	NE_DEBUG(DEBUG_RCFILE, "stat failed on %s: %s\n", 
	       rcfile, strerror(errno));
	return RC_OPENFILE;
    }
#if !defined (__EMX__) && !defined(__CYGWIN__)
    if (!S_ISREG(st.st_mode)) {
        return RC_OPENFILE;
    }
    if ((st.st_mode & ~(S_IFREG | S_IREAD | S_IWRITE)) > 0) {
	return RC_PERMS;
    }
#endif
    if ((netrcfile == 0) || (stat(netrcfile, &st) < 0)) {
	havenetrc = false;
#if !defined (__EMX__) && !defined(__CYGWIN__)
    } else if ((st.st_mode & ~(S_IFREG | S_IREAD | S_IWRITE)) > 0) {
	return RC_NETRCPERMS;
#endif
    } else {
	havenetrc = true;
    }
    if (stat(copypath, &st) < 0) {
	NE_DEBUG(DEBUG_RCFILE, "stat failed on %s: %s\n", 
	       copypath, strerror(errno));
	return RC_DIROPEN;
    }
#if !defined (__EMX__) && !defined(__CYGWIN__)
    if (st.st_mode & (S_IRWXG | S_IRWXO)) {
	return RC_DIRPERMS;
    }
#endif
    return 0;
}

int init_env() {
    /* Assign default filenames if they didn't give us any */
    home = getenv("HOME");
    if (home == NULL) {
	if ((rcfile == NULL) || (copypath == NULL)) {
	    /* We need a $HOME or both rcfile and info dir path */
	    return 1;
	} else {
	    /* No $HOME, but we've got the rcfile and info dir path */
	    return 0;
	}
    }
    if (rcfile == NULL) {
	rcfile = ne_concat(home, RCNAME, NULL);
    }
    if (copypath == NULL) {
	copypath = ne_concat(home, COPYNAME, NULL);
    }
    netrcfile = ne_concat(home, NETRCNAME, NULL);
    return 0;
}

/* rcfile_write() by Lee Mallabone, cleaned by JO.
 * Write the contents of list_of_sites to the specified 'filename'
 * in the standard sitecopy rc format.
 *
 * Any data already in 'filename' is over-written.
 */
int rcfile_write (char *filename, struct site *list_of_sites) 
{
    struct site *current;
    struct fnlist *item;
    FILE *fp;
    
    fp = fopen (filename, "w");
    if (fp == NULL) {
	printf ("There was a problem writing to the sitecopy configuration file.\n\nCheck permissions on %s.", filename);
	return RC_OPENFILE;
   }

    /* Set rcfile permissions properly */
#if !defined (__EMX__) && !defined(__CYGWIN__)
    if (fchmod (fileno(fp), 00600) == -1) {
	return RC_PERMS;
    }
#endif
    
    for (current=list_of_sites; current!=NULL; current=current->next) {
	/* Okay so this maybe isn't the most intuitive thing to look at.
	 * With any luck though, the rcfile's it produces will be. :) */
	if (fprintf (fp, "site %s\n", current->name) == -1) {
	    return RC_CORRUPT;
	}
	if (fprintf (fp, "  server %s\n", current->server.hostname) == -1) {
	    return RC_CORRUPT;
	}
       
	if ((current->server.username != NULL) && 
	    (strlen(current->server.username) > 0))
	    if (fprintf(fp, "  username %s\n", 
			current->server.username) == -1) {
		return RC_CORRUPT;
	    }
	
	if ((current->server.password != NULL) 
	    && (strlen(current->server.password) > 0))
	    if (fprintf(fp, "  password %s\n", 
			current->server.password) == -1) {
		return RC_CORRUPT;
	    }
	
        if (fprintf(fp, "  remote %s\n  local %s\n",
		    current->remote_root_user,
		    current->local_root_user) == -1) {
	    return RC_CORRUPT;
	}
	
	if (fprintf (fp, "  protocol %s\n", current->proto_string) == -1) {
	    return RC_CORRUPT;
	}
	
	/* Makes sense to have protocol (ish) options after we specify
	 * the protocol.  Warning, if the http declarations in site_t
	 * are ever surrounded by an ifdef USE_DAV, then this will need
	 * to be changed.  */
	
	/* Write out the boolean fields */
	
#define RCWRITEBOOL(field,name) \
      if ((field) && (fprintf(fp, "  %s\n", name) == -1)) return RC_CORRUPT;
	
	RCWRITEBOOL(current->nodelete, "nodelete");
	if (current->checkmoved) {
	    if (current->checkrenames) {
		if (fprintf(fp, "  checkmoved renames\n") == -1)
		    return RC_CORRUPT;
	    } else {
		if (fprintf(fp, "  checkmoved\n") == -1)
		    return RC_CORRUPT;
	    }
	}
	
	RCWRITEBOOL(current->nooverwrite, "nooverwrite");
	RCWRITEBOOL(current->safemode, "safe");
	RCWRITEBOOL(current->lowercase, "lowercase");
	RCWRITEBOOL(current->tempupload, "tempupload");
	
	RCWRITEBOOL(!current->ftp_pasv_mode, "ftp nopasv");
	RCWRITEBOOL(current->ftp_echo_quit, "ftp showquit");
	RCWRITEBOOL(current->ftp_use_cwd, "ftp usecwd");
	RCWRITEBOOL(current->http_limit, "http limit");
	RCWRITEBOOL(current->http_use_expect, "http expect");
	
#undef RCWRITEBOOL
	
	if (current->server.port > 0) { /* Sanity check */
	    if (fprintf (fp, "  port %d\n", current->server.port) == -1) {
		return RC_CORRUPT;
	    }
	}
	
	/* Add the site's URL if one has been supplied. */
	if (current->url) {
	    if (fprintf (fp, "  url %s\n", current->url) == -1) {
		return RC_CORRUPT;
	    }
	}
      
	/* State method */
	switch (current->state_method) {
	case (state_timesize):
	    if (fprintf (fp, "  state timesize\n") == -1) {
		return RC_CORRUPT;
	    }   
	    break;
	case (state_checksum):
	    if (fprintf (fp, "  state checksum\n") == -1) {
		return RC_CORRUPT;
	    }
	    break;
	}
	
	/* Permissions now */
	switch (current->perms) {
	case (sitep_ignore): 
	    if (fprintf (fp, "  permissions ignore\n") == -1) {
		return RC_CORRUPT;
	    }
	    break;
	case (sitep_exec):
	    if (fprintf (fp, "  permissions exec\n") == -1) {
		return RC_CORRUPT;
	    }
	    break;
	case (sitep_all):
	    if (fprintf (fp, "  permissions all\n") == -1) {
		return RC_CORRUPT;
	    }
	    break;
	}
	
        if (current->dirperms) {
            if (fprintf(fp, "  permissions dir\n") == -1) {
                return RC_CORRUPT;
            }
        }

	/* Sym link mode */
	switch (current->symlinks) {
	case (sitesym_ignore): 
	    if (fprintf (fp, "  symlinks ignore\n") == -1) {
		return RC_CORRUPT;
	    }
	    break;
	case (sitesym_follow):
	    if (fprintf (fp, "  symlinks follow\n") == -1) {
		return RC_CORRUPT;
	    }
	    break;
	case (sitesym_maintain):
	    if (fprintf (fp, "  symlinks maintain\n") == -1) {
		return RC_CORRUPT;
	    }
	    break;
	}
	
#define DUMP_FNLIST(list, name) 					  \
do {\
for (item = list; item != NULL; item = item->next)		  	  \
    if (fprintf(fp, "  " name " \"%s%s\"\n", item->haspath?"/":"",	  \
	item->pattern) == -1)						  \
    return RC_CORRUPT;					     \
} while(0)
	
	DUMP_FNLIST(current->excludes, "exclude");
	DUMP_FNLIST(current->asciis, "ascii");
	DUMP_FNLIST(current->ignores, "ignore");
	
#undef DUMP_FNLIST
	
    }
    if (fclose (fp) != 0)
	return RC_CORRUPT;

    return 0;
}