summaryrefslogtreecommitdiffabout
path: root/cvs/sv_sync_www.c
blob: 7af12716fc879c0944a5b006b29bb2a63790ce20 (plain)
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
/*

 Copyright (C) , Loic Dachary <loic@gnu.org>, 2001
 Copyright (C) 2005, 2006 Sergey Poznyakoff
 
 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 3 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, see <http://www.gnu.org/licenses/>.

*/

/* sv_sync_www.c - Allow unprivileged users to do a web update on
   www.gnu.org.ua

  In CVSROOT/loginfo file
  ALL      /bin/sv_sync_www_schedule ${USER} ${CVSROOT} %s

  In crontab (remove the backslash before /):
  0 *\/1 * * * /bin/sv_sync_www -s /var/spool/savane

  Or, to synchronize right after commit, in CVSROOT/loginfo:
  
  ALL     (date; cat; (sleep 2; /bin/sv_sync_www %{s} ) &\ ) \
             >> /var/log/sv_sync_www 2>&1

  For test purpose:
  ./sv_sync_www -n ' bla bla'
  ./sv_sync_www -n 'foo/bar bla bla'
  ./sv_sync_www -n 'f'\''oo bla bla'
  ./sv_sync_www -n 'f'\''oo - New directory'
  ./sv_sync_www -n 'bla - Imported sources'

  The idea is that it runs a cvs update -l (to prevent recursion) in the 
  directory where the commit was done. Since the command will be called
  once for each directory where a commit did some action there is no
  need for recursion. In the case of an import command this does not
  hold and a recursion must always be done since there is only one
  call to the script for a whole imported tree (this happens when the
  argument contains the Imported source string).

  After running update, the program looks for file ".symlinks" and executes
  it, when present. File format is: empty lines and lines started with ';'
  or '#' are ignored. Rest of lines are split in two words: first word is
  the name of an existing file, second one is the link name. Both names
  should be in the current directory. Care is taken to prevent linking to
  upper-level directories.
	  
  The %{s} argument in loginfo invocation is a single argument that lists
  the directory and all the files involved. As a special case if the
  directory was added the file list is replaced by '- New directory'. This
  is lame since adding the files -, New and directory will produce the same
  effect, but it's unlikely. The same applies when a whole source tree is
  imported using cvs import in which case the file list is replaced by
  '- Imported sources'.

  There are three cases to take in account (topdir is the absolute path
  of the directory in which the CVS tree was extracted, subdirectory is
  the directory given in argument):
  - commit that modify the top level directory files
    cd topdir ; cvs update -l 
  - commit that adds a new directory or that import a whole source tree
    cd topdir ; cvs update 'subdirectory'
  - commit that modify files in a subdirectory
    cd topdir/subdirectory ; cvs update -l

  In order to prevent security compromise the directory name is quoted.

  Originaly by
  Gordon Matzigkeit <gord@fig.org>, 2000-11-28

  Update CVS_COMMANDS to reduce noise
  Loic Dachary <loic@gnu.org>, 2001-02-26

  Modify to allow generic call from loginfo file in an efficient way
  Loic Dachary <loic@gnu.org>, 2001-03-10

  Introduce command line options, make program configurable and
  implement .symlinks functionality
  Sergey Poznyakoff <gray@gnu.org.ua>, 2005-07-31

  Implement 'scan directory' mode
  Sergey Poznyakoff <gray@gnu.org.ua>, 2005-02-09
  
*/

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <ctype.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <signal.h>
#include <error.h>
#include <errno.h>
#include <getopt.h>
#include <pwd.h>
#include <grp.h>
#include "gsc.h"

#define obstack_chunk_alloc malloc
#define obstack_chunk_free free
#include <obstack.h>

char *rsh_program = "/usr/bin/ssh";
char *hostname;
char *default_repository = "/webcvs";
char *default_spooldir = "/var/spool/savane";
char *access_method = NULL;
char *www_directory = "/home/puszcza/software";
char *cvs_command = "CVS_RSH=ssh cvs -q -z3";
uid_t savane_uid;
gid_t savane_gid;
uid_t min_uid = 100;
gid_t min_gid = 100;
int dry_run = 0;
int debug = 0;
int verbose_level = 0;


