source: trunk/src/slib.c@ 327

Last change on this file since 327 was 318, checked in by katerina, 14 years ago

Fix for ticket #238: Avoid mutex in sl_create_ticket()

File size: 70.9 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
1597
1598char * sl_check_stale()
1599{
1600 if (stale_orig_line == -1)
1601 return NULL;
1602 sl_snprintf(stale_orig_mesg, sizeof(stale_orig_mesg),
1603 _("stale handle, %s, %d"), stale_orig_file, stale_orig_line);
1604 stale_orig_file[0] = '\0';
1605 stale_orig_line = -1;
1606 return stale_orig_mesg;
1607}
1608
1609char * sl_check_badfd()
1610{
1611 if (badfd_orig_line == -1)
1612 return NULL;
1613 sl_snprintf(badfd_orig_mesg, sizeof(badfd_orig_mesg),
1614 _("close on file descriptor with allocated handle, %s, %d"),
1615 badfd_orig_file, badfd_orig_line);
1616 badfd_orig_file[0] = '\0';
1617 badfd_orig_line = -1;
1618 return badfd_orig_mesg;
1619}
1620
1621typedef struct { volatile unsigned int atom; } atomic_t;
1622static atomic_t nonce_counter = { TOFFSET };
1623
1624#if defined(__GNUC__) && (defined(__i486__) || defined(__x86_64__))
1625/* from linux/include/asm-i386/atomic.h */
1626static unsigned int atomic_add ( unsigned int i, atomic_t *var)
1627{
1628 unsigned int j = i;
1629
1630 __asm__ __volatile__ ("lock; xaddl %0, %1"
1631 : "+r" (i), "+m" (var->atom)
1632 : : "memory");
1633 return j+i;
1634}
1635#else
1636SH_MUTEX_STATIC(mutex_ticket, PTHREAD_MUTEX_INITIALIZER);
1637
1638static unsigned int atomic_add ( unsigned int i, atomic_t *var)
1639{
1640 volatile unsigned int j;
1641
1642 SH_MUTEX_LOCK_UNSAFE(mutex_ticket);
1643 var->atom += i;
1644 j = var->atom;
1645 SH_MUTEX_UNLOCK_UNSAFE(mutex_ticket);
1646
1647 return j;
1648}
1649#endif
1650
1651static
1652SL_TICKET sl_create_ticket (unsigned int myindex)
1653{
1654 unsigned int high; /* index */
1655 unsigned int low; /* nonce */
1656 SL_TICKET retval = SL_EINTERNAL;
1657 unsigned int nonce;/* nonce */
1658
1659 SL_ENTER(_("sl_create_ticket"));
1660
1661 if (myindex >= MAXFD)
1662 {
1663 retval = SL_EINTERNAL01;
1664 goto out_ticket;
1665 }
1666
1667 /* mask out the high bit and check that it is not used
1668 * -> verify that it fits into 16 bits as positive
1669 */
1670 high = (myindex + TOFFSET) & 0x7fff;
1671
1672 if (high != myindex + TOFFSET)
1673 {
1674 retval = SL_EINTERNAL02;
1675 goto out_ticket;
1676 }
1677
1678 nonce = atomic_add(1, &nonce_counter);
1679
1680 /* Wrap around the nonce counter.
1681 * This is a dirty trick.
1682 */
1683 if (nonce > 0x7fff)
1684 {
1685 nonce_counter.atom = TOFFSET;
1686 nonce = atomic_add(1, &nonce_counter);
1687 }
1688
1689 low = nonce & 0xffff;
1690
1691 /* Overflow -> nonce too big.
1692 */
1693 if ((low != nonce) || low == 0)
1694 {
1695 retval = SL_EINTERNAL03;
1696 goto out_ticket;
1697 }
1698
1699 retval = (SL_TICKET) ((high << 16) | low);
1700
1701 out_ticket:
1702 SL_RETURN (retval, _("sl_create_ticket"));
1703}
1704
1705static
1706int sl_read_ticket (SL_TICKET fno)
1707{
1708 register unsigned myindex;
1709 register SL_OFILE *of;
1710
1711 myindex = ((fno >> 16) & 0xffff) - TOFFSET;
1712 if (myindex >= MAXFD)
1713 return (SL_ETICKET);
1714
1715 if (ofiles[myindex] == NULL)
1716 return (SL_ETICKET);
1717
1718 if (ofiles[myindex]->ticket != fno)
1719 return (SL_ETICKET);
1720
1721 if ((of = ofiles[myindex])->fd < 0 || of->fd >= MAXFD )
1722 return (SL_EINTERNAL04);
1723
1724 if (((of->ticket) & 0xffff) == 0)
1725 return (SL_EINTERNAL05);
1726
1727 return (myindex);
1728}
1729
1730SL_TICKET sl_make_ticket (const char * ofile, int oline,
1731 int fd, const char * filename, FILE * stream)
1732{
1733 size_t len;
1734 SL_TICKET ticket;
1735 SL_ENTER(_("sl_make_ticket"));
1736 /* Make entry.
1737 */
1738 if (fd >= MAXFD || fd < 0)
1739 {
1740 SL_IRETURN(SL_TOOMANY, _("sl_make_ticket"));
1741 }
1742
1743 if (ofiles[fd] != NULL) /* stale entry */
1744 {
1745 /* SL_IRETURN(SL_EINTERNAL06, _("sl_make_ticket")); */
1746 sl_strlcpy(stale_orig_file, ofiles[fd]->ofile, sizeof(stale_orig_file));
1747 stale_orig_line = ofiles[fd]->oline;
1748
1749 if (ofiles[fd]->content)
1750 sh_string_destroy(&(ofiles[fd]->content));
1751 (void) free (ofiles[fd]->path);
1752 (void) free (ofiles[fd]);
1753 ofiles[fd] = NULL;
1754 }
1755
1756 if ( (ofiles[fd] = (SL_OFILE *) malloc(sizeof(SL_OFILE))) == NULL)
1757 {
1758 SL_IRETURN(SL_EMEM, _("sl_make_ticket"));
1759 }
1760
1761 len = sl_strlen(filename)+1;
1762
1763 if ( (ofiles[fd]->path = (char *) malloc(len) ) == NULL)
1764 {
1765 free (ofiles[fd]);
1766 ofiles[fd] = NULL;
1767 SL_IRETURN(SL_EMEM, _("sl_make_ticket"));
1768 }
1769
1770 /* Get a ticket.
1771 */
1772 ticket = sl_create_ticket((unsigned int)fd);
1773
1774 if (SL_ISERROR(ticket))
1775 {
1776 (void) free (ofiles[fd]->path);
1777 (void) free (ofiles[fd]);
1778 ofiles[fd] = NULL;
1779 SL_IRETURN(ticket, _("sl_make_ticket"));
1780 }
1781
1782 sl_strlcpy (ofiles[fd]->path, filename, len);
1783 ofiles[fd]->ticket = ticket;
1784 ofiles[fd]->fd = fd;
1785 ofiles[fd]->content = NULL;
1786 ofiles[fd]->stream = stream;
1787 ofiles[fd]->flush = SL_FALSE;
1788
1789 sl_strlcpy(ofiles[fd]->ofile, ofile, SL_OFILE_SIZE);
1790 ofiles[fd]->oline = oline;
1791
1792 SL_IRETURN(ticket, _("sl_make_ticket"));
1793}
1794
1795#define SL_OPEN_MIN 113
1796#define SL_OPEN_FOR_READ 113
1797#define SL_OPEN_FOR_WRITE 114
1798#define SL_OPEN_FOR_RDWR 115
1799#define SL_OPEN_FOR_WTRUNC 116
1800#define SL_OPEN_FOR_RWTRUNC 117
1801#define SL_OPEN_SAFE_RDWR 118
1802#define SL_OPEN_FOR_FASTREAD 119
1803#define SL_OPEN_MAX 119
1804
1805#if !defined(O_NOATIME)
1806#if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
1807#define O_NOATIME 01000000
1808#else
1809 /*
1810 * bitwise 'or' with zero does not modify any bit
1811 */
1812#define O_NOATIME 0
1813#endif
1814#endif
1815
1816static int o_noatime = O_NOATIME;
1817static mode_t open_mode = (S_IWUSR|S_IRUSR|S_IRGRP);
1818
1819
1820static
1821int sl_open_file (const char * ofile, int oline,
1822 const char *filename, int mode, int priv)
1823{
1824 struct stat lbuf;
1825 struct stat buf;
1826 int errval = 0;
1827 int lstat_return;
1828 int stat_return;
1829 int fd;
1830 int sflags;
1831 size_t len;
1832 SL_TICKET ticket;
1833
1834#if !defined(O_NONBLOCK)
1835#if defined(O_NDELAY)
1836#define O_NONBLOCK O_NDELAY
1837#else
1838#define O_NONBLOCK 0
1839#endif
1840#endif
1841
1842 SL_ENTER(_("sl_open_file"));
1843
1844 if (filename == NULL)
1845 SL_IRETURN(SL_ENULL, _("sl_open_file"));
1846 if (mode < SL_OPEN_MIN || mode > SL_OPEN_MAX)
1847 SL_IRETURN(SL_EINTERNAL07, _("sl_open_file"));
1848
1849 /* "This system call always succeeds and the previous value of
1850 * the mask is returned."
1851 */
1852 (void) umask (0);
1853
1854 if (mode == SL_OPEN_FOR_FASTREAD)
1855 {
1856 fd = aud_open_noatime (FIL__, __LINE__, priv, filename,
1857 O_RDONLY|O_NONBLOCK, 0, &o_noatime);
1858 /*
1859 if (fd >= 0) {
1860 sflags = retry_fcntl(FIL__, __LINE__, fd, F_GETFL, 0);
1861 retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags & ~O_NONBLOCK);
1862 }
1863 */
1864 if (fd < 0)
1865 SL_IRETURN(SL_EBADFILE, _("sl_open_file"));
1866 goto createTicket;
1867 }
1868
1869#ifdef USE_SUID
1870 if (priv == SL_YESPRIV)
1871 sl_set_suid();
1872#endif
1873 if (mode == SL_OPEN_FOR_READ)
1874 lstat_return = retry_stat (FIL__, __LINE__, filename, &lbuf);
1875 else
1876 lstat_return = retry_lstat(FIL__, __LINE__, filename, &lbuf);
1877 errval = errno;
1878#ifdef USE_SUID
1879 if (priv == SL_YESPRIV)
1880 sl_unset_suid();
1881#endif
1882
1883 if (lstat_return == -1)
1884 {
1885 lstat_return = ENOENT;
1886 if ( (mode == SL_OPEN_FOR_READ && lstat_return == ENOENT) ||
1887 (errval != ENOENT))
1888 {
1889 TPT(( 0, FIL__, __LINE__, _("msg=<lstat: %s> errno=<%d>\n"),
1890 filename, errval));
1891 errno = errval;
1892 SL_IRETURN(SL_ESTAT, _("sl_open_file"));
1893 }
1894 }
1895
1896 if ( (mode != SL_OPEN_FOR_READ) && (lstat_return != ENOENT) &&
1897 ( S_ISDIR(lbuf.st_mode) || (S_IWOTH & lbuf.st_mode) )
1898 )
1899 {
1900 int retval = S_ISDIR(lbuf.st_mode) ? SL_EISDIR : SL_EBADOTH;
1901 errno = 0;
1902 SL_IRETURN(retval, _("sl_open_file"));
1903 }
1904
1905 /* O_NOATIME has an effect for read(). But write() ?.
1906 */
1907 switch (mode)
1908 {
1909 case SL_OPEN_FOR_READ:
1910 fd = aud_open_noatime (FIL__, __LINE__, priv, filename,
1911 O_RDONLY|O_NONBLOCK, 0, &o_noatime);
1912 errval = errno;
1913 if (fd >= 0) {
1914 sflags = retry_fcntl(FIL__, __LINE__, fd, F_GETFL, 0);
1915 retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags & ~O_NONBLOCK);
1916 }
1917 break;
1918 case SL_OPEN_FOR_WRITE:
1919 if (lstat_return == ENOENT)
1920 fd = aud_open (FIL__, __LINE__, priv, filename,
1921 O_WRONLY|O_CREAT|O_EXCL, open_mode);
1922 else
1923 fd = aud_open (FIL__, __LINE__, priv, filename,
1924 O_WRONLY, open_mode);
1925 errval = errno;
1926 break;
1927 case SL_OPEN_SAFE_RDWR:
1928 if (lstat_return == ENOENT)
1929 {
1930 fd = aud_open (FIL__, __LINE__, priv, filename,
1931 O_RDWR|O_CREAT|O_EXCL, open_mode);
1932 errval = errno;
1933 }
1934 else
1935 {
1936 errno = errval;
1937 SL_IRETURN(SL_EBADFILE, _("sl_open_file"));
1938 }
1939 break;
1940 case SL_OPEN_FOR_RDWR:
1941 if (lstat_return == ENOENT)
1942 fd = aud_open (FIL__, __LINE__, priv, filename,
1943 O_RDWR|O_CREAT|O_EXCL, open_mode);
1944 else
1945 fd = aud_open (FIL__, __LINE__, priv, filename,
1946 O_RDWR, open_mode);
1947 errval = errno;
1948 break;
1949 case SL_OPEN_FOR_WTRUNC:
1950 if (lstat_return == ENOENT)
1951 fd = aud_open (FIL__, __LINE__, priv, filename,
1952 O_WRONLY|O_CREAT|O_EXCL, open_mode);
1953 else
1954 fd = aud_open (FIL__, __LINE__, priv, filename,
1955 O_WRONLY|O_TRUNC, open_mode);
1956 errval = errno;
1957 break;
1958 case SL_OPEN_FOR_RWTRUNC:
1959 if (lstat_return == ENOENT)
1960 fd = aud_open (FIL__, __LINE__, priv, filename,
1961 O_RDWR|O_CREAT|O_EXCL, open_mode);
1962 else
1963 fd = aud_open (FIL__, __LINE__, priv, filename,
1964 O_RDWR|O_TRUNC, open_mode);
1965 errval = errno;
1966 break;
1967 default:
1968 errno = 0;
1969 SL_IRETURN(SL_EINTERNAL08, _("sl_open_file"));
1970 }
1971
1972 if (fd < 0)
1973 {
1974 TPT(( 0, FIL__, __LINE__, _("msg=<Error opening: %s> errno=<%d>\n"),
1975 filename, errval));
1976 errno = errval;
1977 SL_IRETURN(SL_EBADFILE, _("sl_open_file"));
1978 }
1979
1980#ifdef USE_SUID
1981 if (priv == SL_YESPRIV)
1982 sl_set_suid();
1983#endif
1984 stat_return = retry_fstat(FIL__, __LINE__, fd, &buf);
1985 errval = errno;
1986#ifdef USE_SUID
1987 if (priv == SL_YESPRIV)
1988 sl_unset_suid();
1989#endif
1990
1991 if (stat_return < 0)
1992 {
1993 sl_close_fd (FIL__, __LINE__, fd);
1994 errno = errval;
1995 SL_IRETURN(SL_EFSTAT, _("sl_open_file"));
1996 }
1997
1998 errno = 0;
1999
2000 if (lstat_return != ENOENT && buf.st_ino != lbuf.st_ino)
2001 {
2002 sl_close_fd (FIL__, __LINE__, fd);
2003 SL_IRETURN(SL_EBOGUS, _("sl_open_file"));
2004 }
2005
2006 createTicket:
2007
2008 /* Make entry.
2009 */
2010 if (fd >= MAXFD)
2011 {
2012 sl_close_fd(FIL__, __LINE__, fd);
2013 SL_IRETURN(SL_TOOMANY, _("sl_open_file"));
2014 }
2015
2016 if (ofiles[fd] != NULL) /* stale entry */
2017 {
2018 /*
2019 sl_close_fd(FIL__, __LINE__, fd);
2020 SL_IRETURN(SL_EINTERNAL09, _("sl_open_file"));
2021 */
2022 sl_strlcpy(stale_orig_file, ofiles[fd]->ofile, sizeof(stale_orig_file));
2023 stale_orig_line = ofiles[fd]->oline;
2024
2025 if (ofiles[fd]->content)
2026 sh_string_destroy(&(ofiles[fd]->content));
2027 (void) free (ofiles[fd]->path);
2028 (void) free (ofiles[fd]);
2029 ofiles[fd] = NULL;
2030 }
2031
2032 if ( (ofiles[fd] = (SL_OFILE *) malloc(sizeof(SL_OFILE))) == NULL)
2033 {
2034 sl_close_fd(FIL__, __LINE__, fd);
2035 SL_IRETURN(SL_EMEM, _("sl_open_file"));
2036 }
2037
2038 len = sl_strlen(filename)+1;
2039
2040 if ( (ofiles[fd]->path = (char *) malloc(len) ) == NULL)
2041 {
2042 free (ofiles[fd]);
2043 ofiles[fd] = NULL;
2044 sl_close_fd(FIL__, __LINE__, fd);
2045 SL_IRETURN(SL_EMEM, _("sl_open_file"));
2046 }
2047
2048 /* Get a ticket.
2049 */
2050 ticket = sl_create_ticket(fd);
2051
2052 if (SL_ISERROR(ticket))
2053 {
2054 (void) free (ofiles[fd]->path);
2055 (void) free (ofiles[fd]);
2056 ofiles[fd] = NULL;
2057 sl_close_fd(FIL__, __LINE__, fd);
2058 SL_IRETURN(ticket, _("sl_open_file"));
2059 }
2060
2061 sl_strlcpy (ofiles[fd]->path, filename, len);
2062 ofiles[fd]->ticket = ticket;
2063 ofiles[fd]->fd = fd;
2064 ofiles[fd]->content = NULL;
2065 ofiles[fd]->stream = NULL;
2066 ofiles[fd]->flush = SL_FALSE;
2067
2068 sl_strlcpy(ofiles[fd]->ofile, ofile, SL_OFILE_SIZE);
2069 ofiles[fd]->oline = oline;
2070
2071 SL_IRETURN(ticket, _("sl_open_file"));
2072}
2073
2074FILE * sl_stream (SL_TICKET ticket, char * mode)
2075{
2076 int fd;
2077
2078 if (SL_ISERROR(fd = sl_read_ticket(ticket)))
2079 return (NULL);
2080
2081 if (ofiles[fd] == NULL || fd != ofiles[fd]->fd ||
2082 ticket != ofiles[fd]->ticket || fd < 0)
2083 return (NULL);
2084
2085 if (!ofiles[fd]->stream)
2086 ofiles[fd]->stream = fdopen(fd, mode);
2087
2088 return ofiles[fd]->stream;
2089}
2090
2091int get_the_fd (SL_TICKET ticket)
2092{
2093 int fd;
2094
2095 if (SL_ISERROR(fd = sl_read_ticket(ticket)))
2096 return (fd);
2097
2098 if (ofiles[fd] == NULL || fd != ofiles[fd]->fd ||
2099 ticket != ofiles[fd]->ticket || fd < 0)
2100 return (SL_EINTERNAL10);
2101
2102 return (fd);
2103}
2104
2105static
2106int check_fname_priv (const char * fname, int priv)
2107{
2108 SL_ENTER(_("check_fname_priv"));
2109 if (fname == NULL)
2110 SL_IRETURN(SL_ENULL, _("check_fname_priv"));
2111 if (priv != SL_YESPRIV && priv != SL_NOPRIV)
2112 SL_IRETURN(SL_EINTERNAL11, _("check_fname_priv"));
2113 SL_IRETURN(SL_ENONE, _("check_fname_priv"));
2114}
2115
2116SL_TICKET sl_open_write (const char * ofile, int oline,
2117 const char * fname, int priv)
2118{
2119 long status;
2120 SL_ENTER(_("sl_open_write"));
2121
2122 if (SL_ENONE != (status = check_fname_priv (fname, priv)))
2123 SL_IRETURN(status, _("sl_open_write"));
2124
2125 status = sl_open_file(ofile, oline, fname, SL_OPEN_FOR_WRITE, priv);
2126 SL_IRETURN(status, _("sl_open_write"));
2127}
2128
2129SL_TICKET sl_open_read (const char * ofile, int oline,
2130 const char * fname, int priv)
2131{
2132 long status;
2133 SL_ENTER(_("sl_open_read"));
2134
2135 if (SL_ENONE != (status = check_fname_priv (fname, priv)))
2136 {
2137 TPT(( 0, FIL__, __LINE__,
2138 _("msg=<Error in check_fname_priv.> status=<%ld>\n"),
2139 status));
2140 SL_IRETURN(status, _("sl_open_read"));
2141 }
2142
2143 status = sl_open_file(ofile, oline, fname, SL_OPEN_FOR_READ, priv);
2144 SL_IRETURN(status, _("sl_open_read"));
2145}
2146
2147#if defined(HAVE_POSIX_FADVISE) && defined(HAVE_MINCORE) && defined(POSIX_FADV_DONTNEED)
2148static int sl_check_mincore(int fd)
2149{
2150 /* Idea from Tobias Oetiker (http://insights.oetiker.ch/linux/fadvise.html)
2151 */
2152 struct stat fbuf;
2153 int retval = -1;
2154
2155 if (0 == fstat(fd, &fbuf))
2156 {
2157 void *f_map;
2158
2159 f_map = mmap((void *)0, fbuf.st_size, PROT_NONE, MAP_SHARED, fd, 0);
2160 if (MAP_FAILED != f_map)
2161 {
2162 extern int sh_unix_pagesize(void);
2163 size_t i;
2164 size_t page_size = sh_unix_pagesize();
2165 size_t vec_size = (fbuf.st_size+page_size-1)/page_size;
2166 unsigned char * vec = calloc(1, vec_size);
2167
2168 if (vec)
2169 {
2170 mincore(f_map, fbuf.st_size, vec);
2171 /* imax = fbuf.st_size/page_size; */
2172 for (i = 0; i <= vec_size; ++i)
2173 {
2174 if (vec[i]&1)
2175 {
2176 goto incore;
2177 }
2178 }
2179 retval = 0;
2180 incore:
2181 free(vec);
2182 }
2183 munmap(f_map, fbuf.st_size);
2184 }
2185 }
2186 return retval;
2187}
2188#endif
2189
2190static int sl_drop_cache = SL_FALSE;
2191
2192int sl_set_drop_cache(const char * str)
2193{
2194 extern int sh_util_flagval(const char * c, int * fval);
2195 return sh_util_flagval(str, &sl_drop_cache);
2196}
2197
2198SL_TICKET sl_open_fastread (const char * ofile, int oline,
2199 const char * fname, int priv)
2200{
2201 long status;
2202 SL_ENTER(_("sl_open_fastread"));
2203
2204 if (SL_ENONE != (status = check_fname_priv (fname, priv)))
2205 SL_IRETURN(status, _("sl_open_read"));
2206
2207 status = sl_open_file(ofile, oline, fname, SL_OPEN_FOR_FASTREAD, priv);
2208
2209#if defined(HAVE_POSIX_FADVISE) && defined(HAVE_MINCORE) && defined(POSIX_FADV_DONTNEED)
2210
2211 if (SL_FALSE != sl_drop_cache && !SL_ISERROR(status))
2212 {
2213 int fd = get_the_fd(status);
2214 if (fd >= 0)
2215 {
2216 if (0 == sl_check_mincore(fd))
2217 ofiles[fd]->flush = SL_TRUE;
2218 }
2219 }
2220
2221#endif
2222
2223 SL_IRETURN(status, _("sl_open_fastread"));
2224}
2225
2226SL_TICKET sl_open_rdwr (const char * ofile, int oline,
2227 const char * fname, int priv)
2228{
2229 long status;
2230 SL_ENTER(_("sl_open_rdwr"));
2231
2232 if (SL_ENONE != (status = check_fname_priv (fname, priv)))
2233 SL_IRETURN(status, _("sl_open_rdwr"));
2234
2235 status = sl_open_file(ofile, oline, fname, SL_OPEN_FOR_RDWR, priv);
2236 SL_IRETURN(status, _("sl_open_rdwr"));
2237}
2238
2239SL_TICKET sl_open_safe_rdwr (const char * ofile, int oline,
2240 const char * fname, int priv)
2241{
2242 long status;
2243 SL_ENTER(_("sl_open_safe_rdwr"));
2244
2245 if (SL_ENONE != (status = check_fname_priv (fname, priv)))
2246 SL_IRETURN(status, _("sl_open_safe_rdwr"));
2247
2248 status = sl_open_file(ofile, oline, fname, SL_OPEN_SAFE_RDWR, priv);
2249 SL_IRETURN(status, _("sl_open_safe_rdwr"));
2250}
2251
2252SL_TICKET sl_open_write_trunc (const char * ofile, int oline,
2253 const char * fname, int priv)
2254{
2255 long status;
2256 SL_ENTER(_("sl_open_write_trunc"));
2257
2258 if (SL_ENONE != (status = check_fname_priv (fname, priv)))
2259 SL_IRETURN(status, _("sl_open_write_trunc"));
2260
2261 status = sl_open_file(ofile, oline, fname, SL_OPEN_FOR_WTRUNC, priv);
2262 SL_IRETURN(status, _("sl_open_write_trunc"));
2263}
2264
2265SL_TICKET sl_open_rdwr_trunc (const char * ofile, int oline,
2266 const char * fname, int priv)
2267{
2268 long status;
2269 SL_ENTER(_("sl_open_rdwr_trunc"));
2270
2271 if (SL_ENONE != (status = check_fname_priv (fname, priv)))
2272 SL_IRETURN(status, _("sl_open_rdwr_trunc"));
2273
2274 status = sl_open_file(ofile, oline, fname, SL_OPEN_FOR_RWTRUNC, priv);
2275 SL_IRETURN(status, _("sl_open_rdwr_trunc"));
2276}
2277
2278
2279int sl_init_content (SL_TICKET ticket, size_t size)
2280{
2281 int fd;
2282
2283 if (SL_ISERROR(fd = sl_read_ticket(ticket)))
2284 return (fd);
2285
2286 if (ofiles[fd] == NULL || fd != ofiles[fd]->fd ||
2287 ticket != ofiles[fd]->ticket || fd < 0)
2288 return (SL_EINTERNAL12);
2289
2290 if (ofiles[fd]->content)
2291 sh_string_destroy(&(ofiles[fd]->content));
2292 ofiles[fd]->content = sh_string_new(size);
2293
2294 return SL_ENONE;
2295}
2296
2297sh_string * sl_get_content (SL_TICKET ticket)
2298{
2299 int fd;
2300
2301 if (SL_ISERROR(fd = sl_read_ticket(ticket)))
2302 return (NULL);
2303
2304 if (ofiles[fd] == NULL || fd != ofiles[fd]->fd ||
2305 ticket != ofiles[fd]->ticket || fd < 0)
2306 return (NULL);
2307
2308 return (ofiles[fd]->content);
2309}
2310
2311int sl_lock (SL_TICKET ticket)
2312{
2313 int fd;
2314 struct flock lock;
2315 int retval;
2316
2317 SL_ENTER(_("sl_lock"));
2318
2319 if (SL_ISERROR(fd = get_the_fd (ticket)))
2320 SL_IRETURN(fd, _("sl_lock"));
2321
2322 lock.l_type = F_WRLCK;
2323 lock.l_whence = SEEK_SET;
2324 lock.l_start = 0;
2325 lock.l_len = 0;
2326
2327 /* F_SETLK returns if the lock cannot be obtained */
2328 do {
2329 retval = fcntl(fd, F_SETLK, &lock);
2330 } while (retval < 0 && errno == EINTR);
2331
2332 if (retval < 0 && errno == EBADF)
2333 SL_IRETURN(SL_ETICKET, _("sl_lock"));
2334 else if (retval < 0)
2335 SL_IRETURN(SL_EBADFILE, _("sl_lock"));
2336 else
2337 SL_IRETURN(SL_ENONE, _("sl_lock"));
2338 }
2339
2340int sl_close (SL_TICKET ticket)
2341{
2342 register int fd;
2343 FILE * fp = NULL;
2344
2345 SL_ENTER(_("sl_close"));
2346
2347 if (SL_ISERROR(fd = get_the_fd (ticket)))
2348 SL_IRETURN(fd, _("sl_close"));
2349
2350 if (ofiles[fd] != NULL)
2351 {
2352#if defined(HAVE_POSIX_FADVISE) && defined(HAVE_MINCORE) && defined(POSIX_FADV_DONTNEED)
2353 if (ofiles[fd]->flush == SL_TRUE)
2354 {
2355 posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
2356 }
2357#endif
2358 if (ofiles[fd]->content)
2359 sh_string_destroy(&(ofiles[fd]->content));
2360 (void) free (ofiles[fd]->path);
2361 fp = ofiles[fd]->stream;
2362 (void) free (ofiles[fd]);
2363 ofiles[fd] = NULL;
2364 }
2365
2366 /* This may fail, but what to do then ?
2367 */
2368 if (fp)
2369 {
2370 if (0 != fclose (fp)) /* within sl_close */
2371 {
2372 TPT((0, FIL__, __LINE__,
2373 _("msg=<Error fclosing file.>, fd=<%d>, err=<%s>\n"),
2374 fd, strerror(errno)));
2375 }
2376 }
2377 else
2378 {
2379 if (0 != close(fd)) /* within sl_close */
2380 {
2381 TPT((0, FIL__, __LINE__,
2382 _("msg=<Error closing file.>, fd=<%d>, err=<%s>\n"),
2383 fd, strerror(errno)));
2384 }
2385 }
2386
2387 SL_IRETURN(SL_ENONE, _("sl_close"));
2388}
2389
2390int sl_close_fd (const char * file, int line, int fd)
2391{
2392 int ret = -1;
2393
2394 SL_ENTER(_("sl_close_fd"));
2395
2396 if (fd >= 0 && fd < MAXFD && ofiles[fd] != NULL) /* stale ofiles[fd] handle */
2397 {
2398 sl_strlcpy(badfd_orig_file, file, sizeof(badfd_orig_file));
2399 badfd_orig_line = line;
2400 }
2401
2402 ret = close(fd); /* within sl_close_fd wrapper */
2403
2404 SL_IRETURN(ret, _("sl_close_fd"));
2405}
2406
2407int sl_fclose (const char * file, int line, FILE * fp)
2408{
2409 int ret = -1;
2410 int fd;
2411
2412 SL_ENTER(_("sl_fclose"));
2413
2414 fd = fileno(fp);
2415
2416 if (fd >= 0 && fd < MAXFD && ofiles[fd] != NULL) /* stale ofiles[fd] handle */
2417 {
2418 sl_strlcpy(badfd_orig_file, file, sizeof(badfd_orig_file));
2419 badfd_orig_line = line;
2420 }
2421
2422 ret = fclose(fp); /* within sl_fclose wrapper */
2423
2424 SL_IRETURN(ret, _("sl_fclose"));
2425}
2426
2427int sl_dropall(int fd, int except)
2428{
2429 while (fd < MAXFD)
2430 {
2431 if (ofiles[fd] != NULL && fd != except)
2432 {
2433 if (ofiles[fd]->content)
2434 sh_string_destroy(&(ofiles[fd]->content));
2435 if (ofiles[fd]->path != NULL)
2436 (void) free (ofiles[fd]->path);
2437 (void) free (ofiles[fd]);
2438 ofiles[fd] = NULL;
2439 }
2440 ++fd;
2441 }
2442 return 0;
2443}
2444
2445int sl_dropall_dirty(int fd, int except)
2446{
2447 while (fd < MAXFD)
2448 {
2449 if (ofiles[fd] != NULL && fd != except)
2450 {
2451 ofiles[fd] = NULL;
2452 }
2453 ++fd;
2454 }
2455 return 0;
2456}
2457
2458
2459int sl_unlink (SL_TICKET ticket)
2460{
2461 register int fd;
2462
2463 SL_ENTER(_("sl_unlink"));
2464
2465 if (SL_ISERROR(fd = get_the_fd(ticket)))
2466 SL_IRETURN(fd, _("sl_unlink"));
2467
2468 if (retry_aud_unlink(FIL__, __LINE__, ofiles[fd]->path) < 0)
2469 SL_IRETURN(SL_EUNLINK, _("sl_unlink"));
2470
2471 SL_IRETURN(SL_ENONE, _("sl_unlink"));
2472}
2473
2474
2475int sl_seek (SL_TICKET ticket, off_t off_data)
2476{
2477 register int fd;
2478
2479 SL_ENTER(_("sl_seek"));
2480
2481 if (SL_ISERROR(fd = get_the_fd(ticket)))
2482 SL_IRETURN(fd, _("sl_seek"));
2483
2484 if (lseek(fd, off_data, SEEK_SET) == (off_t)-1)
2485 SL_IRETURN(SL_EREWIND, _("sl_seek"));
2486
2487 SL_IRETURN(SL_ENONE, _("sl_seek"));
2488}
2489
2490int sl_rewind (SL_TICKET ticket)
2491{
2492 register int fd;
2493
2494 SL_ENTER(_("sl_rewind"));
2495
2496 if (SL_ISERROR(fd = get_the_fd(ticket)))
2497 SL_IRETURN(fd, _("sl_rewind"));
2498
2499 if (lseek (fd, 0L, SEEK_SET) == (off_t)-1)
2500 SL_IRETURN(SL_EREWIND, _("sl_rewind"));
2501
2502 SL_IRETURN(SL_ENONE, _("sl_rewind"));
2503}
2504
2505int sl_forward (SL_TICKET ticket)
2506{
2507 register int fd;
2508
2509 SL_ENTER(_("sl_forward"));
2510
2511 if (SL_ISERROR(fd = get_the_fd(ticket)))
2512 SL_IRETURN(fd, _("sl_forward"));
2513
2514 if (lseek (fd, 0L, SEEK_END) == (off_t)-1)
2515 SL_IRETURN(SL_EFORWARD, _("sl_forward"));
2516
2517 SL_IRETURN(SL_ENONE, _("sl_forward"));
2518}
2519
2520
2521int sl_sync (SL_TICKET ticket)
2522{
2523 register int fd;
2524
2525 SL_ENTER(_("sl_sync"));
2526
2527 if (SL_ISERROR(fd = get_the_fd(ticket)))
2528 SL_IRETURN(fd, _("sl_sync"));
2529
2530 if (fsync (fd) == -1)
2531 SL_IRETURN(SL_ESYNC, _("sl_sync"));
2532
2533 SL_IRETURN(SL_ENONE, _("sl_sync"));
2534}
2535
2536int sl_read_timeout_prep (SL_TICKET ticket)
2537{
2538 int fd;
2539 int sflags;
2540
2541 SL_ENTER(_("sl_read_timeout_prep"));
2542
2543 if (SL_ISERROR(fd = get_the_fd(ticket)))
2544 {
2545 TPT(( 0, FIL__, __LINE__, _("msg=<ticket error> errno=<%d>"), fd));
2546 SL_IRETURN(fd, _("sl_read_timeout_prep"));
2547 }
2548
2549 /* set to non-blocking mode
2550 */
2551 sflags = retry_fcntl(FIL__, __LINE__, fd, F_GETFL, 0);
2552 retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags | O_NONBLOCK);
2553
2554 SL_IRETURN(SL_ENONE, _("sl_read_timeout_prep"));
2555}
2556
2557
2558int sl_read_timeout_fd (int fd, void * buf_in, size_t count,
2559 int timeout, int is_nonblocking)
2560{
2561 int sflags = 0;
2562 fd_set readfds;
2563 struct timeval tv;
2564 /* int sflags; */
2565 int retval;
2566 int error;
2567
2568 int byteread = 0;
2569 int bytes = 0;
2570 char * buf;
2571
2572 time_t tnow;
2573 time_t tstart;
2574 time_t tdiff;
2575 extern volatile int sig_termfast;
2576
2577 if (is_nonblocking == SL_FALSE)
2578 {
2579 /* set to non-blocking mode
2580 */
2581 sflags = retry_fcntl(FIL__, __LINE__, fd, F_GETFL, 0);
2582 retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags | O_NONBLOCK);
2583 }
2584
2585 buf = (char *) buf_in;
2586
2587 tstart = time(NULL);
2588 tdiff = 0;
2589
2590 while (count > 0)
2591 {
2592 FD_ZERO(&readfds);
2593 FD_SET(fd, &readfds);
2594
2595 tv.tv_sec = timeout - tdiff;
2596 tv.tv_usec = 0;
2597
2598 retval = select (fd+1, &readfds, NULL, NULL, &tv);
2599
2600 if (retval > 0)
2601 {
2602 byteread = read (fd, buf, count);
2603
2604 if (byteread > 0)
2605 {
2606 bytes += byteread; count -= byteread;
2607 buf += byteread;
2608 if (count == 0)
2609 break;
2610 }
2611 else if (byteread == 0)
2612 {
2613 break;
2614 }
2615 else
2616 {
2617 if (errno == EINTR || errno == EAGAIN)
2618 {
2619 retry_msleep(1, 0);
2620 tnow = time(NULL);
2621 tdiff = tnow - tstart;
2622 continue;
2623 }
2624 else
2625 {
2626 error = errno;
2627 if (is_nonblocking == SL_FALSE)
2628 retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags);
2629 TPT(( 0, FIL__, __LINE__, _("msg=<read error>")));
2630 errno = error;
2631 return (SL_EREAD);
2632 }
2633 }
2634 }
2635 else if ((retval == -1) && (errno == EINTR || errno == EAGAIN))
2636 {
2637 retry_msleep(1, 0);
2638 tnow = time(NULL);
2639 tdiff = tnow - tstart;
2640 continue;
2641 }
2642 else if (retval == 0)
2643 {
2644 if (is_nonblocking == SL_FALSE)
2645 retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags);
2646 TPT(( 0, FIL__, __LINE__, _("msg=<timeout>")));
2647 errno = 0;
2648 return (SL_TIMEOUT);
2649 }
2650 else
2651 {
2652 error = errno;
2653 if (is_nonblocking == SL_FALSE)
2654 retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags);
2655 TPT(( 0, FIL__, __LINE__, _("msg=<timeout>")));
2656 errno = error;
2657 return (SL_EREAD);
2658 }
2659
2660 if (sig_termfast == 1)
2661 {
2662 if (is_nonblocking == SL_FALSE)
2663 retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags);
2664 TPT(( 0, FIL__, __LINE__, _("msg=<terminated>")));
2665 errno = 0;
2666 return (SL_EREAD);
2667 }
2668
2669 tnow = time(NULL);
2670 tdiff = tnow - tstart;
2671
2672 if (tdiff > timeout)
2673 {
2674 if (is_nonblocking == SL_FALSE)
2675 retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags);
2676 TPT(( 0, FIL__, __LINE__, _("msg=<timeout>")));
2677 errno = 0;
2678 return (SL_TIMEOUT);
2679 }
2680 }
2681
2682 if (is_nonblocking == SL_FALSE)
2683 retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags);
2684 return ((int) bytes);
2685}
2686
2687int sl_read_timeout (SL_TICKET ticket, void * buf_in, size_t count,
2688 int timeout, int is_nonblocking)
2689{
2690 int fd, retval;
2691
2692 SL_ENTER(_("sl_read_timeout"));
2693
2694 if (buf_in == NULL || SL_ISERROR(fd = get_the_fd(ticket)))
2695 {
2696 if (buf_in == NULL)
2697 {
2698 TPT(( 0, FIL__, __LINE__, _("msg=<null buffer>")));
2699 SL_IRETURN((SL_ENULL), _("sl_read_timeout"));
2700 }
2701 if (SL_ISERROR(fd = get_the_fd(ticket)))
2702 {
2703 TPT(( 0, FIL__, __LINE__, _("msg=<ticket error> errno=<%d>"), fd));
2704 SL_IRETURN((fd), _("sl_read_timeout"));
2705 }
2706 }
2707
2708 retval = sl_read_timeout_fd (fd, buf_in, count, timeout, is_nonblocking);
2709 SL_IRETURN((retval), _("sl_read_timeout"));
2710}
2711
2712
2713int sl_read (SL_TICKET ticket, void * buf_in, size_t count)
2714{
2715 int fd;
2716 int byteread = 0;
2717 int bytes = 0;
2718
2719 char * buf;
2720
2721 SL_ENTER(_("sl_read"));
2722
2723 if (count < 1)
2724 {
2725 TPT(( 0, FIL__, __LINE__, _("msg=<range error>")));
2726 SL_IRETURN((SL_ERANGE), _("sl_read"));
2727 }
2728 if (buf_in == NULL)
2729 {
2730 TPT(( 0, FIL__, __LINE__, _("msg=<null buffer>")));
2731 SL_IRETURN((SL_ENULL), _("sl_read"));
2732 }
2733
2734 if (SL_ISERROR(fd = get_the_fd(ticket)))
2735 {
2736 TPT(( 0, FIL__, __LINE__, _("msg=<ticket error> errno=<%d>"), fd));
2737 SL_IRETURN((fd), _("sl_read"));
2738 }
2739
2740 buf = (char *) buf_in;
2741
2742 do
2743 {
2744 byteread = read (fd, buf, count);
2745 if (byteread > 0)
2746 {
2747 bytes += byteread; count -= byteread;
2748 buf += byteread;
2749 }
2750 } while ( byteread > 0 ||
2751 ( byteread == -1 && (errno == EINTR || errno == EAGAIN))
2752 );
2753
2754
2755 if (byteread == (-1))
2756 {
2757 TPT(( 0, FIL__, __LINE__, _("msg=<read error> errno=<%d>\n"), errno));
2758 SL_IRETURN((SL_EREAD), _("sl_read"));
2759 }
2760 SL_IRETURN((bytes), _("sl_read"));
2761}
2762
2763int sl_read_fast (SL_TICKET ticket, void * buf_in, size_t count)
2764{
2765 int fd;
2766 int byteread = 0;
2767
2768 char * buf;
2769
2770 SL_ENTER(_("sl_read_fast"));
2771
2772 if (count < 1)
2773 {
2774 TPT(( 0, FIL__, __LINE__, _("msg=<range error>")));
2775 SL_IRETURN((SL_ERANGE), _("sl_read_fast"));
2776 }
2777 if (buf_in == NULL)
2778 {
2779 TPT(( 0, FIL__, __LINE__, _("msg=<null buffer>")));
2780 SL_IRETURN((SL_ENULL), _("sl_read_fast"));
2781 }
2782
2783 if (SL_ISERROR(fd = get_the_fd(ticket)))
2784 {
2785 TPT(( 0, FIL__, __LINE__, _("msg=<ticket error> errno=<%d>"), fd));
2786 SL_IRETURN((fd), _("sl_read_fast"));
2787 }
2788
2789 buf = (char *) buf_in;
2790
2791 do
2792 {
2793 byteread = read (fd, buf, count);
2794 if (byteread >= 0)
2795 {
2796 SL_IRETURN((byteread), _("sl_read_fast"));
2797 }
2798 } while ( byteread == -1 && (errno == EINTR || errno == EAGAIN));
2799
2800
2801 if (byteread == (-1))
2802 {
2803 TPT(( 0, FIL__, __LINE__, _("msg=<read error> errno=<%d>\n"), errno));
2804 SL_IRETURN((SL_EREAD), _("sl_read_fast"));
2805 }
2806 SL_IRETURN((0), _("sl_read_fast"));
2807}
2808
2809
2810int sl_write (SL_TICKET ticket, const void * msg_in, long nbytes)
2811{
2812 long bytewritten;
2813 long bytecount;
2814 int fd;
2815
2816 const char * msg;
2817
2818 SL_ENTER(_("sl_write"));
2819
2820 if (nbytes < 1)
2821 SL_IRETURN(SL_ERANGE, _("sl_write"));
2822 if (msg_in == NULL)
2823 SL_IRETURN(SL_ENULL, _("sl_write"));
2824 if (SL_ISERROR(fd = get_the_fd(ticket)))
2825 SL_IRETURN(fd, _("sl_write"));
2826
2827 msg = (const char *) msg_in;
2828
2829 /* write
2830 */
2831 bytecount = 0;
2832 bytewritten = 0;
2833 while (bytecount < nbytes)
2834 {
2835 if ((bytewritten = write (fd, msg, nbytes-bytecount)) > 0)
2836 {
2837 bytecount += bytewritten;
2838 msg += bytewritten; /* move buffer pointer forward */
2839 }
2840 else if (bytewritten <= 0)
2841 {
2842 if ( errno == EINTR || errno == EAGAIN) /* try again */
2843 continue;
2844 else
2845 SL_IRETURN(SL_EWRITE, _("sl_write"));
2846 }
2847 }
2848 SL_IRETURN(SL_ENONE, _("sl_write"));
2849}
2850
2851int sl_write_line (SL_TICKET ticket, const void * msg, long nbytes)
2852{
2853 int status;
2854
2855 SL_ENTER(_("sl_write_line"));
2856
2857 status = sl_write(ticket, msg, nbytes);
2858 if (!SL_ISERROR(status))
2859 status = sl_write(ticket, "\n", 1);
2860
2861 SL_IRETURN(status, _("sl_write_line"));
2862}
2863
2864int sl_write_line_fast (SL_TICKET ticket, void * msg, long nbytes)
2865{
2866 int status;
2867 char * p = (char *) msg;
2868
2869 SL_ENTER(_("sl_write_line_fast"));
2870
2871 /* Here nbytes is strlen(msg), so p[nbytes] is the terminating '\0'
2872 * Overwrite the terminator, write out, then write back the terminator.
2873 */
2874 p[nbytes] = '\n';
2875 status = sl_write(ticket, msg, nbytes+1);
2876 p[nbytes] = '\0';
2877
2878 SL_IRETURN(status, _("sl_write_line_fast"));
2879}
2880
2881
2882/* ----------------------------------------------------------------
2883 *
2884 * Trustfile interface
2885 *
2886 * ---------------------------------------------------------------- */
2887
2888extern uid_t rootonly[];
2889extern int EUIDSLOT;
2890extern int ORIG_EUIDSLOT;
2891
2892extern char tf_path[MAXFILENAME]; /* Error path for trust function. */
2893extern uid_t tf_euid; /* Space for EUID of process. */
2894
2895char * sl_error_string(int errorcode)
2896{
2897
2898 switch (errorcode)
2899 {
2900 case SL_EBOGUS:
2901 return _("Bogus file, modified during access");
2902 case SL_EWRITE:
2903 return _("Write error");
2904 case SL_EREAD:
2905 return _("Read error");
2906 case SL_ESYNC:
2907 return _("Error in fsync()");
2908 case SL_EFORWARD:
2909 return _("Error in lseek()");
2910 case SL_EREWIND:
2911 return _("Error in lseek()");
2912 case SL_EUNLINK:
2913 return _("Error in unlink()");
2914 case SL_EMEM:
2915 return _("Out of memory");
2916 case SL_EINTERNAL:
2917 return _("Internal error");
2918 case SL_EINTERNAL01:
2919 return _("Internal error 01");
2920 case SL_EINTERNAL02:
2921 return _("Internal error 02");
2922 case SL_EINTERNAL03:
2923 return _("Internal error 03");
2924 case SL_EINTERNAL04:
2925 return _("Internal error 04");
2926 case SL_EINTERNAL05:
2927 return _("Internal error 05");
2928 case SL_EINTERNAL06:
2929 return _("Internal error 06");
2930 case SL_EINTERNAL07:
2931 return _("Internal error 07");
2932 case SL_EINTERNAL08:
2933 return _("Internal error 08");
2934 case SL_EINTERNAL09:
2935 return _("Internal error 09");
2936 case SL_EINTERNAL10:
2937 return _("Internal error 10");
2938 case SL_EINTERNAL11:
2939 return _("Internal error 11");
2940 case SL_EINTERNAL12:
2941 return _("Internal error 12");
2942 case SL_ETICKET:
2943 return _("Bad ticket");
2944 case SL_EREPEAT:
2945 return _("Illegal repeated use of function");
2946 case SL_ERANGE:
2947 return _("Argument out of range");
2948 case SL_ENULL:
2949 return _("Dereferenced NULL pointer");
2950
2951 case SL_EBADUID:
2952 return _("Owner not trustworthy");
2953 case SL_EBADGID:
2954 return _("Group writeable and member not trustworthy");
2955 case SL_EBADOTH:
2956 return _("World writeable");
2957 case SL_EISDIR:
2958 return _("Is a directory");
2959 case SL_EBADFILE:
2960 return _("File access error");
2961 case SL_EBADNAME:
2962 return _("Invalid filename (prob. too long or null)");
2963
2964 case SL_ETRUNC:
2965 return _("Truncation occured");
2966 case SL_ESTAT:
2967 return _("stat() failed");
2968 case SL_EFSTAT:
2969 return _("fstat() failed");
2970 default:
2971 return _("Unknown error");
2972 }
2973}
2974
2975
2976
2977char * sl_trust_errfile(void)
2978{
2979 return &tf_path[0];
2980}
2981
2982extern uid_t tf_baduid;
2983uid_t sl_trust_baduid(void)
2984{
2985 return tf_baduid;
2986}
2987
2988extern gid_t tf_badgid;
2989gid_t sl_trust_badgid(void)
2990{
2991 return tf_badgid;
2992}
2993
2994
2995static int trust_count = 0;
2996
2997int sl_trust_purge_user (void)
2998{
2999 int i;
3000
3001 EUIDSLOT = ORIG_EUIDSLOT;
3002 trust_count = 0;
3003
3004 for (i = EUIDSLOT; i < (EUIDSLOT + 15); ++i)
3005 rootonly[i] = sh_uid_neg;
3006 return 0;
3007}
3008
3009int sl_trust_add_user (uid_t pwid)
3010{
3011 SL_ENTER(_("sl_trust_add_user"));
3012
3013 if (trust_count == 15)
3014 SL_IRETURN(SL_ERANGE, _("sl_trust_add_user"));
3015
3016 rootonly[EUIDSLOT] = pwid;
3017 ++EUIDSLOT;
3018 ++trust_count;
3019
3020 SL_IRETURN(SL_ENONE, _("sl_trust_add_user"));
3021}
3022
3023#include "sh_mem.h"
3024extern char * sh_util_strdup (const char * str);
3025
3026struct sl_trustfile_store {
3027 char * filename;
3028 uid_t teuid;
3029 struct sl_trustfile_store * next;
3030};
3031
3032static struct sl_trustfile_store * sl_trusted_files = NULL;
3033
3034static void sl_add_trusted_file(const char * filename, uid_t teuid)
3035{
3036 struct sl_trustfile_store *new = SH_ALLOC(sizeof(struct sl_trustfile_store));
3037
3038 new->filename = sh_util_strdup (filename);
3039 new->teuid = teuid;
3040 new->next = sl_trusted_files;
3041
3042 sl_trusted_files = new;
3043 return;
3044}
3045
3046static const char * sl_check_trusted_file(const char * filename, uid_t teuid)
3047{
3048 struct sl_trustfile_store *new = sl_trusted_files;
3049
3050 while (new)
3051 {
3052 if ((new->teuid == teuid) && (0 == strcmp(new->filename, filename)))
3053 return filename;
3054 new = new->next;
3055 }
3056
3057 return NULL;
3058}
3059
3060static void sl_clear_trusted_file(struct sl_trustfile_store * file)
3061{
3062 if (file)
3063 {
3064 if (file->next != NULL)
3065 sl_clear_trusted_file(file->next);
3066 SH_FREE(file->filename);
3067 SH_FREE(file);
3068 }
3069 return;
3070}
3071
3072int sl_trustfile_euid(const char * filename, uid_t teuid)
3073{
3074 long status;
3075 static time_t old = 0;
3076 static time_t now;
3077
3078 SL_ENTER(_("sl_trustfile_euid"));
3079
3080 tf_path[0] = '\0';
3081 if (filename == NULL || filename[0] == '\0')
3082 SL_IRETURN(SL_EBADNAME, _("sl_trustfile_euid"));
3083
3084 now = time(NULL);
3085 if (now < (old + 300))
3086 {
3087 if (NULL != sl_check_trusted_file(filename, teuid))
3088 {
3089 sl_strlcpy(tf_path, filename, sizeof(tf_path));
3090 SL_IRETURN(SL_ENONE, _("sl_trustfile_euid"));
3091 }
3092 }
3093 else
3094 {
3095 sl_clear_trusted_file(sl_trusted_files);
3096 sl_trusted_files = NULL;
3097 old = now;
3098 }
3099
3100 tf_euid = teuid;
3101 status = sl_trustfile(filename, NULL, NULL);
3102 if (status == SL_ENONE)
3103 sl_add_trusted_file(filename, teuid);
3104 SL_IRETURN(status, _("sl_trustfile_euid"));
3105}
3106
3107/* ----------------------------------------------------------------
3108 *
3109 * Overflow tests
3110 *
3111 * ---------------------------------------------------------------- */
3112
3113#ifndef SIZE_MAX
3114#define SIZE_MAX (4294967295U)
3115#endif
3116
3117int sl_ok_muli (int a, int b) /* a*b */
3118{
3119 if ((b == 0) || (a >= (INT_MIN / b) && a <= (INT_MAX / b)))
3120 return SL_TRUE; /* no overflow */
3121 return SL_FALSE;
3122}
3123
3124int sl_ok_muls (size_t a, size_t b) /* a*b */
3125{
3126 if ((b == 0) || (a <= (SIZE_MAX / b)))
3127 return SL_TRUE; /* no overflow */
3128 return SL_FALSE;
3129}
3130
3131int sl_ok_divi (int a, int b) /* a/b */
3132{
3133 (void) a;
3134 if (b != 0)
3135 return SL_TRUE; /* no overflow */
3136 return SL_FALSE;
3137}
3138
3139int sl_ok_addi (int a, int b) /* a+b */
3140{
3141 if (a >= 0 && b >= 0)
3142 {
3143 if (a <= (INT_MAX - b))
3144 return SL_TRUE; /* no overflow */
3145 else
3146 return SL_FALSE;
3147 }
3148 else if (a < 0 && b < 0)
3149 {
3150 if (a >= (INT_MIN - b))
3151 return SL_TRUE; /* no overflow */
3152 else
3153 return SL_FALSE;
3154 }
3155 return SL_TRUE;
3156}
3157
3158int sl_ok_adds (size_t a, size_t b) /* a+b */
3159{
3160 if (a <= (SIZE_MAX - b))
3161 return SL_TRUE; /* no overflow */
3162 else
3163 return SL_FALSE;
3164}
3165
3166int sl_ok_subi (int a, int b) /* a-b */
3167{
3168 if (a >= 0 && b < 0)
3169 {
3170 if (a <= (INT_MAX + b))
3171 return SL_TRUE; /* no overflow */
3172 else
3173 return SL_FALSE;
3174 }
3175 else if (a < 0 && b >= 0)
3176 {
3177 if (a >= (INT_MIN + b))
3178 return SL_TRUE; /* no overflow */
3179 else
3180 return SL_FALSE;
3181 }
3182 return SL_TRUE;
3183}
Note: See TracBrowser for help on using the repository browser.