1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
|
/* $OpenBSD: client.c,v 1.10 1999/02/23 21:27:15 millert Exp $ */
/*
* Copyright (c) 1983 Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
#if 0
static char RCSid[] =
"$From: client.c,v 6.80 1996/02/28 20:34:27 mcooper Exp $";
#else
static char RCSid[] =
"$OpenBSD: client.c,v 1.10 1999/02/23 21:27:15 millert Exp $";
#endif
static char sccsid[] = "@(#)client.c";
static char copyright[] =
"@(#) Copyright (c) 1983 Regents of the University of California.\n\
All rights reserved.\n";
#endif /* not lint */
/*
* Routines used in client mode to communicate with remove server.
*/
#include "defs.h"
#include "y.tab.h"
/*
* Update status
*/
#define US_NOTHING 0 /* No update needed */
#define US_NOENT 1 /* Entry does not exist */
#define US_OUTDATE 2 /* Entry is out of date */
#define US_DOCOMP 3 /* Do a binary comparison */
#define US_MODE 4 /* Modes of file differ */
struct linkbuf *ihead = NULL; /* list of files with more than one link */
char buf[BUFSIZ]; /* general purpose buffer */
u_char respbuff[BUFSIZ]; /* Response buffer */
char target[BUFSIZ]; /* target/source directory name */
char source[BUFSIZ]; /* source directory name */
char *ptarget; /* pointer to end of target name */
char *Tdest; /* pointer to last T dest*/
struct namelist *updfilelist = NULL; /* List of updated files */
static int sendit();
/*
* return remote file pathname (relative from target)
*/
char *remfilename(src, dest, path, rname, destdir)
char *src, *dest, *path, *rname;
int destdir;
{
extern struct namelist *filelist;
register char *lname, *cp;
static char buff[BUFSIZ];
int srclen, pathlen;
char *p;
debugmsg(DM_MISC,
"remfilename: src=%s dest=%s path=%s rname=%s destdir=%d\n",
A(src), A(dest), A(path), A(rname), destdir);
if (!dest) {
debugmsg(DM_MISC, "remfilename: remote filename=%s\n", path);
return(path);
}
if (!destdir) {
debugmsg(DM_MISC, "remfilename: remote filename=%s\n", dest);
return(dest);
}
buff[0] = CNULL;
lname = buff;
if (path && *path) {
cp = strrchr(path, '/');
if (cp == NULL)
(void) sprintf(buff, "%s/%s", dest, path);
else {
srclen = strlen(src);
pathlen = strlen(path);
if (srclen >= pathlen)
cp++; /* xbasename(path) */
else {
if (filelist && filelist->n_next == NULL)
/* path relative to src */
cp = path + srclen;
else {
if ((p = strrchr(src, '/')))
cp = path + srclen - strlen(p);
else
cp = path;
}
}
if ((*cp != '/') && *cp)
(void) sprintf(buff, "%s/%s", dest, cp);
else
(void) sprintf(buff, "%s%s", dest, cp);
}
} else
strcpy(lname, dest);
debugmsg(DM_MISC, "remfilename: remote filename=%s\n", lname);
return(lname);
}
/*
* Return true if name is in the list.
*/
int inlist(list, file)
struct namelist *list;
char *file;
{
register struct namelist *nl;
for (nl = list; nl != NULL; nl = nl->n_next)
if (strcmp(file, nl->n_name) == 0)
return(1);
return(0);
}
/*
* Run any special commands for this file
*/
static void runspecial(starget, opts, rname, destdir)
char *starget;
opt_t opts;
char *rname;
int destdir;
{
register struct subcmd *sc;
extern struct subcmd *subcmds;
char *rfile;
rfile = remfilename(source, Tdest, target, rname, destdir);
for (sc = subcmds; sc != NULL; sc = sc->sc_next) {
if (sc->sc_type != SPECIAL)
continue;
if (sc->sc_args != NULL && !inlist(sc->sc_args, starget))
continue;
message(MT_CHANGE, "special \"%s\"", sc->sc_name);
if (IS_ON(opts, DO_VERIFY))
continue;
(void) sendcmd(C_SPECIAL,
"%s=%s;%s=%s;%s=%s;export %s %s %s;%s",
E_LOCFILE, starget,
E_REMFILE, rfile,
E_BASEFILE, xbasename(rfile),
E_LOCFILE, E_REMFILE, E_BASEFILE,
sc->sc_name);
while (response() > 0)
;
}
}
/*
* If we're doing a target with a "cmdspecial" in it, then
* save the name of the file being updated for use with "cmdspecial".
*/
static void addcmdspecialfile(starget, rname, destdir)
char *starget;
char *rname;
int destdir;
{
char *rfile;
struct namelist *new;
register struct subcmd *sc;
extern struct subcmd *subcmds;
int isokay = 0;
rfile = remfilename(source, Tdest, target, rname, destdir);
for (sc = subcmds; sc != NULL && !isokay; sc = sc->sc_next) {
if (sc->sc_type != CMDSPECIAL)
continue;
if (sc->sc_args != NULL && !inlist(sc->sc_args, starget))
continue;
isokay = TRUE;
}
if (isokay) {
new = (struct namelist *) xmalloc(sizeof(struct namelist));
new->n_name = xstrdup(rfile);
new->n_next = updfilelist;
updfilelist = new;
}
}
/*
* Free the file list
*/
static void freecmdspecialfiles()
{
register struct namelist *ptr, *save;
for (ptr = updfilelist; ptr; ) {
if (ptr->n_name) (void) free(ptr->n_name);
save = ptr->n_next;
(void) free(ptr);
if (save)
ptr = save->n_next;
else
ptr = NULL;
}
updfilelist = NULL;
}
/*
* Run commands for an entire cmd
*/
extern void runcmdspecial(cmd, filev, opts)
struct cmd *cmd;
char **filev;
opt_t opts;
{
register struct subcmd *sc;
register struct namelist *f;
int first = TRUE;
for (sc = cmd->c_cmds; sc != NULL; sc = sc->sc_next) {
if (sc->sc_type != CMDSPECIAL)
continue;
message(MT_CHANGE, "cmdspecial \"%s\"", sc->sc_name);
if (IS_ON(opts, DO_VERIFY))
continue;
/* Send all the file names */
for (f = updfilelist; f != NULL; f = f->n_next) {
if (first) {
(void) sendcmd(C_CMDSPECIAL, NULL);
if (response() < 0)
return;
first = FALSE;
}
(void) sendcmd(RC_FILE, f->n_name);
if (response() < 0)
return;
}
if (first) {
(void) sendcmd(C_CMDSPECIAL, NULL);
if (response() < 0)
return;
first = FALSE;
}
/* Send command to run and wait for it to complete */
(void) sendcmd(RC_COMMAND, sc->sc_name);
while (response() > 0)
;
first = TRUE; /* Reset in case there are more CMDSPECIAL's */
}
freecmdspecialfiles();
}
/*
* For security, reject filenames that contains a newline
*/
int checkfilename(name)
char *name;
{
register char *cp;
if (strchr(name, '\n')) {
for (cp = name; *cp; cp++)
if (*cp == '\n')
*cp = '?';
message(MT_NERROR,
"Refuse to handle filename containing newline: %s",
name);
return(-1);
}
return(0);
}
void freelinkinfo(lp)
struct linkbuf *lp;
{
if (lp->pathname)
free(lp->pathname);
if (lp->src)
free(lp->src);
if (lp->target)
free(lp->target);
free(lp);
}
/*
* Save and retrieve hard link info
*/
static struct linkbuf *linkinfo(statp)
struct stat *statp;
{
struct linkbuf *lp;
/* XXX - linear search doesn't scale with many links */
for (lp = ihead; lp != NULL; lp = lp->nextp)
if (lp->inum == statp->st_ino && lp->devnum == statp->st_dev) {
lp->count--;
return(lp);
}
lp = (struct linkbuf *) xmalloc(sizeof(*lp));
lp->nextp = ihead;
ihead = lp;
lp->inum = statp->st_ino;
lp->devnum = statp->st_dev;
lp->count = statp->st_nlink - 1;
lp->pathname = xstrdup(target);
lp->src = xstrdup(source);
if (Tdest)
lp->target = xstrdup(Tdest);
else
lp->target = NULL;
return(NULL);
}
/*
* Send a hardlink
*/
static int sendhardlink(opts, lp, rname, destdir)
opt_t opts;
struct linkbuf *lp;
char *rname;
int destdir;
{
static char buff[MAXPATHLEN];
char *lname; /* name of file to link to */
debugmsg(DM_MISC,
"sendhardlink: rname='%s' pathname='%s' src='%s' target='%s'\n",
rname, lp->pathname, lp->src, lp->target);
if (lp->target == NULL)
(void) sendcmd(C_RECVHARDLINK, "%o %s %s",
opts, lp->pathname, rname);
else {
lname = buff;
strcpy(lname, remfilename(lp->src, lp->target,
lp->pathname, rname,
destdir));
debugmsg(DM_MISC, "sendhardlink: lname=%s\n", lname);
(void) sendcmd(C_RECVHARDLINK, "%o %s %s",
opts, lname, rname);
}
return(response());
}
/*
* Send a file
*/
static int sendfile(rname, opts, stb, user, group, destdir)
char *rname;
opt_t opts;
struct stat *stb;
char *user, *group;
int destdir;
{
int goterr, f;
off_t i;
if (stb->st_nlink > 1) {
struct linkbuf *lp;
if ((lp = linkinfo(stb)) != NULL)
return(sendhardlink(opts, lp, rname, destdir));
}
if ((f = open(target, O_RDONLY)) < 0) {
error("%s: open for read failed: %s", target, SYSERR);
return(-1);
}
/*
* Send file info
*/
(void) sendcmd(C_RECVREG, "%o %04o %ld %ld %ld %s %s %s",
opts, stb->st_mode & 07777,
(long) stb->st_size,
stb->st_mtime, stb->st_atime,
user, group, rname);
if (response() < 0) {
(void) close(f);
return(-1);
}
debugmsg(DM_MISC, "Send file '%s' %d bytes\n",
rname, (long) stb->st_size);
/*
* Set remote time out alarm handler.
*/
(void) signal(SIGALRM, sighandler);
/*
* Actually transfer the file
*/
goterr = 0;
for (i = 0; i < stb->st_size; i += BUFSIZ) {
int amt = BUFSIZ;
(void) alarm(rtimeout);
if (i + amt > stb->st_size)
amt = stb->st_size - i;
if (read(f, buf, amt) != amt) {
error("%s: File changed size", target);
err();
++goterr;
/*
* XXX - We have to keep going because the
* server expects to receive a fixed number
* of bytes that we specified as the file size.
* We need Out Of Band communication to handle
* this situation gracefully.
*/
}
if (xwrite(rem_w, buf, amt) < 0) {
error("%s: Error writing to client: %s",
target, SYSERR);
err();
++goterr;
break;
}
(void) alarm(0);
}
(void) alarm(0); /* Insure alarm is off */
(void) close(f);
debugmsg(DM_MISC, "Send file '%s' %s.\n",
(goterr) ? "failed" : "complete", rname);
/*
* Check for errors and end send
*/
if (goterr)
return(-1);
else {
ack();
f = response();
if (f < 0)
return(-1);
else if (f == 0 && IS_ON(opts, DO_COMPARE))
return(0);
runspecial(target, opts, rname, destdir);
addcmdspecialfile(target, rname, destdir);
return(0);
}
}
/*
* Check for files on the machine being updated that are not on the master
* machine and remove them.
*
* Return < 0 on error.
* Return 0 if nothing happened.
* Return > 0 if anything is updated.
*/
static int rmchk(opts)
opt_t opts;
{
register u_char *s;
struct stat stb;
int didupdate = 0;
int n;
debugmsg(DM_CALL, "rmchk()\n");
/*
* Tell the remote to clean the files from the last directory sent.
*/
(void) sendcmd(C_CLEAN, "%o", IS_ON(opts, DO_VERIFY));
if (response() < 0)
return(-1);
for ( ; ; ) {
n = remline(s = respbuff, sizeof(respbuff), TRUE);
if (n <= 0) {
error("rmchk: unexpected control record");
return(didupdate);
}
switch (*s++) {
case CC_QUERY: /* Query if file should be removed */
/*
* Return the following codes to remove query.
* CC_NO -- file exists - DON'T remove.
* CC_YES -- file doesn't exist - REMOVE.
*/
(void) sprintf(ptarget, "%s%s",
(ptarget[-1] == '/' ? "" : "/"), s);
debugmsg(DM_MISC, "check %s\n", target);
if (except(target))
(void) sendcmd(CC_NO, NULL);
else if (lstat(target, &stb) < 0) {
if (sendcmd(CC_YES, NULL) == 0)
didupdate = 1;
} else
(void) sendcmd(CC_NO, NULL);
break;
case CC_END:
*ptarget = CNULL;
ack();
return(didupdate);
case C_LOGMSG:
if (n > 0)
message(MT_INFO, "%s", s);
break;
case C_NOTEMSG:
if (n > 0)
message(MT_NOTICE, "%s", s);
break;
/* Goto top of loop */
case C_ERRMSG:
message(MT_NERROR, "%s", s);
return(didupdate);
case C_FERRMSG:
message(MT_FERROR, "%s", s);
finish();
default:
error("rmchk: unexpected response '%s'", respbuff);
err();
}
}
/*NOTREACHED*/
}
/*
* Send a directory
*
* Return < 0 on error.
* Return 0 if nothing happened.
* Return > 0 if anything is updated.
*/
static int senddir(rname, opts, stb, user, group, destdir)
char *rname;
opt_t opts;
struct stat *stb;
char *user, *group;
int destdir;
{
DIRENTRY *dp;
DIR *d;
char *optarget, *cp;
int len;
int didupdate = 0;
/*
* Don't descend into directory
*/
if (IS_ON(opts, DO_NODESCEND))
return(0);
if ((d = opendir(target)) == NULL) {
error("%s: opendir failed: %s", target, SYSERR);
return(-1);
}
/*
* Send recvdir command in recvit() format.
*/
(void) sendcmd(C_RECVDIR, "%o %04o 0 0 0 %s %s %s",
opts, stb->st_mode & 07777, user, group, rname);
if (response() < 0)
return(-1);
if (IS_ON(opts, DO_REMOVE))
if (rmchk(opts) > 0)
++didupdate;
optarget = ptarget;
len = ptarget - target;
while ((dp = readdir(d))) {
if (!strcmp(dp->d_name, ".") ||
!strcmp(dp->d_name, ".."))
continue;
if (len + 1 + (int) strlen(dp->d_name) >= MAXPATHLEN - 1) {
error("%s/%s: Name too long", target,
dp->d_name);
continue;
}
ptarget = optarget;
if (ptarget[-1] != '/')
*ptarget++ = '/';
cp = dp->d_name;
while ((*ptarget++ = *cp++))
;
ptarget--;
if (sendit(dp->d_name, opts, destdir) > 0)
didupdate = 1;
}
(void) closedir(d);
(void) sendcmd(C_END, NULL);
(void) response();
ptarget = optarget;
*ptarget = CNULL;
return(didupdate);
}
/*
* Send a link
*/
static int sendlink(rname, opts, stb, user, group, destdir)
char *rname;
opt_t opts;
struct stat *stb;
char *user;
char *group;
int destdir;
{
int f, n;
static char tbuf[BUFSIZ];
char lbuf[MAXPATHLEN];
u_char *s;
debugmsg(DM_CALL, "sendlink(%s, %x, stb, %d)\n", rname, opts, destdir);
if (stb->st_nlink > 1) {
struct linkbuf *lp;
if ((lp = linkinfo(stb)) != NULL)
return(sendhardlink(opts, lp, rname, destdir));
}
/*
* Gather and send basic link info
*/
(void) sendcmd(C_RECVSYMLINK, "%o %04o %ld %ld %ld %s %s %s",
opts, stb->st_mode & 07777,
(long) stb->st_size,
stb->st_mtime, stb->st_atime,
user, group, rname);
if (response() < 0)
return(-1);
/*
* Gather and send additional link info
*/
if ((n = readlink(target, lbuf, sizeof(lbuf)-1)) != -1)
lbuf[n] = '\0';
else {
error("%s: readlink failed", target);
err();
}
(void) sprintf(tbuf, "%.*s", (int) stb->st_size, lbuf);
(void) sendcmd(C_NONE, "%s\n", tbuf);
if (n != stb->st_size) {
error("%s: file changed size", target);
err();
} else
ack();
/*
* Check response
*/
f = response();
if (f < 0)
return(-1);
else if (f == 0 && IS_ON(opts, DO_COMPARE))
return(0);
/*
* Read and process responses from server.
* The server may send multiple messages regarding
* file deletes if the remote target is a directory.
*/
for (;;) {
n = remline(s = respbuff, sizeof(respbuff), TRUE);
if (n == -1) /* normal EOF */
return(0);
if (n == 0) {
error("expected control record");
continue;
}
switch (*s++) {
case C_END: /* End of send operation */
*ptarget = CNULL;
ack();
runspecial(target, opts, rname, destdir);
addcmdspecialfile(target, rname, destdir);
return(0);
case C_LOGMSG:
if (n > 0)
message(MT_INFO, "%s", s);
break;
case C_NOTEMSG:
if (n > 0)
message(MT_NOTICE, "%s", s);
break;
/* Goto top of loop */
case C_ERRMSG:
message(MT_NERROR, "%s", s);
return(-1);
case C_FERRMSG:
message(MT_FERROR, "%s", s);
finish();
default:
error("install link: unexpected response '%s'",
respbuff);
err();
}
}
/*NOTREACHED*/
}
/*
* Check to see if file needs to be updated on the remote machine.
* Returns:
* US_NOTHING - no update
* US_NOENT - remote doesn't exist
* US_OUTDATE - out of date
* US_DOCOMP - comparing binaries to determine if out of date
* US_MODE - File modes do not match
*/
static int update(rname, opts, statp)
char *rname;
opt_t opts;
struct stat *statp;
{
register off_t size;
register time_t mtime;
unsigned short lmode;
unsigned short rmode;
char *owner = NULL, *group = NULL;
int done, n;
u_char *cp;
debugmsg(DM_CALL, "update(%s, 0x%x, 0x%x)\n", rname, opts, statp);
if (IS_ON(opts, DO_NOEXEC))
if (isexec(target, statp)) {
debugmsg(DM_MISC, "%s is an executable\n", target);
return(US_NOTHING);
}
/*
* Check to see if the file exists on the remote machine.
*/
(void) sendcmd(C_QUERY, "%s", rname);
for (done = 0; !done;) {
n = remline(cp = respbuff, sizeof(respbuff), TRUE);
if (n <= 0) {
error("update: unexpected control record in response to query");
return(US_NOTHING);
}
switch (*cp++) {
case QC_ONNFS: /* Resides on a NFS */
debugmsg(DM_MISC,
"update: %s is on a NFS. Skipping...\n",
rname);
return(US_NOTHING);
case QC_SYM: /* Is a symbolic link */
debugmsg(DM_MISC,
"update: %s is a symlink. Skipping...\n",
rname);
return(US_NOTHING);
case QC_ONRO: /* Resides on a Read-Only fs */
debugmsg(DM_MISC,
"update: %s is on a RO fs. Skipping...\n",
rname);
return(US_NOTHING);
case QC_YES:
done = 1;
break;
case QC_NO: /* file doesn't exist so install it */
return(US_NOENT);
case C_ERRMSG:
if (cp)
message(MT_NERROR, "%s", cp);
return(US_NOTHING);
case C_FERRMSG:
if (cp)
message(MT_FERROR, "%s", cp);
finish();
case C_NOTEMSG:
if (cp)
message(MT_NOTICE, "%s", cp);
break;
/* Goto top of loop */
default:
error("update: unexpected response to query '%s'", cp);
return(US_NOTHING);
}
}
/*
* Target exists, but no other info passed
*/
if (n <= 1 || !S_ISREG(statp->st_mode))
return(US_OUTDATE);
if (IS_ON(opts, DO_COMPARE))
return(US_DOCOMP);
/*
* Parse size
*/
size = strtol(cp, (char **)&cp, 10);
if (*cp++ != ' ') {
error("update: size not delimited");
return(US_NOTHING);
}
/*
* Parse mtime
*/
mtime = strtol(cp, (char **)&cp, 10);
if (*cp++ != ' ') {
error("update: mtime not delimited");
return(US_NOTHING);
}
/*
* Parse remote file mode
*/
rmode = strtol(cp, (char **)&cp, 8);
if (cp && *cp)
++cp;
/*
* Be backwards compatible
*/
if (cp && *cp != CNULL) {
/*
* Parse remote file owner
*/
owner = strtok((char *)cp, " ");
if (owner == NULL) {
error("update: owner not delimited");
return(US_NOTHING);
}
/*
* Parse remote file group
*/
group = strtok(NULL, " ");
if (group == NULL) {
error("update: group not delimited");
return(US_NOTHING);
}
}
/*
* File needs to be updated?
*/
lmode = statp->st_mode & 07777;
debugmsg(DM_MISC, "update(%s,) local mode %04o remote mode %04o\n",
rname, lmode, rmode);
debugmsg(DM_MISC, "update(%s,) size %d mtime %d owner '%s' grp '%s'\n",
rname, (int) size, mtime, owner, group);
if (statp->st_mtime != mtime) {
if (statp->st_mtime < mtime && IS_ON(opts, DO_YOUNGER)) {
message(MT_WARNING,
"%s: Warning: remote copy is newer",
target);
return(US_NOTHING);
}
return(US_OUTDATE);
}
/*
* If the mode of a file does not match the local mode, the
* whole file is updated. This is done both to insure that
* a bogus version of the file has not been installed and to
* avoid having to handle weird cases of chmod'ing symlinks
* and such.
*/
if (!IS_ON(opts, DO_NOCHKMODE) && lmode != rmode) {
debugmsg(DM_MISC, "modes do not match (%04o != %04o).\n",
lmode, rmode);
return(US_OUTDATE);
}
if (statp->st_size != size) {
debugmsg(DM_MISC, "size does not match (%d != %d).\n",
(int) statp->st_size, size);
return(US_OUTDATE);
}
/*
* Check ownership
*/
if (!IS_ON(opts, DO_NOCHKOWNER) && owner) {
if (!IS_ON(opts, DO_NUMCHKOWNER)) {
/* Check by string compare */
if (strcmp(owner, getusername(statp->st_uid,
target, opts)) != 0) {
debugmsg(DM_MISC,
"owner does not match (%s != %s).\n",
getusername(statp->st_uid,
target, opts), owner);
return(US_OUTDATE);
}
} else {
/*
* Check numerically.
* Allow negative numbers.
*/
while (*owner && !isdigit(*owner) && (*owner != '-'))
++owner;
if (owner && atoi(owner) != statp->st_uid) {
debugmsg(DM_MISC,
"owner does not match (%d != %s).\n",
statp->st_uid, owner);
return(US_OUTDATE);
}
}
}
if (!IS_ON(opts, DO_NOCHKGROUP) && group) {
if (!IS_ON(opts, DO_NUMCHKGROUP)) {
/* Check by string compare */
if (strcmp(group, getgroupname(statp->st_gid,
target, opts)) != 0) {
debugmsg(DM_MISC,
"group does not match (%s != %s).\n",
getgroupname(statp->st_gid,
target, opts), group);
return(US_OUTDATE);
}
} else {
/* Check numerically */
/* Allow negative gid */
while (*group && !isdigit(*group) && (*group != '-'))
++group;
if (group && atoi(group) != statp->st_gid) {
debugmsg(DM_MISC,
"group does not match (%d != %s).\n",
statp->st_gid, group);
return(US_OUTDATE);
}
}
}
return(US_NOTHING);
}
/*
* Stat a file
*/
static int dostat(file, statbuf, opts)
char *file;
struct stat *statbuf;
opt_t opts;
{
int s;
if (IS_ON(opts, DO_FOLLOW))
s = stat(file, statbuf);
else
s = lstat(file, statbuf);
if (s < 0)
error("%s: %s failed: %s", file,
IS_ON(opts, DO_FOLLOW) ? "stat" : "lstat", SYSERR);
return(s);
}
/*
* Transfer the file or directory in target[].
* rname is the name of the file on the remote host.
*
* Return < 0 on error.
* Return 0 if nothing happened.
* Return > 0 if anything is updated.
*/
static int sendit(rname, opts, destdir)
char *rname;
opt_t opts;
int destdir;
{
static struct stat stb;
extern struct subcmd *subcmds;
char *user, *group;
int u, len;
int didupdate = 0;
/*
* Remove possible accidental newline
*/
len = strlen(rname);
if (len > 0 && rname[len-1] == '\n')
rname[len-1] = CNULL;
if (checkfilename(rname) != 0)
return(-1);
debugmsg(DM_CALL, "sendit(%s, 0x%x) called\n", rname, opts);
if (except(target))
return(0);
if (dostat(target, &stb, opts) < 0)
return(-1);
/*
* Does rname need updating?
*/
u = update(rname, opts, &stb);
debugmsg(DM_MISC, "sendit(%s, 0x%x): update status of %s is %d\n",
rname, opts, target, u);
/*
* Don't need to update the file, but we may need to save hardlink
* info.
*/
if (u == US_NOTHING) {
if (S_ISREG(stb.st_mode) && stb.st_nlink > 1)
(void) linkinfo(&stb);
return(0);
}
/*
* File mode needs changing
*/
if (u == US_MODE) {
if (IS_ON(opts, DO_VERIFY)) {
message(MT_INFO, "%s: need to chmod to %04o",
target, stb.st_mode & 07777);
runspecial(target, opts, rname, destdir);
return(1);
}
message(MT_CHANGE, "%s: chmod to %04o",
target, stb.st_mode & 07777);
(void) sendcmd(C_CHMOD, "%o %04o %s",
opts, stb.st_mode & 07777, rname);
(void) response();
return(1);
}
user = getusername(stb.st_uid, target, opts);
group = getgroupname(stb.st_gid, target, opts);
/*
* No entry - need to install
*/
if (u == US_NOENT) {
if (IS_ON(opts, DO_VERIFY)) {
message(MT_INFO, "%s: need to install", target);
runspecial(target, opts, rname, destdir);
return(1);
}
if (!IS_ON(opts, DO_QUIET))
message(MT_CHANGE, "%s: installing", target);
FLAG_OFF(opts, (DO_COMPARE|DO_REMOVE));
}
/*
* Handle special file types, including directories and symlinks
*/
if (S_ISDIR(stb.st_mode)) {
if (senddir(rname, opts, &stb, user, group, destdir) > 0)
didupdate = 1;
} else if (S_ISLNK(stb.st_mode)) {
if (u != US_NOENT)
FLAG_ON(opts, DO_COMPARE);
/*
* Since we always send link info to the server
* so the server can determine if the remote link
* is correct, we never get any acknowledge meant
* from the server whether the link was really
* updated or not.
*/
(void) sendlink(rname, opts, &stb, user, group, destdir);
} else if (S_ISREG(stb.st_mode)) {
if (u == US_OUTDATE) {
if (IS_ON(opts, DO_VERIFY)) {
message(MT_INFO, "%s: need to update", target);
runspecial(target, opts, rname, destdir);
return(1);
}
if (!IS_ON(opts, DO_QUIET))
message(MT_CHANGE, "%s: updating", target);
}
if (sendfile(rname, opts, &stb, user, group, destdir) == 0)
didupdate = 1;
} else
error("%s: unknown file type", target);
return(didupdate);
}
/*
* Remove temporary files and do any cleanup operations before exiting.
*/
extern void cleanup()
{
char *file;
#ifdef USE_STATDB
extern char statfile[];
(void) unlink(statfile);
#endif
if ((file = getnotifyfile()))
(void) unlink(file);
}
/*
* Update the file(s) if they are different.
* destdir = 1 if destination should be a directory
* (i.e., more than one source is being copied to the same destination).
*
* Return < 0 on error.
* Return 0 if nothing updated.
* Return > 0 if something was updated.
*/
extern int install(src, dest, ddir, destdir, opts)
char *src, *dest;
int ddir, destdir;
opt_t opts;
{
static char destcopy[MAXPATHLEN];
char *rname;
int didupdate = 0;
debugmsg(DM_CALL,
"install(src=%s,dest=%s,ddir=%d,destdir=%d,opts=%d) start\n",
(src?src:"NULL"), (dest?dest:"NULL"), ddir, destdir, opts);
/*
* Save source name
*/
if (IS_ON(opts, DO_WHOLE))
source[0] = CNULL;
else
(void) strcpy(source, src);
if (dest == NULL) {
FLAG_OFF(opts, DO_WHOLE); /* WHOLE only useful if renaming */
dest = src;
}
if (checkfilename(dest) != 0)
return(-1);
if (nflag || debug) {
static char buff[BUFSIZ];
char *cp;
cp = getondistoptlist(opts);
(void) sprintf(buff, "%s%s%s %s %s",
IS_ON(opts, DO_VERIFY) ? "verify" : "install",
(cp) ? " -o" : "", (cp) ? cp : "",
src, dest);
if (nflag) {
printf("%s\n", buff);
return(0);
} else
debugmsg(DM_MISC, "%s\n", buff);
}
rname = exptilde(target, src);
if (rname == NULL)
return(-1);
ptarget = target;
while (*ptarget)
ptarget++;
/*
* If we are renaming a directory and we want to preserve
* the directory heirarchy (-w), we must strip off the leading
* directory name and preserve the rest.
*/
if (IS_ON(opts, DO_WHOLE)) {
while (*rname == '/')
rname++;
ddir = 1;
destdir = 1;
} else {
rname = strrchr(target, '/');
/* Check if no '/' or target ends in '/' */
if (rname == NULL ||
rname+1 == NULL ||
*(rname+1) == CNULL)
rname = target;
else
rname++;
}
debugmsg(DM_MISC,
"install: target=%s src=%s rname=%s dest='%s' destdir=%d, ddir=%d\n",
target, source, rname, dest, destdir, ddir);
/*
* Pass the destination file/directory name to remote.
*/
if (ddir)
(void) sendcmd(C_DIRTARGET, "%o %s", opts, dest);
else
(void) sendcmd(C_TARGET, "%o %s", opts, dest);
if (response() < 0)
return(-1);
/*
* Save the name of the remote target destination if we are
* in WHOLE mode (destdir > 0) or if the source and destination
* are not the same. This info will be used later for maintaining
* hardlink info.
*/
if (destdir || (src && dest && strcmp(src, dest))) {
(void) strcpy(destcopy, dest);
Tdest = destcopy;
}
didupdate = sendit(rname, opts, destdir);
Tdest = 0;
return(didupdate);
}
|