/* Utility functions */

/*
 * Return a malloc'ed copy of the string argument with all '
 * substituted by '\'' and a trailing and leading '.
 * For instance : foo -> 'foo'
 *                fib'ou -> 'fib'\''ou'
 */
char *
quote (const char *string)
{
  int i;
  int length;
  int count = 0;
  char *out;

  if (!string)
    return NULL;
  
  length = strlen (string);
  for (i = 0; i < length; i++)
    if (string[i] == '\'')
      count++;

  /* +1 for null at end, (count * 3) for '\'', +2 for leading ' and
     trailing ' */
  out = (char *) malloc (length + 1 + count * 3 + 2);
  if (count > 0)
    {
      const char *from_p = string;
      char *to_p = out;
      *to_p++ = '\'';
      while (*from_p)
        {
          if (*from_p == '\'')
            {
              strcpy (to_p, "'\\''");
              to_p += 4;
              from_p++;
            }
          else
            *to_p++ = *from_p++;
        }
      *to_p++ = '\'';
      *to_p = '\0';
    }
  else
    sprintf (out, "'%s'", string);
  return out;
}

void
usage ()
{
  printf ("sv_sync_www [OPTIONS] DIRECTORY\n\n");
  printf ("OPTIONS are:\n");
  printf ("\nMode selection:\n");
  printf (" -h, --help        Display this help summary\n");
  printf (" -s, --cron        Cron job mode: process each file in DIRECTORY\n");
  printf ("                   The argument is optional, it defaults to %s\n",
	  default_spooldir);
  printf ("\nMode modification:\n");
  printf (" -n, --dry-run     Dry run: do nothing, only print what would have been done\n");
  printf (" -x, --debug       Enable debugging output\n");
  printf ("\nConfiguration:\n");
  printf (" -c, --cvs-command=COMMAND\n");
  printf ("                   Set cvs command to use (default %s)\n",
	  cvs_command);
  printf (" -H, --hostname=HOSTNAME\n");
  printf ("                   Set hostname or IP address of the host where WWW directory\n");
  printf ("                   resides (default: none, WWW and CVS reside on the same host)\n");
  printf (" -m, --method=METHOD\n");
  printf ("                   Set default access method. METHOD will be prepended to\n");
  printf ("                   REPOSITORY (see -R option below) before accessing the\n");
  printf ("                   repository\n");
  printf (" -r, --rsh-program=RSH_PROGRAM\n");
  printf ("                   Set full name of RSH program\n");
  printf ("                   (default: %s)\n", rsh_program);
  printf (" -R, --repository=REPOSITORY\n");
  printf ("                   Set full name of the CVS repository\n");
  printf ("                   (default: %s)\n", default_repository);
  printf (" -w, --destination-directory, --www-directory=DIRECTORY\n");
  printf ("                   Set full name of the WWW directory\n");
  printf ("                   (default: %s)\n", www_directory);
  printf (" -v, --verbose     Increase verbosity level\n");				 
  printf (" -U, --min-uid=UID Set minimum allowable UID value\n");
  printf (" -G, --min-gid=GID Set minimum allowable GID value\n");
}

void
switch_to_privs (uid_t uid, gid_t gid)
{
  if (gsc_userprivs (uid, &gid, 1))
    error (1, 0, "%s", gsc_userprivs_errstring ());
}


/* Core functions (single job support) */

