source: trunk/include/slib.h@ 194

Last change on this file since 194 was 192, checked in by katerina, 16 years ago

Fixes for cygwin, and improved error diagnostics (tickets #126, #127, #128).

File size: 14.7 KB
RevLine 
[1]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
[167]26#include "sh_string.h"
27
[1]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
[138]64#define SH_GRBUF_SIZE 4096
65#define SH_PWBUF_SIZE 4096
[1]66
67
[149]68#if defined(__GNUC__) && (__GNUC__ >= 3)
69#undef SL_GNUC_CONST
70#define SL_GNUC_CONST __attribute__((const))
71#else
[156]72#undef SL_GNUC_CONST
73#define SL_GNUC_CONST
[149]74#endif
75
[1]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. */
[192]110#define SL_EFSTAT -1042 /* fstat of file failed. Check errno. */
[1]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 */
[192]118
119#define SL_EISDIR -1055 /* Is a directory */
[1]120/*
121 * All int functions return SL_NONE on success.
122 */
123
124#ifdef __cplusplus
125extern "C" {
126#endif
127
[170]128 int dlog (int flag, const char * file, int line, const char *fmt, ...);
[1]129
[170]130 char * sl_get_errmsg(void);
[1]131
132 /* ----------------------------------------------------------------
133 *
134 * Heap consistency routines
135 *
136 * ---------------------------------------------------------------- */
137
[170]138 int sl_test_heap(void);
[1]139
140 /* ----------------------------------------------------------------
141 *
142 * Capability routines
143 *
144 * ---------------------------------------------------------------- */
145
146 extern int sl_useCaps;
147
[170]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);
[1]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);
[11]164#if !defined(SH_REAL_SET)
[1]165#undef memset
166#define memset sl_memset
[11]167#endif
[1]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 */
[190]205 const char * sl_strstr (const char * haystack, const char * needle);
[1]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 /*
[169]218 * robust strcasecmp replacement
219 */
220 int sl_strcasecmp(const char * one, const char * two);
221
222 /*
[1]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 */
[170]238 int sl_policy_get_user(const char *username); /* drop SUID to <username> */
[1]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);
[170]255 uid_t sl_ret_euid(void);
[1]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
[76]315 SL_TICKET sl_make_ticket (int fd, const char * path);
[1]316
317 /* Open for writing.
318 */
[20]319 SL_TICKET sl_open_write (const char * fname, int priviledge_mode);
[1]320
321 /* Open for reading.
322 */
[20]323 SL_TICKET sl_open_read (const char * fname, int priviledge_mode);
[1]324
325 /* Open for reading w/minimum checking.
326 */
[20]327 SL_TICKET sl_open_fastread (const char * fname, int priviledge_mode);
[1]328
329 /* Open for read and write.
330 */
[20]331 SL_TICKET sl_open_rdwr (const char * fname, int priviledge_mode);
[1]332
333 /* Open for read and write, fail if file exists.
334 */
[20]335 SL_TICKET sl_open_safe_rdwr (const char * fname, int priv);
[1]336
337 /* Open for write, truncate.
338 */
[20]339 SL_TICKET sl_open_write_trunc (const char * fname, int priviledge_mode);
[1]340
341 /* Open for read and write, truncate.
342 */
[20]343 SL_TICKET sl_open_rdwr_trunc (const char * fname, int priviledge_mode);
[1]344
[167]345 /* Initialize the content sh_string.
346 */
347 int sl_init_content (SL_TICKET ticket, size_t size);
348
349 /* Get the (pointer to) the content sh_string.
350 */
351 sh_string * sl_get_content (SL_TICKET ticket);
352
[1]353 /* Close file.
354 */
355 int sl_close (SL_TICKET ticket);
356
357 /* Unlink file.
358 */
359 int sl_unlink (SL_TICKET ticket);
360
361 /* Rewind file.
362 */
363 int sl_rewind (SL_TICKET ticket);
364
365 /* Seek file.
366 */
367 int sl_seek (SL_TICKET ticket, off_t off_data);
368
369 /* Forward file.
370 */
371 int sl_forward (SL_TICKET ticket);
372
373 /* Sync file.
374 */
375 int sl_sync (SL_TICKET ticket);
376
377 /* Read file.
378 */
379 int sl_read (SL_TICKET ticket, void * buf, size_t count);
380
[8]381 int sl_read_timeout_prep (SL_TICKET ticket);
382
[131]383 int sl_read_timeout_fd (int fd, void * buf,
384 size_t count, int timeout, int is_nonblocking);
385
[1]386 int sl_read_timeout (SL_TICKET ticket, void * buf,
[131]387 size_t count, int timeout, int is_nonblocking);
[1]388
389 int sl_read_fast (SL_TICKET ticket, void * buf_in, size_t count);
390
391 /* Write file.
392 */
[170]393 int sl_write (SL_TICKET ticket, const void * msg, long nbytes);
[1]394
395 /* Write file, terminate with newline.
396 */
[170]397 int sl_write_line (SL_TICKET ticket, const void * msg, long nbytes);
[1]398
[76]399 /* As above, but only for non-constant strings.
400 */
401 int sl_write_line_fast (SL_TICKET ticket, void * msg, long nbytes);
402
[1]403 /* Drop all metadata for file descriptors >= fd.
404 */
405 int sl_dropall(int fd, int except);
[174]406 int sl_dropall_dirty(int fd, int except); /* don't deallocate */
[1]407
408 /* Check whether file is trustworthy.
409 */
[183]410 int sl_trustfile(const char * path, uid_t * ok, uid_t * bad);
[1]411
412 /* Check whether file is trustworthy.
413 */
[183]414 int sl_trustfile_euid(const char * filename, uid_t euid);
[1]415
416 /* purge list of trusted users
417 */
[170]418 int sl_trust_purge_user (void);
[1]419
420 /* Add a trusted user.
421 */
422 int sl_trust_add_user (uid_t pwid);
423
424 /* Get error string.
425 */
426 char * sl_error_string(int errorcode);
427
428 /* Get error file.
429 */
430 char * sl_trust_errfile(void);
431
[20]432 /* Overflow tests
433 */
[149]434 int sl_ok_muli (int a, int b) SL_GNUC_CONST;
435 int sl_ok_divi (int a, int b) SL_GNUC_CONST;
436 int sl_ok_addi (int a, int b) SL_GNUC_CONST;
437 int sl_ok_subi (int a, int b) SL_GNUC_CONST;
[1]438
[149]439 int sl_ok_muls (size_t a, size_t b) SL_GNUC_CONST;
440 int sl_ok_adds (size_t a, size_t b) SL_GNUC_CONST;
[34]441
442
[1]443#ifdef __cplusplus
444}
445#endif
446
447/* Privilege modes for file access.
448 */
449#define SL_YESPRIV 0x33
450#define SL_NOPRIV 0x34
451
[76]452/* Suitable for Linux
453 */
454#define MAXFILENAME 4096
[1]455
456
457/*
458 * This macro is TRUE if (x) < 0.
459 */
460#define SL_ISERROR(x) ((long)(x) < 0)
461
462#if defined(WITH_TPT)
463#define TPT(arg) dlog arg ;
464#else
465#define TPT(arg)
466#endif
467
468
469/*
470 * The 'require' macro.
471 */
472#define SL_REQUIRE(assertion, astext) \
473do { \
474 /*@i@*/ if (assertion) ; \
475 else { \
476 dlog(0, FIL__, __LINE__, SDG_AFAIL, \
477 FIL__, __LINE__, astext); \
478 _exit(EXIT_FAILURE); \
479 } \
480} while (0)
481
482
483/*
484 * The enter macro. Prints the trace if TRACE is on.
485 */
486extern int slib_do_trace;
487extern int slib_trace_fd;
488
489#if defined(SL_DEBUG)
490#define SL_ENTER(s) sl_stack_push(s, FIL__, __LINE__);
491#else
492#define SL_ENTER(s) if (slib_do_trace != 0) sl_trace_in(s, FIL__, __LINE__);
493#endif
494
495/*
496 * The return macro.
497 */
498#if defined(SL_DEBUG)
499#ifndef S_SPLINT_S
500#define SL_RETURN(x, s) \
501do { \
502 sl_stack_pop(s, FIL__, __LINE__); \
503 return(x); \
504} while(0)
505#else
506/*@notfunction@*/
507#define SL_RETURN(x, s) return(x);
508#endif /* S_SPLINT_S */
509#else
510#ifndef S_SPLINT_S
511#define SL_RETURN(x, s) \
512do { \
513 if (slib_do_trace != 0) \
514 sl_trace_out(s, FIL__, __LINE__); \
515 return(x); \
516} while(0)
517#else
518/*@notfunction@*/
519#define SL_RETURN(x, s) return(x);
520#endif /* S_SPLINT_S */
521#endif /* SL_RETURN macro */
522
523#if defined(SL_DEBUG)
524#define SL_RET0(s) \
525do { \
526 sl_stack_pop(s, FIL__, __LINE__); \
527 return; \
528} while(0)
529#else
530#ifndef S_SPLINT_S
531#define SL_RET0(s) \
532do { \
533 if (slib_do_trace != 0) \
534 sl_trace_out(s, FIL__, __LINE__); \
535 return; \
536} while(0)
537#else
538/*@notfunction@*/
539#define SL_RET0(s) return;
540#endif /* S_SPLINT_S */
541#endif /* SL_RETURN macro */
542
543#if defined(SL_DEBUG)
544void sl_stack_push(char * c, char * file, int line);
545void sl_stack_pop(char * c, char * file, int line);
[170]546void sl_stack_print(void);
[1]547#endif
[170]548void sl_trace_in (const char * str, const char * file, int line);
549void sl_trace_out (const char * str, const char * file, int line);
[20]550int sl_trace_file (const char * str);
551int sl_trace_use (const char * str);
[1]552
553
554
555
556/*
557 * The internal return macro. Sets sl_errno to the return value.
558 */
559
560#if defined(SL_DEBUG)
561#define SL_IRETURN(x, s) \
562do { \
563 if((long)(x) < 0) { \
564 TPT((0, FIL__, __LINE__, SDG_ERROR, (long)(x))) \
565 sl_errno=(x); \
566 } \
567 sl_stack_pop(s, FIL__, __LINE__); \
568 if (1) return(x); \
569} while(0)
570#else
571#define SL_IRETURN(x, s) \
572do { \
573 if ((long)(x) < 0) sl_errno=(x); \
574 if (slib_do_trace) \
575 sl_trace_out(s, FIL__, __LINE__); \
576 if (1) return(x); \
577} while(0)
578
579#endif /* SL_IRETURN macro */
580
581
582
583/* slib.h */
584#endif
585
586
587
588
Note: See TracBrowser for help on using the repository browser.