forked from lunar-linux/installwatch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstallwatch.c
More file actions
4710 lines (3759 loc) · 106 KB
/
installwatch.c
File metadata and controls
4710 lines (3759 loc) · 106 KB
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
/*
* Copyright (C) 1998-9 Pancrazio `Ezio' de Mauro <p@demauro.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Id: installwatch.c,v 0.7.0.11 2008/11/09 07:47:00 izto Exp $
*
* april-15-2001 - Modifications by Felipe Eduardo Sanchez Diaz Duran
* <izto@asic-linux.com.mx>
* Added backup() and make_path() functions.
*
* november-25-2002 - Modifications by Olivier Fleurigeon
* <olivier.fleurigeon@cegedim.fr>
*
* march-31-2007 - Modifications by Frederick Emmott
* <mail@fredemmott.co.uk>
*
* july-17-2018 - Modifications by Stefan Wold <ratler@lunar-linux.org>
* - a few changes to build with autotools
*
*/
#ifndef _GNU_SOURCE
# define _GNU_SOURCE 1
#endif
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <syslog.h>
#include <errno.h>
#include <unistd.h>
#include <libgen.h>
#include <inttypes.h>
#undef basename
#include <string.h>
#include <time.h>
#include <utime.h>
#include <dlfcn.h>
#include <dirent.h>
/* There's no d_off on GNU/kFreeBSD */
#if defined(__FreeBSD_kernel__)
#define D_OFF(X) (-1)
#else
#define D_OFF(X) (X)
#endif
#include <config.h>
#ifdef HAVE_SIZE_T_TRUNCATE
# define TRUNCATE_T size_t
#else
# define TRUNCATE_T off_t
#endif
#ifdef HAVE_SSIZE_T_READLINKAT
# define READLINKAT_T ssize_t
#else
# define READLINKAT_T int
#endif
#define LOGLEVEL (LOG_USER | LOG_INFO | LOG_PID)
#define BUFSIZE 1024
#define error(X) (X < 0 ? strerror(errno) : "success")
int __installwatch_refcount = 0;
int __installwatch_timecount = 0;
#define REFCOUNT __installwatch_refcount++
#define TIMECOUNT __installwatch_timecount++
static time_t (*true_time) (time_t *);
static int (*true_chdir)(const char *);
static int (*true_chmod)(const char *, mode_t);
static int (*true_chown)(const char *, uid_t, gid_t);
static int (*true_chroot)(const char *);
static int (*true_creat)(const char *, mode_t);
static int (*true_fchmod)(int, mode_t);
static int (*true_fchown)(int, uid_t, gid_t);
static FILE *(*true_fopen)(const char *,const char*);
static int (*true_ftruncate)(int, TRUNCATE_T);
static char *(*true_getcwd)(char*,size_t);
static int (*true_lchown)(const char *, uid_t, gid_t);
static int (*true_link)(const char *, const char *);
static int (*true_mkdir)(const char *, mode_t);
static int (*true_xmknod)(int ver,const char *, mode_t, dev_t *);
static int (*true_open)(const char *, int, ...);
static DIR *(*true_opendir)(const char *);
static struct dirent *(*true_readdir)(DIR *dir);
#if (GLIBC_MINOR <= 4)
static int (*true_readlink)(const char*,char *,size_t);
#else
static ssize_t (*true_readlink)(const char*,char *,size_t);
#endif
static char *(*true_realpath)(const char *,char *);
static int (*true_rename)(const char *, const char *);
static int (*true_rmdir)(const char *);
static int (*true_xstat)(int,const char *,struct stat *);
static int (*true_lxstat)(int,const char *,struct stat *);
static int (*true_symlink)(const char *, const char *);
static int (*true_truncate)(const char *, TRUNCATE_T);
static int (*true_unlink)(const char *);
static int (*true_utime)(const char *,const struct utimbuf *);
static int (*true_utimes)(const char *,const struct timeval *);
#ifdef ENABLE_ACCESS_SYSCALL
static int (*true_access)(const char *, int);
#endif
#ifdef ENABLE_ACL_SYSCALL
static int (*true_setxattr)(const char *,const char *,const void *,
size_t, int);
static int (*true_removexattr)(const char *,const char *);
#endif
#if(GLIBC_MINOR >= 1)
static int (*true_creat64)(const char *, __mode_t);
static FILE *(*true_fopen64)(const char *,const char *);
static int (*true_ftruncate64)(int, __off64_t);
static int (*true_open64)(const char *, int, ...);
static struct dirent64 *(*true_readdir64)(DIR *dir);
#if(GLIBC_MINOR >= 10)
static int (*true_scandir)( const char *,struct dirent ***,
int (*)(const struct dirent *),
int (*)(const struct dirent **,const struct dirent **));
static int (*true_scandir64)( const char *,struct dirent64 ***,
int (*)(const struct dirent64 *),
int (*)(const struct dirent64 **,const struct dirent64 **));
#else
static int (*true_scandir)( const char *,struct dirent ***,
int (*)(const struct dirent *),
int (*)(const void *,const void *));
static int (*true_scandir64)( const char *,struct dirent64 ***,
int (*)(const struct dirent64 *),
int (*)(const void *,const void *));
#endif
static int (*true_xstat64)(int,const char *, struct stat64 *);
static int (*true_lxstat64)(int,const char *, struct stat64 *);
static int (*true_truncate64)(const char *, __off64_t);
#endif
#if (GLIBC_MINOR >= 4)
static int (*true_openat)(int, const char *, int, ...);
static int (*true_fchmodat)(int, const char *, mode_t, int);
static int (*true_fchownat)(int, const char *, uid_t, gid_t, int);
static int (*true_fxstatat)(int, int, const char *, struct stat *, int);
static int (*true_fxstatat64)(int, int, const char *, struct stat64 *, int);
static int (*true_linkat)(int, const char *, int, const char *, int);
static int (*true_mkdirat)(int, const char *, mode_t);
static int (*true_readlinkat)(int, const char *, char *, size_t);
static int (*true_xmknodat)(int, int, const char *, mode_t, dev_t *);
static int (*true_renameat)(int, const char *, int, const char *);
static int (*true_symlinkat)(const char *, int, const char *);
static int (*true_unlinkat)(int, const char *, int);
#endif
#ifdef HAVE_RENAMEAT2
static int (*true_renameat2)(int, const char *, int, const char *, unsigned int);
#endif
#ifdef HAVE_UTIMENSAT
static int (*true_utimensat)(int, const char *, const struct timespec[2], int);
#endif
#if defined __GNUC__ && __GNUC__>=2
#define inline inline
#else
#define inline
#endif
#ifndef _STAT_VER
#if defined(__aarch64__)
# define _STAT_VER 0
#elif defined(__x86_64__)
# define _STAT_VER 1
#else
# define _STAT_VER 3
#endif
#endif
#ifndef _MKNOD_VER
#if defined(__aarch64__)
# define _MKNOD_VER 0
#elif defined(__x86_64__)
# define _MKNOD_VER 0
#else
# define _MKNOD_VER 1
#endif
#endif
static inline int true_stat(const char *pathname,struct stat *info) {
return true_xstat(_STAT_VER,pathname,info);
}
static inline int true_mknod(const char *pathname,mode_t mode,dev_t dev) {
return true_xmknod(_MKNOD_VER,pathname,mode,&dev);
}
static inline int true_lstat(const char *pathname,struct stat *info) {
return true_lxstat(_STAT_VER,pathname,info);
}
#if (GLIBC_MINOR >= 4)
static inline int true_fstatat(int dirfd, const char *pathname, struct stat *info, int flags) {
return true_fxstatat(_STAT_VER, dirfd, pathname, info, flags);
}
static inline int true_fstatat64(int dirfd, const char *pathname, struct stat64 *info, int flags) {
return true_fxstatat64(_STAT_VER, dirfd, pathname, info, flags);
}
static inline int true_mknodat(int dirfd, const char *pathname,mode_t mode,dev_t dev) {
return true_xmknodat(_MKNOD_VER, dirfd, pathname, mode, &dev);
}
#endif
/* A few defines to fix things a little */
#define INSTW_OK 0
/* If not set, no work with translation is allowed */
#define INSTW_INITIALIZED (1<<0)
/* If not set, a wrapped function only do its "real" job */
#define INSTW_OKWRAP (1<<1)
#define INSTW_OKBACKUP (1<<2)
#define INSTW_OKTRANSL (1<<3)
#define INSTW_TRANSLATED (1<<0)
/* Indicates that a translated file is identical to original */
#define INSTW_IDENTITY (1<<1)
/* The file currently exists in the root filesystem */
#define INSTW_ISINROOT (1<<6)
/* The file currently exists in the translated filesystem */
#define INSTW_ISINTRANSL (1<<7)
#define _BACKUP "/BACKUP"
#define _TRANSL "/TRANSL"
/* The root that contains all the needed metas-infos */
#define _META "/META"
/* We store under this subtree the translated status */
#define _MTRANSL _TRANSL
/* We construct under this subtree fake directory listings */
#define _MDIRLS "/DIRLS"
/* String cell used to chain excluded paths */
typedef struct string_t string_t;
struct string_t {
char *string;
string_t *next;
};
/* Used to keep all infos needed to cope with backup, translated fs... */
typedef struct instw_t {
/*
* global fields
*/
int gstatus;
int dbglvl;
pid_t pid;
char *root;
char *backup;
char *transl;
char *meta;
char *mtransl;
char *mdirls;
/* the list of all the paths excluded from translation */
string_t *exclude;
/*
* per instance fields
*/
int error;
int status;
/* the public path, hiding translation */
char path[PATH_MAX+1];
/* the public resolved path, hiding translation */
char reslvpath[PATH_MAX+1];
/* the real resolved path, exposing tranlsation */
char truepath[PATH_MAX+1];
/* the real translated path */
char translpath[PATH_MAX+1];
/* the list of all the equiv paths conducing to "reslvpath" */
string_t *equivpaths;
/* the real path used to flag translation status */
char mtranslpath[PATH_MAX+1];
/* the path given to a wrapped opendir */
char mdirlspath[PATH_MAX+1];
} instw_t;
static instw_t __instw;
static int canonicalize(const char *,char *);
static int reduce(char *);
static int make_path(const char *);
static int copy_path(const char *,const char *);
static inline int path_excluded(const char *);
static int unlink_recursive(const char *);
int expand_path(string_t **,const char *,const char *);
int parse_suffix(char *,char *,const char *);
/* a lazy way to avoid sizeof */
#define mallok(T,N) (T *)malloc((N)*sizeof(T))
/* single method used to minimize excessive returns */
#define finalize(code) {rcod=code;goto finalize;}
#if DEBUG
static int __instw_printdirent(struct dirent*);
#if(GLIBC_MINOR >= 1)
static int __instw_printdirent64(struct dirent64*);
#endif
#endif
#ifdef DEBUG
static int instw_print(instw_t *);
#endif
static int instw_init(void);
static int instw_fini(void);
static int instw_new(instw_t *);
static int instw_delete(instw_t *);
/* references a translated file in /mtransl */
static int instw_setmetatransl(instw_t *);
static int instw_setpath(instw_t *,const char *);
#if (GLIBC_MINOR >= 4)
static int instw_setpathrel(instw_t *, int, const char *);
#endif
static int instw_getstatus(instw_t *,int *);
static int instw_apply(instw_t *);
static int instw_filldirls(instw_t *);
static int instw_makedirls(instw_t *);
static int backup(const char *);
static int vlambda_log(const char *logname,const char *format,va_list ap);
/*
static int lambda_log(const char *logname,const char *format,...)
#ifdef __GNUC__
__attribute__((format(printf,2,3)))
#endif
;
*/
static inline int logg(const char *format,...)
#ifdef __GNUC__
/* Tell gcc that this function behaves like printf()
* for parameters 1 and 2 */
__attribute__((format(printf, 1, 2)))
#endif /* defined __GNUC__ */
;
static inline int debug(int dbglvl,const char *format,...)
#ifdef __GNUC__
__attribute__((format(printf, 2, 3)))
#endif
;
#define unset_okwrap() (__instw.gstatus &= ~INSTW_OKWRAP)
#define reset_okwrap() (__instw.gstatus |= INSTW_OKWRAP)
/*
* *****************************************************************************
*/
static void *libc_handle=NULL;
static char installwatch_initializing= 0;
static void initialize(void) {
if (libc_handle)
return;
if(installwatch_initializing){
fprintf(stderr,
"installwatch: "
"Something went very wrong. "
"installwatch accidentally caught a function call during initialize."
"This will not end well.");
return;
}
installwatch_initializing = 1;
void* handle = NULL;
#ifdef BROKEN_RTLD_NEXT
// printf ("RTLD_LAZY");
handle = dlopen(LIBC_FILE, RTLD_LAZY);
#else
// printf ("RTLD_NEXT");
handle = RTLD_NEXT;
#endif
true_time = dlsym(handle, "time");
true_chdir = dlsym(handle, "chdir");
true_chmod = dlsym(handle, "chmod");
true_chown = dlsym(handle, "chown");
true_chroot = dlsym(handle, "chroot");
true_creat = dlsym(handle, "creat");
true_fchmod = dlsym(handle, "fchmod");
true_fchown = dlsym(handle, "fchown");
true_fopen = dlsym(handle, "fopen");
true_ftruncate = dlsym(handle, "ftruncate");
true_getcwd = dlsym(handle, "getcwd");
true_lchown = dlsym(handle, "lchown");
true_link = dlsym(handle, "link");
true_mkdir = dlsym(handle, "mkdir");
true_xmknod = dlsym(handle, "__xmknod");
true_open = dlsym(handle, "open");
true_opendir = dlsym(handle, "opendir");
true_readdir = dlsym(handle, "readdir");
true_readlink = dlsym(handle, "readlink");
true_realpath = dlvsym(handle, "realpath", "GLIBC_2.3");
if (true_realpath == NULL)
dlsym(handle, "realpath");
true_rename = dlsym(handle, "rename");
true_rmdir = dlsym(handle, "rmdir");
true_scandir = dlsym(handle, "scandir");
true_xstat = dlsym(handle, "__xstat");
true_lxstat = dlsym(handle, "__lxstat");
true_symlink = dlsym(handle, "symlink");
true_truncate = dlsym(handle, "truncate");
true_unlink = dlsym(handle, "unlink");
true_utime = dlsym(handle, "utime");
#ifdef HAVE_UTIMENSAT
true_utimensat = dlsym(handle, "utimensat");
#endif
#ifdef ENABLE_ACL_SYSCALL
true_setxattr = dlsym(handle, "setxattr");
#endif
true_utimes = dlsym(handle, "utimes");
#ifdef ENABLE_ACCESS_SYSCALL
true_access = dlsym(handle, "access");
#endif
#ifdef HAVE_RENAMEAT2
true_renameat2 = dlsym(handle, "renameat2");
#endif
#if(GLIBC_MINOR >= 1)
true_creat64 = dlsym(handle, "creat64");
true_fopen64 = dlsym(handle, "fopen64");
true_ftruncate64 = dlsym(handle, "ftruncate64");
true_open64 = dlsym(handle, "open64");
true_readdir64 = dlsym(handle, "readdir64");
true_scandir64 = dlsym(handle, "scandir64");
true_xstat64 = dlsym(handle, "__xstat64");
true_lxstat64 = dlsym(handle, "__lxstat64");
true_truncate64 = dlsym(handle, "truncate64");
#ifdef ENABLE_ACL_SYSCALL
true_removexattr = dlsym(handle, "removexattr");
#endif
#endif
#if (GLIBC_MINOR >= 4)
true_openat = dlsym(handle, "openat");
true_fchmodat = dlsym(handle, "fchmodat");
true_fchownat = dlsym(handle, "fchownat");
true_fxstatat = dlsym(handle, "__fxstatat");
true_fxstatat64 = dlsym(handle, "__fxstatat64");
true_linkat = dlsym(handle, "linkat");
true_mkdirat = dlsym(handle, "mkdirat");
true_readlinkat = dlsym(handle, "readlinkat");
true_xmknodat = dlsym(handle, "__xmknodat");
true_renameat = dlsym(handle, "renameat");
true_symlinkat = dlsym(handle, "symlinkat");
true_unlinkat = dlsym(handle, "unlinkat");
#endif
libc_handle = handle;
installwatch_initializing= 0;
if(instw_init()) exit(-1);
}
int main(){}
void __attribute__ ((constructor)) my_init(void) {
initialize();
}
void __attribute__ ((destructor)) my_fini(void) {
instw_fini();
}
/*
* *****************************************************************************
*/
/*
* procedure = / rc:=vlambda_log(logname,format,ap) /
*
* task = / the va_list version of the lambda_log() procedure. /
*/
static int vlambda_log(const char *logname,const char *format,va_list ap) {
char buffer[BUFSIZE];
int count;
int logfd;
int rcod=0;
int s_errno;
/* save errno */
s_errno = errno;
buffer[BUFSIZE-2] = '\n';
buffer[BUFSIZE-1] = '\0';
count=vsnprintf(buffer,BUFSIZE,format,ap);
if(count == -1) {
/* The buffer was not big enough */
strcpy(&(buffer[BUFSIZE - 5]), "...\n");
count=BUFSIZE-1;
}
else
{
count = strlen(buffer);
}
if(logname!=NULL) {
logfd=true_open(logname,O_WRONLY|O_CREAT|O_APPEND,0666);
if(logfd>=0) {
if(write(logfd,buffer,count)!=count)
syslog( LOGLEVEL,
"Count not write `%s' in `%s': %s\n",
buffer,logname,strerror(errno));
if(close(logfd) < 0)
syslog( LOGLEVEL,
"Could not close `%s': %s\n",
logname,strerror(errno));
} else {
syslog( LOGLEVEL,
"Could not open `%s' to write `%s': %s\n",
logname,buffer,strerror(errno));
}
} else {
syslog(LOGLEVEL, "%s", buffer);
}
/* restore errno */
errno = s_errno;
return rcod;
}
/*
* procedure = / rc:=lambda_log(logname,format,...) /
*
* task = / logs a message to the specified file, or via syslog
* if no file is specified. /
*
* returns = / 0 ok. message logged /
*
* note = /
* --This *general* log procedure was justified by the debug mode
* which used either stdout or stderr, thus interfering with the
* observed process.
* --From now, we output nothing to stdout/stderr.
* /
*
*/
/*
static int lambda_log(const char *logname,const char *format, ...) {
va_list ap;
int rcod=0;;
va_start(ap,format);
rcod=vlambda_log(logname,format,ap);
va_end(ap);
return rcod;
}
*/
static inline int logg(const char *format,...) {
char *logname;
va_list ap;
int rcod;
logname=getenv("INSTALLWATCHFILE");
va_start(ap,format);
rcod=vlambda_log(logname,format,ap);
va_end(ap);
return rcod;
}
static inline int debug(int dbglvl,const char *format,...) {
int rcod=0;
#ifdef DEBUG
char *logname;
va_list ap;
if( __instw.dbglvl==0 ||
dbglvl>__instw.dbglvl ||
dbglvl<0 ) return rcod;
logname=getenv("INSTW_DBGFILE");
va_start(ap,format);
rcod=vlambda_log(logname,format,ap);
va_end(ap);
#endif
return rcod;
}
/*
* procedure = / rc:=canonicalize(path,resolved_path) /
*
* note = /
* --We use realpath here, but this function calls __lxstat().
* We want to only use "real" calls in wrapping code, hence the
* barrier established by unset_okwrap()/reset_okwrap().
* --We try to canonicalize as much as possible, considering that
* /
*/
static int canonicalize(const char *path, char *resolved_path) {
int s_errno;
/* save errno */
s_errno = errno;
unset_okwrap();
if(!realpath(path,resolved_path)) {
if((path[0] != '/')) {
/* The path could not be canonicalized, append it
* to the current working directory if it was not
* an absolute path */
true_getcwd(resolved_path, PATH_MAX-2);
resolved_path[MAXPATHLEN-2] = '\0';
strcat(resolved_path, "/");
strncat(resolved_path, path, MAXPATHLEN - 1 - strlen(resolved_path));
} else {
strcpy(resolved_path,path);
}
}
reset_okwrap();
#if DEBUG
debug(4,"canonicalize(%s,%s)\n",path,resolved_path);
#endif
/* restore errno */
errno = s_errno;
return 0;
}
/*
* procedure = / rc:=reduce(path) /
*
* task = / reduces all occurences of "..", ".", and extra "/" in path.
*
* inputs = / path The modifiable string containing the path
* outputs = / path The reduced path.
*
* returns = / 0 ok. path reduced
* -1 failed. cf errno /
* note = /
* --Very similar to canonicalize()/realpath() except we don’t do link-
* expansion
* --This is purely a string manipulation function (i.e., no verification
* of a path’s validity occurs).
* --Additionally, we try to do reduction “in-place” since the ending
* path is shorter than the beginning path.
* --Also, we want only absolute paths (other paths will throw an error)
* /
*/
static int reduce(char *path) {
int len;
char *off;
if(path == NULL || *path != '/') {
errno = EINVAL;
return -1;
}
len = strlen(path);
/* First, get rid of double / */
if((off = strstr(path, "//"))) {
memmove(off, off+1, len - (off-path));
return reduce(path);
}
/* Then, worry about /./ */
if((off = strstr(path, "/./"))) {
memmove(off, off+2, len - 1 - (off-path));
return reduce(path);
}
/* Finally, do /../ */
if((off = strstr(path, "/../"))) {
char *off2 = off;
if(off2++ != path)
while((--off2)[-1] != '/');
memmove(off2, off+4, len - 3 - (off-path));
return reduce(path);
}
/* Beautify ending */
switch(path[len - 1]) {
case '.':
switch(path[len - 2]) {
default:
return 0;
case '.':
if(len != 3) {
off = path+len-3;
if(*off-- != '/')
return 0;
while(*--off != '/');
off[1] = 0;
return reduce(path);
}
case '/': ;
}
case '/':
if(len != 1) {
path[len-1] = 0;
return reduce(path);
}
default:
return 0;
}
}
static int make_path (const char *path) {
char checkdir[BUFSIZ];
struct stat inode;
int s_errno;
int i = 0;
/* save errno */
s_errno = errno;
#if DEBUG
debug(2,"===== make_path: %s\n", path);
#endif
while ( path[i] != '\0' ) {
checkdir[i] = path[i];
if (checkdir[i] == '/') { /* Each time a '/' is found, check if the */
checkdir[i+1] = '\0'; /* path exists. If it doesn't, we create it. */
if (true_stat (checkdir, &inode) < 0)
true_mkdir (checkdir, S_IRWXU);
}
i++;
}
/* restore errno */
errno = s_errno;
return 0;
}
/*
* procedure = / rc:=copy_path(truepath,translroot) /
*
* task = / do an exact translation of 'truepath' under 'translroot',
* the directory path to the new objet is not created /
*
* returns = / 0 ok. translation done
* -1 failed. cf errno /
*
* note = /
* --we suppose that 'translroot' has no trailing '/'
* --no overwrite is done is the target object already exists
* --the copy method depends on the source object type.
* --we don't fail if the source object doesn't exist.
* --we don't create the directory path because that would lead in the
* the translation case not to reference the newly created directories
* /
*/
static int copy_path(const char *truepath,const char *translroot) {
int rcod;
char buffer[BUFSIZ];
int bytes;
char translpath[PATH_MAX+1];
struct stat trueinfo;
struct stat translinfo;
int truefd;
int translfd;
struct utimbuf timbuf;
size_t truesz;
char linkpath[PATH_MAX+1];
ssize_t linksz;
#if DEBUG
debug(2,"copy_path(%s,%s)\n",truepath,translroot);
#endif
rcod=true_lstat(truepath,&trueinfo);
if(rcod<0 && errno!=ENOENT) return -1;
if(!rcod) {
if((truesz=strlen(truepath)+strlen(translpath))>PATH_MAX) {
errno=ENAMETOOLONG;
return -1;
}
strncpy(translpath,translroot,PATH_MAX);
strncat(translpath,truepath,PATH_MAX-truesz);
if(!true_lstat(translpath,&translinfo)) return 0;
/* symbolic links */
if(S_ISLNK(trueinfo.st_mode)) {
if((linksz=true_readlink(truepath,linkpath,PATH_MAX))<0) return -1;
linkpath[linksz]='\0';
if(true_symlink(linkpath,translpath)!=0) return -1;
}
/* regular file */
if(S_ISREG(trueinfo.st_mode)) {
if((truefd=true_open(truepath,O_RDONLY))<0) return -1;
if((translfd=true_open( translpath,
O_WRONLY|O_CREAT|O_TRUNC,
trueinfo.st_mode))<0 ) {
close(truefd);
return -1;
}
while((bytes=read(truefd,buffer,BUFSIZ))>0)
write(translfd,buffer,bytes);
close(truefd);
close(translfd);
}
/* directory */
if(S_ISDIR(trueinfo.st_mode)) {
if(true_mkdir(translpath,trueinfo.st_mode)) return -1;
}
/* block special file */
if(S_ISBLK(trueinfo.st_mode)) {
if(true_mknod( translpath,trueinfo.st_mode|S_IFBLK,
trueinfo.st_rdev )) return -1;
}
/* character special file */
if(S_ISCHR(trueinfo.st_mode)) {
if(true_mknod( translpath,trueinfo.st_mode|S_IFCHR,
trueinfo.st_rdev )) return -1;
}
/* fifo special file */
if(S_ISFIFO(trueinfo.st_mode)) {
if(true_mknod(translpath,trueinfo.st_mode|S_IFIFO,0))
return -1;
}
timbuf.actime=trueinfo.st_atime;
timbuf.modtime=trueinfo.st_mtime;
true_utime(translpath,&timbuf);
if(!S_ISLNK(trueinfo.st_mode)) {
true_chown(translpath,trueinfo.st_uid,trueinfo.st_gid);
true_chmod(translpath,trueinfo.st_mode);
}
}
return 0;
}
/*
* procedure = / rc:=path_excluded(truepath) /
*
* task = / indicates if the given path is or is hosted under any
* of the exclusion list members. /
*
* returns = / 0 is not a member
* 1 is a member /
*
* note = / __instw.exclude must be initialized /
*
*/
static inline int path_excluded(const char *truepath) {
string_t *pnext;
int result;
result=0;
pnext=__instw.exclude;
while(pnext!=NULL) {
if(strstr(truepath,pnext->string)==truepath) {
result=1;
break;
}
pnext=pnext->next;
}
return result;
}
/*
* procedure = / rc:=unlink_recursive(truepath) /
*
* task = / dangerous function that unlink either a file or
* an entire subtree. /
*
* returns = / 0 ok. recursive unlink done
* -1 failed. cf errno /
*
* note = /
* --this procedure was needed by instw_makedirls(), in order to
* erase a previously created temporary subtree.
* --it must be called with an absolute path, and only to delete
* well identified trees.
* --i think it is a very weak implementation, so avoid using it
* to unlink too deep trees, or rewrite it to avoid recursivity.
* /
*
*/
static int unlink_recursive(const char *truepath) {
int rcod;
struct stat trueinfo;
DIR *wdir;
struct dirent *went;
char wpath[PATH_MAX+1];
struct stat winfo;
#if DEBUG
debug(2,"unlink_recursive(%s)\n",truepath);
#endif
rcod=true_lstat(truepath,&trueinfo);
if(rcod<0 && errno!=ENOENT) return -1;
if(rcod!=0) return 0;
if(S_ISDIR(trueinfo.st_mode)) {
wdir=true_opendir(truepath);
if(wdir==NULL) return -1;
while((went=true_readdir(wdir))!=NULL) {
/* we avoid big inifinite recursion troubles */
if( went->d_name[0]=='.' &&
( (went->d_name[1]=='\0') ||
( went->d_name[1]=='.' &&
went->d_name[2]=='\0') ) )
{ continue; }
/* let's get the absolute path to this entry */
strcpy(wpath,truepath);
strcat(wpath,"/");
strcat(wpath,went->d_name);
rcod=true_lstat(wpath,&winfo);
if(rcod!=0) {
closedir(wdir);
return -1;
}
if(S_ISDIR(winfo.st_mode)) {
unlink_recursive(wpath);
true_rmdir(wpath);
} else {
true_unlink(wpath);
}
}
closedir(wdir);
true_rmdir(truepath);
} else {
true_unlink(truepath);
}
return rcod;
}
/*
* procedure = / rc:=expand_path(&list,prefix,suffix) /
*
* task = / from a given path, generates all the paths that could
* be derived from it, through symlinks expansion. /
*
* note = /
* --this procedure has been implemented to enhance the method used
* to reference files that have been translated.
* --briefly, it is necessary to reference all the paths that could