static int
run_cmd (char *cmd)
{
  pid_t pid;
  char *xargv[6];
  int xargc = 0;
  char *new_envp[] = { NULL };
  char *p;
  char *program;

  pid = fork ();
  if (pid < 0)
    error (1, errno, "cannot fork");

  if (pid > 0) /* Master process */
    {
      int status;
      waitpid (pid, &status, 0);
      if (WIFEXITED (status))
        {
          status = WEXITSTATUS (status);
          if (status)
            error (0, 0, "subprocess returned exit code %d",
                   status);
          return status;
        }
      else if (WIFSIGNALED (status))
        {
          error (0, 0, "subprocess terminated on signal %d",
		 WTERMSIG (status));
          return -1;
        }
      else
        {
          error (0, 0, "subprocess terminated");
          return -1;
        }
    }
  
  if (hostname)
    {
      program = dry_run ? "/bin/echo" : rsh_program;
      xargv[xargc++] = rsh_program;
      xargv[xargc++] = hostname;
    }
  else
    {
      if (dry_run)
        {
          program = "/bin/echo";
          xargv[xargc++] = "echo";
        }
      else
        {
          program = "/bin/sh";
          xargv[xargc++] = "-sh";
          xargv[xargc++] = "-c";
          if (debug)
            xargv[xargc++] = "-x";
        }
    }
    
  xargv[xargc++] = cmd;
  xargv[xargc++] = NULL;
  
  /* Pick out the name of the program. */
  p = strrchr (xargv[0], '/');
  if (p)
    xargv[0] = p;
  
  execve (program, xargv, new_envp);
  error (1, errno, "Cannot exec `%s'", program);
}  

int
has_file_list (const char *arg)
{
  /*
   * If this is a new directory creation or an import command,
   * there are no files to collect in the argument list.
   * Otherwise, there are files to collect. 
   */
  return (strcmp (arg, "- New directory")
	  && strcmp (arg, "- Imported sources"));
}

static int
split_arg (const char *arg, char **pdir, char **pfiles)
{
  char *tmp;
  /*
   * First comes the relative directory name or empty string if current.
   * directory = 0 if no subdirectory
   * directory = relative path if subdirectory
   */
  if ((tmp = strchr (arg, ' ')))
    {
      size_t len = tmp - arg;
      if (len > 0)
	{
	  *pdir = malloc (len + 1);
	  if (!*pdir)
	    return -1;
	  memcpy (*pdir, arg, len);
	  (*pdir)[len] = 0;
	}
      else
	*pdir = NULL;
      *pfiles = strdup (tmp + 1);
      if (!*pfiles)
	{
	  free (*pdir);
	  return -1;
	}
      return 0;
    }
  return 1;
}

int
sync_www (const char *repository, const char *unquoted_directory, int files)
{
  int rc;
  char *directory = quote (unquoted_directory);
  char *cmd;
  
  /* Check that the local directory exists. */
  if (!dry_run && files && unquoted_directory)
    {
      struct stat stbuf;
      char *localdir;
      size_t len = strlen (repository);

      localdir = malloc (len + 1 + strlen (unquoted_directory) + 1);
      if (!localdir)
	{
	  error (0, 0, "not enough memory");
	  return 1;
	}

      strcpy (localdir, repository);
      if (localdir[len - 1] != '/')
        localdir[len++] = '/';
      strcpy (localdir + len, unquoted_directory);

      /* Check to see that the local unquoted_directory actually exists. */
      if (stat (localdir, &stbuf) ||
          (!S_ISDIR (stbuf.st_mode) && (errno = ENOTDIR)))
	{
	  error (0, errno, "Cannot find `%s'", localdir);
	  return 1;
	}
    }

  /* Update from CVS */
  if (directory && !files)
    asprintf (&cmd, "cd %s && ( %s -d %s%s checkout %s )",
              www_directory, cvs_command, access_method ? access_method : "",
	      repository, directory);
  else if (!directory && files)
    asprintf (&cmd, "cd %s && ( %s update -l )",
              www_directory, cvs_command);
  else if (directory && files)
    asprintf (&cmd, "cd %s/%s && ( %s update -l )",
              www_directory, directory, cvs_command);
  else
    {
      error (0, 0, "Unexpected null directory and files");
      return 1;
    }
  
  if (run_cmd (cmd))
    return 1;

  free (cmd);

  /* Restore symlinks */
  asprintf (&cmd,
            "cd %s/%s && if test -r .symlinks; then "
            "grep -v '^[ \t]*[;#]' .symlinks | "
            "sed 's,^/,_,;s,\\.\\./,__/,g' |"
            "while read S T; do ln -sf $S $T; done; fi",
            www_directory, directory);
  
  rc = run_cmd (cmd);
  free (cmd);
  return rc;
}  



static int need_cleanup = 0;

static RETSIGTYPE
sig_child (int sig)
{
  need_cleanup = 1;
  signal (sig, sig_child);
}

