source: trunk/src/slib.c@ 308

Last change on this file since 308 was 290, checked in by katerina, 14 years ago

Fixes for tickets #215, #216, #217, #218, version bumped to 2.7.2

File size: 70.2 KB
Line 
1#include "config_xor.h"
2
3#if defined(HAVE_POSIX_FADVISE) && defined(HAVE_MINCORE)
4#define _XOPEN_SOURCE 600
5#define _BSD_SOURCE
6#endif
7
8#include <stdio.h>
9#include <stdlib.h>
10#include <stdarg.h>
11#include <string.h>
12#include <limits.h>
13#ifdef HAVE_STDINT_H
14/* for SIZE_MAX */
15#include <stdint.h>
16#endif
17
18#include <sys/types.h>
19#include <sys/stat.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <signal.h>
23
24#if defined(HAVE_POSIX_FADVISE) && defined(HAVE_MINCORE)
25#include <sys/mman.h>
26#endif
27
28#if TIME_WITH_SYS_TIME
29#include <sys/time.h>
30#include <time.h>
31#else
32#if HAVE_SYS_TIME_H
33#include <sys/time.h>
34#else
35#include <time.h>
36#endif
37#endif
38
39#ifdef HAVE_MEMORY_H
40#include <memory.h>
41#endif
42#ifdef HAVE_SYS_SELECT_H
43#include <sys/select.h>
44#endif
45
46#ifndef FD_SET
47#define NFDBITS 32
48#define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
49#define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
50#define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
51#endif /* !FD_SET */
52#ifndef FD_SETSIZE
53#define FD_SETSIZE 32
54#endif
55#ifndef FD_ZERO
56#define FD_ZERO(p) memset((char *)(p), '\0', sizeof(*(p)))
57#endif
58
59#define SH_REAL_SET
60
61#include "slib.h"
62#include "sh_calls.h"
63#define SH_NEED_PWD_GRP 1
64#include "sh_static.h"
65#include "sh_pthread.h"
66#include "sh_string.h"
67
68#undef FIL__
69#define FIL__ _("slib.c")
70
71const uid_t sh_uid_neg = ((uid_t) -1);
72const gid_t sh_gid_neg = ((gid_t) -1);
73
74#undef BREAKEXIT
75#if defined(SCREW_IT_UP) && defined(__linux__) && defined(__i386__)
76
77#ifdef SH_DEBUG
78#define BREAKEXIT(expr) \
79 do { \
80 int ixi; \
81 for (ixi = 0; ixi < 8; ++ixi) { \
82 if ((*(volatile unsigned *)((unsigned) expr + ixi) & 0xff) == 0xcc) \
83 { dlog(0, FIL__, __LINE__, _("BREAKEXIT")); _exit(EXIT_FAILURE); } \
84 } \
85 } \
86 while (1 == 0)
87#else
88#define BREAKEXIT(expr) \
89 do { \
90 int ixi; \
91 for (ixi = 0; ixi < 8; ++ixi) { \
92 if ((*(volatile unsigned *)((unsigned) expr + ixi) & 0xff) == 0xcc) \
93 _exit(EXIT_FAILURE); \
94 } \
95 } \
96 while (1 == 0)
97#endif
98
99#else
100#define BREAKEXIT(expr)
101#endif
102
103/****************************************************************
104 *
105 * The debug/trace subsystem
106 *
107 ****************************************************************/
108
109int slib_do_trace = 0;
110int slib_trace_fd = -1;
111
112static char trace_log[256] = { '\0' };
113static int trace_level = 0;
114static FILE * trace_fp = NULL;
115
116int sl_trace_use (const char * dummy)
117{
118 (void) dummy;
119 slib_do_trace = 1;
120 return 0;
121}
122
123int sl_trace_file (const char * str)
124{
125 if (!str)
126 return -1;
127 if (str[0] != '/')
128 return -1;
129 sl_strlcpy(trace_log, str, 256);
130 return 0;
131}
132
133FILE * sl_tracefile_open(const char * file, const char * mode)
134{
135 FILE * xp = NULL;
136 slib_trace_fd = open(file, O_WRONLY|O_CREAT|O_APPEND, 0600);
137 if (slib_trace_fd >= 0)
138 xp = fdopen(slib_trace_fd, mode);
139 return xp;
140}
141
142void sl_trace_in(const char * str, const char * file, int line)
143{
144 int i;
145 if (trace_log[0] == '\0')
146 {
147 fprintf(stderr, "++ ");
148 for (i = 0; i < trace_level; ++i)
149 fprintf(stderr, ". ");
150 fprintf(stderr, "[%2d] %s \t - File %c%s%c at line %d\n",
151 trace_level, str, 0x22, file, 0x22, line);
152 }
153 else if (!sl_is_suid())
154 {
155 if (!trace_fp)
156 trace_fp = sl_tracefile_open(trace_log, "a");
157 if (trace_fp)
158 {
159 fprintf(trace_fp, "++ ");
160 for (i = 0; i < trace_level; ++i)
161 fprintf(trace_fp, ". ");
162 fprintf(trace_fp, "[%2d] %s \t - File %c%s%c at line %d\n",
163 trace_level, str, 0x22, file, 0x22, line);
164 fflush(trace_fp);
165 }
166 else
167 {
168 perror(_("sl_trace_in: fopen"));
169 _exit(1);
170 }
171 }
172 ++trace_level;
173}
174
175void sl_trace_out(const char * str, const char * file, int line)
176{
177 int i;
178
179 --trace_level; if (trace_level < 0) trace_level = 0;
180
181 if (trace_log[0] == '\0')
182 {
183 fprintf(stderr, "-- ");
184 for (i = 0; i < trace_level; ++i)
185 fprintf(stderr, ". ");
186 fprintf(stderr, _("[%2d] %s \t - File %c%s%c at line %d\n"),
187 trace_level, str, 0x22, file, 0x22, line);
188 }
189 else if (!sl_is_suid())
190 {
191 if (!trace_fp)
192 trace_fp = sl_tracefile_open(trace_log, "a");
193 if (trace_fp)
194 {
195 fprintf(trace_fp, "-- ");
196 for (i = 0; i < trace_level; ++i)
197 fprintf(trace_fp, ". ");
198 fprintf(trace_fp, _("[%2d] %s \t - File %c%s%c at line %d\n"),
199 trace_level, str, 0x22, file, 0x22, line);
200 fflush(trace_fp);
201 }
202 else
203 {
204 perror(_("sl_trace_out: fopen"));
205 _exit(1);
206 }
207 }
208}
209
210extern int sh_log_console (const char * msg);
211
212static int dlogActive = 0;
213
214/* this is called from sh_error_setprint()
215 */
216void dlog_set_active(int flag)
217{
218 dlogActive = flag;
219}
220
221/* flag = 0 debug messages
222 * = 1 descriptive error messages
223 * = 3 backtrace
224 */
225int dlog (int flag, const char * file, int line, const char *fmt, ...)
226{
227 va_list ap;
228 char val[81];
229 char msg[512];
230 char tmp[512];
231 int retval = 0;
232 int i;
233
234#ifdef SH_STEALTH
235 /*
236 * do not even print descriptive failure messages in stealth mode
237 */
238 if (dlogActive == 0)
239 return 0;
240 if (dlogActive == 1 && flag == 0) /* debug requires debug level */
241 return 0;
242#else
243 if (dlogActive <= 1 && flag == 0) /* debug requires debug level */
244 return 0;
245#endif
246
247 if (flag == 1)
248 {
249 sl_snprintf (val, 81, _("\n--------- %10s "), file);
250 sl_strlcpy (msg, val, 80);
251 sl_snprintf (val, 81, _(" --- %6d ---------\n"), line);
252 sl_strlcat (msg, val, 80);
253 sh_log_console (msg);
254 }
255
256 va_start (ap, fmt);
257 if (flag == 1)
258 sl_strlcpy(tmp, fmt, 512);
259 else
260 sl_strlcpy(tmp, fmt, 256);
261 retval = strlen(tmp);
262 if (retval > 0 && tmp[retval-1] == '\n')
263 tmp[retval-1] = '\0';
264 retval = 0;
265 if (flag == 1)
266 {
267 sl_vsnprintf (msg, 511, tmp, ap);
268 }
269 else
270 {
271 sl_strlcpy (msg, "## ", 256);
272 for (i = 0; i < trace_level; ++i)
273 sl_strlcat (msg, ". ", 256);
274 sprintf (val, _("[%2d] "), trace_level);
275 sl_strlcat (msg, val, 256);
276 sl_vsnprintf (&msg[strlen(msg)], 255, tmp, ap);
277 sl_snprintf (tmp, 255, _(" \t - File %c%s%c at line %d"),
278 0x22, file, 0x22, line);
279 sl_strlcat (msg, tmp, 512);
280 }
281 va_end (ap);
282 if (flag != 0 || sl_is_suid())
283 retval = sh_log_console (msg);
284 else
285 {
286 if (trace_log[0] == '\0')
287 {
288 /* sh_log_console (msg); */
289 fprintf(stderr, "%s\n", msg);
290 }
291 else
292 {
293 if (!trace_fp)
294 trace_fp = sl_tracefile_open(trace_log, "a");
295 if (trace_fp)
296 {
297 fprintf(trace_fp, "%s\n", msg);
298 }
299 else
300 {
301 perror(_("dlog: fopen"));
302 _exit(1);
303 }
304 }
305 }
306 if (flag == 1)
307 sh_log_console (_("\n----------------------------------------------\n"));
308 return retval;
309}
310
311extern char aud_err_message[64];
312static char alt_err_message[64];
313char * sl_get_errmsg()
314{
315 if (aud_err_message[0] == '\0')
316 {
317 sl_strlcpy(alt_err_message, sl_error_string(sl_errno), 64);
318 return &alt_err_message[0];
319 }
320 return &aud_err_message[0];
321}
322
323
324#if defined(SL_DEBUG)
325#define SL_MAX_MYSTACK 128
326
327static char sl_mystack[SL_MAX_MYSTACK][32];
328static int sl_mystack_count = 0;
329
330void sl_stack_push(char * c, char * file, int line )
331{
332 if (slib_do_trace)
333 sl_trace_in(c, file, line);
334 if (c && sl_mystack_count < SL_MAX_MYSTACK)
335 {
336 strncpy(sl_mystack[sl_mystack_count], c, 31);
337 sl_mystack[sl_mystack_count][31] = '\0';
338 ++sl_mystack_count;
339 /*
340 fprintf(stderr, "#%03d %s\n", sl_mystack_count,
341 sl_mystack[sl_mystack_count-1]);
342 */
343 }
344 return;
345}
346
347void sl_stack_pop(char * c, char * file, int line)
348{
349 if (slib_do_trace)
350 sl_trace_out(c, file, line);
351 if (sl_mystack_count > 0)
352 {
353 /*
354 fprintf(stderr, " <- #%03d %s\n", sl_mystack_count,
355 sl_mystack[sl_mystack_count-1]);
356 */
357 --sl_mystack_count;
358 }
359 return;
360}
361
362void sl_stack_print()
363{
364 int i;
365 /* FILE * dfile; */
366
367 if (sl_mystack_count > 0)
368 {
369 sh_log_console(_("\nBacktrace:\n"));
370 /* dlog(3, FIL__, __LINE__, _("\nBacktrace:\n")); */
371 for (i = 0; i < sl_mystack_count; ++i)
372 sh_log_console(sl_mystack[i]);
373 /* dlog(3, FIL__, __LINE__, _("#%03d %s\n"), i, sl_mystack[i]); */
374 }
375 return;
376}
377
378#endif
379
380
381/*
382 * The global errno.
383 * On error, this is set to the return value of the function.
384 */
385long int sl_errno;
386
387
388/* ----------------------------------------------------------------
389 *
390 * Capability routines
391 *
392 * ---------------------------------------------------------------- */
393
394int sl_useCaps = 0;
395
396#ifdef FANCY_LIBCAP
397#include <sys/capability.h>
398
399/*
400 * While these routines are tested and work, we don't use POSIX
401 * capabilities, as they don't seem to be useful (root can write
402 * to root-owned files anyway). Things would be more interesting
403 * if we could switch to a non-root UID with just a few capabilities
404 * enabled.
405 */
406int sl_drop_cap ()
407{
408 int error;
409 cap_t caps;
410 cap_flag_t capflag;
411 cap_flag_value_t capfval = CAP_CLEAR;
412 cap_value_t capvals_e[] =
413 {
414 CAP_CHOWN, CAP_FOWNER, CAP_FSETID,
415 CAP_LINUX_IMMUTABLE, CAP_MKNOD, CAP_NET_ADMIN,
416 CAP_NET_BIND_SERVICE, CAP_NET_BROADCAST, CAP_NET_RAW,
417 CAP_SYS_ADMIN, CAP_SYS_BOOT, CAP_SYS_CHROOT,
418 CAP_SYS_PACCT, CAP_SYS_PTRACE, CAP_SYS_RAWIO,
419 CAP_SYS_RESOURCE, CAP_SYS_TIME, CAP_SYS_TTY_CONFIG,
420 CAP_SETGID, CAP_SETUID, CAP_KILL,
421 CAP_DAC_OVERRIDE,
422#if !defined(WITH_MESSAGE_QUEUE)
423 CAP_IPC_OWNER,
424#endif
425 CAP_SYS_MODULE, CAP_LEASE
426 };
427 cap_value_t capvals_p[] =
428 {
429 CAP_CHOWN, CAP_LEASE, CAP_FSETID,
430 CAP_LINUX_IMMUTABLE, CAP_MKNOD, CAP_NET_ADMIN,
431 CAP_NET_BIND_SERVICE, CAP_NET_BROADCAST, CAP_NET_RAW,
432 CAP_SYS_ADMIN, CAP_SYS_BOOT, CAP_SYS_CHROOT,
433 CAP_SYS_PACCT, CAP_SYS_PTRACE, CAP_SYS_RAWIO,
434 CAP_SYS_RESOURCE, CAP_SYS_TIME, CAP_SYS_TTY_CONFIG,
435#if !defined(WITH_EXTERNAL) && !defined(HAVE_UNIX_RANDOM)
436 CAP_SETGID, CAP_SETUID, CAP_KILL,
437#endif
438#if !defined(SH_USE_SUIDCHK)
439 CAP_DAC_OVERRIDE, CAP_FOWNER,
440#endif
441#if !defined(WITH_MESSAGE_QUEUE)
442 CAP_IPC_OWNER,
443#endif
444 CAP_SYS_MODULE
445 };
446
447 if (0 == sl_useCaps) /* 0 = S_FALSE */
448 {
449 return 0;
450 }
451
452 if(NULL == (caps = cap_get_proc()))
453 {
454 return errno;
455 }
456
457 capflag = CAP_EFFECTIVE;
458 if (0 != cap_set_flag(caps, capflag, sizeof(capvals_e)/sizeof(cap_value_t),
459 capvals_e, capfval))
460 {
461 error = errno;
462 cap_free(caps);
463 return error;
464 }
465 if (0 != cap_set_proc(caps))
466 {
467 error = errno;
468 cap_free(caps);
469 return error;
470 }
471
472 capflag = CAP_PERMITTED;
473 if (0 != cap_set_flag(caps, capflag, sizeof(capvals_p)/sizeof(cap_value_t),
474 capvals_p, capfval))
475 {
476 error = errno;
477 cap_free(caps);
478 return error;
479 }
480 if (0 != cap_set_proc(caps))
481 {
482 error = errno;
483 cap_free(caps);
484 return error;
485 }
486 cap_free(caps);
487 return 0;
488}
489
490int sl_drop_cap_int(int what)
491{
492#if defined(SL_DEBUG)
493 char * captext;
494#endif
495 cap_flag_t capflag = CAP_EFFECTIVE;
496 cap_flag_value_t capfval = CAP_CLEAR;
497 cap_value_t capvals_a[] = { CAP_SETGID, CAP_SETUID, CAP_KILL };
498 cap_value_t capvals_b[] = { CAP_DAC_OVERRIDE, CAP_FOWNER };
499 cap_value_t * capvals;
500 int nvals;
501 int error = 0;
502 cap_t caps = cap_get_proc();
503
504 if (0 == sl_useCaps) /* 0 = S_FALSE */
505 {
506 return 0;
507 }
508
509 if (caps == NULL)
510 {
511 return errno;
512 }
513
514 switch (what) {
515 case 1:
516 capvals = capvals_a;
517 nvals = 3;
518 capfval = CAP_CLEAR;
519 break;
520 case 2:
521 capvals = capvals_a;
522 nvals = 3;
523 capfval = CAP_SET;
524 break;
525 case 3:
526 capvals = capvals_b;
527 nvals = 2;
528 capfval = CAP_CLEAR;
529 break;
530 case 4:
531 capvals = capvals_b;
532 nvals = 2;
533 capfval = CAP_SET;
534 break;
535 default:
536 return (0);
537 }
538
539 if (0 != cap_set_flag(caps, capflag, nvals, capvals, capfval))
540 {
541 error = errno;
542 cap_free(caps);
543 return error;
544 }
545 if (0 != cap_set_proc(caps))
546 {
547 error = errno;
548 cap_free(caps);
549 return error;
550 }
551#if defined(SL_DEBUG)
552 captext = cap_to_text(caps, NULL);
553 TPT(( 0, FIL__, __LINE__, _("msg=<cap_int %d: %s>\n"), what, captext));
554 cap_free(captext);
555#endif
556 cap_free(caps);
557 return 0;
558}
559
560int sl_drop_cap_sub() { return sl_drop_cap_int(1); }
561int sl_get_cap_sub() { return sl_drop_cap_int(2); }
562int sl_drop_cap_qdel() { return sl_drop_cap_int(3); }
563int sl_get_cap_qdel() { return sl_drop_cap_int(4); }
564
565#else
566int sl_drop_cap () { return 0; }
567int sl_drop_cap_sub() { return 0; }
568int sl_get_cap_sub() { return 0; }
569int sl_drop_cap_qdel() { return 0; }
570int sl_get_cap_qdel() { return 0; }
571#endif
572
573/* ----------------------------------------------------------------
574 *
575 * String handling routines
576 *
577 * ---------------------------------------------------------------- */
578
579/*
580 * Have memset in a different translation unit (i.e. this) to prevent
581 * it to get optimized away
582 */
583void *sl_memset(void *s, int c, size_t n)
584{
585 return memset(s, c,n);
586}
587
588
589#if !defined (VA_COPY)
590#if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
591#define VA_COPY(ap1, ap2) (*(ap1) = *(ap2))
592#elif defined (VA_COPY_AS_ARRAY)
593#define VA_COPY(ap1, ap2) memmove ((ap1), (ap2), sizeof (va_list))
594#else /* va_list is a pointer */
595#define VA_COPY(ap1, ap2) ((ap1) = (ap2))
596#endif
597#endif
598
599#if !defined(HAVE_VSNPRINTF) || defined(HAVE_BROKEN_VSNPRINTF)
600static
601size_t sl_printf_count (const char * fmt, va_list vl)
602{
603 size_t length = 1;
604 int fini = 0;
605 int islong = 0;
606 int islonglong = 0;
607 int islongdouble = 0;
608 char * string_arg;
609
610 SL_ENTER(_("sl_printf_count"));
611
612 if (fmt == NULL)
613 SL_IRETURN(SL_ENULL, _("sl_printf_count"));
614
615 while (*fmt) {
616
617 if ( (*fmt) == '%' ) { /* a format specifier */
618
619 fmt++; /* point to first char after '%' */
620
621 fini = 0;
622 islong = 0;
623 islongdouble = 0;
624
625 while (*fmt && (fini == 0) ) {
626
627 switch (*fmt) {
628
629 case '*': /* field width supplied by an integer */
630 length = length + va_arg (vl, int);
631 ++fmt;
632 break;
633 case '1':
634 case '2':
635 case '3':
636 case '4':
637 case '5':
638 case '6':
639 case '7':
640 case '8':
641 case '9':
642 length = length + strtol (fmt, (char**) &fmt, 10);
643 /* strtol makes FastForward to first invalid char */
644 break;
645
646 case 'l': /* 'long' modifier */
647 if (islong == 0)
648 islong = 1;
649 else
650 {
651 islonglong = 1;
652 islong = 0;
653 }
654 ++fmt;
655 break;
656
657 case 'L': /* 'long double' modifier */
658#ifdef HAVE_LONG_DOUBLE
659 islongdouble = 1;
660#else
661 islong = 1;
662#endif
663 ++fmt;
664 break;
665
666 case 'd':
667 case 'i':
668 case 'o':
669 case 'u':
670 case 'x':
671 case 'X':
672 if (islonglong == 1)
673#ifdef HAVE_LONG_LONG
674 (void) va_arg (vl, long long);
675#else
676 (void) va_arg (vl, long);
677#endif
678 else if (islong == 1)
679 (void) va_arg (vl, long);
680 else
681 (void) va_arg (vl, int);
682 islong = 0;
683 islonglong = 0;
684 length = length + 24;
685 ++fmt;
686 fini = 1;
687 break;
688
689 case 'D':
690 case 'O':
691 case 'U':
692 (void) va_arg (vl, long);
693 length = length + 24;
694 fmt++;
695 fini = 1;
696 break;
697
698 case 'e':
699 case 'E':
700 case 'f':
701 case 'g':
702#ifdef HAVE_LONG_DOUBLE
703 if (islongdouble == 1) {
704 (void) va_arg (vl, long double);
705 islongdouble = 0;
706 length = length + 20;
707 }
708 else
709#endif
710 (void) va_arg (vl, double);
711 length = length + 20;
712 fini = 1;
713 ++fmt;
714 break;
715
716 case 's':
717 string_arg = va_arg (vl, char *);
718 if (string_arg != NULL)
719 length = length + sl_strlen (string_arg);
720 else
721 length = length + 16;
722 fini = 1;
723 ++fmt;
724 break;
725
726 case 'c':
727 (void) va_arg (vl, int);
728 length = length + 1;
729 fini = 1;
730 ++fmt;
731 break;
732
733 case 'p':
734 case 'n':
735 (void) va_arg (vl, void * );
736 length = length + 32;
737 fini = 1;
738 ++fmt;
739 break;
740
741 case '%': /* %% will print '%' */
742 length = length + 1;
743 fini = 1;
744 ++fmt;
745 break;
746
747 default:
748 length = length + 1;
749 ++fmt;
750 break;
751
752 } /* end switch */
753 }
754 /* end parsing a single format specifier */
755 } else {
756 length = length + 1;
757 fmt++;
758 }
759 }
760 SL_IRETURN(length, _("sl_printf_count"));
761}
762#endif /* #ifndef HAVE_VSNPRINTF */
763
764/*
765 * An implementation of vsnprintf. va_start/va_end are in the caller
766 * function.
767 * Returns C99 (#bytes that would heve been written) on success.
768 */
769int sl_vsnprintf(char *str, size_t n,
770 const char *format, va_list vl )
771{
772 int len = 0;
773#if !defined(HAVE_VSNPRINTF) || defined(HAVE_BROKEN_VSNPRINTF)
774 size_t total;
775 va_list vl2;
776#endif
777
778 SL_ENTER(_("sl_vsnprintf"));
779 if (str == NULL || format == NULL)
780 SL_IRETURN(0, _("sl_vsnprintf"));
781
782#if defined(HAVE_VSNPRINTF) && !defined(HAVE_BROKEN_VSNPRINTF)
783 len = vsnprintf (str, n, format, vl); /* flawfinder: ignore */
784 str[n-1] = '\0';
785#else
786 VA_COPY (vl2, vl); /* save the argument list */
787 total = sl_printf_count (format, vl);
788 len = (int) total;
789 if (total < n)
790 {
791 /* flawfinder: ignore */
792 vsprintf (str, format, vl2); /* program has checked that it fits */
793 str[n-1] = '\0';
794 }
795 else
796 {
797 sl_strlcpy (str, format, n);
798 va_end(vl2);
799 SL_IRETURN(len, _("sl_vsnprintf"));
800 }
801 va_end(vl2);
802#endif
803 SL_IRETURN(len, _("sl_vsnprintf"));
804}
805
806/*
807 * An implementation of snprintf.
808 * Returns SL_ENONE on success.
809 * ENULL: src || format == NULL
810 * ERANGE: n out of range
811 * ETRUNC: truncated (unimplemented)
812 */
813int sl_snprintf(char *str, size_t n,
814 const char *format, ... )
815{
816 va_list vl;
817#if !defined(HAVE_VSNPRINTF) || defined(HAVE_BROKEN_VSNPRINTF)
818 size_t total = 0;
819 va_list vl2;
820#endif
821
822 SL_ENTER(_("sl_snprintf"));
823 if (str == NULL || format == NULL)
824 SL_IRETURN(SL_ENULL, _("sl_snprintf"));
825
826 va_start (vl, format);
827#if defined(HAVE_VSNPRINTF) && !defined(HAVE_BROKEN_VSNPRINTF)
828 /* flawfinder: ignore */
829 vsnprintf (str, n, format, vl);
830 str[n-1] = '\0';
831#else
832 VA_COPY (vl2, vl); /* save the argument list */
833 total = sl_printf_count (format, vl);
834 if (total < n)
835 {
836 /* flawfinder: ignore */
837 vsprintf (str, format, vl2); /* program has checked that it fits */
838 str[n-1] = '\0';
839 }
840 else
841 {
842 sl_strlcpy (str, format, n);
843 va_end(vl2);
844 va_end(vl);
845 SL_IRETURN(SL_ETRUNC, _("sl_snprintf"));
846 }
847 va_end(vl2);
848#endif
849 va_end(vl);
850 SL_IRETURN(SL_ENONE, _("sl_snprintf"));
851}
852
853/*
854 * Appends src to string dst of size siz (unlike strncat, siz is the
855 * full size of dst, not space left). At most siz-1 characters
856 * will be copied. Always NUL terminates (unless siz == 0).
857 * Returns SL_NONE on success, errcode on failure.
858 *
859 * ENULL: dst == NULL
860 * ERANGE: siz out of range
861 * ETRUNC: src truncated
862 */
863int sl_strlcat(char * dst, /*@null@*/const char *src, size_t siz)
864{
865 register size_t dst_end;
866 register size_t dst_free;
867
868 register char * p;
869 register const char * q;
870
871 if (!(dst == NULL || src == NULL || *src == '\0'))
872 {
873 if (siz > 0)
874 {
875
876 /* How much free space do we have ?
877 */
878 dst_end = strlen(dst);
879 dst_free = siz - dst_end - 1;
880
881 p = &dst[dst_end];
882 q = src;
883
884 while (dst_free > 0 && *q != '\0')
885 {
886 *p++ = *q++;
887 --dst_free;
888 }
889
890 /* NULL terminate dst.
891 */
892 *p = '\0';
893
894 if (*q == '\0')
895 return SL_ENONE;
896 else
897 return SL_ETRUNC;
898 }
899 }
900 return SL_ENONE;
901}
902
903/*
904 * An alternative implementation of the OpenBSD strlcpy() function.
905 *
906 * Copy src to string dst of size siz. At most siz-1 characters
907 * will be copied. Always NUL terminates (unless siz == 0).
908 * Returns SL_NONE on success, errcode on failure.
909 *
910 * ENULL: dst == NULL
911 * ERANGE: siz out of range
912 * ETRUNC: src truncated
913 */
914int sl_strlcpy(char * dst, /*@null@*/const char * src, size_t siz)
915{
916 /* SL_ENTER(_("sl_strlcpy")); */
917
918 if (!((dst == NULL) || (src == NULL)))
919 {
920 if (siz > 0) {
921 /* copy siz-1 characters
922 */
923 (void) strncpy(dst, src, siz-1);
924
925 /* NULL terminate
926 */
927 dst[siz-1] = '\0';
928 }
929 return SL_ENONE;
930 }
931 else if (src == NULL)
932 {
933 if (siz > 0)
934 dst[0] = '\0';
935 return SL_ENONE;
936 }
937 else
938 {
939 return SL_ENULL;
940 }
941}
942
943/*
944 * A robust drop-in replacement of strncpy. strlcpy is preferable.
945 */
946char * sl_strncpy(char *dst, const char *src, size_t size)
947{
948
949#ifdef SL_FAIL_ON_ERROR
950 SL_REQUIRE(dst != NULL, _("dst != NULL"));
951 SL_REQUIRE(src != NULL, _("src != NULL"));
952 SL_REQUIRE(size > 0, _("size > 0"));
953#endif
954
955 if (dst == NULL)
956 {
957 sl_errno = SL_ENULL;
958 return (NULL);
959 }
960 if (size < 1)
961 {
962 sl_errno = SL_ERANGE;
963 return (dst);
964 }
965 if (!src)
966 {
967 sl_errno = SL_ENULL;
968 dst[0] = '\0';
969 }
970 else if (src[0] == '\0')
971 dst[0] = '\0';
972 else
973 strncpy(dst, src, size);
974
975 if (sl_strlen(src) >= size)
976 {
977 errno = ENOSPC;
978 dst[size-1] = '\0';
979 }
980 return (dst);
981}
982
983/*
984 * A robust drop-in replacement of strncat. strlcat is preferable.
985 */
986char * sl_strncat(char *dst, const char *src, size_t n)
987{
988#ifdef SL_FAIL_ON_ERROR
989 SL_REQUIRE(dst != NULL, _("dst != NULL"));
990 SL_REQUIRE(src != NULL, _("src != NULL"));
991 SL_REQUIRE(n > 0, _("n > 0"));
992#endif
993
994 if (dst == NULL)
995 {
996 sl_errno = SL_ENULL;
997 return (NULL);
998 }
999 if (n < 1)
1000 {
1001 sl_errno = SL_ERANGE;
1002 return (dst);
1003 }
1004 if (!src)
1005 {
1006 sl_errno = SL_ENULL;
1007 return (dst);
1008 }
1009 else if (src[0] == '\0')
1010 dst[0] = '\0';
1011 else
1012 strncat(dst, src, n);
1013
1014 return (dst);
1015}
1016
1017#include <ctype.h>
1018int sl_strcasecmp(const char * one, const char * two)
1019{
1020#ifdef SL_FAIL_ON_ERROR
1021 SL_REQUIRE (one != NULL, _("one != NULL"));
1022 SL_REQUIRE (two != NULL, _("two != NULL"));
1023#endif
1024
1025 if (one && two)
1026 {
1027 do {
1028 if (*one && *two)
1029 {
1030 if (tolower((int) *one) == tolower((int) *two))
1031 {
1032 ++one; ++two;
1033 }
1034 else if (tolower((int) *one) < tolower((int) *two))
1035 return -1;
1036 else
1037 return 1;
1038 }
1039 else if (*one == '\0' && *two == '\0')
1040 return 0;
1041 else if (*one == '\0')
1042 return -1;
1043 else
1044 return 1;
1045 } while (1 == 1);
1046 }
1047 else if (one == NULL && two != NULL)
1048 return -1;
1049 else if (one != NULL && two == NULL)
1050 return 1;
1051 else
1052 return -7; /* default to not equal */
1053}
1054
1055int sl_strcmp(const char * a, const char * b)
1056{
1057#ifdef SL_FAIL_ON_ERROR
1058 SL_REQUIRE (a != NULL, _("a != NULL"));
1059 SL_REQUIRE (b != NULL, _("b != NULL"));
1060#endif
1061
1062 if (a != NULL && b != NULL)
1063 return (strcmp(a, b));
1064 else if (a == NULL && b != NULL)
1065 return (-1);
1066 else if (a != NULL && b == NULL)
1067 return (1);
1068 else
1069 return (-7); /* default to not equal */
1070}
1071
1072int sl_strncmp(const char * a, const char * b, size_t n)
1073{
1074#ifdef SL_FAIL_ON_ERROR
1075 SL_REQUIRE (a != NULL, _("a != NULL"));
1076 SL_REQUIRE (b != NULL, _("b != NULL"));
1077 SL_REQUIRE (n > 0, _("n > 0"));
1078#endif
1079
1080 if (a != NULL && b != NULL)
1081 return (strncmp(a, b, n));
1082 else if (a == NULL && b != NULL)
1083 return (-1);
1084 else if (a != NULL && b == NULL)
1085 return (1);
1086 else
1087 return (-7); /* default to not equal */
1088}
1089
1090int sl_strncasecmp(const char * a, const char * b, size_t n)
1091{
1092#ifdef SL_FAIL_ON_ERROR
1093 SL_REQUIRE (a != NULL, _("a != NULL"));
1094 SL_REQUIRE (b != NULL, _("b != NULL"));
1095 SL_REQUIRE (n > 0, _("n > 0"));
1096#endif
1097
1098 if (a != NULL && b != NULL)
1099 return (strncasecmp(a, b, n));
1100 else if (a == NULL && b != NULL)
1101 return (-1);
1102 else if (a != NULL && b == NULL)
1103 return (1);
1104 else
1105 return (-7); /* default to not equal */
1106}
1107
1108/* string searching
1109 */
1110
1111char * sl_strstr (const char * haystack, const char * needle)
1112{
1113#ifndef HAVE_STRSTR
1114 unsigned int i;
1115 size_t needle_len;
1116 size_t haystack_len;
1117#endif
1118
1119 if (haystack == NULL || needle == NULL)
1120 return NULL;
1121 if (*needle == '\0' || *haystack == '\0')
1122 return NULL;
1123
1124#if defined(HAVE_STRSTR)
1125 return (strstr(haystack, needle));
1126#else
1127 needle_len = strlen(needle);
1128 haystack_len = strlen(haystack);
1129
1130 for (i = 0; i <= (haystack_len-needle_len); ++i)
1131 if (0 == sl_strncmp(&haystack[i], needle, needle_len))
1132 return (needle);
1133 return NULL;
1134#endif
1135}
1136
1137
1138/* ----------------------------------------------------------------
1139 *
1140 * Privilege handling routines
1141 *
1142 * ---------------------------------------------------------------- */
1143
1144
1145
1146static uid_t euid;
1147static uid_t ruid;
1148static uid_t ruid_orig;
1149static gid_t egid;
1150static gid_t rgid;
1151static gid_t rgid_orig;
1152
1153static int uids_are_stored = SL_FALSE;
1154static int suid_is_set = SL_TRUE;
1155
1156#ifdef HAVE_SETRESUID
1157extern int setresuid (uid_t truid, uid_t teuid, uid_t tsuid);
1158extern int setresgid (gid_t trgid, gid_t tegid, gid_t tsgid);
1159#endif
1160
1161
1162/*
1163 * This function returns true if the program is SUID.
1164 * It calls abort() if the uid's are not saved already.
1165 */
1166int sl_is_suid()
1167{
1168 if (uids_are_stored == SL_FALSE)
1169 {
1170 if (getuid() == geteuid() && getgid() == getegid())
1171 return (0); /* FALSE */
1172 else
1173 return (1); /* TRUE */
1174 }
1175 else
1176 {
1177 if (euid == ruid && egid == rgid)
1178 return (0); /* FALSE */
1179 else
1180 return (1); /* TRUE */
1181 }
1182}
1183
1184/*
1185 * This function returns the saved euid.
1186 * It calls abort() if the uid's are not saved already.
1187 */
1188int sl_get_euid(uid_t * ret)
1189{
1190 SL_ENTER(_("sl_get_euid"));
1191 /* SL_REQUIRE(uids_are_stored == SL_TRUE, _("uids_are_stored == SL_TRUE"));*/
1192 if (uids_are_stored == SL_TRUE)
1193 *ret = euid;
1194 else
1195 *ret = geteuid();
1196 SL_IRETURN (SL_ENONE, _("sl_get_euid"));
1197}
1198
1199uid_t sl_ret_euid()
1200{
1201 /* SL_REQUIRE(uids_are_stored == SL_TRUE, _("uids_are_stored == SL_TRUE"));*/
1202 if (uids_are_stored == SL_TRUE)
1203 return (euid);
1204 else
1205 return (geteuid());
1206}
1207
1208/*
1209 * This function returns the saved egid.
1210 * It calls abort() if the uid's are not saved already.
1211 */
1212int sl_get_egid(gid_t * ret)
1213{
1214 SL_ENTER(_("sl_get_egid"));
1215 /* SL_REQUIRE(uids_are_stored == SL_TRUE, _("uids_are_stored == SL_TRUE"));*/
1216 if (uids_are_stored == SL_TRUE)
1217 *ret = egid;
1218 else
1219 *ret = getegid();
1220 SL_IRETURN (SL_ENONE, _("sl_get_egid"));
1221}
1222
1223/*
1224 * This function returns the saved ruid.
1225 * It calls abort() if the uid's are not saved already.
1226 */
1227int sl_get_ruid(uid_t * ret)
1228{
1229 SL_ENTER(_("sl_get_ruid"));
1230 /* SL_REQUIRE(uids_are_stored == SL_TRUE, _("uids_are_stored == SL_TRUE"));*/
1231 if (uids_are_stored == SL_TRUE)
1232 *ret = ruid;
1233 else
1234 *ret = getuid();
1235 SL_IRETURN (SL_ENONE, _("sl_get_ruid"));
1236}
1237
1238/*
1239 * This function returns the saved rgid.
1240 * It calls abort() if the uid's are not saved already.
1241 */
1242int sl_get_rgid(gid_t * ret)
1243{
1244 SL_ENTER(_("sl_get_rgid"));
1245 /* SL_REQUIRE(uids_are_stored == SL_TRUE, _("uids_are_stored == SL_TRUE"));*/
1246 if (uids_are_stored == SL_TRUE)
1247 *ret = rgid;
1248 else
1249 *ret = getgid();
1250 SL_IRETURN (SL_ENONE, _("sl_get_rgid"));
1251}
1252
1253/*
1254 * This function returns the saved original ruid.
1255 * It calls abort() if the uid's are not saved already.
1256 */
1257int sl_get_ruid_orig(uid_t * ret)
1258{
1259 SL_ENTER(_("sl_get_ruid_orig"));
1260 /* SL_REQUIRE(uids_are_stored == SL_TRUE, _("uids_are_stored == SL_TRUE"));*/
1261 if (uids_are_stored == SL_TRUE)
1262 *ret = ruid_orig;
1263 else
1264 *ret = getuid();
1265 SL_IRETURN (SL_ENONE, _("sl_get_ruid_orig"));
1266}
1267
1268/*
1269 * This function returns the saved original rgid.
1270 * It calls abort() if the uid's are not saved already.
1271 */
1272int sl_get_rgid_orig(gid_t * ret)
1273{
1274 SL_ENTER(_("sl_get_rgid_orig"));
1275 /* SL_REQUIRE(uids_are_stored == SL_TRUE, _("uids_are_stored == SL_TRUE"));*/
1276 if (uids_are_stored == SL_TRUE)
1277 *ret = rgid_orig;
1278 else
1279 *ret = getgid();
1280 SL_IRETURN (SL_ENONE, _("sl_get_rgid_orig"));
1281}
1282
1283static int suid_warn_flag = 1;
1284static void suid_warn(int a)
1285{
1286 fprintf(stderr, _("ERROR: open set/unset suid !!! %d\n"), a);
1287 return;
1288}
1289
1290/*
1291 * This function sets the effective uid
1292 * to the saved effective uid.
1293 * It will abort on failure.
1294 */
1295int sl_set_suid ()
1296{
1297 int retval;
1298
1299 SL_ENTER(_("sl_set_suid"));
1300
1301 if (uids_are_stored == SL_FALSE)
1302 {
1303 SL_IRETURN(SL_ENONE, _("sl_set_suid"));
1304 }
1305
1306 SL_REQUIRE(uids_are_stored == SL_TRUE, _("uids_are_stored == SL_TRUE"));
1307
1308 if (ruid == euid && rgid == egid)
1309 {
1310 suid_is_set = SL_TRUE;
1311 SL_IRETURN(SL_ENONE, _("sl_set_suid"));
1312 }
1313 SL_REQUIRE(suid_is_set == SL_FALSE, _("suid_is_set == SL_FALSE"));
1314
1315#if defined(HAVE_SETRESUID)
1316 retval = setresuid (sh_uid_neg, euid, sh_uid_neg);
1317 if (retval == 0)
1318 retval = setresgid (sh_gid_neg, egid, sh_gid_neg);
1319
1320#elif defined(HAVE_SETEUID)
1321 retval = seteuid (egid);
1322 if (retval == 0)
1323 retval = setegid (euid);
1324
1325 /* on AIX, setreuid does not behave well for non-root users.
1326 */
1327#elif defined(HAVE_SETREUID)
1328 retval = setreuid (ruid, euid);
1329 if (retval == 0)
1330 retval = setregid (rgid, egid);
1331
1332#else
1333 retval = setuid (euid);
1334 if (retval == 0)
1335 retval = setgid (egid);
1336#endif
1337 if (suid_warn_flag == 1)
1338 suid_warn(1);
1339 suid_warn_flag = 1;
1340
1341 SL_REQUIRE(retval == 0, _("retval == 0"));
1342 suid_is_set = SL_TRUE;
1343 SL_IRETURN(SL_ENONE, _("sl_set_suid"));
1344}
1345
1346/*
1347 * This function sets the effective uid to the real uid.
1348 * It will abort on failure.
1349 */
1350int sl_unset_suid ()
1351{
1352 register int retval;
1353
1354 SL_ENTER(_("sl_unset_suid"));
1355
1356 if (uids_are_stored == SL_FALSE)
1357 {
1358 SL_IRETURN(SL_ENONE, _("sl_unset_suid"));
1359 }
1360
1361 SL_REQUIRE(uids_are_stored == SL_TRUE, _("uids_are_stored == SL_TRUE"));
1362
1363 if (ruid == euid && rgid == egid)
1364 {
1365 suid_is_set = SL_FALSE;
1366 SL_IRETURN(SL_ENONE, _("sl_unset_suid"));
1367 }
1368 SL_REQUIRE(suid_is_set == SL_TRUE, _("suid_is_set == SL_TRUE"));
1369
1370#if defined(HAVE_SETRESUID)
1371 retval = setresgid (sh_gid_neg, rgid, sh_gid_neg);
1372 if (retval == 0)
1373 retval = setresuid (sh_uid_neg, ruid, sh_uid_neg);
1374
1375#elif defined(HAVE_SETEUID)
1376 retval = setegid (rgid);
1377 if (retval == 0)
1378 retval = seteuid (ruid);
1379
1380#elif defined(HAVE_SETREUID)
1381 retval = setregid (egid, rgid);
1382 if (retval == 0)
1383 retval = setreuid (euid, ruid);
1384
1385#else
1386 retval = setgid (rgid);
1387 if (retval == 0)
1388 retval = setuid (ruid);
1389#endif
1390
1391 if (suid_warn_flag == 0)
1392 suid_warn(0);
1393 suid_warn_flag = 0;
1394
1395 SL_REQUIRE(retval == 0, _("retval == 0"));
1396 suid_is_set = SL_FALSE;
1397 SL_IRETURN(SL_ENONE, _("sl_unset_suid"));
1398}
1399
1400
1401/*
1402 * This function saves the uid's.
1403 */
1404int sl_save_uids()
1405{
1406 SL_ENTER(_("sl_save_uids"));
1407 if (uids_are_stored == SL_TRUE)
1408 SL_IRETURN(SL_EREPEAT, _("sl_save_uids"));
1409
1410 ruid_orig = getuid();
1411 rgid_orig = getgid();
1412 egid = getegid();
1413 euid = geteuid();
1414 ruid = ruid_orig;
1415 rgid = rgid_orig;
1416 uids_are_stored = SL_TRUE;
1417
1418 SL_IRETURN(SL_ENONE, _("sl_save_uids"));
1419}
1420
1421/*
1422 * This function drops SUID privileges irrevocably.
1423 * It set the effective uid to the original real uid.
1424 */
1425extern int sh_unix_initgroups2 (uid_t in_pid, gid_t in_gid);
1426int sl_drop_privileges()
1427{
1428 SL_ENTER(_("sl_drop_privileges"));
1429 SL_REQUIRE(uids_are_stored == SL_TRUE, _("uids_are_stored == SL_TRUE"));
1430
1431 SL_REQUIRE(setgid(rgid_orig) == 0, _("setgid(rgid_orig) == 0"));
1432 SL_REQUIRE(sh_unix_initgroups2(ruid_orig, rgid_orig) == 0, _("sh_unix_initgroups2(ruid_orig,rgid_orig) == 0"));
1433 SL_REQUIRE(setuid(ruid_orig) == 0, _("setuid(ruid_orig) == 0"));
1434
1435 /* make sure that setuid(0) fails
1436 */
1437 SL_REQUIRE(setuid(0) < 0, _("setuid(0) < 0"));
1438
1439 euid = ruid_orig;
1440 egid = rgid_orig;
1441 ruid = ruid_orig;
1442 rgid = rgid_orig;
1443
1444 SL_IRETURN(SL_ENONE, _("sl_drop_privileges"));
1445}
1446
1447/*
1448 * Define a policy: Stay root.
1449 * Do nothing if not SUID.
1450 */
1451int sl_policy_get_root()
1452{
1453 SL_ENTER(_("sl_policy_get_root"));
1454 SL_REQUIRE(uids_are_stored == SL_FALSE, _("uids_are_stored == SL_FALSE"));
1455
1456 SL_REQUIRE (sl_save_uids() == SL_ENONE, _("sl_save_uids() == SL_ENONE"));
1457
1458 if (euid != ruid || egid != rgid)
1459 {
1460 SL_REQUIRE(setgid(egid) == 0, _("setgid(egid) == 0"));
1461 SL_REQUIRE(setuid(euid) == 0, _("setuid(euid) == 0"));
1462 SL_REQUIRE(ruid == getuid() && rgid == getgid(),
1463 _("ruid == getuid() && rgid == getgid()"));
1464 ruid = euid;
1465 rgid = egid;
1466 }
1467 suid_is_set = SL_TRUE;
1468 if (euid == 0)
1469 {
1470 SL_REQUIRE(sh_unix_initgroups2(euid, egid) == 0, _("sh_unix_initgroups2(euid,egid) == 0"));
1471 }
1472 SL_IRETURN(SL_ENONE, _("sl_policy_get_root"));
1473}
1474
1475#include <pwd.h>
1476
1477/*
1478 * Define a policy: Get real (irrevocably).
1479 * This function drops SUID privileges irrevocably.
1480 * Do nothing if not SUID (? not true - drops if root).
1481 */
1482
1483int sl_policy_get_real(char * user)
1484{
1485 SL_ENTER(_("sl_policy_get_real"));
1486 SL_REQUIRE(uids_are_stored == SL_FALSE, _("uids_are_stored == SL_FALSE"));
1487 SL_REQUIRE (sl_save_uids() == SL_ENONE, _("sl_save_uids() == SL_ENONE"));
1488
1489 if (euid == 0 || ruid == 0)
1490 {
1491#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
1492 struct passwd pwd;
1493 char * buffer;
1494 struct passwd * tempres;
1495 buffer = malloc(SH_PWBUF_SIZE);
1496 SL_REQUIRE (buffer != NULL, _("buffer != NULL"));
1497 sh_getpwnam_r(user, &pwd, buffer, SH_PWBUF_SIZE, &tempres);
1498#else
1499 struct passwd * tempres = sh_getpwnam(user);
1500#endif
1501
1502 SL_REQUIRE (NULL != tempres, _("tempres != NULL"));
1503
1504 rgid_orig = tempres->pw_gid;
1505 ruid_orig = tempres->pw_uid;
1506#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
1507 free(buffer);
1508#endif
1509 }
1510 else
1511 {
1512 rgid_orig = rgid;
1513 ruid_orig = ruid;
1514 }
1515
1516 SL_REQUIRE (sl_drop_privileges() == SL_ENONE,
1517 _("sl_drop_privileges() == SL_ENONE"));
1518
1519 suid_is_set = SL_TRUE;
1520 SL_IRETURN(SL_ENONE, _("sl_policy_get_real"));
1521}
1522
1523
1524/*
1525 * Define a policy: Get user.
1526 * Drops privileges.
1527 * Do nothing if not SUID.
1528 */
1529int sl_policy_get_user(const char * user)
1530{
1531 SL_ENTER(_("sl_policy_get_user"));
1532
1533 SL_REQUIRE(user != NULL, _("user != NULL"));
1534 SL_REQUIRE(uids_are_stored == SL_FALSE, _("uids_are_stored == SL_FALSE"));
1535 SL_REQUIRE (sl_save_uids() == SL_ENONE, _("sl_save_uids() == SL_ENONE"));
1536
1537 if (euid != ruid || egid != rgid)
1538 {
1539#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
1540 struct passwd pwd;
1541 char * buffer;
1542 struct passwd * tempres;
1543 buffer = malloc(SH_PWBUF_SIZE);
1544 SL_REQUIRE (buffer != NULL, _("buffer != NULL"));
1545 sh_getpwnam_r(user, &pwd, buffer, SH_PWBUF_SIZE, &tempres);
1546#else
1547 struct passwd * tempres = sh_getpwnam(user);
1548#endif
1549
1550 SL_REQUIRE (NULL != tempres, _("tempres != NULL"));
1551
1552 SL_REQUIRE (sl_drop_privileges() == SL_ENONE,
1553 _("sl_drop_privileges() == SL_ENONE"));
1554#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
1555 free(buffer);
1556#endif
1557 }
1558 SL_IRETURN(SL_ENONE, _("sl_policy_get_user"));
1559}
1560
1561
1562
1563/* ----------------------------------------------------------------
1564 *
1565 * File access routines
1566 *
1567 * ---------------------------------------------------------------- */
1568
1569#define TOFFSET 0x1234
1570
1571/* this would prevent opening files if the first 16 fds are open :( */
1572/* #define MAXFD FOPEN_MAX */
1573
1574#define MAXFD 1024
1575
1576typedef struct openfiles {
1577 SL_TICKET ticket; /* The unique ID. */
1578 int fd; /* The file descriptor. */
1579 FILE * stream; /* The file descriptor. */
1580 char * path; /* The file path. */
1581 int flush; /* Whether we want to flush the cache */
1582 char ofile[SL_OFILE_SIZE]; /* origin file */
1583 int oline; /* origin line */
1584 sh_string * content; /* The file content */
1585} SL_OFILE;
1586
1587static SL_OFILE * ofiles[MAXFD];
1588
1589static char stale_orig_file[64] = { '\0' };
1590static int stale_orig_line = -1;
1591static char stale_orig_mesg[128];
1592
1593static char badfd_orig_file[64] = { '\0' };
1594static int badfd_orig_line = -1;
1595static char badfd_orig_mesg[128];
1596
1597SH_MUTEX_STATIC(mutex_ticket, PTHREAD_MUTEX_INITIALIZER);
1598
1599static unsigned int nonce_counter = TOFFSET;
1600
1601char * sl_check_stale()
1602{
1603 if (stale_orig_line == -1)
1604 return NULL;
1605 sl_snprintf(stale_orig_mesg, sizeof(stale_orig_mesg),
1606 _("stale handle, %s, %d"), stale_orig_file, stale_orig_line);
1607 stale_orig_file[0] = '\0';
1608 stale_orig_line = -1;
1609 return stale_orig_mesg;
1610}
1611
1612char * sl_check_badfd()
1613{
1614 if (badfd_orig_line == -1)
1615 return NULL;
1616 sl_snprintf(badfd_orig_mesg, sizeof(badfd_orig_mesg),
1617 _("close on file descriptor with allocated handle, %s, %d"),
1618 badfd_orig_file, badfd_orig_line);
1619 badfd_orig_file[0] = '\0';
1620 badfd_orig_line = -1;
1621 return badfd_orig_mesg;
1622}
1623
1624static
1625SL_TICKET sl_create_ticket (unsigned int myindex)
1626{
1627 unsigned int high; /* index */
1628 unsigned int low; /* nonce */
1629 SL_TICKET retval = SL_EINTERNAL;
1630
1631 SL_ENTER(_("sl_create_ticket"));
1632
1633 if (myindex >= MAXFD)
1634 {
1635 retval = SL_EINTERNAL01;
1636 goto out_ticket;
1637 }
1638
1639 /* mask out the high bit and check that it is not used
1640 * -> verify that it fits into 16 bits as positive
1641 */
1642 high = (myindex + TOFFSET) & 0x7fff;
1643
1644 if (high != myindex + TOFFSET)
1645 {
1646 retval = SL_EINTERNAL02;
1647 goto out_ticket;
1648 }
1649
1650 SH_MUTEX_LOCK_UNSAFE(mutex_ticket);
1651
1652 low = nonce_counter & 0xffff;
1653
1654 /* Overflow -> nonce too big.
1655 */
1656 if ((low != nonce_counter++) || low == 0)
1657 {
1658 retval = SL_EINTERNAL03;
1659 goto out_ticket;
1660 }
1661
1662 /* Wrap around the nonce counter.
1663 * This is a dirty trick.
1664 */
1665 if (nonce_counter > 0x7fff)
1666 nonce_counter = TOFFSET;
1667
1668 retval = (SL_TICKET) ((high << 16) | low);
1669
1670 out_ticket:
1671 ;
1672
1673 SH_MUTEX_UNLOCK_UNSAFE(mutex_ticket);
1674 SL_RETURN (retval, _("sl_create_ticket"));
1675}
1676
1677static
1678int sl_read_ticket (SL_TICKET fno)
1679{
1680 register unsigned myindex;
1681 register SL_OFILE *of;
1682
1683 myindex = ((fno >> 16) & 0xffff) - TOFFSET;
1684 if (myindex >= MAXFD)
1685 return (SL_ETICKET);
1686
1687 if (ofiles[myindex] == NULL)
1688 return (SL_ETICKET);
1689
1690 if (ofiles[myindex]->ticket != fno)
1691 return (SL_ETICKET);
1692
1693 if ((of = ofiles[myindex])->fd < 0 || of->fd >= MAXFD )
1694 return (SL_EINTERNAL04);
1695
1696 if (((of->ticket) & 0xffff) == 0)
1697 return (SL_EINTERNAL05);
1698
1699 return (myindex);
1700}
1701
1702SL_TICKET sl_make_ticket (const char * ofile, int oline,
1703 int fd, const char * filename, FILE * stream)
1704{
1705 size_t len;
1706 SL_TICKET ticket;
1707 SL_ENTER(_("sl_make_ticket"));
1708 /* Make entry.
1709 */
1710 if (fd >= MAXFD || fd < 0)
1711 {
1712 SL_IRETURN(SL_TOOMANY, _("sl_make_ticket"));
1713 }
1714
1715 if (ofiles[fd] != NULL) /* stale entry */
1716 {
1717 /* SL_IRETURN(SL_EINTERNAL06, _("sl_make_ticket")); */
1718 sl_strlcpy(stale_orig_file, ofiles[fd]->ofile, sizeof(stale_orig_file));
1719 stale_orig_line = ofiles[fd]->oline;
1720
1721 if (ofiles[fd]->content)
1722 sh_string_destroy(&(ofiles[fd]->content));
1723 (void) free (ofiles[fd]->path);
1724 (void) free (ofiles[fd]);
1725 ofiles[fd] = NULL;
1726 }
1727
1728 if ( (ofiles[fd] = (SL_OFILE *) malloc(sizeof(SL_OFILE))) == NULL)
1729 {
1730 SL_IRETURN(SL_EMEM, _("sl_make_ticket"));
1731 }
1732
1733 len = sl_strlen(filename)+1;
1734
1735 if ( (ofiles[fd]->path = (char *) malloc(len) ) == NULL)
1736 {
1737 free (ofiles[fd]);
1738 ofiles[fd] = NULL;
1739 SL_IRETURN(SL_EMEM, _("sl_make_ticket"));
1740 }
1741
1742 /* Get a ticket.
1743 */
1744 ticket = sl_create_ticket((unsigned int)fd);
1745
1746 if (SL_ISERROR(ticket))
1747 {
1748 (void) free (ofiles[fd]->path);
1749 (void) free (ofiles[fd]);
1750 ofiles[fd] = NULL;
1751 SL_IRETURN(ticket, _("sl_make_ticket"));
1752 }
1753
1754 sl_strlcpy (ofiles[fd]->path, filename, len);
1755 ofiles[fd]->ticket = ticket;
1756 ofiles[fd]->fd = fd;
1757 ofiles[fd]->content = NULL;
1758 ofiles[fd]->stream = stream;
1759 ofiles[fd]->flush = SL_FALSE;
1760
1761 sl_strlcpy(ofiles[fd]->ofile, ofile, SL_OFILE_SIZE);
1762 ofiles[fd]->oline = oline;
1763
1764 SL_IRETURN(ticket, _("sl_make_ticket"));
1765}
1766
1767#define SL_OPEN_MIN 113
1768#define SL_OPEN_FOR_READ 113
1769#define SL_OPEN_FOR_WRITE 114
1770#define SL_OPEN_FOR_RDWR 115
1771#define SL_OPEN_FOR_WTRUNC 116
1772#define SL_OPEN_FOR_RWTRUNC 117
1773#define SL_OPEN_SAFE_RDWR 118
1774#define SL_OPEN_FOR_FASTREAD 119
1775#define SL_OPEN_MAX 119
1776
1777#if !defined(O_NOATIME)
1778#if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
1779#define O_NOATIME 01000000
1780#else
1781 /*
1782 * bitwise 'or' with zero does not modify any bit
1783 */
1784#define O_NOATIME 0
1785#endif
1786#endif
1787
1788static int o_noatime = O_NOATIME;
1789static mode_t open_mode = (S_IWUSR|S_IRUSR|S_IRGRP);
1790
1791
1792static
1793int sl_open_file (const char * ofile, int oline,
1794 const char *filename, int mode, int priv)
1795{
1796 struct stat lbuf;
1797 struct stat buf;
1798 int errval = 0;
1799 int lstat_return;
1800 int stat_return;
1801 int fd;
1802 int sflags;
1803 size_t len;
1804 SL_TICKET ticket;
1805
1806#if !defined(O_NONBLOCK)
1807#if defined(O_NDELAY)
1808#define O_NONBLOCK O_NDELAY
1809#else
1810#define O_NONBLOCK 0
1811#endif
1812#endif
1813
1814 SL_ENTER(_("sl_open_file"));
1815
1816 if (filename == NULL)
1817 SL_IRETURN(SL_ENULL, _("sl_open_file"));
1818 if (mode < SL_OPEN_MIN || mode > SL_OPEN_MAX)
1819 SL_IRETURN(SL_EINTERNAL07, _("sl_open_file"));
1820
1821 /* "This system call always succeeds and the previous value of
1822 * the mask is returned."
1823 */
1824 (void) umask (0);
1825
1826 if (mode == SL_OPEN_FOR_FASTREAD)
1827 {
1828 fd = aud_open_noatime (FIL__, __LINE__, priv, filename,
1829 O_RDONLY|O_NONBLOCK, 0, &o_noatime);
1830 /*
1831 if (fd >= 0) {
1832 sflags = retry_fcntl(FIL__, __LINE__, fd, F_GETFL, 0);
1833 retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags & ~O_NONBLOCK);
1834 }
1835 */
1836 if (fd < 0)
1837 SL_IRETURN(SL_EBADFILE, _("sl_open_file"));
1838 goto createTicket;
1839 }
1840
1841#ifdef USE_SUID
1842 if (priv == SL_YESPRIV)
1843 sl_set_suid();
1844#endif
1845 if (mode == SL_OPEN_FOR_READ)
1846 lstat_return = retry_stat (FIL__, __LINE__, filename, &lbuf);
1847 else
1848 lstat_return = retry_lstat(FIL__, __LINE__, filename, &lbuf);
1849 errval = errno;
1850#ifdef USE_SUID
1851 if (priv == SL_YESPRIV)
1852 sl_unset_suid();
1853#endif
1854
1855 if (lstat_return == -1)
1856 {
1857 lstat_return = ENOENT;
1858 if ( (mode == SL_OPEN_FOR_READ && lstat_return == ENOENT) ||
1859 (errval != ENOENT))
1860 {
1861 TPT(( 0, FIL__, __LINE__, _("msg=<lstat: %s> errno=<%d>\n"),
1862 filename, errval));
1863 errno = errval;
1864 SL_IRETURN(SL_ESTAT, _("sl_open_file"));
1865 }
1866 }
1867
1868 if ( (mode != SL_OPEN_FOR_READ) && (lstat_return != ENOENT) &&
1869 ( S_ISDIR(lbuf.st_mode) || (S_IWOTH & lbuf.st_mode) )
1870 )
1871 {
1872 int retval = S_ISDIR(lbuf.st_mode) ? SL_EISDIR : SL_EBADOTH;
1873 errno = 0;
1874 SL_IRETURN(retval, _("sl_open_file"));
1875 }
1876
1877 /* O_NOATIME has an effect for read(). But write() ?.
1878 */
1879 switch (mode)
1880 {
1881 case SL_OPEN_FOR_READ:
1882 fd = aud_open_noatime (FIL__, __LINE__, priv, filename,
1883 O_RDONLY|O_NONBLOCK, 0, &o_noatime);
1884 errval = errno;
1885 if (fd >= 0) {
1886 sflags = retry_fcntl(FIL__, __LINE__, fd, F_GETFL, 0);
1887 retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags & ~O_NONBLOCK);
1888 }
1889 break;
1890 case SL_OPEN_FOR_WRITE:
1891 if (lstat_return == ENOENT)
1892 fd = aud_open (FIL__, __LINE__, priv, filename,
1893 O_WRONLY|O_CREAT|O_EXCL, open_mode);
1894 else
1895 fd = aud_open (FIL__, __LINE__, priv, filename,
1896 O_WRONLY, open_mode);
1897 errval = errno;
1898 break;
1899 case SL_OPEN_SAFE_RDWR:
1900 if (lstat_return == ENOENT)
1901 {
1902 fd = aud_open (FIL__, __LINE__, priv, filename,
1903 O_RDWR|O_CREAT|O_EXCL, open_mode);
1904 errval = errno;
1905 }
1906 else
1907 {
1908 errno = errval;
1909 SL_IRETURN(SL_EBADFILE, _("sl_open_file"));
1910 }
1911 break;
1912 case SL_OPEN_FOR_RDWR:
1913 if (lstat_return == ENOENT)
1914 fd = aud_open (FIL__, __LINE__, priv, filename,
1915 O_RDWR|O_CREAT|O_EXCL, open_mode);
1916 else
1917 fd = aud_open (FIL__, __LINE__, priv, filename,
1918 O_RDWR, open_mode);
1919 errval = errno;
1920 break;
1921 case SL_OPEN_FOR_WTRUNC:
1922 if (lstat_return == ENOENT)
1923 fd = aud_open (FIL__, __LINE__, priv, filename,
1924 O_WRONLY|O_CREAT|O_EXCL, open_mode);
1925 else
1926 fd = aud_open (FIL__, __LINE__, priv, filename,
1927 O_WRONLY|O_TRUNC, open_mode);
1928 errval = errno;
1929 break;
1930 case SL_OPEN_FOR_RWTRUNC:
1931 if (lstat_return == ENOENT)
1932 fd = aud_open (FIL__, __LINE__, priv, filename,
1933 O_RDWR|O_CREAT|O_EXCL, open_mode);
1934 else
1935 fd = aud_open (FIL__, __LINE__, priv, filename,
1936 O_RDWR|O_TRUNC, open_mode);
1937 errval = errno;
1938 break;
1939 default:
1940 errno = 0;
1941 SL_IRETURN(SL_EINTERNAL08, _("sl_open_file"));
1942 }
1943
1944 if (fd < 0)
1945 {
1946 TPT(( 0, FIL__, __LINE__, _("msg=<Error opening: %s> errno=<%d>\n"),
1947 filename, errval));
1948 errno = errval;
1949 SL_IRETURN(SL_EBADFILE, _("sl_open_file"));
1950 }
1951
1952#ifdef USE_SUID
1953 if (priv == SL_YESPRIV)
1954 sl_set_suid();
1955#endif
1956 stat_return = retry_fstat(FIL__, __LINE__, fd, &buf);
1957 errval = errno;
1958#ifdef USE_SUID
1959 if (priv == SL_YESPRIV)
1960 sl_unset_suid();
1961#endif
1962
1963 if (stat_return < 0)
1964 {
1965 sl_close_fd (FIL__, __LINE__, fd);
1966 errno = errval;
1967 SL_IRETURN(SL_EFSTAT, _("sl_open_file"));
1968 }
1969
1970 errno = 0;
1971
1972 if (lstat_return != ENOENT && buf.st_ino != lbuf.st_ino)
1973 {
1974 sl_close_fd (FIL__, __LINE__, fd);
1975 SL_IRETURN(SL_EBOGUS, _("sl_open_file"));
1976 }
1977
1978 createTicket:
1979
1980 /* Make entry.
1981 */
1982 if (fd >= MAXFD)
1983 {
1984 sl_close_fd(FIL__, __LINE__, fd);
1985 SL_IRETURN(SL_TOOMANY, _("sl_open_file"));
1986 }
1987
1988 if (ofiles[fd] != NULL) /* stale entry */
1989 {
1990 /*
1991 sl_close_fd(FIL__, __LINE__, fd);
1992 SL_IRETURN(SL_EINTERNAL09, _("sl_open_file"));
1993 */
1994 sl_strlcpy(stale_orig_file, ofiles[fd]->ofile, sizeof(stale_orig_file));
1995 stale_orig_line = ofiles[fd]->oline;
1996
1997 if (ofiles[fd]->content)
1998 sh_string_destroy(&(ofiles[fd]->content));
1999 (void) free (ofiles[fd]->path);
2000 (void) free (ofiles[fd]);
2001 ofiles[fd] = NULL;
2002 }
2003
2004 if ( (ofiles[fd] = (SL_OFILE *) malloc(sizeof(SL_OFILE))) == NULL)
2005 {
2006 sl_close_fd(FIL__, __LINE__, fd);
2007 SL_IRETURN(SL_EMEM, _("sl_open_file"));
2008 }
2009
2010 len = sl_strlen(filename)+1;
2011
2012 if ( (ofiles[fd]->path = (char *) malloc(len) ) == NULL)
2013 {
2014 free (ofiles[fd]);
2015 ofiles[fd] = NULL;
2016 sl_close_fd(FIL__, __LINE__, fd);
2017 SL_IRETURN(SL_EMEM, _("sl_open_file"));
2018 }
2019
2020 /* Get a ticket.
2021 */
2022 ticket = sl_create_ticket(fd);
2023
2024 if (SL_ISERROR(ticket))
2025 {
2026 (void) free (ofiles[fd]->path);
2027 (void) free (ofiles[fd]);
2028 ofiles[fd] = NULL;
2029 sl_close_fd(FIL__, __LINE__, fd);
2030 SL_IRETURN(ticket, _("sl_open_file"));
2031 }
2032
2033 sl_strlcpy (ofiles[fd]->path, filename, len);
2034 ofiles[fd]->ticket = ticket;
2035 ofiles[fd]->fd = fd;
2036 ofiles[fd]->content = NULL;
2037 ofiles[fd]->stream = NULL;
2038 ofiles[fd]->flush = SL_FALSE;
2039
2040 sl_strlcpy(ofiles[fd]->ofile, ofile, SL_OFILE_SIZE);
2041 ofiles[fd]->oline = oline;
2042
2043 SL_IRETURN(ticket, _("sl_open_file"));
2044}
2045
2046FILE * sl_stream (SL_TICKET ticket, char * mode)
2047{
2048 int fd;
2049
2050 if (SL_ISERROR(fd = sl_read_ticket(ticket)))
2051 return (NULL);
2052
2053 if (ofiles[fd] == NULL || fd != ofiles[fd]->fd ||
2054 ticket != ofiles[fd]->ticket || fd < 0)
2055 return (NULL);
2056
2057 if (!ofiles[fd]->stream)
2058 ofiles[fd]->stream = fdopen(fd, mode);
2059
2060 return ofiles[fd]->stream;
2061}
2062
2063int get_the_fd (SL_TICKET ticket)
2064{
2065 int fd;
2066
2067 if (SL_ISERROR(fd = sl_read_ticket(ticket)))
2068 return (fd);
2069
2070 if (ofiles[fd] == NULL || fd != ofiles[fd]->fd ||
2071 ticket != ofiles[fd]->ticket || fd < 0)
2072 return (SL_EINTERNAL10);
2073
2074 return (fd);
2075}
2076
2077static
2078int check_fname_priv (const char * fname, int priv)
2079{
2080 SL_ENTER(_("check_fname_priv"));
2081 if (fname == NULL)
2082 SL_IRETURN(SL_ENULL, _("check_fname_priv"));
2083 if (priv != SL_YESPRIV && priv != SL_NOPRIV)
2084 SL_IRETURN(SL_EINTERNAL11, _("check_fname_priv"));
2085 SL_IRETURN(SL_ENONE, _("check_fname_priv"));
2086}
2087
2088SL_TICKET sl_open_write (const char * ofile, int oline,
2089 const char * fname, int priv)
2090{
2091 long status;
2092 SL_ENTER(_("sl_open_write"));
2093
2094 if (SL_ENONE != (status = check_fname_priv (fname, priv)))
2095 SL_IRETURN(status, _("sl_open_write"));
2096
2097 status = sl_open_file(ofile, oline, fname, SL_OPEN_FOR_WRITE, priv);
2098 SL_IRETURN(status, _("sl_open_write"));
2099}
2100
2101SL_TICKET sl_open_read (const char * ofile, int oline,
2102 const char * fname, int priv)
2103{
2104 long status;
2105 SL_ENTER(_("sl_open_read"));
2106
2107 if (SL_ENONE != (status = check_fname_priv (fname, priv)))
2108 {
2109 TPT(( 0, FIL__, __LINE__,
2110 _("msg=<Error in check_fname_priv.> status=<%ld>\n"),
2111 status));
2112 SL_IRETURN(status, _("sl_open_read"));
2113 }
2114
2115 status = sl_open_file(ofile, oline, fname, SL_OPEN_FOR_READ, priv);
2116 SL_IRETURN(status, _("sl_open_read"));
2117}
2118
2119#if defined(HAVE_POSIX_FADVISE) && defined(HAVE_MINCORE) && defined(POSIX_FADV_DONTNEED)
2120static int sl_check_mincore(int fd)
2121{
2122 /* Idea from Tobias Oetiker (http://insights.oetiker.ch/linux/fadvise.html)
2123 */
2124 struct stat fbuf;
2125 int retval = -1;
2126
2127 if (0 == fstat(fd, &fbuf))
2128 {
2129 void *f_map;
2130
2131 f_map = mmap((void *)0, fbuf.st_size, PROT_NONE, MAP_SHARED, fd, 0);
2132 if (MAP_FAILED != f_map)
2133 {
2134 extern int sh_unix_pagesize(void);
2135 size_t i;
2136 size_t page_size = sh_unix_pagesize();
2137 size_t vec_size = (fbuf.st_size+page_size-1)/page_size;
2138 unsigned char * vec = calloc(1, vec_size);
2139
2140 if (vec)
2141 {
2142 mincore(f_map, fbuf.st_size, vec);
2143 /* imax = fbuf.st_size/page_size; */
2144 for (i = 0; i <= vec_size; ++i)
2145 {
2146 if (vec[i]&1)
2147 {
2148 goto incore;
2149 }
2150 }
2151 retval = 0;
2152 incore:
2153 free(vec);
2154 }
2155 munmap(f_map, fbuf.st_size);
2156 }
2157 }
2158 return retval;
2159}
2160#endif
2161
2162static int sl_drop_cache = SL_FALSE;
2163
2164int sl_set_drop_cache(const char * str)
2165{
2166 extern int sh_util_flagval(const char * c, int * fval);
2167 return sh_util_flagval(str, &sl_drop_cache);
2168}
2169
2170SL_TICKET sl_open_fastread (const char * ofile, int oline,
2171 const char * fname, int priv)
2172{
2173 long status;
2174 SL_ENTER(_("sl_open_fastread"));
2175
2176 if (SL_ENONE != (status = check_fname_priv (fname, priv)))
2177 SL_IRETURN(status, _("sl_open_read"));
2178
2179 status = sl_open_file(ofile, oline, fname, SL_OPEN_FOR_FASTREAD, priv);
2180
2181#if defined(HAVE_POSIX_FADVISE) && defined(HAVE_MINCORE) && defined(POSIX_FADV_DONTNEED)
2182
2183 if (SL_FALSE != sl_drop_cache && !SL_ISERROR(status))
2184 {
2185 int fd = get_the_fd(status);
2186 if (fd >= 0)
2187 {
2188 if (0 == sl_check_mincore(fd))
2189 ofiles[fd]->flush = SL_TRUE;
2190 }
2191 }
2192
2193#endif
2194
2195 SL_IRETURN(status, _("sl_open_fastread"));
2196}
2197
2198SL_TICKET sl_open_rdwr (const char * ofile, int oline,
2199 const char * fname, int priv)
2200{
2201 long status;
2202 SL_ENTER(_("sl_open_rdwr"));
2203
2204 if (SL_ENONE != (status = check_fname_priv (fname, priv)))
2205 SL_IRETURN(status, _("sl_open_rdwr"));
2206
2207 status = sl_open_file(ofile, oline, fname, SL_OPEN_FOR_RDWR, priv);
2208 SL_IRETURN(status, _("sl_open_rdwr"));
2209}
2210
2211SL_TICKET sl_open_safe_rdwr (const char * ofile, int oline,
2212 const char * fname, int priv)
2213{
2214 long status;
2215 SL_ENTER(_("sl_open_safe_rdwr"));
2216
2217 if (SL_ENONE != (status = check_fname_priv (fname, priv)))
2218 SL_IRETURN(status, _("sl_open_safe_rdwr"));
2219
2220 status = sl_open_file(ofile, oline, fname, SL_OPEN_SAFE_RDWR, priv);
2221 SL_IRETURN(status, _("sl_open_safe_rdwr"));
2222}
2223
2224SL_TICKET sl_open_write_trunc (const char * ofile, int oline,
2225 const char * fname, int priv)
2226{
2227 long status;
2228 SL_ENTER(_("sl_open_write_trunc"));
2229
2230 if (SL_ENONE != (status = check_fname_priv (fname, priv)))
2231 SL_IRETURN(status, _("sl_open_write_trunc"));
2232
2233 status = sl_open_file(ofile, oline, fname, SL_OPEN_FOR_WTRUNC, priv);
2234 SL_IRETURN(status, _("sl_open_write_trunc"));
2235}
2236
2237SL_TICKET sl_open_rdwr_trunc (const char * ofile, int oline,
2238 const char * fname, int priv)
2239{
2240 long status;
2241 SL_ENTER(_("sl_open_rdwr_trunc"));
2242
2243 if (SL_ENONE != (status = check_fname_priv (fname, priv)))
2244 SL_IRETURN(status, _("sl_open_rdwr_trunc"));
2245
2246 status = sl_open_file(ofile, oline, fname, SL_OPEN_FOR_RWTRUNC, priv);
2247 SL_IRETURN(status, _("sl_open_rdwr_trunc"));
2248}
2249
2250
2251int sl_init_content (SL_TICKET ticket, size_t size)
2252{
2253 int fd;
2254
2255 if (SL_ISERROR(fd = sl_read_ticket(ticket)))
2256 return (fd);
2257
2258 if (ofiles[fd] == NULL || fd != ofiles[fd]->fd ||
2259 ticket != ofiles[fd]->ticket || fd < 0)
2260 return (SL_EINTERNAL12);
2261
2262 if (ofiles[fd]->content)
2263 sh_string_destroy(&(ofiles[fd]->content));
2264 ofiles[fd]->content = sh_string_new(size);
2265
2266 return SL_ENONE;
2267}
2268
2269sh_string * sl_get_content (SL_TICKET ticket)
2270{
2271 int fd;
2272
2273 if (SL_ISERROR(fd = sl_read_ticket(ticket)))
2274 return (NULL);
2275
2276 if (ofiles[fd] == NULL || fd != ofiles[fd]->fd ||
2277 ticket != ofiles[fd]->ticket || fd < 0)
2278 return (NULL);
2279
2280 return (ofiles[fd]->content);
2281}
2282
2283int sl_lock (SL_TICKET ticket)
2284{
2285 int fd;
2286 struct flock lock;
2287 int retval;
2288
2289 SL_ENTER(_("sl_lock"));
2290
2291 if (SL_ISERROR(fd = get_the_fd (ticket)))
2292 SL_IRETURN(fd, _("sl_lock"));
2293
2294 lock.l_type = F_WRLCK;
2295 lock.l_whence = SEEK_SET;
2296 lock.l_start = 0;
2297 lock.l_len = 0;
2298
2299 /* F_SETLK returns if the lock cannot be obtained */
2300 do {
2301 retval = fcntl(fd, F_SETLK, &lock);
2302 } while (retval < 0 && errno == EINTR);
2303
2304 if (retval < 0 && errno == EBADF)
2305 SL_IRETURN(SL_ETICKET, _("sl_lock"));
2306 else if (retval < 0)
2307 SL_IRETURN(SL_EBADFILE, _("sl_lock"));
2308 else
2309 SL_IRETURN(SL_ENONE, _("sl_lock"));
2310 }
2311
2312int sl_close (SL_TICKET ticket)
2313{
2314 register int fd;
2315 FILE * fp = NULL;
2316
2317 SL_ENTER(_("sl_close"));
2318
2319 if (SL_ISERROR(fd = get_the_fd (ticket)))
2320 SL_IRETURN(fd, _("sl_close"));
2321
2322 if (ofiles[fd] != NULL)
2323 {
2324#if defined(HAVE_POSIX_FADVISE) && defined(HAVE_MINCORE) && defined(POSIX_FADV_DONTNEED)
2325 if (ofiles[fd]->flush == SL_TRUE)
2326 {
2327 posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
2328 }
2329#endif
2330 if (ofiles[fd]->content)
2331 sh_string_destroy(&(ofiles[fd]->content));
2332 (void) free (ofiles[fd]->path);
2333 fp = ofiles[fd]->stream;
2334 (void) free (ofiles[fd]);
2335 ofiles[fd] = NULL;
2336 }
2337
2338 /* This may fail, but what to do then ?
2339 */
2340 if (fp)
2341 {
2342 if (0 != fclose (fp)) /* within sl_close */
2343 {
2344 TPT((0, FIL__, __LINE__,
2345 _("msg=<Error fclosing file.>, fd=<%d>, err=<%s>\n"),
2346 fd, strerror(errno)));
2347 }
2348 }
2349 else
2350 {
2351 if (0 != close(fd)) /* within sl_close */
2352 {
2353 TPT((0, FIL__, __LINE__,
2354 _("msg=<Error closing file.>, fd=<%d>, err=<%s>\n"),
2355 fd, strerror(errno)));
2356 }
2357 }
2358
2359 SL_IRETURN(SL_ENONE, _("sl_close"));
2360}
2361
2362int sl_close_fd (const char * file, int line, int fd)
2363{
2364 int ret = -1;
2365
2366 SL_ENTER(_("sl_close_fd"));
2367
2368 if (fd >= 0 && fd < MAXFD && ofiles[fd] != NULL) /* stale ofiles[fd] handle */
2369 {
2370 sl_strlcpy(badfd_orig_file, file, sizeof(badfd_orig_file));
2371 badfd_orig_line = line;
2372 }
2373
2374 ret = close(fd); /* within sl_close_fd wrapper */
2375
2376 SL_IRETURN(ret, _("sl_close_fd"));
2377}
2378
2379int sl_fclose (const char * file, int line, FILE * fp)
2380{
2381 int ret = -1;
2382 int fd;
2383
2384 SL_ENTER(_("sl_fclose"));
2385
2386 fd = fileno(fp);
2387
2388 if (fd >= 0 && fd < MAXFD && ofiles[fd] != NULL) /* stale ofiles[fd] handle */
2389 {
2390 sl_strlcpy(badfd_orig_file, file, sizeof(badfd_orig_file));
2391 badfd_orig_line = line;
2392 }
2393
2394 ret = fclose(fp); /* within sl_fclose wrapper */
2395
2396 SL_IRETURN(ret, _("sl_fclose"));
2397}
2398
2399int sl_dropall(int fd, int except)
2400{
2401 while (fd < MAXFD)
2402 {
2403 if (ofiles[fd] != NULL && fd != except)
2404 {
2405 if (ofiles[fd]->content)
2406 sh_string_destroy(&(ofiles[fd]->content));
2407 if (ofiles[fd]->path != NULL)
2408 (void) free (ofiles[fd]->path);
2409 (void) free (ofiles[fd]);
2410 ofiles[fd] = NULL;
2411 }
2412 ++fd;
2413 }
2414 return 0;
2415}
2416
2417int sl_dropall_dirty(int fd, int except)
2418{
2419 while (fd < MAXFD)
2420 {
2421 if (ofiles[fd] != NULL && fd != except)
2422 {
2423 ofiles[fd] = NULL;
2424 }
2425 ++fd;
2426 }
2427 return 0;
2428}
2429
2430
2431int sl_unlink (SL_TICKET ticket)
2432{
2433 register int fd;
2434
2435 SL_ENTER(_("sl_unlink"));
2436
2437 if (SL_ISERROR(fd = get_the_fd(ticket)))
2438 SL_IRETURN(fd, _("sl_unlink"));
2439
2440 if (retry_aud_unlink(FIL__, __LINE__, ofiles[fd]->path) < 0)
2441 SL_IRETURN(SL_EUNLINK, _("sl_unlink"));
2442
2443 SL_IRETURN(SL_ENONE, _("sl_unlink"));
2444}
2445
2446
2447int sl_seek (SL_TICKET ticket, off_t off_data)
2448{
2449 register int fd;
2450
2451 SL_ENTER(_("sl_seek"));
2452
2453 if (SL_ISERROR(fd = get_the_fd(ticket)))
2454 SL_IRETURN(fd, _("sl_seek"));
2455
2456 if (lseek(fd, off_data, SEEK_SET) == (off_t)-1)
2457 SL_IRETURN(SL_EREWIND, _("sl_seek"));
2458
2459 SL_IRETURN(SL_ENONE, _("sl_seek"));
2460}
2461
2462int sl_rewind (SL_TICKET ticket)
2463{
2464 register int fd;
2465
2466 SL_ENTER(_("sl_rewind"));
2467
2468 if (SL_ISERROR(fd = get_the_fd(ticket)))
2469 SL_IRETURN(fd, _("sl_rewind"));
2470
2471 if (lseek (fd, 0L, SEEK_SET) == (off_t)-1)
2472 SL_IRETURN(SL_EREWIND, _("sl_rewind"));
2473
2474 SL_IRETURN(SL_ENONE, _("sl_rewind"));
2475}
2476
2477int sl_forward (SL_TICKET ticket)
2478{
2479 register int fd;
2480
2481 SL_ENTER(_("sl_forward"));
2482
2483 if (SL_ISERROR(fd = get_the_fd(ticket)))
2484 SL_IRETURN(fd, _("sl_forward"));
2485
2486 if (lseek (fd, 0L, SEEK_END) == (off_t)-1)
2487 SL_IRETURN(SL_EFORWARD, _("sl_forward"));
2488
2489 SL_IRETURN(SL_ENONE, _("sl_forward"));
2490}
2491
2492
2493int sl_sync (SL_TICKET ticket)
2494{
2495 register int fd;
2496
2497 SL_ENTER(_("sl_sync"));
2498
2499 if (SL_ISERROR(fd = get_the_fd(ticket)))
2500 SL_IRETURN(fd, _("sl_sync"));
2501
2502 if (fsync (fd) == -1)
2503 SL_IRETURN(SL_ESYNC, _("sl_sync"));
2504
2505 SL_IRETURN(SL_ENONE, _("sl_sync"));
2506}
2507
2508int sl_read_timeout_prep (SL_TICKET ticket)
2509{
2510 int fd;
2511 int sflags;
2512
2513 SL_ENTER(_("sl_read_timeout_prep"));
2514
2515 if (SL_ISERROR(fd = get_the_fd(ticket)))
2516 {
2517 TPT(( 0, FIL__, __LINE__, _("msg=<ticket error> errno=<%d>"), fd));
2518 SL_IRETURN(fd, _("sl_read_timeout_prep"));
2519 }
2520
2521 /* set to non-blocking mode
2522 */
2523 sflags = retry_fcntl(FIL__, __LINE__, fd, F_GETFL, 0);
2524 retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags | O_NONBLOCK);
2525
2526 SL_IRETURN(SL_ENONE, _("sl_read_timeout_prep"));
2527}
2528
2529
2530int sl_read_timeout_fd (int fd, void * buf_in, size_t count,
2531 int timeout, int is_nonblocking)
2532{
2533 int sflags = 0;
2534 fd_set readfds;
2535 struct timeval tv;
2536 /* int sflags; */
2537 int retval;
2538 int error;
2539
2540 int byteread = 0;
2541 int bytes = 0;
2542 char * buf;
2543
2544 time_t tnow;
2545 time_t tstart;
2546 time_t tdiff;
2547 extern volatile int sig_termfast;
2548
2549 if (is_nonblocking == SL_FALSE)
2550 {
2551 /* set to non-blocking mode
2552 */
2553 sflags = retry_fcntl(FIL__, __LINE__, fd, F_GETFL, 0);
2554 retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags | O_NONBLOCK);
2555 }
2556
2557 buf = (char *) buf_in;
2558
2559 tstart = time(NULL);
2560 tdiff = 0;
2561
2562 while (count > 0)
2563 {
2564 FD_ZERO(&readfds);
2565 FD_SET(fd, &readfds);
2566
2567 tv.tv_sec = timeout - tdiff;
2568 tv.tv_usec = 0;
2569
2570 retval = select (fd+1, &readfds, NULL, NULL, &tv);
2571
2572 if (retval > 0)
2573 {
2574 byteread = read (fd, buf, count);
2575
2576 if (byteread > 0)
2577 {
2578 bytes += byteread; count -= byteread;
2579 buf += byteread;
2580 if (count == 0)
2581 break;
2582 }
2583 else if (byteread == 0)
2584 {
2585 break;
2586 }
2587 else
2588 {
2589 if (errno == EINTR || errno == EAGAIN)
2590 {
2591 retry_msleep(1, 0);
2592 tnow = time(NULL);
2593 tdiff = tnow - tstart;
2594 continue;
2595 }
2596 else
2597 {
2598 error = errno;
2599 if (is_nonblocking == SL_FALSE)
2600 retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags);
2601 TPT(( 0, FIL__, __LINE__, _("msg=<read error>")));
2602 errno = error;
2603 return (SL_EREAD);
2604 }
2605 }
2606 }
2607 else if ((retval == -1) && (errno == EINTR || errno == EAGAIN))
2608 {
2609 retry_msleep(1, 0);
2610 tnow = time(NULL);
2611 tdiff = tnow - tstart;
2612 continue;
2613 }
2614 else if (retval == 0)
2615 {
2616 if (is_nonblocking == SL_FALSE)
2617 retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags);
2618 TPT(( 0, FIL__, __LINE__, _("msg=<timeout>")));
2619 errno = 0;
2620 return (SL_TIMEOUT);
2621 }
2622 else
2623 {
2624 error = errno;
2625 if (is_nonblocking == SL_FALSE)
2626 retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags);
2627 TPT(( 0, FIL__, __LINE__, _("msg=<timeout>")));
2628 errno = error;
2629 return (SL_EREAD);
2630 }
2631
2632 if (sig_termfast == 1)
2633 {
2634 if (is_nonblocking == SL_FALSE)
2635 retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags);
2636 TPT(( 0, FIL__, __LINE__, _("msg=<terminated>")));
2637 errno = 0;
2638 return (SL_EREAD);
2639 }
2640
2641 tnow = time(NULL);
2642 tdiff = tnow - tstart;
2643
2644 if (tdiff > timeout)
2645 {
2646 if (is_nonblocking == SL_FALSE)
2647 retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags);
2648 TPT(( 0, FIL__, __LINE__, _("msg=<timeout>")));
2649 errno = 0;
2650 return (SL_TIMEOUT);
2651 }
2652 }
2653
2654 if (is_nonblocking == SL_FALSE)
2655 retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags);
2656 return ((int) bytes);
2657}
2658
2659int sl_read_timeout (SL_TICKET ticket, void * buf_in, size_t count,
2660 int timeout, int is_nonblocking)
2661{
2662 int fd, retval;
2663
2664 SL_ENTER(_("sl_read_timeout"));
2665
2666 if (buf_in == NULL || SL_ISERROR(fd = get_the_fd(ticket)))
2667 {
2668 if (buf_in == NULL)
2669 {
2670 TPT(( 0, FIL__, __LINE__, _("msg=<null buffer>")));
2671 SL_IRETURN((SL_ENULL), _("sl_read_timeout"));
2672 }
2673 if (SL_ISERROR(fd = get_the_fd(ticket)))
2674 {
2675 TPT(( 0, FIL__, __LINE__, _("msg=<ticket error> errno=<%d>"), fd));
2676 SL_IRETURN((fd), _("sl_read_timeout"));
2677 }
2678 }
2679
2680 retval = sl_read_timeout_fd (fd, buf_in, count, timeout, is_nonblocking);
2681 SL_IRETURN((retval), _("sl_read_timeout"));
2682}
2683
2684
2685int sl_read (SL_TICKET ticket, void * buf_in, size_t count)
2686{
2687 int fd;
2688 int byteread = 0;
2689 int bytes = 0;
2690
2691 char * buf;
2692
2693 SL_ENTER(_("sl_read"));
2694
2695 if (count < 1)
2696 {
2697 TPT(( 0, FIL__, __LINE__, _("msg=<range error>")));
2698 SL_IRETURN((SL_ERANGE), _("sl_read"));
2699 }
2700 if (buf_in == NULL)
2701 {
2702 TPT(( 0, FIL__, __LINE__, _("msg=<null buffer>")));
2703 SL_IRETURN((SL_ENULL), _("sl_read"));
2704 }
2705
2706 if (SL_ISERROR(fd = get_the_fd(ticket)))
2707 {
2708 TPT(( 0, FIL__, __LINE__, _("msg=<ticket error> errno=<%d>"), fd));
2709 SL_IRETURN((fd), _("sl_read"));
2710 }
2711
2712 buf = (char *) buf_in;
2713
2714 do
2715 {
2716 byteread = read (fd, buf, count);
2717 if (byteread > 0)
2718 {
2719 bytes += byteread; count -= byteread;
2720 buf += byteread;
2721 }
2722 } while ( byteread > 0 ||
2723 ( byteread == -1 && (errno == EINTR || errno == EAGAIN))
2724 );
2725
2726
2727 if (byteread == (-1))
2728 {
2729 TPT(( 0, FIL__, __LINE__, _("msg=<read error> errno=<%d>\n"), errno));
2730 SL_IRETURN((SL_EREAD), _("sl_read"));
2731 }
2732 SL_IRETURN((bytes), _("sl_read"));
2733}
2734
2735int sl_read_fast (SL_TICKET ticket, void * buf_in, size_t count)
2736{
2737 int fd;
2738 int byteread = 0;
2739
2740 char * buf;
2741
2742 SL_ENTER(_("sl_read_fast"));
2743
2744 if (count < 1)
2745 {
2746 TPT(( 0, FIL__, __LINE__, _("msg=<range error>")));
2747 SL_IRETURN((SL_ERANGE), _("sl_read_fast"));
2748 }
2749 if (buf_in == NULL)
2750 {
2751 TPT(( 0, FIL__, __LINE__, _("msg=<null buffer>")));
2752 SL_IRETURN((SL_ENULL), _("sl_read_fast"));
2753 }
2754
2755 if (SL_ISERROR(fd = get_the_fd(ticket)))
2756 {
2757 TPT(( 0, FIL__, __LINE__, _("msg=<ticket error> errno=<%d>"), fd));
2758 SL_IRETURN((fd), _("sl_read_fast"));
2759 }
2760
2761 buf = (char *) buf_in;
2762
2763 do
2764 {
2765 byteread = read (fd, buf, count);
2766 if (byteread >= 0)
2767 {
2768 SL_IRETURN((byteread), _("sl_read_fast"));
2769 }
2770 } while ( byteread == -1 && (errno == EINTR || errno == EAGAIN));
2771
2772
2773 if (byteread == (-1))
2774 {
2775 TPT(( 0, FIL__, __LINE__, _("msg=<read error> errno=<%d>\n"), errno));
2776 SL_IRETURN((SL_EREAD), _("sl_read_fast"));
2777 }
2778 SL_IRETURN((0), _("sl_read_fast"));
2779}
2780
2781
2782int sl_write (SL_TICKET ticket, const void * msg_in, long nbytes)
2783{
2784 long bytewritten;
2785 long bytecount;
2786 int fd;
2787
2788 const char * msg;
2789
2790 SL_ENTER(_("sl_write"));
2791
2792 if (nbytes < 1)
2793 SL_IRETURN(SL_ERANGE, _("sl_write"));
2794 if (msg_in == NULL)
2795 SL_IRETURN(SL_ENULL, _("sl_write"));
2796 if (SL_ISERROR(fd = get_the_fd(ticket)))
2797 SL_IRETURN(fd, _("sl_write"));
2798
2799 msg = (const char *) msg_in;
2800
2801 /* write
2802 */
2803 bytecount = 0;
2804 bytewritten = 0;
2805 while (bytecount < nbytes)
2806 {
2807 if ((bytewritten = write (fd, msg, nbytes-bytecount)) > 0)
2808 {
2809 bytecount += bytewritten;
2810 msg += bytewritten; /* move buffer pointer forward */
2811 }
2812 else if (bytewritten <= 0)
2813 {
2814 if ( errno == EINTR || errno == EAGAIN) /* try again */
2815 continue;
2816 else
2817 SL_IRETURN(SL_EWRITE, _("sl_write"));
2818 }
2819 }
2820 SL_IRETURN(SL_ENONE, _("sl_write"));
2821}
2822
2823int sl_write_line (SL_TICKET ticket, const void * msg, long nbytes)
2824{
2825 int status;
2826
2827 SL_ENTER(_("sl_write_line"));
2828
2829 status = sl_write(ticket, msg, nbytes);
2830 if (!SL_ISERROR(status))
2831 status = sl_write(ticket, "\n", 1);
2832
2833 SL_IRETURN(status, _("sl_write_line"));
2834}
2835
2836int sl_write_line_fast (SL_TICKET ticket, void * msg, long nbytes)
2837{
2838 int status;
2839 char * p = (char *) msg;
2840
2841 SL_ENTER(_("sl_write_line_fast"));
2842
2843 /* Here nbytes is strlen(msg), so p[nbytes] is the terminating '\0'
2844 * Overwrite the terminator, write out, then write back the terminator.
2845 */
2846 p[nbytes] = '\n';
2847 status = sl_write(ticket, msg, nbytes+1);
2848 p[nbytes] = '\0';
2849
2850 SL_IRETURN(status, _("sl_write_line_fast"));
2851}
2852
2853
2854/* ----------------------------------------------------------------
2855 *
2856 * Trustfile interface
2857 *
2858 * ---------------------------------------------------------------- */
2859
2860extern uid_t rootonly[];
2861extern int EUIDSLOT;
2862extern int ORIG_EUIDSLOT;
2863
2864extern char tf_path[MAXFILENAME]; /* Error path for trust function. */
2865extern uid_t tf_euid; /* Space for EUID of process. */
2866
2867char * sl_error_string(int errorcode)
2868{
2869
2870 switch (errorcode)
2871 {
2872 case SL_EBOGUS:
2873 return _("Bogus file, modified during access");
2874 case SL_EWRITE:
2875 return _("Write error");
2876 case SL_EREAD:
2877 return _("Read error");
2878 case SL_ESYNC:
2879 return _("Error in fsync()");
2880 case SL_EFORWARD:
2881 return _("Error in lseek()");
2882 case SL_EREWIND:
2883 return _("Error in lseek()");
2884 case SL_EUNLINK:
2885 return _("Error in unlink()");
2886 case SL_EMEM:
2887 return _("Out of memory");
2888 case SL_EINTERNAL:
2889 return _("Internal error");
2890 case SL_EINTERNAL01:
2891 return _("Internal error 01");
2892 case SL_EINTERNAL02:
2893 return _("Internal error 02");
2894 case SL_EINTERNAL03:
2895 return _("Internal error 03");
2896 case SL_EINTERNAL04:
2897 return _("Internal error 04");
2898 case SL_EINTERNAL05:
2899 return _("Internal error 05");
2900 case SL_EINTERNAL06:
2901 return _("Internal error 06");
2902 case SL_EINTERNAL07:
2903 return _("Internal error 07");
2904 case SL_EINTERNAL08:
2905 return _("Internal error 08");
2906 case SL_EINTERNAL09:
2907 return _("Internal error 09");
2908 case SL_EINTERNAL10:
2909 return _("Internal error 10");
2910 case SL_EINTERNAL11:
2911 return _("Internal error 11");
2912 case SL_EINTERNAL12:
2913 return _("Internal error 12");
2914 case SL_ETICKET:
2915 return _("Bad ticket");
2916 case SL_EREPEAT:
2917 return _("Illegal repeated use of function");
2918 case SL_ERANGE:
2919 return _("Argument out of range");
2920 case SL_ENULL:
2921 return _("Dereferenced NULL pointer");
2922
2923 case SL_EBADUID:
2924 return _("Owner not trustworthy");
2925 case SL_EBADGID:
2926 return _("Group writeable and member not trustworthy");
2927 case SL_EBADOTH:
2928 return _("World writeable");
2929 case SL_EISDIR:
2930 return _("Is a directory");
2931 case SL_EBADFILE:
2932 return _("File access error");
2933 case SL_EBADNAME:
2934 return _("Invalid filename (prob. too long or null)");
2935
2936 case SL_ETRUNC:
2937 return _("Truncation occured");
2938 case SL_ESTAT:
2939 return _("stat() failed");
2940 case SL_EFSTAT:
2941 return _("fstat() failed");
2942 default:
2943 return _("Unknown error");
2944 }
2945}
2946
2947
2948
2949char * sl_trust_errfile(void)
2950{
2951 return &tf_path[0];
2952}
2953
2954extern uid_t tf_baduid;
2955uid_t sl_trust_baduid(void)
2956{
2957 return tf_baduid;
2958}
2959
2960extern gid_t tf_badgid;
2961gid_t sl_trust_badgid(void)
2962{
2963 return tf_badgid;
2964}
2965
2966
2967static int trust_count = 0;
2968
2969int sl_trust_purge_user (void)
2970{
2971 int i;
2972
2973 EUIDSLOT = ORIG_EUIDSLOT;
2974 trust_count = 0;
2975
2976 for (i = EUIDSLOT; i < (EUIDSLOT + 15); ++i)
2977 rootonly[i] = sh_uid_neg;
2978 return 0;
2979}
2980
2981int sl_trust_add_user (uid_t pwid)
2982{
2983 SL_ENTER(_("sl_trust_add_user"));
2984
2985 if (trust_count == 15)
2986 SL_IRETURN(SL_ERANGE, _("sl_trust_add_user"));
2987
2988 rootonly[EUIDSLOT] = pwid;
2989 ++EUIDSLOT;
2990 ++trust_count;
2991
2992 SL_IRETURN(SL_ENONE, _("sl_trust_add_user"));
2993}
2994
2995#include "sh_mem.h"
2996extern char * sh_util_strdup (const char * str);
2997
2998struct sl_trustfile_store {
2999 char * filename;
3000 uid_t teuid;
3001 struct sl_trustfile_store * next;
3002};
3003
3004static struct sl_trustfile_store * sl_trusted_files = NULL;
3005
3006static void sl_add_trusted_file(const char * filename, uid_t teuid)
3007{
3008 struct sl_trustfile_store *new = SH_ALLOC(sizeof(struct sl_trustfile_store));
3009
3010 new->filename = sh_util_strdup (filename);
3011 new->teuid = teuid;
3012 new->next = sl_trusted_files;
3013
3014 sl_trusted_files = new;
3015 return;
3016}
3017
3018static const char * sl_check_trusted_file(const char * filename, uid_t teuid)
3019{
3020 struct sl_trustfile_store *new = sl_trusted_files;
3021
3022 while (new)
3023 {
3024 if ((new->teuid == teuid) && (0 == strcmp(new->filename, filename)))
3025 return filename;
3026 new = new->next;
3027 }
3028
3029 return NULL;
3030}
3031
3032static void sl_clear_trusted_file(struct sl_trustfile_store * file)
3033{
3034 if (file)
3035 {
3036 if (file->next != NULL)
3037 sl_clear_trusted_file(file->next);
3038 SH_FREE(file->filename);
3039 SH_FREE(file);
3040 }
3041 return;
3042}
3043
3044int sl_trustfile_euid(const char * filename, uid_t teuid)
3045{
3046 long status;
3047 static time_t old = 0;
3048 static time_t now;
3049
3050 SL_ENTER(_("sl_trustfile_euid"));
3051
3052 tf_path[0] = '\0';
3053 if (filename == NULL || filename[0] == '\0')
3054 SL_IRETURN(SL_EBADNAME, _("sl_trustfile_euid"));
3055
3056 now = time(NULL);
3057 if (now < (old + 300))
3058 {
3059 if (NULL != sl_check_trusted_file(filename, teuid))
3060 {
3061 sl_strlcpy(tf_path, filename, sizeof(tf_path));
3062 SL_IRETURN(SL_ENONE, _("sl_trustfile_euid"));
3063 }
3064 }
3065 else
3066 {
3067 sl_clear_trusted_file(sl_trusted_files);
3068 sl_trusted_files = NULL;
3069 old = now;
3070 }
3071
3072 tf_euid = teuid;
3073 status = sl_trustfile(filename, NULL, NULL);
3074 if (status == SL_ENONE)
3075 sl_add_trusted_file(filename, teuid);
3076 SL_IRETURN(status, _("sl_trustfile_euid"));
3077}
3078
3079/* ----------------------------------------------------------------
3080 *
3081 * Overflow tests
3082 *
3083 * ---------------------------------------------------------------- */
3084
3085#ifndef SIZE_MAX
3086#define SIZE_MAX (4294967295U)
3087#endif
3088
3089int sl_ok_muli (int a, int b) /* a*b */
3090{
3091 if ((b == 0) || (a >= (INT_MIN / b) && a <= (INT_MAX / b)))
3092 return SL_TRUE; /* no overflow */
3093 return SL_FALSE;
3094}
3095
3096int sl_ok_muls (size_t a, size_t b) /* a*b */
3097{
3098 if ((b == 0) || (a <= (SIZE_MAX / b)))
3099 return SL_TRUE; /* no overflow */
3100 return SL_FALSE;
3101}
3102
3103int sl_ok_divi (int a, int b) /* a/b */
3104{
3105 (void) a;
3106 if (b != 0)
3107 return SL_TRUE; /* no overflow */
3108 return SL_FALSE;
3109}
3110
3111int sl_ok_addi (int a, int b) /* a+b */
3112{
3113 if (a >= 0 && b >= 0)
3114 {
3115 if (a <= (INT_MAX - b))
3116 return SL_TRUE; /* no overflow */
3117 else
3118 return SL_FALSE;
3119 }
3120 else if (a < 0 && b < 0)
3121 {
3122 if (a >= (INT_MIN - b))
3123 return SL_TRUE; /* no overflow */
3124 else
3125 return SL_FALSE;
3126 }
3127 return SL_TRUE;
3128}
3129
3130int sl_ok_adds (size_t a, size_t b) /* a+b */
3131{
3132 if (a <= (SIZE_MAX - b))
3133 return SL_TRUE; /* no overflow */
3134 else
3135 return SL_FALSE;
3136}
3137
3138int sl_ok_subi (int a, int b) /* a-b */
3139{
3140 if (a >= 0 && b < 0)
3141 {
3142 if (a <= (INT_MAX + b))
3143 return SL_TRUE; /* no overflow */
3144 else
3145 return SL_FALSE;
3146 }
3147 else if (a < 0 && b >= 0)
3148 {
3149 if (a >= (INT_MIN + b))
3150 return SL_TRUE; /* no overflow */
3151 else
3152 return SL_FALSE;
3153 }
3154 return SL_TRUE;
3155}
Note: See TracBrowser for help on using the repository browser.