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
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
|
/* $OpenBSD: server.c,v 1.33 2014/07/12 03:10:03 guenther 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. 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.
*/
#include <dirent.h>
#include "defs.h"
/*
* Server routines
*/
char tempname[sizeof _RDIST_TMP + 1]; /* Tmp file name */
char buf[BUFSIZ]; /* general purpose buffer */
char target[MAXPATHLEN]; /* target/source directory name */
char *ptarget; /* pointer to end of target name */
int catname = 0; /* cat name to target name */
char *sptarget[32]; /* stack of saved ptarget's for directories */
char *fromhost = NULL; /* Client hostname */
static int64_t min_freespace = 0; /* Minimium free space on a filesystem */
static int64_t min_freefiles = 0; /* Minimium free # files on a filesystem */
int oumask; /* Old umask */
static int cattarget(char *);
static int setownership(char *, int, uid_t, gid_t, int);
static int setfilemode(char *, int, int, int);
static int fchog(int, char *, char *, char *, int);
static int removefile(struct stat *, int);
static void doclean(char *);
static void clean(char *);
static void dospecial(char *);
static void docmdspecial(void);
static void query(char *);
static int chkparent(char *, opt_t);
static char *savetarget(char *, opt_t);
static void recvfile(char *, opt_t, int, char *, char *, time_t, time_t, off_t);
static void recvdir(opt_t, int, char *, char *);
static void recvlink(char *, opt_t, int, off_t);
static void hardlink(char *);
static void setconfig(char *);
static void recvit(char *, int);
static void dochmog(char *);
static void settarget(char *, int);
/*
* Cat "string" onto the target buffer with error checking.
*/
static int
cattarget(char *string)
{
if (strlen(string) + strlen(target) + 2 > sizeof(target)) {
message(MT_INFO, "target buffer is not large enough.");
return(-1);
}
if (!ptarget) {
message(MT_INFO, "NULL target pointer set.");
return(-10);
}
(void) snprintf(ptarget, sizeof(target) - (ptarget - target),
"/%s", string);
return(0);
}
/*
* Set uid and gid ownership of a file.
*/
static int
setownership(char *file, int fd, uid_t uid, gid_t gid, int islink)
{
int status = -1;
/*
* We assume only the Superuser can change uid ownership.
*/
if (getuid() != 0)
uid = -1;
if (islink)
status = lchown(file, uid, gid);
if (fd != -1 && !islink)
status = fchown(fd, uid, gid);
if (status < 0 && !islink)
status = chown(file, uid, gid);
if (status < 0) {
if (uid == (uid_t)-1)
message(MT_NOTICE, "%s: chgrp %d failed: %s",
target, gid, SYSERR);
else
message(MT_NOTICE, "%s: chown %d.%d failed: %s",
target, uid, gid, SYSERR);
return(-1);
}
return(0);
}
/*
* Set mode of a file
*/
static int
setfilemode(char *file, int fd, int mode, int islink)
{
int status = -1;
if (mode == -1)
return(0);
if (islink)
status = fchmodat(AT_FDCWD, file, mode, AT_SYMLINK_NOFOLLOW);
if (fd != -1 && !islink)
status = fchmod(fd, mode);
if (status < 0 && !islink)
status = chmod(file, mode);
if (status < 0) {
message(MT_NOTICE, "%s: chmod failed: %s", target, SYSERR);
return(-1);
}
return(0);
}
/*
* Change owner, group and mode of file.
*/
static int
fchog(int fd, char *file, char *owner, char *group, int mode)
{
static struct group *gr = NULL;
extern char *locuser;
int i;
struct stat st;
uid_t uid;
gid_t gid;
gid_t primegid = (gid_t)-2;
uid = userid;
if (userid == 0) { /* running as root; take anything */
if (*owner == ':') {
uid = (uid_t) atoi(owner + 1);
} else if (pw == NULL || strcmp(owner, pw->pw_name) != 0) {
if ((pw = getpwnam(owner)) == NULL) {
if (mode != -1 && IS_ON(mode, S_ISUID)) {
message(MT_NOTICE,
"%s: unknown login name \"%s\", clearing setuid",
target, owner);
mode &= ~S_ISUID;
uid = 0;
} else
message(MT_NOTICE,
"%s: unknown login name \"%s\"",
target, owner);
} else
uid = pw->pw_uid;
} else {
uid = pw->pw_uid;
primegid = pw->pw_gid;
}
if (*group == ':') {
gid = (gid_t)atoi(group + 1);
goto ok;
}
} else { /* not root, setuid only if user==owner */
struct passwd *lupw;
if (mode != -1) {
if (IS_ON(mode, S_ISUID) &&
strcmp(locuser, owner) != 0)
mode &= ~S_ISUID;
if (mode)
mode &= ~S_ISVTX; /* and strip sticky too */
}
if ((lupw = getpwnam(locuser)) != NULL)
primegid = lupw->pw_gid;
}
gid = (gid_t)-1;
if (gr == NULL || strcmp(group, gr->gr_name) != 0) {
if ((*group == ':' &&
(getgrgid(gid = atoi(group + 1)) == NULL))
|| ((gr = (struct group *)getgrnam(group)) == NULL)) {
if (mode != -1 && IS_ON(mode, S_ISGID)) {
message(MT_NOTICE,
"%s: unknown group \"%s\", clearing setgid",
target, group);
mode &= ~S_ISGID;
} else
message(MT_NOTICE,
"%s: unknown group \"%s\"",
target, group);
} else
gid = gr->gr_gid;
} else
gid = gr->gr_gid;
if (userid && gid >= 0 && gid != primegid) {
if (gr)
for (i = 0; gr->gr_mem[i] != NULL; i++)
if (strcmp(locuser, gr->gr_mem[i]) == 0)
goto ok;
if (mode != -1 && IS_ON(mode, S_ISGID)) {
message(MT_NOTICE,
"%s: user %s not in group %s, clearing setgid",
target, locuser, group);
mode &= ~S_ISGID;
}
gid = (gid_t)-1;
}
ok:
if (stat(file, &st) == -1) {
error("%s: Stat failed %s", file, SYSERR);
return -1;
}
/*
* Set uid and gid ownership. If that fails, strip setuid and
* setgid bits from mode. Once ownership is set, successful
* or otherwise, set the new file mode.
*/
if (setownership(file, fd, uid, gid, S_ISLNK(st.st_mode)) < 0) {
if (mode != -1 && IS_ON(mode, S_ISUID)) {
message(MT_NOTICE,
"%s: chown failed, clearing setuid", target);
mode &= ~S_ISUID;
}
if (mode != -1 && IS_ON(mode, S_ISGID)) {
message(MT_NOTICE,
"%s: chown failed, clearing setgid", target);
mode &= ~S_ISGID;
}
}
(void) setfilemode(file, fd, mode, S_ISLNK(st.st_mode));
return(0);
}
/*
* Remove a file or directory (recursively) and send back an acknowledge
* or an error message.
*/
static int
removefile(struct stat *statb, int silent)
{
DIR *d;
static struct dirent *dp;
char *cp;
struct stat stb;
char *optarget;
int len, failures = 0;
switch (statb->st_mode & S_IFMT) {
case S_IFREG:
case S_IFLNK:
case S_IFCHR:
case S_IFBLK:
case S_IFSOCK:
case S_IFIFO:
if (unlink(target) < 0) {
if (errno == ETXTBSY) {
if (!silent)
message(MT_REMOTE|MT_NOTICE,
"%s: unlink failed: %s",
target, SYSERR);
return(0);
} else {
error("%s: unlink failed: %s", target, SYSERR);
return(-1);
}
}
goto removed;
case S_IFDIR:
break;
default:
error("%s: not a plain file", target);
return(-1);
}
errno = 0;
if ((d = opendir(target)) == NULL) {
error("%s: opendir failed: %s", target, SYSERR);
return(-1);
}
optarget = ptarget;
len = ptarget - target;
while ((dp = readdir(d)) != NULL) {
if (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' ||
(dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
continue;
if (len + 1 + (int)strlen(dp->d_name) >= MAXPATHLEN - 1) {
if (!silent)
message(MT_REMOTE|MT_WARNING,
"%s/%s: Name too long",
target, dp->d_name);
continue;
}
ptarget = optarget;
*ptarget++ = '/';
cp = dp->d_name;
while ((*ptarget++ = *cp++) != '\0')
continue;
ptarget--;
if (lstat(target, &stb) < 0) {
if (!silent)
message(MT_REMOTE|MT_WARNING,
"%s: lstat failed: %s",
target, SYSERR);
continue;
}
if (removefile(&stb, 0) < 0)
++failures;
}
(void) closedir(d);
ptarget = optarget;
*ptarget = CNULL;
if (failures)
return(-1);
if (rmdir(target) < 0) {
error("%s: rmdir failed: %s", target, SYSERR);
return(-1);
}
removed:
#if NEWWAY
if (!silent)
message(MT_CHANGE|MT_REMOTE, "%s: removed", target);
#else
/*
* We use MT_NOTICE instead of MT_CHANGE because this function is
* sometimes called by other functions that are suppose to return a
* single ack() back to the client (rdist). This is a kludge until
* the Rdist protocol is re-done. Sigh.
*/
message(MT_NOTICE|MT_REMOTE, "%s: removed", target);
#endif
return(0);
}
/*
* Check the current directory (initialized by the 'T' command to server())
* for extraneous files and remove them.
*/
static void
doclean(char *cp)
{
DIR *d;
struct dirent *dp;
struct stat stb;
char *optarget, *ep;
int len;
opt_t opts;
char targ[MAXPATHLEN*4];
opts = strtol(cp, &ep, 8);
if (*ep != CNULL) {
error("clean: options not delimited");
return;
}
if ((d = opendir(target)) == NULL) {
error("%s: opendir failed: %s", target, SYSERR);
return;
}
ack();
optarget = ptarget;
len = ptarget - target;
while ((dp = readdir(d)) != NULL) {
if (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' ||
(dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
continue;
if (len + 1 + (int)strlen(dp->d_name) >= MAXPATHLEN - 1) {
message(MT_REMOTE|MT_WARNING, "%s/%s: Name too long",
target, dp->d_name);
continue;
}
ptarget = optarget;
*ptarget++ = '/';
cp = dp->d_name;
while ((*ptarget++ = *cp++) != '\0')
continue;
ptarget--;
if (lstat(target, &stb) < 0) {
message(MT_REMOTE|MT_WARNING, "%s: lstat failed: %s",
target, SYSERR);
continue;
}
ENCODE(targ, dp->d_name);
(void) sendcmd(CC_QUERY, "%s", targ);
(void) remline(cp = buf, sizeof(buf), TRUE);
if (*cp != CC_YES)
continue;
if (IS_ON(opts, DO_VERIFY))
message(MT_REMOTE|MT_INFO, "%s: need to remove",
target);
else
(void) removefile(&stb, 0);
}
(void) closedir(d);
ptarget = optarget;
*ptarget = CNULL;
}
/*
* Frontend to doclean().
*/
static void
clean(char *cp)
{
doclean(cp);
(void) sendcmd(CC_END, NULL);
(void) response();
}
/*
* Execute a shell command to handle special cases.
* We can't really set an alarm timeout here since we
* have no idea how long the command should take.
*/
static void
dospecial(char *xcmd)
{
char cmd[BUFSIZ];
if (DECODE(cmd, xcmd) == -1) {
error("dospecial: Cannot decode command.");
return;
}
runcommand(cmd);
}
/*
* Do a special cmd command. This differs from normal special
* commands in that it's done after an entire command has been updated.
* The list of updated target files is sent one at a time with RC_FILE
* commands. Each one is added to an environment variable defined by
* E_FILES. When an RC_COMMAND is finally received, the E_FILES variable
* is stuffed into our environment and a normal dospecial() command is run.
*/
static void
docmdspecial(void)
{
char *cp;
char *cmd, *env = NULL;
int n;
size_t len;
/* We're ready */
ack();
for ( ; ; ) {
n = remline(cp = buf, sizeof(buf), FALSE);
if (n <= 0) {
error("cmdspecial: premature end of input.");
return;
}
switch (*cp++) {
case RC_FILE:
if (env == NULL) {
len = (2 * sizeof(E_FILES)) + strlen(cp) + 10;
env = (char *) xmalloc(len);
(void) snprintf(env, len, "export %s;%s=%s",
E_FILES, E_FILES, cp);
} else {
len = strlen(env) + 1 + strlen(cp) + 1;
env = (char *) xrealloc(env, len);
(void) strlcat(env, ":", len);
(void) strlcat(env, cp, len);
}
ack();
break;
case RC_COMMAND:
if (env) {
len = strlen(env) + 1 + strlen(cp) + 1;
env = (char *) xrealloc(env, len);
(void) strlcat(env, ";", len);
(void) strlcat(env, cp, len);
cmd = env;
} else
cmd = cp;
dospecial(cmd);
if (env)
(void) free(env);
return;
default:
error("Unknown cmdspecial command '%s'.", cp);
return;
}
}
}
/*
* Query. Check to see if file exists. Return one of the following:
*
#ifdef NFS_CHECK
* QC_ONNFS - resides on a NFS
#endif NFS_CHECK
#ifdef RO_CHECK
* QC_ONRO - resides on a Read-Only filesystem
#endif RO_CHECK
* QC_NO - doesn't exist
* QC_YESsize mtime - exists and its a regular file (size & mtime of file)
* QC_YES - exists and its a directory or symbolic link
* QC_ERRMSGmessage - error message
*/
static void
query(char *xname)
{
static struct stat stb;
int s = -1, stbvalid = 0;
char name[MAXPATHLEN];
if (DECODE(name, xname) == -1) {
error("query: Cannot decode filename");
return;
}
if (catname && cattarget(name) < 0)
return;
#if defined(NFS_CHECK)
if (IS_ON(options, DO_CHKNFS)) {
s = is_nfs_mounted(target, &stb, &stbvalid);
if (s > 0)
(void) sendcmd(QC_ONNFS, NULL);
/* Either the above check was true or an error occurred */
/* and is_nfs_mounted sent the error message */
if (s != 0) {
*ptarget = CNULL;
return;
}
}
#endif /* NFS_CHECK */
#if defined(RO_CHECK)
if (IS_ON(options, DO_CHKREADONLY)) {
s = is_ro_mounted(target, &stb, &stbvalid);
if (s > 0)
(void) sendcmd(QC_ONRO, NULL);
/* Either the above check was true or an error occurred */
/* and is_ro_mounted sent the error message */
if (s != 0) {
*ptarget = CNULL;
return;
}
}
#endif /* RO_CHECK */
if (IS_ON(options, DO_CHKSYM)) {
if (is_symlinked(target, &stb, &stbvalid) > 0) {
(void) sendcmd(QC_SYM, NULL);
return;
}
}
/*
* If stbvalid is false, "stb" is not valid because:
* a) RO_CHECK and NFS_CHECK were not defined
* b) The stat by is_*_mounted() either failed or
* does not match "target".
*/
if (!stbvalid && lstat(target, &stb) < 0) {
if (errno == ENOENT)
(void) sendcmd(QC_NO, NULL);
else
error("%s: lstat failed: %s", target, SYSERR);
*ptarget = CNULL;
return;
}
switch (stb.st_mode & S_IFMT) {
case S_IFLNK:
case S_IFDIR:
case S_IFREG:
(void) sendcmd(QC_YES, "%lld %lld %o %s %s",
(long long) stb.st_size,
(long long) stb.st_mtime,
stb.st_mode & 07777,
getusername(stb.st_uid, target, options),
getgroupname(stb.st_gid, target, options));
break;
default:
error("%s: not a file or directory", target);
break;
}
*ptarget = CNULL;
}
/*
* Check to see if parent directory exists and create one if not.
*/
static int
chkparent(char *name, opt_t opts)
{
char *cp;
struct stat stb;
int r = -1;
debugmsg(DM_CALL, "chkparent(%s, %lo) start\n", name, opts);
cp = strrchr(name, '/');
if (cp == NULL || cp == name)
return(0);
*cp = CNULL;
if (lstat(name, &stb) < 0) {
if (errno == ENOENT && chkparent(name, opts) >= 0) {
if (mkdir(name, 0777 & ~oumask) == 0) {
message(MT_NOTICE, "%s: mkdir", name);
r = 0;
} else
debugmsg(DM_MISC,
"chkparent(%s, %lo) mkdir fail: %s\n",
name, opts, SYSERR);
}
} else /* It exists */
r = 0;
/* Put back what we took away */
*cp = '/';
return(r);
}
/*
* Save a copy of 'file' by renaming it.
*/
static char *
savetarget(char *file, opt_t opts)
{
static char savefile[MAXPATHLEN];
if (strlen(file) + sizeof(SAVE_SUFFIX) + 1 > MAXPATHLEN) {
error("%s: Cannot save: Save name too long", file);
return(NULL);
}
if (IS_ON(opts, DO_HISTORY)) {
int i;
struct stat st;
/*
* There is a race here, but the worst that can happen
* is to lose a version of the file
*/
for (i = 1; i < 1000; i++) {
(void) snprintf(savefile, sizeof(savefile),
"%s;%.3d", file, i);
if (stat(savefile, &st) == -1 && errno == ENOENT)
break;
}
if (i == 1000) {
message(MT_NOTICE,
"%s: More than 1000 versions for %s; reusing 1\n",
savefile, SYSERR);
i = 1;
(void) snprintf(savefile, sizeof(savefile),
"%s;%.3d", file, i);
}
}
else {
(void) snprintf(savefile, sizeof(savefile), "%s%s",
file, SAVE_SUFFIX);
if (unlink(savefile) != 0 && errno != ENOENT) {
message(MT_NOTICE, "%s: remove failed: %s",
savefile, SYSERR);
return(NULL);
}
}
if (rename(file, savefile) != 0 && errno != ENOENT) {
error("%s -> %s: rename failed: %s",
file, savefile, SYSERR);
return(NULL);
}
return(savefile);
}
/*
* Receive a file
*/
static void
recvfile(char *new, opt_t opts, int mode, char *owner, char *group,
time_t mtime, time_t atime, off_t size)
{
int f, wrerr, olderrno;
off_t i;
char *cp;
char *savefile = NULL;
static struct stat statbuff;
/*
* Create temporary file
*/
if ((f = mkstemp(new)) < 0) {
if (errno != ENOENT || chkparent(new, opts) < 0 ||
(f = mkstemp(new)) < 0) {
error("%s: create failed: %s", new, SYSERR);
return;
}
}
/*
* Receive the file itself
*/
ack();
wrerr = 0;
olderrno = 0;
for (i = 0; i < size; i += BUFSIZ) {
off_t amt = BUFSIZ;
cp = buf;
if (i + amt > size)
amt = size - i;
do {
ssize_t j;
j = readrem(cp, amt);
if (j <= 0) {
(void) close(f);
(void) unlink(new);
fatalerr(
"Read error occurred while receiving file.");
finish();
}
amt -= j;
cp += j;
} while (amt > 0);
amt = BUFSIZ;
if (i + amt > size)
amt = size - i;
if (wrerr == 0 && xwrite(f, buf, amt) != amt) {
olderrno = errno;
wrerr++;
}
}
if (response() < 0) {
(void) close(f);
(void) unlink(new);
return;
}
if (wrerr) {
error("%s: Write error: %s", new, strerror(olderrno));
(void) close(f);
(void) unlink(new);
return;
}
/*
* Do file comparison if enabled
*/
if (IS_ON(opts, DO_COMPARE)) {
FILE *f1, *f2;
int c;
errno = 0; /* fopen is not a syscall */
if ((f1 = fopen(target, "r")) == NULL) {
error("%s: open for read failed: %s", target, SYSERR);
(void) close(f);
(void) unlink(new);
return;
}
errno = 0;
if ((f2 = fopen(new, "r")) == NULL) {
error("%s: open for read failed: %s", new, SYSERR);
(void) fclose(f1);
(void) close(f);
(void) unlink(new);
return;
}
while ((c = getc(f1)) == getc(f2))
if (c == EOF) {
debugmsg(DM_MISC,
"Files are the same '%s' '%s'.",
target, new);
(void) fclose(f1);
(void) fclose(f2);
(void) close(f);
(void) unlink(new);
/*
* This isn't an error per-se, but we
* need to indicate to the master that
* the file was not updated.
*/
error("");
return;
}
debugmsg(DM_MISC, "Files are different '%s' '%s'.",
target, new);
(void) fclose(f1);
(void) fclose(f2);
if (IS_ON(opts, DO_VERIFY)) {
message(MT_REMOTE|MT_INFO, "%s: need to update",
target);
(void) close(f);
(void) unlink(new);
return;
}
}
/*
* Set owner, group, and file mode
*/
if (fchog(f, new, owner, group, mode) < 0) {
(void) close(f);
(void) unlink(new);
return;
}
(void) close(f);
/*
* Perform utimes() after file is closed to make
* certain OS's, such as NeXT 2.1, happy.
*/
if (setfiletime(new, time(NULL), mtime) < 0)
message(MT_NOTICE, "%s: utimes failed: %s", new, SYSERR);
/*
* Try to save target file from being over-written
*/
if (IS_ON(opts, DO_SAVETARGETS))
if ((savefile = savetarget(target, opts)) == NULL) {
(void) unlink(new);
return;
}
/*
* If the target is a directory, we need to remove it first
* before we can rename the new file.
*/
if ((stat(target, &statbuff) == 0) && S_ISDIR(statbuff.st_mode)) {
char *saveptr = ptarget;
ptarget = &target[strlen(target)];
removefile(&statbuff, 0);
ptarget = saveptr;
}
/*
* Install new (temporary) file as the actual target
*/
if (rename(new, target) < 0) {
static const char fmt[] = "%s -> %s: rename failed: %s";
struct stat stb;
/*
* If the rename failed due to "Text file busy", then
* try to rename the target file and retry the rename.
*/
switch (errno) {
case ETXTBSY:
/* Save the target */
if ((savefile = savetarget(target, opts)) != NULL) {
/* Retry installing new file as target */
if (rename(new, target) < 0) {
error(fmt, new, target, SYSERR);
/* Try to put back save file */
if (rename(savefile, target) < 0)
error(fmt,
savefile, target, SYSERR);
(void) unlink(new);
} else
message(MT_NOTICE, "%s: renamed to %s",
target, savefile);
/*
* XXX: We should remove the savefile here.
* But we are nice to nfs clients and
* we keep it.
*/
}
break;
case EISDIR:
/*
* See if target is a directory and remove it if it is
*/
if (lstat(target, &stb) == 0) {
if (S_ISDIR(stb.st_mode)) {
char *optarget = ptarget;
for (ptarget = target; *ptarget;
ptarget++);
/* If we failed to remove, we'll catch
it later */
(void) removefile(&stb, 1);
ptarget = optarget;
}
}
if (rename(new, target) >= 0)
break;
/*FALLTHROUGH*/
default:
error(fmt, new, target, SYSERR);
(void) unlink(new);
break;
}
}
if (IS_ON(opts, DO_COMPARE))
message(MT_REMOTE|MT_CHANGE, "%s: updated", target);
else
ack();
}
/*
* Receive a directory
*/
static void
recvdir(opt_t opts, int mode, char *owner, char *group)
{
static char lowner[100], lgroup[100];
char *cp;
struct stat stb;
int s;
s = lstat(target, &stb);
if (s == 0) {
/*
* If target is not a directory, remove it
*/
if (!S_ISDIR(stb.st_mode)) {
if (IS_ON(opts, DO_VERIFY))
message(MT_NOTICE, "%s: need to remove",
target);
else {
if (unlink(target) < 0) {
error("%s: remove failed: %s",
target, SYSERR);
return;
}
}
s = -1;
errno = ENOENT;
} else {
if (!IS_ON(opts, DO_NOCHKMODE) &&
(stb.st_mode & 07777) != mode) {
if (IS_ON(opts, DO_VERIFY))
message(MT_NOTICE,
"%s: need to chmod to %o",
target, mode);
else {
if (chmod(target, mode) != 0)
message(MT_NOTICE,
"%s: chmod from %o to %o failed: %s",
target,
stb.st_mode & 07777,
mode,
SYSERR);
else
message(MT_NOTICE,
"%s: chmod from %o to %o",
target,
stb.st_mode & 07777,
mode);
}
}
/*
* Check ownership and set if necessary
*/
lowner[0] = CNULL;
lgroup[0] = CNULL;
if (!IS_ON(opts, DO_NOCHKOWNER) && owner) {
int o;
o = (owner[0] == ':') ? opts & DO_NUMCHKOWNER :
opts;
if ((cp = getusername(stb.st_uid, target, o))
!= NULL)
if (strcmp(owner, cp))
(void) strlcpy(lowner, cp,
sizeof(lowner));
}
if (!IS_ON(opts, DO_NOCHKGROUP) && group) {
int o;
o = (group[0] == ':') ? opts & DO_NUMCHKGROUP :
opts;
if ((cp = getgroupname(stb.st_gid, target, o))
!= NULL)
if (strcmp(group, cp))
(void) strlcpy(lgroup, cp,
sizeof(lgroup));
}
/*
* Need to set owner and/or group
*/
#define PRN(n) ((n[0] == ':') ? n+1 : n)
if (lowner[0] != CNULL || lgroup[0] != CNULL) {
if (lowner[0] == CNULL &&
(cp = getusername(stb.st_uid,
target, opts)))
(void) strlcpy(lowner, cp,
sizeof(lowner));
if (lgroup[0] == CNULL &&
(cp = getgroupname(stb.st_gid,
target, opts)))
(void) strlcpy(lgroup, cp,
sizeof(lgroup));
if (IS_ON(opts, DO_VERIFY))
message(MT_NOTICE,
"%s: need to chown from %s:%s to %s:%s",
target,
PRN(lowner), PRN(lgroup),
PRN(owner), PRN(group));
else {
if (fchog(-1, target, owner,
group, -1) == 0)
message(MT_NOTICE,
"%s: chown from %s:%s to %s:%s",
target,
PRN(lowner),
PRN(lgroup),
PRN(owner),
PRN(group));
}
}
#undef PRN
ack();
return;
}
}
if (IS_ON(opts, DO_VERIFY)) {
ack();
return;
}
/*
* Create the directory
*/
if (s < 0) {
if (errno == ENOENT) {
if (mkdir(target, mode) == 0 ||
(chkparent(target, opts) == 0 &&
mkdir(target, mode) == 0)) {
message(MT_NOTICE, "%s: mkdir", target);
(void) fchog(-1, target, owner, group, mode);
ack();
} else {
error("%s: mkdir failed: %s", target, SYSERR);
ptarget = sptarget[--catname];
*ptarget = CNULL;
}
return;
}
}
error("%s: lstat failed: %s", target, SYSERR);
ptarget = sptarget[--catname];
*ptarget = CNULL;
}
/*
* Receive a link
*/
static void
recvlink(char *new, opt_t opts, int mode, off_t size)
{
char tbuf[MAXPATHLEN], dbuf[BUFSIZ];
struct stat stb;
char *optarget;
int uptodate;
off_t i;
/*
* Read basic link info
*/
ack();
(void) remline(buf, sizeof(buf), TRUE);
if (response() < 0) {
err();
return;
}
if (DECODE(dbuf, buf) == -1) {
error("recvlink: cannot decode symlink target");
return;
}
uptodate = 0;
if ((i = readlink(target, tbuf, sizeof(tbuf)-1)) != -1) {
tbuf[i] = '\0';
if (i == size && strncmp(dbuf, tbuf, (int) size) == 0)
uptodate = 1;
}
mode &= 0777;
if (IS_ON(opts, DO_VERIFY) || uptodate) {
if (uptodate)
message(MT_REMOTE|MT_INFO, "");
else
message(MT_REMOTE|MT_INFO, "%s: need to update",
target);
if (IS_ON(opts, DO_COMPARE))
return;
(void) sendcmd(C_END, NULL);
(void) response();
return;
}
/*
* Make new symlink using a temporary name
*/
if (mktemp(new) == NULL || symlink(dbuf, new) < 0) {
if (errno != ENOENT || chkparent(new, opts) < 0 ||
mktemp(new) == NULL || symlink(dbuf, new) < 0) {
error("%s -> %s: symlink failed: %s", new, dbuf,
SYSERR);
return;
}
}
/*
* See if target is a directory and remove it if it is
*/
if (lstat(target, &stb) == 0) {
if (S_ISDIR(stb.st_mode)) {
optarget = ptarget;
for (ptarget = target; *ptarget; ptarget++);
if (removefile(&stb, 0) < 0) {
ptarget = optarget;
(void) unlink(new);
(void) sendcmd(C_END, NULL);
(void) response();
return;
}
ptarget = optarget;
}
}
/*
* Install link as the target
*/
if (rename(new, target) < 0) {
error("%s -> %s: symlink rename failed: %s",
new, target, SYSERR);
(void) unlink(new);
(void) sendcmd(C_END, NULL);
(void) response();
return;
}
message(MT_REMOTE|MT_CHANGE, "%s: updated", target);
/*
* Indicate end of receive operation
*/
(void) sendcmd(C_END, NULL);
(void) response();
}
/*
* Creat a hard link to existing file.
*/
static void
hardlink(char *cmd)
{
struct stat stb;
int exists = 0;
char *xoldname, *xnewname;
char *cp = cmd;
static char expbuf[BUFSIZ];
char oldname[BUFSIZ], newname[BUFSIZ];
/* Skip over opts */
(void) strtol(cp, &cp, 8);
if (*cp++ != ' ') {
error("hardlink: options not delimited");
return;
}
xoldname = strtok(cp, " ");
if (xoldname == NULL) {
error("hardlink: oldname name not delimited");
return;
}
if (DECODE(oldname, xoldname) == -1) {
error("hardlink: Cannot decode oldname");
return;
}
xnewname = strtok(NULL, " ");
if (xnewname == NULL) {
error("hardlink: new name not specified");
return;
}
if (DECODE(newname, xnewname) == -1) {
error("hardlink: Cannot decode newname");
return;
}
if (exptilde(expbuf, oldname, sizeof(expbuf)) == NULL) {
error("hardlink: tilde expansion failed");
return;
}
if (catname && cattarget(newname) < 0) {
error("Cannot set newname target.");
return;
}
if (lstat(target, &stb) == 0) {
int mode = stb.st_mode & S_IFMT;
if (mode != S_IFREG && mode != S_IFLNK) {
error("%s: not a regular file", target);
return;
}
exists = 1;
}
if (chkparent(target, options) < 0 ) {
error("%s: no parent: %s ", target, SYSERR);
return;
}
if (exists && (unlink(target) < 0)) {
error("%s: unlink failed: %s", target, SYSERR);
return;
}
if (link(expbuf, target) < 0) {
error("%s: cannot link to %s: %s", target, oldname, SYSERR);
return;
}
ack();
}
/*
* Set configuration information.
*
* A key letter is followed immediately by the value
* to set. The keys are:
* SC_FREESPACE - Set minimium free space of filesystem
* SC_FREEFILES - Set minimium free number of files of filesystem
*/
static void
setconfig(char *cmd)
{
char *cp = cmd;
char *estr;
const char *errstr;
switch (*cp++) {
case SC_HOSTNAME: /* Set hostname */
/*
* Only use info if we don't know who this is.
*/
if (!fromhost) {
fromhost = xstrdup(cp);
message(MT_SYSLOG, "startup for %s", fromhost);
setproctitle("serving %s", cp);
}
break;
case SC_FREESPACE: /* Minimium free space */
min_freespace = (int64_t)strtonum(cp, 0, LLONG_MAX, &errstr);
if (errstr)
fatalerr("Minimum free space is %s: '%s'", errstr,
optarg);
break;
case SC_FREEFILES: /* Minimium free files */
min_freefiles = (int64_t)strtonum(cp, 0, LLONG_MAX, &errstr);
if (errstr)
fatalerr("Minimum free files is %s: '%s'", errstr,
optarg);
break;
case SC_LOGGING: /* Logging options */
if ((estr = msgparseopts(cp, TRUE)) != NULL) {
fatalerr("Bad message option string (%s): %s",
cp, estr);
return;
}
break;
case SC_DEFOWNER:
(void) strlcpy(defowner, cp, sizeof(defowner));
break;
case SC_DEFGROUP:
(void) strlcpy(defgroup, cp, sizeof(defgroup));
break;
default:
message(MT_NOTICE, "Unknown config command \"%s\".", cp-1);
return;
}
}
/*
* Receive something
*/
static void
recvit(char *cmd, int type)
{
int mode;
opt_t opts;
off_t size;
time_t mtime, atime;
char *owner, *group, *file;
char new[MAXPATHLEN];
char fileb[MAXPATHLEN];
int64_t freespace = -1, freefiles = -1;
char *cp = cmd;
/*
* Get rdist option flags
*/
opts = strtol(cp, &cp, 8);
if (*cp++ != ' ') {
error("recvit: options not delimited");
return;
}
/*
* Get file mode
*/
mode = strtol(cp, &cp, 8);
if (*cp++ != ' ') {
error("recvit: mode not delimited");
return;
}
/*
* Get file size
*/
size = (off_t) strtoll(cp, &cp, 10);
if (*cp++ != ' ') {
error("recvit: size not delimited");
return;
}
/*
* Get modification time
*/
mtime = (time_t) strtoll(cp, &cp, 10);
if (*cp++ != ' ') {
error("recvit: mtime not delimited");
return;
}
/*
* Get access time
*/
atime = (time_t) strtoll(cp, &cp, 10);
if (*cp++ != ' ') {
error("recvit: atime not delimited");
return;
}
/*
* Get file owner name
*/
owner = strtok(cp, " ");
if (owner == NULL) {
error("recvit: owner name not delimited");
return;
}
/*
* Get file group name
*/
group = strtok(NULL, " ");
if (group == NULL) {
error("recvit: group name not delimited");
return;
}
/*
* Get file name. Can't use strtok() since there could
* be white space in the file name.
*/
if (DECODE(fileb, group + strlen(group) + 1) == -1) {
error("recvit: Cannot decode file name");
return;
}
if (fileb[0] == '\0') {
error("recvit: no file name");
return;
}
file = fileb;
debugmsg(DM_MISC,
"recvit: opts = %04lo mode = %04o size = %lld mtime = %lld",
opts, mode, (long long) size, (long long)mtime);
debugmsg(DM_MISC,
"recvit: owner = '%s' group = '%s' file = '%s' catname = %d isdir = %d",
owner, group, file, catname, (type == S_IFDIR) ? 1 : 0);
if (type == S_IFDIR) {
if ((size_t) catname >= sizeof(sptarget)) {
error("%s: too many directory levels", target);
return;
}
sptarget[catname] = ptarget;
if (catname++) {
*ptarget++ = '/';
while ((*ptarget++ = *file++) != '\0')
continue;
ptarget--;
}
} else {
/*
* Create name of temporary file
*/
if (catname && cattarget(file) < 0) {
error("Cannot set file name.");
return;
}
file = strrchr(target, '/');
if (file == NULL)
(void) strlcpy(new, tempname, sizeof(new));
else if (file == target)
(void) snprintf(new, sizeof(new), "/%s", tempname);
else {
*file = CNULL;
(void) snprintf(new, sizeof(new), "%s/%s", target,
tempname);
*file = '/';
}
}
/*
* Check to see if there is enough free space and inodes
* to install this file.
*/
if (min_freespace || min_freefiles) {
/* Convert file size to kilobytes */
int64_t fsize = (int64_t)size / 1024;
if (getfilesysinfo(target, &freespace, &freefiles) != 0)
return;
/*
* filesystem values < 0 indicate unsupported or unavailable
* information.
*/
if (min_freespace && (freespace >= 0) &&
(freespace - fsize < min_freespace)) {
error(
"%s: Not enough free space on filesystem: min %lld "
"free %lld", target, min_freespace, freespace);
return;
}
if (min_freefiles && (freefiles >= 0) &&
(freefiles - 1 < min_freefiles)) {
error(
"%s: Not enough free files on filesystem: min %lld free "
"%lld", target, min_freefiles, freefiles);
return;
}
}
/*
* Call appropriate receive function to receive file
*/
switch (type) {
case S_IFDIR:
recvdir(opts, mode, owner, group);
break;
case S_IFLNK:
recvlink(new, opts, mode, size);
break;
case S_IFREG:
recvfile(new, opts, mode, owner, group, mtime, atime, size);
break;
default:
error("%d: unknown file type", type);
break;
}
}
/*
* Chmog something
*/
static void
dochmog(char *cmd)
{
int mode;
opt_t opts;
char *owner, *group, *file;
char *cp = cmd;
char fileb[MAXPATHLEN];
/*
* Get rdist option flags
*/
opts = strtol(cp, &cp, 8);
if (*cp++ != ' ') {
error("dochmog: options not delimited");
return;
}
/*
* Get file mode
*/
mode = strtol(cp, &cp, 8);
if (*cp++ != ' ') {
error("dochmog: mode not delimited");
return;
}
/*
* Get file owner name
*/
owner = strtok(cp, " ");
if (owner == NULL) {
error("dochmog: owner name not delimited");
return;
}
/*
* Get file group name
*/
group = strtok(NULL, " ");
if (group == NULL) {
error("dochmog: group name not delimited");
return;
}
/*
* Get file name. Can't use strtok() since there could
* be white space in the file name.
*/
if (DECODE(fileb, group + strlen(group) + 1) == -1) {
error("dochmog: Cannot decode file name");
return;
}
if (fileb[0] == '\0') {
error("dochmog: no file name");
return;
}
file = fileb;
debugmsg(DM_MISC,
"dochmog: opts = %04lo mode = %04o", opts, mode);
debugmsg(DM_MISC,
"dochmog: owner = '%s' group = '%s' file = '%s' catname = %d",
owner, group, file, catname);
if (catname && cattarget(file) < 0) {
error("Cannot set newname target.");
return;
}
(void) fchog(-1, target, owner, group, mode);
ack();
}
/*
* Set target information
*/
static void
settarget(char *cmd, int isdir)
{
char *cp = cmd;
opt_t opts;
char file[BUFSIZ];
catname = isdir;
/*
* Parse options for this target
*/
opts = strtol(cp, &cp, 8);
if (*cp++ != ' ') {
error("settarget: options not delimited");
return;
}
options = opts;
if (DECODE(file, cp) == -1) {
error("settarget: Cannot decode target name");
return;
}
/*
* Handle target
*/
if (exptilde(target, cp, sizeof(target)) == NULL)
return;
ptarget = target;
while (*ptarget)
ptarget++;
ack();
}
/*
* Cleanup in preparation for exiting.
*/
void
cleanup(int dummy)
{
/* We don't need to do anything */
}
/*
* Server routine to read requests and process them.
*/
void
server(void)
{
static char cmdbuf[BUFSIZ];
char *cp;
int n;
extern jmp_buf finish_jmpbuf;
if (setjmp(finish_jmpbuf))
return;
(void) signal(SIGHUP, sighandler);
(void) signal(SIGINT, sighandler);
(void) signal(SIGQUIT, sighandler);
(void) signal(SIGTERM, sighandler);
(void) signal(SIGPIPE, sighandler);
(void) umask(oumask = umask(0));
(void) strlcpy(tempname, _RDIST_TMP, sizeof(tempname));
if (fromhost) {
message(MT_SYSLOG, "Startup for %s", fromhost);
#if defined(SETARGS)
setproctitle("Serving %s", fromhost);
#endif /* SETARGS */
}
/*
* Let client know we want it to send it's version number
*/
(void) sendcmd(S_VERSION, NULL);
if (remline(cmdbuf, sizeof(cmdbuf), TRUE) < 0) {
error("server: expected control record");
return;
}
if (cmdbuf[0] != S_VERSION || !isdigit((unsigned char)cmdbuf[1])) {
error("Expected version command, received: \"%s\".", cmdbuf);
return;
}
proto_version = atoi(&cmdbuf[1]);
if (proto_version != VERSION) {
error("Protocol version %d is not supported.", proto_version);
return;
}
/* Version number is okay */
ack();
/*
* Main command loop
*/
for ( ; ; ) {
n = remline(cp = cmdbuf, sizeof(cmdbuf), TRUE);
if (n == -1) /* EOF */
return;
if (n == 0) {
error("server: expected control record");
continue;
}
switch (*cp++) {
case C_SETCONFIG: /* Configuration info */
setconfig(cp);
ack();
continue;
case C_DIRTARGET: /* init target file/directory name */
settarget(cp, TRUE);
continue;
case C_TARGET: /* init target file/directory name */
settarget(cp, FALSE);
continue;
case C_RECVREG: /* Transfer a regular file. */
recvit(cp, S_IFREG);
continue;
case C_RECVDIR: /* Transfer a directory. */
recvit(cp, S_IFDIR);
continue;
case C_RECVSYMLINK: /* Transfer symbolic link. */
recvit(cp, S_IFLNK);
continue;
case C_RECVHARDLINK: /* Transfer hard link. */
hardlink(cp);
continue;
case C_END: /* End of transfer */
*ptarget = CNULL;
if (catname <= 0) {
error("server: too many '%c's", C_END);
continue;
}
ptarget = sptarget[--catname];
*ptarget = CNULL;
ack();
continue;
case C_CLEAN: /* Clean. Cleanup a directory */
clean(cp);
continue;
case C_QUERY: /* Query file/directory */
query(cp);
continue;
case C_SPECIAL: /* Special. Execute commands */
dospecial(cp);
continue;
case C_CMDSPECIAL: /* Cmd Special. Execute commands */
docmdspecial();
continue;
case C_CHMOG: /* Set owner, group, mode */
dochmog(cp);
continue;
case C_ERRMSG: /* Normal error message */
if (cp && *cp)
message(MT_NERROR|MT_NOREMOTE, "%s", cp);
continue;
case C_FERRMSG: /* Fatal error message */
if (cp && *cp)
message(MT_FERROR|MT_NOREMOTE, "%s", cp);
return;
default:
error("server: unknown command '%s'", cp - 1);
case CNULL:
continue;
}
}
}
|