void
setup_signals ()
{
  signal(SIGCHLD, sig_child);
}

void
reset_signals ()
{
  signal(SIGCHLD, SIG_DFL);
}


/* Job control */

struct sync_entry
{
  char *repo;
  char *dir;
  int files;
};

int do_sync (struct sync_entry *array, size_t count);

struct sync_job
{
  const char *file;          /* Control file */
  pid_t pid;                 /* PID if the job is running
				0  if the job is free */
};

struct sync_job *jobtab;
size_t job_max = 128;
size_t total_files;
size_t processed_files;

void
setup_jobs ()
{
  jobtab = calloc (job_max, sizeof (jobtab[0]));
  if (!jobtab)
    error (1, errno, "cannot initialize job table");
}

struct sync_job *
find_job_pid (pid_t pid)
{
  struct sync_job *sp;

  for (sp = jobtab; sp < jobtab + job_max; sp++)
    if (sp->pid == pid)
      return sp;
  return NULL;
}

size_t
job_count ()
{
  struct sync_job *sp;
  size_t count = 0;
  
  for (sp = jobtab; sp < jobtab + job_max; sp++)
    if (sp->pid)
      count++;
  return count;
}

int
waitjob ()
{
  pid_t pid;
  int status;
  int rc = 0;
  
  while ((pid = waitpid (-1, &status, WNOHANG)) > 0)
    {
      struct sync_job *jp = find_job_pid (pid);
      if (!jp)
	{
 	  error (0, 0, "Unknown job terminated");
	  continue;
	}

      jp->pid = 0;
      rc++;
      total_files++;
      if (WIFEXITED (status))
	{
	  if (WEXITSTATUS (status))
	    error (0, 0, "Child %lu exited with status %d",
		   (unsigned long) pid,
		   WEXITSTATUS (status));
	  else
	    {
	      if (!dry_run && unlink (jp->file))
		error (0, errno, "Cannot unlink file `%s'", jp->file);
	      else
		processed_files++;
	    }
	}
      else if (WIFSIGNALED (status))
	error (0, 0, "Child %lu terminated on signal %d",
	       (unsigned long) pid,
	       WTERMSIG (status));
      else
	error (0, 0, "Child %lu terminated, reason unknown",
	       (unsigned long) pid);
    }
  return rc;
}

int
schedule_job (uid_t uid, gid_t gid, const char *file,
	      struct sync_entry *array, size_t count)
{
  struct sync_job *jp;

  while (!waitjob () && job_count () == job_max)
    ;

  jp = find_job_pid (0);
  jp->pid = fork ();

  switch (jp->pid)
    {
    case -1:
      error (0, errno, "Cannot start process");
      return -1;

    case 0:
      reset_signals ();
      switch_to_privs (savane_uid, savane_gid);
      exit (do_sync (array, count));

    default:
      jp->file = file;
    }
  return 0;
}


/* Multi-job support */

static int
dir_cmp (const char *a, const char *b)
{
  if (!a)
    {
      if (b)
	return -1;
      else
	return 0;
    }
  else if (!b)
    return 1;
  return strcmp (a, b);
}

static int
ent_cmp (const void *a, const void *b)
{
  const struct sync_entry *sa = a; 
  const struct sync_entry *sb = b; 
  int rc;

  rc = strcmp (sa->repo, sb->repo);
  if (rc)
    return rc;
  rc = dir_cmp (sa->dir, sb->dir);
  if (rc)
    return rc;
  return sa->files - sb->files;
}

int
do_sync (struct sync_entry *array, size_t count)
{
  int rc = 0;
  struct sync_entry *sp, *lastp = NULL;
      
  /* Sort the array */
  qsort (array, count, sizeof array[0], ent_cmp);

  for (sp = array; sp < array + count; sp++)
    {
      if (lastp)
	{
	  if (strcmp (lastp->repo, sp->repo) == 0
	      && dir_cmp (lastp->dir, sp->dir) == 0)
	    continue;
	}
      rc |= sync_www (sp->repo, sp->dir, sp->files);
      lastp = sp;
    }
  if (debug)
    fprintf (stderr, "do_sync: returning %d\n",rc);
  return rc;
}

