source: trunk/include/slib.h@ 167

Last change on this file since 167 was 167, checked in by katerina, 17 years ago

First parts of changes for MODI_TXT

File size: 14.3 KB
Line 
1/* --------------------------------------------------------------
2 *
3 * The developement of this library has been stimulated by reading
4 * a paper on 'Robust Programming' by Matt Bishop, although
5 * not all of his ideas might be implemented in the same
6 * strictness as discussed in the paper.
7 *
8 * --------------------------------------------------------------
9 */
10
11#ifndef SL_SLIB_H
12#define SL_SLIB_H
13
14#include <errno.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <stdarg.h>
18#include <sys/types.h>
19
20#include "config_xor.h"
21
22#ifdef HAVE_UNISTD_H
23#include <unistd.h>
24#endif
25
26#include "sh_string.h"
27
28/****************
29
30 -- Defined in config.h. --
31
32 #ifndef _(string)
33 #define _(string) string
34 #endif
35
36 #ifndef N_(string)
37 #define N_(string) string
38 #endif
39
40*****************/
41
42/* --------------------------------------------------------------
43 *
44 * Typedefs, global variables, macros.
45 *
46 * --------------------------------------------------------------
47 */
48
49extern long int sl_errno; /* Global error variable. */
50
51
52/* The ticketing system; used to hide internals from the
53 * programmer.
54 */
55typedef long int SL_TICKET; /* Unique ID for opened files. */
56
57
58/*
59 * TRUE, FALSE
60 */
61#define SL_TRUE 1
62#define SL_FALSE 0
63
64#define SH_GRBUF_SIZE 4096
65#define SH_PWBUF_SIZE 4096
66
67
68#if defined(__GNUC__) && (__GNUC__ >= 3)
69#undef SL_GNUC_CONST
70#define SL_GNUC_CONST __attribute__((const))
71#else
72#undef SL_GNUC_CONST
73#define SL_GNUC_CONST
74#endif
75
76/*
77 * The following macros are provided:
78 *
79 * SL_ISERROR(x) TRUE if return status of 'x' is an error code.
80 * SL_REQUIRE(x, xstr) Abort if 'x' is false.
81 * SL_ENTER(s) Trace entry in function 's'.
82 * SL_RETURN(x, s) Trace return from function 's'.
83 */
84
85
86/*
87 * The error codes.
88 */
89#define SL_ENONE 0
90
91#define SL_ENULL -1024 /* Invalid use of NULL pointer. */
92#define SL_ERANGE -1025 /* Argument out of range. */
93#define SL_ETRUNC -1026 /* Result truncated. */
94#define SL_EREPEAT -1027 /* Illegal repeated use of function. */
95
96#define SL_EINTERNAL -1028 /* Internal error. */
97#define SL_ETICKET -1029 /* Bad ticket. */
98#define SL_EBADFILE -1030 /* File access error. Check errno. */
99#define SL_EBOGUS -1031 /* Bogus file. */
100#define SL_EMEM -1032 /* Out of memory. */
101#define SL_EUNLINK -1033 /* Unlink error. Check errno. */
102#define SL_EREWIND -1034 /* Rewind error. Check errno. */
103#define SL_EFORWARD -1035 /* Forward error. Check errno. */
104#define SL_EREAD -1036 /* Read error. Check errno. */
105#define SL_EWRITE -1037 /* Write error. Check errno. */
106#define SL_ESYNC -1038 /* Write error. Check errno. */
107
108#define SL_EBADNAME -1040 /* Invalid name. */
109#define SL_ESTAT -1041 /* stat of file failed. Check errno. */
110
111#define SL_EBADUID -1050 /* Owner not trustworthy. */
112#define SL_EBADGID -1051 /* Group writeable and not trustworthy.*/
113#define SL_EBADOTH -1052 /* World writeable. */
114
115#define SL_TOOMANY -1053 /* Too many open files */
116#define SL_TIMEOUT -1054 /* Timeout in read */
117/*
118 * All int functions return SL_NONE on success.
119 */
120
121#ifdef __cplusplus
122extern "C" {
123#endif
124
125 int dlog (int flag, char * file, int line, const char *fmt, ...);
126
127 char * sl_get_errmsg();
128
129 /* ----------------------------------------------------------------
130 *
131 * Heap consistency routines
132 *
133 * ---------------------------------------------------------------- */
134
135 int sl_test_heap();
136
137 /* ----------------------------------------------------------------
138 *
139 * Capability routines
140 *
141 * ---------------------------------------------------------------- */
142
143 extern int sl_useCaps;
144
145 int sl_drop_cap ();
146 int sl_drop_cap_sub();
147 int sl_get_cap_sub();
148 int sl_drop_cap_qdel();
149 int sl_get_cap_qdel();
150
151 /* ----------------------------------------------------------------
152 *
153 * String handling routines
154 *
155 * ---------------------------------------------------------------- */
156
157 /*
158 * A memset that does not get optimized away
159 */
160 void *sl_memset(void *s, int c, size_t n);
161#if !defined(SH_REAL_SET)
162#undef memset
163#define memset sl_memset
164#endif
165
166 /*
167 * Copy src to dst. siz is the length of dst.
168 */
169 int sl_strlcpy(char * dst, /*@null@*/const char * src, size_t siz);
170
171 /*
172 * Append src to dst. siz is the length of dst.
173 */
174 int sl_strlcat(char * dst, /*@null@*/const char *src, size_t siz);
175
176 /*
177 * An implementation of vsnprintf. va_start/va_end are in the caller
178 * function.
179 */
180 int sl_vsnprintf(char *str, size_t n,
181 const char *format, va_list vl );
182
183 /*
184 * An implementation of snprintf.
185 */
186 int sl_snprintf(char *str, size_t n,
187 const char *format, ... );
188
189 /*
190 * A robust drop-in replacement of strncpy. strlcpy is preferable.
191 */
192 char * sl_strncpy(/*@out@*/char *dst, const char *src, size_t size);
193
194 /*
195 * Robust strncat.
196 */
197 char * sl_strncat(char *dst, const char *src, size_t n);
198
199 /*
200 * strstr
201 */
202 char * sl_strstr (const char * haystack, const char * needle);
203
204 /*
205 * robust strncmp replacement
206 */
207 int sl_strncmp(const char * a, const char * b, size_t n);
208
209 /*
210 * robust strcmp replacement
211 */
212 int sl_strcmp(const char * a, const char * b);
213
214 /*
215 * robust strlen replacement
216 */
217#define sl_strlen(arg) ((arg == NULL) ? 0 : (strlen(arg)))
218
219 /* ----------------------------------------------------------------
220 *
221 * Privilege handling routines
222 *
223 * ---------------------------------------------------------------- */
224
225 /*
226 * ONE OF THE FOLLOWING THREE FUNCTIONS
227 * SHOULD BE CALLED BEFORE ANY OTHER OF THE
228 * UID HANDLING FUNCTIONS.
229 */
230 int sl_policy_get_user(char *username); /* drop SUID to <username> */
231 int sl_policy_get_real(char *username); /* drop privs to <username> */
232 int sl_policy_get_root(void); /* drop SUID to root */
233
234 /*
235 * If not using one of the above, use this function,
236 * and then call sh_unset_suid().
237 * This function saves the uid's.
238 * It calls abort() on error.
239 */
240 int sl_save_uids(void);
241
242 /*
243 * This function returns the saved euid.
244 * It calls abort() if the uid's are not saved already.
245 */
246 int sl_get_euid(/*@out@*/uid_t * ret);
247 uid_t sl_ret_euid();
248
249 /*
250 * This function returns the saved egid.
251 * It calls abort() if the uid's are not saved already.
252 */
253 int sl_get_egid(/*@out@*/gid_t * ret);
254
255 /*
256 * This function returns the saved current ruid.
257 * It calls abort() if the uid's are not saved already.
258 */
259 int sl_get_ruid(/*@out@*/uid_t * ret);
260
261 /*
262 * This function returns the saved current rgid.
263 * It calls abort() if the uid's are not saved already.
264 */
265 int sl_get_rgid(gid_t * ret);
266
267 /*
268 * This function returns the saved original ruid.
269 * It calls abort() if the uid's are not saved already.
270 */
271 int sl_get_ruid_orig(uid_t * ret);
272
273 /*
274 * This function returns the saved original rgid.
275 * It calls abort() if the uid's are not saved already.
276 */
277 int sl_get_rgid_orig(gid_t * ret);
278
279 /*
280 * This function returns true if the program is SUID.
281 * It calls abort() if the uid's are not saved already.
282 */
283 int sl_is_suid(void);
284
285 /*
286 * This function sets the effective uid
287 * to the saved effective uid.
288 */
289 int sl_set_suid (void);
290
291 /*
292 * This function sets the effective uid to the real uid.
293 */
294 int sl_unset_suid (void);
295
296 /*
297 * This function drops SUID privileges irrevocably.
298 */
299 int sl_drop_privileges(void);
300
301 /* ----------------------------------------------------------------
302 *
303 * File handling routines
304 *
305 * ---------------------------------------------------------------- */
306
307 SL_TICKET sl_make_ticket (int fd, const char * path);
308
309 /* Open for writing.
310 */
311 SL_TICKET sl_open_write (const char * fname, int priviledge_mode);
312
313 /* Open for reading.
314 */
315 SL_TICKET sl_open_read (const char * fname, int priviledge_mode);
316
317 /* Open for reading w/minimum checking.
318 */
319 SL_TICKET sl_open_fastread (const char * fname, int priviledge_mode);
320
321 /* Open for read and write.
322 */
323 SL_TICKET sl_open_rdwr (const char * fname, int priviledge_mode);
324
325 /* Open for read and write, fail if file exists.
326 */
327 SL_TICKET sl_open_safe_rdwr (const char * fname, int priv);
328
329 /* Open for write, truncate.
330 */
331 SL_TICKET sl_open_write_trunc (const char * fname, int priviledge_mode);
332
333 /* Open for read and write, truncate.
334 */
335 SL_TICKET sl_open_rdwr_trunc (const char * fname, int priviledge_mode);
336
337 /* Initialize the content sh_string.
338 */
339 int sl_init_content (SL_TICKET ticket, size_t size);
340
341 /* Get the (pointer to) the content sh_string.
342 */
343 sh_string * sl_get_content (SL_TICKET ticket);
344
345 /* Close file.
346 */
347 int sl_close (SL_TICKET ticket);
348
349 /* Unlink file.
350 */
351 int sl_unlink (SL_TICKET ticket);
352
353 /* Rewind file.
354 */
355 int sl_rewind (SL_TICKET ticket);
356
357 /* Seek file.
358 */
359 int sl_seek (SL_TICKET ticket, off_t off_data);
360
361 /* Forward file.
362 */
363 int sl_forward (SL_TICKET ticket);
364
365 /* Sync file.
366 */
367 int sl_sync (SL_TICKET ticket);
368
369 /* Read file.
370 */
371 int sl_read (SL_TICKET ticket, void * buf, size_t count);
372
373 int sl_read_timeout_prep (SL_TICKET ticket);
374
375 int sl_read_timeout_fd (int fd, void * buf,
376 size_t count, int timeout, int is_nonblocking);
377
378 int sl_read_timeout (SL_TICKET ticket, void * buf,
379 size_t count, int timeout, int is_nonblocking);
380
381 int sl_read_fast (SL_TICKET ticket, void * buf_in, size_t count);
382
383 /* Write file.
384 */
385 int sl_write (SL_TICKET ticket, void * msg, long nbytes);
386
387 /* Write file, terminate with newline.
388 */
389 int sl_write_line (SL_TICKET ticket, void * msg, long nbytes);
390
391 /* As above, but only for non-constant strings.
392 */
393 int sl_write_line_fast (SL_TICKET ticket, void * msg, long nbytes);
394
395 /* Drop all metadata for file descriptors >= fd.
396 */
397 int sl_dropall(int fd, int except);
398
399 /* Check whether file is trustworthy.
400 */
401 int sl_trustfile(char * path, uid_t * ok, uid_t * bad);
402
403 /* Check whether file is trustworthy.
404 */
405 int sl_trustfile_euid(char * filename, uid_t euid);
406
407 /* purge list of trusted users
408 */
409 int sl_trust_purge_user ();
410
411 /* Add a trusted user.
412 */
413 int sl_trust_add_user (uid_t pwid);
414
415 /* Get error string.
416 */
417 char * sl_error_string(int errorcode);
418
419 /* Get error file.
420 */
421 char * sl_trust_errfile(void);
422
423 /* Overflow tests
424 */
425 int sl_ok_muli (int a, int b) SL_GNUC_CONST;
426 int sl_ok_divi (int a, int b) SL_GNUC_CONST;
427 int sl_ok_addi (int a, int b) SL_GNUC_CONST;
428 int sl_ok_subi (int a, int b) SL_GNUC_CONST;
429
430 int sl_ok_muls (size_t a, size_t b) SL_GNUC_CONST;
431 int sl_ok_adds (size_t a, size_t b) SL_GNUC_CONST;
432
433
434#ifdef __cplusplus
435}
436#endif
437
438/* Privilege modes for file access.
439 */
440#define SL_YESPRIV 0x33
441#define SL_NOPRIV 0x34
442
443/* Suitable for Linux
444 */
445#define MAXFILENAME 4096
446
447
448/*
449 * This macro is TRUE if (x) < 0.
450 */
451#define SL_ISERROR(x) ((long)(x) < 0)
452
453#if defined(WITH_TPT)
454#define TPT(arg) dlog arg ;
455#else
456#define TPT(arg)
457#endif
458
459
460/*
461 * The 'require' macro.
462 */
463#define SL_REQUIRE(assertion, astext) \
464do { \
465 /*@i@*/ if (assertion) ; \
466 else { \
467 dlog(0, FIL__, __LINE__, SDG_AFAIL, \
468 FIL__, __LINE__, astext); \
469 _exit(EXIT_FAILURE); \
470 } \
471} while (0)
472
473
474/*
475 * The enter macro. Prints the trace if TRACE is on.
476 */
477extern int slib_do_trace;
478extern int slib_trace_fd;
479
480#if defined(SL_DEBUG)
481#define SL_ENTER(s) sl_stack_push(s, FIL__, __LINE__);
482#else
483#define SL_ENTER(s) if (slib_do_trace != 0) sl_trace_in(s, FIL__, __LINE__);
484#endif
485
486/*
487 * The return macro.
488 */
489#if defined(SL_DEBUG)
490#ifndef S_SPLINT_S
491#define SL_RETURN(x, s) \
492do { \
493 sl_stack_pop(s, FIL__, __LINE__); \
494 return(x); \
495} while(0)
496#else
497/*@notfunction@*/
498#define SL_RETURN(x, s) return(x);
499#endif /* S_SPLINT_S */
500#else
501#ifndef S_SPLINT_S
502#define SL_RETURN(x, s) \
503do { \
504 if (slib_do_trace != 0) \
505 sl_trace_out(s, FIL__, __LINE__); \
506 return(x); \
507} while(0)
508#else
509/*@notfunction@*/
510#define SL_RETURN(x, s) return(x);
511#endif /* S_SPLINT_S */
512#endif /* SL_RETURN macro */
513
514#if defined(SL_DEBUG)
515#define SL_RET0(s) \
516do { \
517 sl_stack_pop(s, FIL__, __LINE__); \
518 return; \
519} while(0)
520#else
521#ifndef S_SPLINT_S
522#define SL_RET0(s) \
523do { \
524 if (slib_do_trace != 0) \
525 sl_trace_out(s, FIL__, __LINE__); \
526 return; \
527} while(0)
528#else
529/*@notfunction@*/
530#define SL_RET0(s) return;
531#endif /* S_SPLINT_S */
532#endif /* SL_RETURN macro */
533
534#if defined(SL_DEBUG)
535void sl_stack_push(char * c, char * file, int line);
536void sl_stack_pop(char * c, char * file, int line);
537void sl_stack_print();
538#endif
539void sl_trace_in (char * str, char * file, int line);
540void sl_trace_out (char * str, char * file, int line);
541int sl_trace_file (const char * str);
542int sl_trace_use (const char * str);
543
544
545
546
547/*
548 * The internal return macro. Sets sl_errno to the return value.
549 */
550
551#if defined(SL_DEBUG)
552#define SL_IRETURN(x, s) \
553do { \
554 if((long)(x) < 0) { \
555 TPT((0, FIL__, __LINE__, SDG_ERROR, (long)(x))) \
556 sl_errno=(x); \
557 } \
558 sl_stack_pop(s, FIL__, __LINE__); \
559 if (1) return(x); \
560} while(0)
561#else
562#define SL_IRETURN(x, s) \
563do { \
564 if ((long)(x) < 0) sl_errno=(x); \
565 if (slib_do_trace) \
566 sl_trace_out(s, FIL__, __LINE__); \
567 if (1) return(x); \
568} while(0)
569
570#endif /* SL_IRETURN macro */
571
572
573
574/* slib.h */
575#endif
576
577
578
579
Note: See TracBrowser for help on using the repository browser.