-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpseudo_client.c
2754 lines (2553 loc) · 74.9 KB
/
pseudo_client.c
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
/*
* pseudo_client.c, pseudo client library code
*
* Copyright (c) 2008-2013 Wind River Systems, Inc.
*
* SPDX-License-Identifier: LGPL-2.1-only
*
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <signal.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pwd.h>
#include <grp.h>
#include <fnmatch.h>
#ifdef PSEUDO_XATTRDB
#include <sys/xattr.h>
#endif
#include "pseudo.h"
#include "pseudo_ipc.h"
#include "pseudo_client.h"
/* GNU extension */
#if PSEUDO_PORT_LINUX
extern char *program_invocation_name;
#else
static char *program_invocation_name = "unknown";
#endif
static char *base_path(int dirfd, const char *path, int leave_last);
static int connect_fd = -1;
static int server_pid = 0;
int pseudo_prefix_dir_fd = -1;
int pseudo_localstate_dir_fd = -1;
int pseudo_pwd_fd = -1;
int pseudo_pwd_lck_fd = -1;
char *pseudo_pwd_lck_name = NULL;
FILE *pseudo_pwd = NULL;
int pseudo_grp_fd = -1;
FILE *pseudo_grp = NULL;
char *pseudo_cwd = NULL;
size_t pseudo_cwd_len;
char *pseudo_chroot = NULL;
char *pseudo_passwd = NULL;
size_t pseudo_chroot_len = 0;
char *pseudo_cwd_rel = NULL;
/* used for PSEUDO_DISABLED */
int pseudo_disabled = 0;
int pseudo_allow_fsync = 0;
static int pseudo_local_only = 0;
static int pseudo_client_logging = 1;
int pseudo_umask = 022;
static char **fd_paths = NULL;
static int nfds = 0;
static char **linked_fd_paths = NULL;
static int linked_nfds = 0;
static const char **passwd_paths = NULL;
static int npasswd_paths = 0;
#ifdef PSEUDO_PROFILING
int pseudo_profile_fd = -1;
static int profile_interval = 1;
static pseudo_profile_t profile_data;
struct timeval *pseudo_wrapper_time = &profile_data.wrapper_time;
#endif
static int pseudo_inited = 0;
static int sent_messages = 0;
int pseudo_nosymlinkexp = 0;
/* note: these are int, not uid_t/gid_t, so I can use 'em with scanf */
uid_t pseudo_ruid;
uid_t pseudo_euid;
uid_t pseudo_suid;
uid_t pseudo_fuid;
gid_t pseudo_rgid;
gid_t pseudo_egid;
gid_t pseudo_sgid;
gid_t pseudo_fgid;
int (*pseudo_real_fork)(void) = fork;
int (*pseudo_real_execv)(const char *, char * const *) = execv;
#define PSEUDO_ETC_FILE(filename, realname, flags) pseudo_etc_file(filename, realname, flags, passwd_paths, npasswd_paths)
/* helper function to make a directory, just like mkdir -p.
* Can't use system() because the child shell would end up trying
* to do the same thing...
*/
static void
mkdir_p(char *path) {
size_t len = strlen(path);
size_t i;
for (i = 1; i < len; ++i) {
/* try to create all the directories in path, ignoring
* failures
*/
if (path[i] == '/') {
path[i] = '\0';
(void) mkdir(path, 0755);
path[i] = '/';
}
}
(void) mkdir(path, 0755);
}
/* Populating an array of unknown size is one of my least favorite
* things. The idea here is to ensure that the logic flow is the same
* both when counting expected items, and when populating them.
*/
static void
build_passwd_paths(void)
{
int np = 0;
int pass = 0;
/* should never happen... */
if (passwd_paths) {
free(passwd_paths);
passwd_paths = 0;
npasswd_paths = 0;
}
#define SHOW_PATH pseudo_debug(PDBGF_CHROOT | PDBGF_VERBOSE, "passwd_paths[%d]: '%s'\n", np, (passwd_paths[np]))
#define ADD_PATH(p) do { if (passwd_paths) { passwd_paths[np] = (p); SHOW_PATH; } ++np; } while(0)
#define NUL_BYTE(p) do { if (passwd_paths) { *(p)++ = '\0'; } else { ++(p); } } while(0)
do {
if (pseudo_chroot) {
ADD_PATH(pseudo_chroot);
}
if (pseudo_passwd) {
char *s = pseudo_passwd;
while (s) {
char *t = strchr(s, ':');
if (t) {
NUL_BYTE(t);
}
ADD_PATH(s);
s = t;
}
}
if (PSEUDO_PASSWD_FALLBACK) {
ADD_PATH(PSEUDO_PASSWD_FALLBACK);
}
/* allocation and/or return */
if (passwd_paths) {
if (np != npasswd_paths) {
pseudo_diag("internal error: path allocation was inconsistent.\n");
} else {
/* yes, we allocated one extra for a trailing
* null pointer.
*/
passwd_paths[np] = NULL;
}
return;
} else {
passwd_paths = malloc((np + 1) * sizeof(*passwd_paths));
npasswd_paths = np;
if (!passwd_paths) {
pseudo_diag("couldn't allocate storage for password paths.\n");
exit(1);
}
np = 0;
}
} while (++pass < 2);
/* in theory the second pass already returned, but. */
pseudo_diag("should totally not have gotten here.\n");
return;
}
#ifdef PSEUDO_XATTRDB
/* We really want to avoid calling the wrappers for these inside the
* implementation. pseudo_wrappers will reinitialize these after it's
* gotten the real_* found.
*/
ssize_t (*pseudo_real_lgetxattr)(const char *, const char *, void *, size_t) = lgetxattr;
ssize_t (*pseudo_real_fgetxattr)(int, const char *, void *, size_t) = fgetxattr;
int (*pseudo_real_lsetxattr)(const char *, const char *, const void *, size_t, int) = lsetxattr;
int (*pseudo_real_fsetxattr)(int, const char *, const void *, size_t, int) = fsetxattr;
/* Executive summary: We use an extended attribute,
* user.pseudo_data, to store exactly the data we would otherwise
* have stored in the database. Which is to say, uid, gid, mode, rdev.
*
* If we don't find a value, save an empty one with a lower version
* number to indicate that we don't have data to reduce round trips.
*/
typedef struct {
int version;
uid_t uid;
gid_t gid;
mode_t mode;
dev_t rdev;
} pseudo_db_data_t;
static pseudo_msg_t xattrdb_data;
pseudo_msg_t *
pseudo_xattrdb_save(int fd, const char *path, const struct stat64 *buf) {
int rc = -1;
if (!path && fd < 0)
return NULL;
if (!buf)
return NULL;
pseudo_db_data_t pseudo_db_data = {
.version = 1,
.uid = buf->st_uid,
.gid = buf->st_gid,
.mode = buf->st_mode,
.rdev = buf->st_rdev
};
if (path) {
rc = pseudo_real_lsetxattr(path, "user.pseudo_data", &pseudo_db_data, sizeof(pseudo_db_data), 0);
} else if (fd >= 0) {
rc = pseudo_real_fsetxattr(fd, "user.pseudo_data", &pseudo_db_data, sizeof(pseudo_db_data), 0);
}
pseudo_debug(PDBGF_XATTRDB, "tried to save data for %s/%d: uid %d, mode %o, rc %d.\n",
path ? path : "<nil>", fd, (int) pseudo_db_data.uid, (int) pseudo_db_data.mode, rc);
/* none of the other fields are checked on save, and the value
* is currently only really used by mknod.
*/
if (rc == 0) {
xattrdb_data.result = RESULT_SUCCEED;
return &xattrdb_data;
}
return NULL;
}
pseudo_msg_t *
pseudo_xattrdb_load(int fd, const char *path, const struct stat64 *buf) {
int rc = -1, retryrc = -1;
if (!path && fd < 0)
return NULL;
/* don't try to getxattr on a thing unless we think it is
* likely to work.
*/
if (buf) {
if (!S_ISDIR(buf->st_mode) && !S_ISREG(buf->st_mode)) {
return NULL;
}
}
pseudo_db_data_t pseudo_db_data;
if (path) {
rc = pseudo_real_lgetxattr(path, "user.pseudo_data", &pseudo_db_data, sizeof(pseudo_db_data));
if (rc == -1) {
pseudo_db_data = (pseudo_db_data_t) { .version = 0 };
retryrc = pseudo_real_lsetxattr(path, "user.pseudo_data", &pseudo_db_data, sizeof(pseudo_db_data), 0);
}
} else if (fd >= 0) {
rc = pseudo_real_fgetxattr(fd, "user.pseudo_data", &pseudo_db_data, sizeof(pseudo_db_data));
if (rc == -1) {
pseudo_db_data = (pseudo_db_data_t) { .version = 0 };
retryrc = pseudo_real_fsetxattr(fd, "user.pseudo_data", &pseudo_db_data, sizeof(pseudo_db_data), 0);
}
}
pseudo_debug(PDBGF_XATTRDB, "tried to load data for %s[%d]: rc %d, version %d.\n",
path ? path : "<nil>", fd, rc, pseudo_db_data.version);
if (rc == -1 && retryrc == 0) {
/* there's no data, but there could have been; treat
* this as an empty database result.
*/
pseudo_debug(PDBGF_XATTRDB, "wrote version 0 for %s[%d]\n",
path ? path : "<nil>", fd);
xattrdb_data.result = RESULT_FAIL;
return &xattrdb_data;
} else if (rc == -1) {
/* we can't create an extended attribute, so we may have
* used the database.
*/
return NULL;
}
/* Version 0 = just recording that we looked and found
* nothing.
* Version 1 = actually implemented.
*/
switch (pseudo_db_data.version) {
case 0:
default:
xattrdb_data.result = RESULT_FAIL;
break;
case 1:
xattrdb_data.uid = pseudo_db_data.uid;
xattrdb_data.gid = pseudo_db_data.gid;
xattrdb_data.mode = pseudo_db_data.mode;
xattrdb_data.rdev = pseudo_db_data.rdev;
xattrdb_data.result = RESULT_SUCCEED;
break;
}
return &xattrdb_data;
}
#endif
#ifdef PSEUDO_PROFILING
static int pseudo_profile_pid = -2;
static void
pseudo_profile_start(void) {
/* We use -1 as a starting value, and -2 as a value
* indicating not to try to open it.
*/
int existing_data = 0;
int pid = getpid();
if (pseudo_profile_pid > 0 && pseudo_profile_pid != pid) {
/* looks like there's been a fork. We intentionally
* abandon any existing stats, since the parent might
* want to write them, and we want to combine with
* any previous stats for this pid.
*/
close(pseudo_profile_fd);
pseudo_profile_fd = -1;
}
if (pseudo_profile_fd == -1) {
int fd = -2;
char *profile_path = pseudo_get_value("PSEUDO_PROFILE_PATH");
pseudo_profile_pid = pid;
if (profile_path) {
fd = open(profile_path, O_RDWR | O_CREAT, 0600);
if (fd >= 0) {
fd = pseudo_fd(fd, MOVE_FD);
}
if (fd < 0) {
fd = -2;
} else {
if (pid > 0) {
pseudo_profile_t data;
off_t rc;
rc = lseek(fd, pid * sizeof(data), SEEK_SET);
if (rc == (off_t) (pid * sizeof(data))) {
rc = read(fd, &profile_data, sizeof(profile_data));
/* cumulative with other values in same file */
if (rc == sizeof(profile_data)) {
pseudo_debug(PDBGF_PROFILE, "pid %d found existing profiling data.\n", pid);
existing_data = 1;
++profile_data.processes;
} else {
pseudo_debug(PDBGF_PROFILE, "read failed for pid %d: %d, %d\n", pid, (int) rc, errno);
}
profile_interval = 1;
}
}
}
}
pseudo_profile_fd = fd;
} else {
pseudo_debug(PDBGF_PROFILE, "_start called with existing fd? (pid %d)", (int) pseudo_profile_pid);
existing_data = 1;
++profile_data.processes;
profile_data.total_ops = 0;
profile_data.messages = 0;
profile_data.wrapper_time = (struct timeval) { .tv_sec = 0 };
profile_data.op_time = (struct timeval) { .tv_sec = 0 };
profile_data.ipc_time = (struct timeval) { .tv_sec = 0 };
}
if (!existing_data) {
pseudo_debug(PDBGF_PROFILE, "pid %d found no existing profiling data.\n", pid);
profile_data = (pseudo_profile_t) {
.processes = 1,
.total_ops = 0,
.messages = 0,
.wrapper_time = (struct timeval) { .tv_sec = 0 },
.op_time = (struct timeval) { .tv_sec = 0 },
.ipc_time = (struct timeval) { .tv_sec = 0 },
};
}
}
static inline void
fix_tv(struct timeval *tv) {
if (tv->tv_usec > 1000000) {
tv->tv_sec += tv->tv_usec / 1000000;
tv->tv_usec %= 1000000;
} else if (tv->tv_usec < 0) {
/* C99 and later guarantee truncate-towards-zero.
* We want -1 through -1000000 usec to produce
* -1 seconds, etcetera. Note that sec is
* negative, so yes, we want to add to tv_sec and
* subtract from tv_usec.
*/
int sec = (tv->tv_usec - 999999) / 1000000;
tv->tv_sec += sec;
tv->tv_usec -= 1000000 * sec;
}
}
static int profile_reported = 0;
static void
pseudo_profile_report(void) {
if (pseudo_profile_fd < 0) {
return;
}
fix_tv(&profile_data.wrapper_time);
fix_tv(&profile_data.op_time);
fix_tv(&profile_data.ipc_time);
if (pseudo_profile_pid >= 0) {
int rc1, rc2;
rc1 = lseek(pseudo_profile_fd, pseudo_profile_pid * sizeof(profile_data), SEEK_SET);
rc2 = write(pseudo_profile_fd, &profile_data, sizeof(profile_data));
if (!profile_reported) {
pseudo_debug(PDBGF_PROFILE, "pid %d (%s) saving profiling info: %d, %d.\n",
pseudo_profile_pid,
program_invocation_name ? program_invocation_name : "unknown",
rc1, rc2);
profile_reported = 1;
}
}
}
#endif
void
pseudo_init_client(void) {
char *env;
int need_free = 0;
pseudo_antimagic();
pseudo_new_pid();
if (connect_fd != -1) {
close(connect_fd);
connect_fd = -1;
}
#ifdef PSEUDO_PROFILING
if (pseudo_profile_fd > -1) {
close(pseudo_profile_fd);
}
pseudo_profile_fd = -1;
#endif
/* in child processes, PSEUDO_DISABLED may have become set to
* some truthy value, in which case we'd disable pseudo,
* or it may have gone away, in which case we'd enable
* pseudo (and cause it to reinit the defaults).
*/
need_free = 0;
env = getenv("PSEUDO_DISABLED");
if (!env) {
env = pseudo_get_value("PSEUDO_DISABLED");
need_free = 1;
}
if (env) {
int actually_disabled = 1;
switch (*env) {
case '0':
case 'f':
case 'F':
case 'n':
case 'N':
actually_disabled = 0;
break;
case 's':
case 'S':
actually_disabled = 0;
pseudo_local_only = 1;
break;
}
if (actually_disabled) {
if (!pseudo_disabled) {
pseudo_antimagic();
pseudo_disabled = 1;
}
pseudo_set_value("PSEUDO_DISABLED", "1");
} else {
if (pseudo_disabled) {
pseudo_magic();
pseudo_disabled = 0;
pseudo_inited = 0; /* Re-read the initial values! */
}
pseudo_set_value("PSEUDO_DISABLED", "0");
}
} else {
pseudo_set_value("PSEUDO_DISABLED", "0");
}
if (need_free)
free(env);
/* ALLOW_FSYNC is here because some crazy hosts will otherwise
* report incorrect values for st_size/st_blocks. I can sort of
* understand st_blocks, but bogus values for st_size? Not cool,
* dudes, not cool.
*/
need_free = 0;
env = getenv("PSEUDO_ALLOW_FSYNC");
if (!env) {
env = pseudo_get_value("PSEUDO_ALLOW_FSYNC");
need_free = 1;
} else {
pseudo_set_value("PSEUDO_ALLOW_FSYNC", env);
}
if (env) {
pseudo_allow_fsync = 1;
} else {
pseudo_allow_fsync = 0;
}
if (need_free)
free(env);
/* in child processes, PSEUDO_UNLOAD may become set to
* some truthy value, in which case we're being asked to
* remove pseudo from the LD_PRELOAD. We need to make sure
* this value gets loaded into the internal variables.
*
* If we've been told to unload, but are still available
* we need to act as if unconditionally disabled.
*/
env = getenv("PSEUDO_UNLOAD");
if (env) {
pseudo_set_value("PSEUDO_UNLOAD", env);
pseudo_disabled = 1;
}
/* Setup global items needed for pseudo to function... */
if (!pseudo_inited) {
/* Ensure that all of the values are reset */
server_pid = 0;
pseudo_prefix_dir_fd = -1;
pseudo_localstate_dir_fd = -1;
pseudo_pwd_fd = -1;
pseudo_pwd_lck_fd = -1;
pseudo_pwd_lck_name = NULL;
pseudo_pwd = NULL;
pseudo_grp_fd = -1;
pseudo_grp = NULL;
pseudo_cwd = NULL;
pseudo_cwd_len = 0;
pseudo_chroot = NULL;
pseudo_passwd = NULL;
pseudo_chroot_len = 0;
pseudo_cwd_rel = NULL;
pseudo_nosymlinkexp = 0;
}
if (!pseudo_disabled && !pseudo_inited) {
char *pseudo_path = 0;
pseudo_umask = umask(022);
umask(pseudo_umask);
pseudo_path = pseudo_prefix_path(NULL);
if (pseudo_prefix_dir_fd == -1) {
if (pseudo_path) {
pseudo_prefix_dir_fd = open(pseudo_path, O_RDONLY);
/* directory is missing? */
if (pseudo_prefix_dir_fd == -1 && errno == ENOENT) {
pseudo_debug(PDBGF_CLIENT, "prefix directory '%s' doesn't exist, trying to create\n", pseudo_path);
mkdir_p(pseudo_path);
pseudo_prefix_dir_fd = open(pseudo_path, O_RDONLY);
}
pseudo_prefix_dir_fd = pseudo_fd(pseudo_prefix_dir_fd, MOVE_FD);
} else {
pseudo_diag("No prefix available to to find server.\n");
exit(1);
}
if (pseudo_prefix_dir_fd == -1) {
pseudo_diag("Can't open prefix path '%s' for server: %s\n",
pseudo_path,
strerror(errno));
exit(1);
}
}
free(pseudo_path);
pseudo_path = pseudo_localstatedir_path(NULL);
if (pseudo_localstate_dir_fd == -1) {
if (pseudo_path) {
pseudo_localstate_dir_fd = open(pseudo_path, O_RDONLY);
/* directory is missing? */
if (pseudo_localstate_dir_fd == -1 && errno == ENOENT) {
pseudo_debug(PDBGF_CLIENT, "local state directory '%s' doesn't exist, trying to create\n", pseudo_path);
mkdir_p(pseudo_path);
pseudo_localstate_dir_fd = open(pseudo_path, O_RDONLY);
}
pseudo_localstate_dir_fd = pseudo_fd(pseudo_localstate_dir_fd, MOVE_FD);
} else {
pseudo_diag("No local state directory available for server/file interactions.\n");
exit(1);
}
if (pseudo_localstate_dir_fd == -1) {
pseudo_diag("Can't open local state path '%s': %s\n",
pseudo_path,
strerror(errno));
exit(1);
}
}
free(pseudo_path);
env = pseudo_get_value("PSEUDO_NOSYMLINKEXP");
if (env) {
char *endptr;
/* if the environment variable is not an empty string,
* parse it; "0" means turn NOSYMLINKEXP off, "1" means
* turn it on (disabling the feature). An empty string
* or something we can't parse means to set the flag; this
* is a safe default because if you didn't want the flag
* set, you normally wouldn't set the environment variable
* at all.
*/
if (*env) {
pseudo_nosymlinkexp = strtol(env, &endptr, 10);
if (*endptr)
pseudo_nosymlinkexp = 1;
} else {
pseudo_nosymlinkexp = 1;
}
} else {
pseudo_nosymlinkexp = 0;
}
free(env);
env = pseudo_get_value("PSEUDO_UIDS");
if (env)
sscanf(env, "%d,%d,%d,%d",
&pseudo_ruid, &pseudo_euid,
&pseudo_suid, &pseudo_fuid);
free(env);
env = pseudo_get_value("PSEUDO_GIDS");
if (env)
sscanf(env, "%d,%d,%d,%d",
&pseudo_rgid, &pseudo_egid,
&pseudo_sgid, &pseudo_fuid);
free(env);
env = pseudo_get_value("PSEUDO_CHROOT");
if (env) {
pseudo_chroot = strdup(env);
if (pseudo_chroot) {
pseudo_chroot_len = strlen(pseudo_chroot);
} else {
pseudo_diag("Can't store chroot path '%s'\n", env);
}
}
free(env);
env = pseudo_get_value("PSEUDO_PASSWD");
if (env) {
/* note: this means that pseudo_passwd is a
* string we're allowed to modify... */
pseudo_passwd = strdup(env);
}
free(env);
build_passwd_paths();
pseudo_inited = 1;
}
if (!pseudo_disabled) {
pseudo_client_getcwd();
#ifdef PSEUDO_PROFILING
pseudo_profile_start();
#endif
}
pseudo_magic();
}
static void
pseudo_file_close(int *fd, FILE **fp) {
if (!fp || !fd) {
pseudo_diag("pseudo_file_close: needs valid pointers.\n");
return;
}
pseudo_antimagic();
if (*fp) {
#if PSEUDO_PORT_DARWIN
if (*fp == pseudo_host_etc_passwd_file) {
endpwent();
} else if (*fp != pseudo_host_etc_group_file) {
endgrent();
} else {
fclose(*fp);
}
#else
fclose(*fp);
#endif
*fd = -1;
*fp = 0;
}
#if PSEUDO_PORT_DARWIN
if (*fd == pseudo_host_etc_passwd_fd ||
*fd == pseudo_host_etc_group_fd) {
*fd = -1;
}
#endif
/* this should be impossible */
if (*fd >= 0) {
close(*fd);
*fd = -1;
}
pseudo_magic();
}
static FILE *
pseudo_file_open(char *name, int *fd, FILE **fp) {
if (!fp || !fd || !name) {
pseudo_diag("pseudo_file_open: needs valid pointers.\n");
return NULL;
}
pseudo_file_close(fd, fp);
pseudo_antimagic();
*fd = PSEUDO_ETC_FILE(name, NULL, O_RDONLY);
#if PSEUDO_PORT_DARWIN
if (*fd == pseudo_host_etc_passwd_fd) {
*fp = pseudo_host_etc_passwd_file;
setpwent();
} else if (*fd == pseudo_host_etc_group_fd) {
*fp = pseudo_host_etc_group_file;
setgrent();
}
#endif
if (*fd >= 0) {
*fd = pseudo_fd(*fd, MOVE_FD);
*fp = fdopen(*fd, "r");
if (!*fp) {
close(*fd);
*fd = -1;
}
}
pseudo_magic();
return *fp;
}
/* there is no spec I know of requiring us to defend this fd
* against being closed by the user.
*/
int
pseudo_pwd_lck_open(void) {
pseudo_pwd_lck_close();
if (!pseudo_pwd_lck_name) {
pseudo_pwd_lck_name = malloc(pseudo_path_max());
if (!pseudo_pwd_lck_name) {
pseudo_diag("couldn't allocate space for passwd lockfile path.\n");
return -1;
}
}
pseudo_antimagic();
pseudo_pwd_lck_fd = PSEUDO_ETC_FILE(".pwd.lock",
pseudo_pwd_lck_name, O_RDWR | O_CREAT);
pseudo_magic();
return pseudo_pwd_lck_fd;
}
int
pseudo_pwd_lck_close(void) {
if (pseudo_pwd_lck_fd != -1) {
pseudo_antimagic();
close(pseudo_pwd_lck_fd);
if (pseudo_pwd_lck_name) {
unlink(pseudo_pwd_lck_name);
free(pseudo_pwd_lck_name);
pseudo_pwd_lck_name = 0;
}
pseudo_magic();
pseudo_pwd_lck_fd = -1;
return 0;
} else {
return -1;
}
}
FILE *
pseudo_pwd_open(void) {
return pseudo_file_open("passwd", &pseudo_pwd_fd, &pseudo_pwd);
}
void
pseudo_pwd_close(void) {
pseudo_file_close(&pseudo_pwd_fd, &pseudo_pwd);
}
FILE *
pseudo_grp_open(void) {
return pseudo_file_open("group", &pseudo_grp_fd, &pseudo_grp);
}
void
pseudo_grp_close(void) {
pseudo_file_close(&pseudo_grp_fd, &pseudo_grp);
}
void
pseudo_client_touchuid(void) {
static char uidbuf[256];
snprintf(uidbuf, 256, "%d,%d,%d,%d",
pseudo_ruid, pseudo_euid, pseudo_suid, pseudo_fuid);
pseudo_set_value("PSEUDO_UIDS", uidbuf);
}
void
pseudo_client_touchgid(void) {
static char gidbuf[256];
snprintf(gidbuf, 256, "%d,%d,%d,%d",
pseudo_rgid, pseudo_egid, pseudo_sgid, pseudo_fgid);
pseudo_set_value("PSEUDO_GIDS", gidbuf);
}
int
pseudo_client_chroot(const char *path) {
/* free old value */
free(pseudo_chroot);
pseudo_debug(PDBGF_CLIENT | PDBGF_CHROOT, "client chroot: %s\n", path);
if (!strcmp(path, "/")) {
pseudo_chroot_len = 0;
pseudo_chroot = 0;
pseudo_set_value("PSEUDO_CHROOT", NULL);
return 0;
}
/* allocate new value */
pseudo_chroot_len = strlen(path);
pseudo_chroot = malloc(pseudo_chroot_len + 1);
if (!pseudo_chroot) {
pseudo_diag("Couldn't allocate chroot directory buffer.\n");
pseudo_chroot_len = 0;
errno = ENOMEM;
return -1;
}
memcpy(pseudo_chroot, path, pseudo_chroot_len + 1);
pseudo_set_value("PSEUDO_CHROOT", pseudo_chroot);
/* Rebuild passwd paths since we've done a chroot */
build_passwd_paths();
return 0;
}
static char *
fd_path(int fd) {
if (fd >= 0 && fd < nfds) {
return fd_paths[fd];
}
if (fd == AT_FDCWD) {
return pseudo_cwd;
}
return 0;
}
char *
pseudo_root_path(const char *func, int line, int dirfd, const char *path, int leave_last) {
char *rc;
if (!path)
return 0;
pseudo_antimagic();
rc = base_path(dirfd, path, leave_last);
pseudo_magic();
if (!rc) {
/* Only print the diagnostics if we can reasonably
* assume that base_path has really failed. This
* needs work as we are second guessing. FIX!
* The real problem is that, e.g., stdout can be probed
* leading to rc==0. This later messes up __fxstatat64.c
* returning an error when the path should have remained
* non-0.
*/
if (path && *path && fd_path(dirfd)) {
pseudo_diag("couldn't allocate absolute path for '%s'.\n",
path ? path : "<nil>");
}
}
pseudo_debug(PDBGF_CHROOT, "root_path [%s, %d]: dirfd %d, '%s' from '%s'\n",
func, line,
dirfd,
rc ? rc : "<nil>",
path ? path : "<nil>");
return rc;
}
int
pseudo_client_getcwd(void) {
char *cwd;
cwd = malloc(pseudo_path_max());
if (!cwd) {
pseudo_diag("Can't allocate CWD buffer!\n");
return -1;
}
pseudo_debug(PDBGF_CLIENT | PDBGF_VERBOSE, "getcwd: trying to find cwd.\n");
if (getcwd(cwd, pseudo_path_max())) {
/* cwd now holds a canonical path to current directory */
free(pseudo_cwd);
pseudo_cwd = cwd;
pseudo_cwd_len = strlen(pseudo_cwd);
pseudo_debug(PDBGF_CLIENT | PDBGF_VERBOSE, "getcwd okay: [%s] %d bytes\n", pseudo_cwd, (int) pseudo_cwd_len);
if (pseudo_chroot_len &&
pseudo_cwd_len >= pseudo_chroot_len &&
!memcmp(pseudo_cwd, pseudo_chroot, pseudo_chroot_len) &&
(pseudo_cwd[pseudo_chroot_len] == '\0' ||
pseudo_cwd[pseudo_chroot_len] == '/')) {
pseudo_cwd_rel = pseudo_cwd + pseudo_chroot_len;
} else {
pseudo_cwd_rel = pseudo_cwd;
}
pseudo_debug(PDBGF_CLIENT | PDBGF_VERBOSE, "abscwd: <%s>\n", pseudo_cwd);
if (pseudo_cwd_rel != pseudo_cwd) {
pseudo_debug(PDBGF_CLIENT | PDBGF_VERBOSE, "relcwd: <%s>\n", pseudo_cwd_rel);
}
return 0;
} else {
pseudo_diag("Can't get CWD: %s\n", strerror(errno));
return -1;
}
}
static void
pseudo_client_path_set(int fd, const char *path, char ***patharray, int *len) {
if (fd < 0)
return;
if (fd >= *len) {
int i;
pseudo_debug(PDBGF_CLIENT, "expanding fds from %d to %d\n",
*len, fd + 1);
(*patharray) = realloc((*patharray), (fd + 1) * sizeof(char *));
for (i = *len; i < fd + 1; ++i)
(*patharray)[i] = 0;
*len = fd + 1;
} else {
if ((*patharray)[fd]) {
pseudo_debug(PDBGF_CLIENT, "reopening fd %d [%s] -- didn't see close\n",
fd, (*patharray)[fd]);
}
/* yes, it is safe to free null pointers. yay for C89 */
free((*patharray)[fd]);
(*patharray)[fd] = 0;
}
if (path) {
(*patharray)[fd] = strdup(path);
}
}
static void
pseudo_client_path(int fd, const char *path) {
pseudo_client_path_set(fd, path, &fd_paths, &nfds);
}
void
pseudo_client_linked_paths(const char *oldpath, const char *newpath) {
int fd;
for (fd = 3; fd < nfds; ++fd) {
if (fd_paths[fd] && !strcmp(oldpath, fd_paths[fd])) {
pseudo_client_path_set(fd, newpath, &linked_fd_paths, &linked_nfds);
}
}
}
static void
pseudo_client_rename_path(const char *oldpath, const char *newpath) {
int fd;
for (fd = 3; fd < nfds; ++fd) {
if (fd_paths[fd] && !strcmp(oldpath, fd_paths[fd])) {
pseudo_client_path(fd, newpath);
}
}
for (fd = 0; fd < linked_nfds; ++fd) {
if (linked_fd_paths[fd] && fd_paths[fd] && !strcmp(oldpath, linked_fd_paths[fd])) {
pseudo_client_path_set(fd, newpath, &linked_fd_paths, &linked_nfds);
}
}
}
static void
pseudo_client_unlinked_path(const char *path) {
int fd;
for (fd = 0; fd < linked_nfds; ++fd) {
if (linked_fd_paths[fd] && fd_paths[fd] && !strcmp(path, fd_paths[fd])) {
pseudo_client_path(fd, linked_fd_paths[fd]);
}
}
}
static void
pseudo_client_close(int fd) {
if (fd < 0 || fd >= nfds)
return;
free(fd_paths[fd]);
fd_paths[fd] = 0;
if (fd < linked_nfds) {
free(linked_fd_paths[fd]);
linked_fd_paths[fd] = 0;
}
}