int
sync_from_file (uid_t uid, const char *spooldir, const char *file)
{
  char *buf = NULL;
  size_t size = 0;
  size_t line = 0;
  int err = 0;
  struct obstack stk;
  struct sync_entry *sp;
  struct sync_entry ent;
  struct sync_entry *sync_array;
  FILE *fp = fopen (file, "r");
  
  if (!fp)
    {
      error (0, errno, "Cannot open file `%s/%s'", spooldir, file);
      return 1;
    }

  obstack_init (&stk);
  while (getline (&buf, &size, fp) > 0)
    {
      char *p;
      struct passwd *pw;


      p = buf + strlen (buf) - 1;
      if (*p == '\n')
	*p = 0;
      
      line++;

      p = strtok (buf, " \t");
      if (!p)
	{
	  error (0, 0, "%s/%s:%lu: Invalid line",
		 spooldir, file,
		 (unsigned long) line);
	  err = 1;
	  break;
	}
      
      pw = getpwnam (p);
      if (!pw)
	{
	  error (0, 0, "%s/%s:%lu: cannot get password entry for user %s", 
		 spooldir, file,
		 (unsigned long) line, p);
	  err = 1;
	  break;
	}
      if (uid != pw->pw_uid)
	{
	  error (0, 0, "%s/%s:%lu: invalid uid", 
		 spooldir, file,
		 (unsigned long) line);
	  err = 1;
	  break;
	} 
      
      p = strtok (NULL, " \t");
      if (!p)
	{
	  error (0, 0, "%s/%s:%lu: Invalid line, second field is missing",
		 spooldir, file,
		 (unsigned long) line);
	  err = 1;
	  break;
	}
      ent.repo = strdup (p);
      if (!ent.repo)
	{
	  error (0, ENOMEM, "%s/%s:%lu",
		 spooldir, file,
		 (unsigned long) line);
	  err = 1;
	  break;
	}

      p = strtok (NULL, "");
      if (!p)
	{
	  error (0, 0, "%s/%s:%lu: Invalid line, only two fields",
		 spooldir, file,
		 (unsigned long) line);
	  err = 1;
	  break;
	}
      err = split_arg (p, &ent.dir, &p);
      if (err)
	{
	  error (0, err < 0 ? errno : 0, "%s/%s:%lu: Cannot split argument",
		 spooldir, file,
		 (unsigned long) line);
	  break;
	}
      ent.files = has_file_list (p);
      free (p);
      
      obstack_grow (&stk, &ent, sizeof (ent));
    }
  fclose (fp);

  sync_array = obstack_finish (&stk);
  if (err == 0)
    {
      if (getuid ())
	uid = savane_uid;
      err = schedule_job (uid, savane_gid, file, sync_array, line);
    }
  
  for (sp = sync_array; sp < sync_array + line; sp++)
    {
      free (sp->repo);
      free (sp->dir);
    }

  obstack_free (&stk, NULL);
  
  return err;
}

int
scan_spool (const char *spooldir)
{
  DIR *dir;
  struct dirent *ent;
  
  if (chdir (spooldir))
    {
      error (0, errno, "Cannot change to `%s'", spooldir);
      return 1;
    }

  dir = opendir (".");
  if (!dir)
    {
      error (0, errno, "Cannot open directory `%s'", spooldir);
      return 1;
    }

  while ((ent = readdir (dir)))
    {
      struct stat st;

      waitjob ();
      if (ent->d_name[0] == '.')
	continue;

      if (stat (ent->d_name, &st))
	{
	  error (0, errno, "Cannot stat file `%s/%s'", spooldir, ent->d_name);
	  continue;
	}

      if (S_ISREG (st.st_mode))
	{
	  if (st.st_uid < min_uid || st.st_gid < min_gid)
	    {
	      error (0, 0, "Ignoring file `%s/%s': UID or GID out of range",
		     spooldir, ent->d_name);
	      continue;
	    }

	  sync_from_file (st.st_uid, spooldir, ent->d_name);
	}
    }
  
  while (job_count () > 0)
    waitjob ();

  if ((verbose_level == 1 && total_files) || verbose_level > 1)
    printf ("Total files: %lu; Jobs processed: %lu\n",
	    (unsigned long) total_files,
	    (unsigned long) processed_files);
  
  closedir (dir);
  return 0;
}



