source: trunk/include/slib.h@ 225

Last change on this file since 225 was 214, checked in by katerina, 16 years ago

Rewrite mail subsystem for more flexibility (closes ticket #141).

File size: 14.9 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#define SL_EFSTAT -1042 /* fstat of file failed. Check errno. */
111
112#define SL_EBADUID -1050 /* Owner not trustworthy. */
113#define SL_EBADGID -1051 /* Group writeable and not trustworthy.*/
114#define SL_EBADOTH -1052 /* World writeable. */
115
116#define SL_TOOMANY -1053 /* Too many open files */
117#define SL_TIMEOUT -1054 /* Timeout in read */
118
119#define SL_EISDIR -1055 /* Is a directory */
120/*
121 * All int functions return SL_NONE on success.
122 */
123
124#ifdef __cplusplus
125extern "C" {
126#endif
127
128 int dlog (int flag, const char * file, int line, const char *fmt, ...);
129
130 char * sl_get_errmsg(void);
131
132 /* ----------------------------------------------------------------
133 *
134 * Heap consistency routines
135 *
136 * ---------------------------------------------------------------- */
137
138 int sl_test_heap(void);
139
140 /* ----------------------------------------------------------------
141 *
142 * Capability routines
143 *
144 * ---------------------------------------------------------------- */
145
146 extern int sl_useCaps;
147
148 int sl_drop_cap (void);
149 int sl_drop_cap_sub(void);
150 int sl_get_cap_sub(void);
151 int sl_drop_cap_qdel(void);
152 int sl_get_cap_qdel(void);
153
154 /* ----------------------------------------------------------------
155 *
156 * String handling routines
157 *
158 * ---------------------------------------------------------------- */
159
160 /*
161 * A memset that does not get optimized away
162 */
163 void *sl_memset(void *s, int c, size_t n);
164#if !defined(SH_REAL_SET)
165#undef memset
166#define memset sl_memset
167#endif
168
169 /*
170 * Copy src to dst. siz is the length of dst.
171 */
172 int sl_strlcpy(char * dst, /*@null@*/const char * src, size_t siz);
173
174 /*
175 * Append src to dst. siz is the length of dst.
176 */
177 int sl_strlcat(char * dst, /*@null@*/const char *src, size_t siz);
178
179 /*
180 * An implementation of vsnprintf. va_start/va_end are in the caller
181 * function.
182 */
183 int sl_vsnprintf(char *str, size_t n,
184 const char *format, va_list vl );
185
186 /*
187 * An implementation of snprintf.
188 */
189 int sl_snprintf(char *str, size_t n,
190 const char *format, ... );
191
192 /*
193 * A robust drop-in replacement of strncpy. strlcpy is preferable.
194 */
195 char * sl_strncpy(/*@out@*/char *dst, const char *src, size_t size);
196
197 /*
198 * Robust strncat.
199 */
200 char * sl_strncat(char *dst, const char *src, size_t n);
201
202 /*
203 * strstr
204 */
205 char * sl_strstr (const char * haystack, const char * needle);
206
207 /*
208 * robust strncmp replacement
209 */
210 int sl_strncmp(const char * a, const char * b, size_t n);
211
212 /*
213 * robust strcmp replacement
214 */
215 int sl_strcmp(const char * a, const char * b);
216
217 /*
218 * robust strcasecmp replacement
219 */
220 int sl_strcasecmp(const char * one, const char * two);
221
222 /*
223 * robust strlen replacement
224 */
225#define sl_strlen(arg) ((arg == NULL) ? 0 : (strlen(arg)))
226
227 /* ----------------------------------------------------------------
228 *
229 * Privilege handling routines
230 *
231 * ---------------------------------------------------------------- */
232
233 /*
234 * ONE OF THE FOLLOWING THREE FUNCTIONS
235 * SHOULD BE CALLED BEFORE ANY OTHER OF THE
236 * UID HANDLING FUNCTIONS.
237 */
238 int sl_policy_get_user(const char *username); /* drop SUID to <username> */
239 int sl_policy_get_real(char *username); /* drop privs to <username> */
240 int sl_policy_get_root(void); /* drop SUID to root */
241
242 /*
243 * If not using one of the above, use this function,
244 * and then call sh_unset_suid().
245 * This function saves the uid's.
246 * It calls abort() on error.
247 */
248 int sl_save_uids(void);
249
250 /*
251 * This function returns the saved euid.
252 * It calls abort() if the uid's are not saved already.
253 */
254 int sl_get_euid(/*@out@*/uid_t * ret);
255 uid_t sl_ret_euid(void);
256
257 /*
258 * This function returns the saved egid.
259 * It calls abort() if the uid's are not saved already.
260 */
261 int sl_get_egid(/*@out@*/gid_t * ret);
262
263 /*
264 * This function returns the saved current ruid.
265 * It calls abort() if the uid's are not saved already.
266 */
267 int sl_get_ruid(/*@out@*/uid_t * ret);
268
269 /*
270 * This function returns the saved current rgid.
271 * It calls abort() if the uid's are not saved already.
272 */
273 int sl_get_rgid(gid_t * ret);
274
275 /*
276 * This function returns the saved original ruid.
277 * It calls abort() if the uid's are not saved already.
278 */
279 int sl_get_ruid_orig(uid_t * ret);
280
281 /*
282 * This function returns the saved original rgid.
283 * It calls abort() if the uid's are not saved already.
284 */
285 int sl_get_rgid_orig(gid_t * ret);
286
287 /*
288 * This function returns true if the program is SUID.
289 * It calls abort() if the uid's are not saved already.
290 */
291 int sl_is_suid(void);
292
293 /*
294 * This function sets the effective uid
295 * to the saved effective uid.
296 */
297 int sl_set_suid (void);
298
299 /*
300 * This function sets the effective uid to the real uid.
301 */
302 int sl_unset_suid (void);
303
304 /*
305 * This function drops SUID privileges irrevocably.
306 */
307 int sl_drop_privileges(void);
308
309 /* ----------------------------------------------------------------
310 *
311 * File handling routines
312 *
313 * ---------------------------------------------------------------- */
314
315 SL_TICKET sl_make_ticket (int fd, const char * path);
316
317 /* Open for writing.
318 */
319 SL_TICKET sl_open_write (const char * fname, int priviledge_mode);
320
321 /* Open for reading.
322 */
323 SL_TICKET sl_open_read (const char * fname, int priviledge_mode);
324
325 /* Drop from cach when closing
326 */
327 int sl_set_drop_cache(const char * str);
328
329 /* Open for reading w/minimum checking.
330 */
331 SL_TICKET sl_open_fastread (const char * fname, int priviledge_mode);
332
333 /* Open for read and write.
334 */
335 SL_TICKET sl_open_rdwr (const char * fname, int priviledge_mode);
336
337 /* Open for read and write, fail if file exists.
338 */
339 SL_TICKET sl_open_safe_rdwr (const char * fname, int priv);
340
341 /* Open for write, truncate.
342 */
343 SL_TICKET sl_open_write_trunc (const char * fname, int priviledge_mode);
344
345 /* Open for read and write, truncate.
346 */
347 SL_TICKET sl_open_rdwr_trunc (const char * fname, int priviledge_mode);
348
349 /* Initialize the content sh_string.
350 */
351 int sl_init_content (SL_TICKET ticket, size_t size);
352
353 /* Get the (pointer to) the content sh_string.
354 */
355 sh_string * sl_get_content (SL_TICKET ticket);
356
357 /* Lock file (uses fcntl F_SETLK).
358 */
359 int sl_lock (SL_TICKET ticket);
360
361 /* Close file.
362 */
363 int sl_close (SL_TICKET ticket);
364
365 /* Unlink file.
366 */
367 int sl_unlink (SL_TICKET ticket);
368
369 /* Rewind file.
370 */
371 int sl_rewind (SL_TICKET ticket);
372
373 /* Seek file.
374 */
375 int sl_seek (SL_TICKET ticket, off_t off_data);
376
377 /* Forward file.
378 */
379 int sl_forward (SL_TICKET ticket);
380
381 /* Sync file.
382 */
383 int sl_sync (SL_TICKET ticket);
384
385 /* Read file.
386 */
387 int sl_read (SL_TICKET ticket, void * buf, size_t count);
388
389 int sl_read_timeout_prep (SL_TICKET ticket);
390
391 int sl_read_timeout_fd (int fd, void * buf,
392 size_t count, int timeout, int is_nonblocking);
393
394 int sl_read_timeout (SL_TICKET ticket, void * buf,
395 size_t count, int timeout, int is_nonblocking);
396
397 int sl_read_fast (SL_TICKET ticket, void * buf_in, size_t count);
398
399 /* Write file.
400 */
401 int sl_write (SL_TICKET ticket, const void * msg, long nbytes);
402
403 /* Write file, terminate with newline.
404 */
405 int sl_write_line (SL_TICKET ticket, const void * msg, long nbytes);
406
407 /* As above, but only for non-constant strings.
408 */
409 int sl_write_line_fast (SL_TICKET ticket, void * msg, long nbytes);
410
411 /* Drop all metadata for file descriptors >= fd.
412 */
413 int sl_dropall(int fd, int except);
414 int sl_dropall_dirty(int fd, int except); /* don't deallocate */
415
416 /* Check whether file is trustworthy.
417 */
418 int sl_trustfile(const char * path, uid_t * ok, uid_t * bad);
419
420 /* Check whether file is trustworthy.
421 */
422 int sl_trustfile_euid(const char * filename, uid_t euid);
423
424 /* purge list of trusted users
425 */
426 int sl_trust_purge_user (void);
427
428 /* Add a trusted user.
429 */
430 int sl_trust_add_user (uid_t pwid);
431
432 /* Get error string.
433 */
434 char * sl_error_string(int errorcode);
435
436 /* Get error file.
437 */
438 char * sl_trust_errfile(void);
439
440 /* Overflow tests
441 */
442 int sl_ok_muli (int a, int b) SL_GNUC_CONST;
443 int sl_ok_divi (int a, int b) SL_GNUC_CONST;
444 int sl_ok_addi (int a, int b) SL_GNUC_CONST;
445 int sl_ok_subi (int a, int b) SL_GNUC_CONST;
446
447 int sl_ok_muls (size_t a, size_t b) SL_GNUC_CONST;
448 int sl_ok_adds (size_t a, size_t b) SL_GNUC_CONST;
449
450
451#ifdef __cplusplus
452}
453#endif
454
455/* Privilege modes for file access.
456 */
457#define SL_YESPRIV 0x33
458#define SL_NOPRIV 0x34
459
460/* Suitable for Linux
461 */
462#define MAXFILENAME 4096
463
464
465/*
466 * This macro is TRUE if (x) < 0.
467 */
468#define SL_ISERROR(x) ((long)(x) < 0)
469
470#if defined(WITH_TPT)
471#define TPT(arg) dlog arg ;
472#else
473#define TPT(arg)
474#endif
475
476
477/*
478 * The 'require' macro.
479 */
480#define SL_REQUIRE(assertion, astext) \
481do { \
482 /*@i@*/ if (assertion) ; \
483 else { \
484 dlog(0, FIL__, __LINE__, SDG_AFAIL, \
485 FIL__, __LINE__, astext); \
486 _exit(EXIT_FAILURE); \
487 } \
488} while (0)
489
490
491/*
492 * The enter macro. Prints the trace if TRACE is on.
493 */
494extern int slib_do_trace;
495extern int slib_trace_fd;
496
497#if defined(SL_DEBUG)
498#define SL_ENTER(s) sl_stack_push(s, FIL__, __LINE__);
499#else
500#define SL_ENTER(s) if (slib_do_trace != 0) sl_trace_in(s, FIL__, __LINE__);
501#endif
502
503/*
504 * The return macro.
505 */
506#if defined(SL_DEBUG)
507#ifndef S_SPLINT_S
508#define SL_RETURN(x, s) \
509do { \
510 sl_stack_pop(s, FIL__, __LINE__); \
511 return(x); \
512} while(0)
513#else
514/*@notfunction@*/
515#define SL_RETURN(x, s) return(x);
516#endif /* S_SPLINT_S */
517#else
518#ifndef S_SPLINT_S
519#define SL_RETURN(x, s) \
520do { \
521 if (slib_do_trace != 0) \
522 sl_trace_out(s, FIL__, __LINE__); \
523 return(x); \
524} while(0)
525#else
526/*@notfunction@*/
527#define SL_RETURN(x, s) return(x);
528#endif /* S_SPLINT_S */
529#endif /* SL_RETURN macro */
530
531#if defined(SL_DEBUG)
532#define SL_RET0(s) \
533do { \
534 sl_stack_pop(s, FIL__, __LINE__); \
535 return; \
536} while(0)
537#else
538#ifndef S_SPLINT_S
539#define SL_RET0(s) \
540do { \
541 if (slib_do_trace != 0) \
542 sl_trace_out(s, FIL__, __LINE__); \
543 return; \
544} while(0)
545#else
546/*@notfunction@*/
547#define SL_RET0(s) return;
548#endif /* S_SPLINT_S */
549#endif /* SL_RETURN macro */
550
551#if defined(SL_DEBUG)
552void sl_stack_push(char * c, char * file, int line);
553void sl_stack_pop(char * c, char * file, int line);
554void sl_stack_print(void);
555#endif
556void sl_trace_in (const char * str, const char * file, int line);
557void sl_trace_out (const char * str, const char * file, int line);
558int sl_trace_file (const char * str);
559int sl_trace_use (const char * str);
560
561
562
563
564/*
565 * The internal return macro. Sets sl_errno to the return value.
566 */
567
568#if defined(SL_DEBUG)
569#define SL_IRETURN(x, s) \
570do { \
571 if((long)(x) < 0) { \
572 TPT((0, FIL__, __LINE__, SDG_ERROR, (long)(x))) \
573 sl_errno=(x); \
574 } \
575 sl_stack_pop(s, FIL__, __LINE__); \
576 if (1) return(x); \
577} while(0)
578#else
579#define SL_IRETURN(x, s) \
580do { \
581 if ((long)(x) < 0) sl_errno=(x); \
582 if (slib_do_trace) \
583 sl_trace_out(s, FIL__, __LINE__); \
584 if (1) return(x); \
585} while(0)
586
587#endif /* SL_IRETURN macro */
588
589
590
591/* slib.h */
592#endif
593
594
595
596
Note: See TracBrowser for help on using the repository browser.