struct option options[] = {
  { "cvs-command", required_argument, NULL, 'c' },
  { "help", no_argument, NULL, 'h' },
  { "hostname", required_argument, NULL, 'H'},
  { "min-gid", required_argument, NULL, 'G' },
  { "method", required_argument, NULL, 'm' },
  { "dry-run", no_argument, NULL, 'n' },
  { "rsh-program", required_argument, NULL, 'r' },
  { "repository", required_argument, NULL, 'R' },
  { "cron", no_argument, NULL, 's' },
  { "user", required_argument, NULL, 'u' },
  { "min-uid", required_argument, NULL, 'U' },
  { "www-directory", required_argument, NULL, 'w' },
  { "destination-directory", required_argument, NULL, 'w' },
  { "debug", no_argument, NULL, 'x' },
  { "verbose", no_argument, NULL, 'v' },
  { NULL }
};

int
main (int argc, char **argv)
{
  int i;
  char *p;
  int spooldir_mode = 0;
  char *user = NULL;
  char *repository = default_repository;
  
  while ((i = getopt_long (argc, argv, "c:hH:G:m:nr:R:su:U:vw:x",
			   options, NULL)) != EOF)
    switch (i)
      {
      case 'c':
	cvs_command = optarg;
	break;

      case 'H':
        hostname = optarg;
        break;

      case 'h':
        usage ();
        exit (0);

      case 'G':
	min_gid = strtoul (optarg, &p, 0);
	if (p)
	  error (1, 0, "invalid numeric value (near %s)", p);
	break;

      case 'm':
	access_method = optarg;
	break;
	
      case 'n':
        dry_run = 1;
        break;

      case 'r':
        rsh_program = optarg;
        break;

      case 'R':
        repository = optarg;
        break;

      case 's':
	spooldir_mode = 1;
	break;

      case 'U':
	min_uid = strtoul (optarg, &p, 0);
	if (p)
	  error (1, 0, "invalid numeric value (near %s)", p);
	break;
	  
      case 'u':
        user = optarg;
        break;

      case 'v':
	verbose_level++;
	break;
	
      case 'w':
        www_directory = optarg;
        break;

      case 'x':
        debug = 1;
        break;
        
      default:
        exit (1);
      }

  if (debug)
    {
      fprintf (stderr, "Invocation:\n");
      for (i = 0; i < argc; i++)
        fprintf (stderr, "%s\n", argv[i]);
      fprintf (stderr, "\n");
    }

  argc -= optind;
  argv += optind;

  if (user)
    {
      struct passwd *pw = getpwnam (user);
      if (!pw)
        error (1, errno, "cannot get password entry for user %s", user);
      savane_uid = pw->pw_uid;
      savane_gid = pw->pw_gid;
    }
  else
    {
      savane_uid = geteuid ();
      savane_gid = getegid ();
    }

  if (spooldir_mode)
    {
      char *spooldir = NULL;

      if (argc > 1)
	error (1, 0, "Extra arguments in scan mode");
      else if (argc == 1)
	spooldir = argv[0];
      else
	spooldir = default_spooldir;

      setsid ();
      setup_signals ();
      setup_jobs ();
      return scan_spool (spooldir);
    }
  else
    {
      char *unquoted_directory = NULL;
      char *tmp;
      
      switch_to_privs (savane_uid, savane_gid);

      if (argc == 1)
	{
	  int rc = split_arg (argv[0], &unquoted_directory, &tmp);
	  if (rc < 0)
	    error (1, errno, "Cannot split argument");
	  else if (rc > 0)
	    error (1, 0, "Failed to find directory in first argument: `%s'",
		   argv[0]);
	}
      else if (argc == 2)
	{
	  unquoted_directory = argv[0];
	  tmp = argv[1];
	}
      else
	error (1, 0, "You must specify one or two arguments");
  
      return sync_www (repository, unquoted_directory, has_file_list (tmp));
    }
  /*NOTREACHED*/
  return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.