Be more verbose if the uid file could not be mapped.
[adu.git] / adu.h
1 /*
2  * Copyright (C) 1997-2008 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file para.h global paraslash definitions */
8
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <sys/wait.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <time.h> /* time(), localtime() */
15 #include <unistd.h>
16 #include <errno.h>
17 #include <limits.h>
18 #include <stdarg.h>
19 #include <ctype.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22 #include <sys/socket.h>
23 #include <sys/un.h> /* needed by create_pf_socket */
24 #include <string.h>
25 #include <assert.h>
26 #include <osl.h>
27 #include "gcc-compat.h"
28
29 /** used in various contexts */
30 #define MAXLINE 255
31
32 /** compute the minimum of \a a and \a b */
33 #define MIN(a,b) ((a) < (b) ? (a) : (b))
34 /** compute the maximum of \a a and \a b */
35 #define MAX(a,b) ((a) > (b) ? (a) : (b))
36 /** compute the absolute value of \a a */
37 #define ABS(a) ((a) > 0 ? (a) : -(a))
38
39 /** debug loglevel, gets really noisy */
40 #define DEBUG 1
41 /** still noisy, but won't fill your disk */
42 #define INFO  2
43 /** normal, but significant event */
44 #define NOTICE 3
45 /** unexpected event that can be handled */
46 #define WARNING 4
47 /** unhandled error condition */
48 #define ERROR 5
49 /** system might be unreliable */
50 #define CRIT 6
51 /** last message before exit */
52 #define EMERG 7
53
54 /** Log messages with lower priority than that will not be compiled in. */
55 #define COMPILE_TIME_LOGLEVEL 0
56
57 /** \cond */
58 #if DEBUG > COMPILE_TIME_LOGLEVEL
59 #define DEBUG_LOG(f,...) __log(DEBUG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
60 #else
61 #define DEBUG_LOG(...) do {;} while (0)
62 #endif
63
64 #if INFO > COMPILE_TIME_LOGLEVEL
65 #define INFO_LOG(f,...) __log(INFO, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
66 #else
67 #define INFO_LOG(...) do {;} while (0)
68 #endif
69
70 #if NOTICE > COMPILE_TIME_LOGLEVEL
71 #define NOTICE_LOG(f,...) __log(NOTICE, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
72 #else
73 #define NOTICE_LOG(...) do {;} while (0)
74 #endif
75
76 #if WARNING > COMPILE_TIME_LOGLEVEL
77 #define WARNING_LOG(f,...) __log(WARNING, "%s: " f, __FUNCTION__, ##  __VA_ARGS__)
78 #else
79 #define WARNING_LOG(...) do {;} while (0)
80 #endif
81
82 #if ERROR > COMPILE_TIME_LOGLEVEL
83 #define ERROR_LOG(f,...) __log(ERROR, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
84 #else
85 #define ERROR_LOG(...) do {;} while (0)
86 #endif
87
88 #if CRIT > COMPILE_TIME_LOGLEVEL
89 #define CRIT_LOG(f,...) __log(CRIT, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
90 #else
91 #define CRIT_LOG(...) do {;} while (0)
92 #endif
93
94 #if EMERG > COMPILE_TIME_LOGLEVEL
95 #define EMERG_LOG(f,...) __log(EMERG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
96 #else
97 #define EMERG_LOG(...)
98 #endif
99 /** \endcond */
100
101 /**
102  * define a standard log function that always writes to stderr
103  *
104  * \param loglevel_barrier If the loglevel of the current message
105  * is less than that, the message is going to be ignored.
106  *
107  */
108 #define INIT_STDERR_LOGGING(loglevel_barrier) \
109         __printf_2_3 void __log(int ll, const char* fmt,...) \
110         { \
111                 va_list argp; \
112                 if (ll < loglevel_barrier) \
113                         return; \
114                 va_start(argp, fmt); \
115                 vfprintf(stderr, fmt, argp); \
116                 va_end(argp); \
117         }
118
119 /** version text used by various commands if -V switch was given */
120 #define VERSION_TEXT(prefix) "para_" prefix " " PACKAGE_VERSION " (" CODENAME ")" "\n" \
121         "Copyright (C) 2008 Andre Noll\n" \
122         "This is free software with ABSOLUTELY NO WARRANTY." \
123         " See COPYING for details.\n" \
124         "Written by Andre Noll.\n" \
125         "Report bugs to <maan@systemlinux.org>.\n"
126
127 /** print out \p VERSION_TEXT and exit if version flag was given */
128 #define HANDLE_VERSION_FLAG(_prefix, _args_info_struct) \
129         if (_args_info_struct.version_given) { \
130                 printf("%s", VERSION_TEXT(_prefix)); \
131                 exit(EXIT_SUCCESS); \
132         }
133 /** sent by para_server for commands that expect a data file */
134 #define AWAITING_DATA_MSG "\nAwaiting Data."
135 /** sent by para_server if authentication was successful */
136 #define PROCEED_MSG "\nProceed.\n"
137 /** length of the \p PROCEED_MSG string */
138 #define PROCEED_MSG_LEN strlen(PROCEED_MSG)
139 /** sent by para_client to indicate the end of the command line */
140 #define EOC_MSG "\nEnd of Command."
141 /** sent by para_client, followed by the decrypted challenge number */
142 #define CHALLENGE_RESPONSE_MSG "challenge_response:"
143
144 /* exec */
145 int para_exec_cmdline_pid(pid_t *pid, const char *cmdline, int *fds);
146
147 /* time */
148 int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *diff);
149 long unsigned tv2ms(const struct timeval*);
150 void d2tv(double, struct timeval*);
151 void tv_add(const struct timeval*, const struct timeval *, struct timeval *);
152 void tv_scale(const unsigned long, const struct timeval *, struct timeval *);
153 void tv_divide(const unsigned long divisor, const struct timeval *tv,
154         struct timeval *result);
155 int tv_convex_combination(const long a, const struct timeval *tv1,
156                 const long b, const struct timeval *tv2,
157                 struct timeval *result);
158 void ms2tv(const long unsigned n, struct timeval *tv);
159 void compute_chunk_time(long unsigned chunk_num,
160                 struct timeval *chunk_tv, struct timeval *stream_start,
161                 struct timeval *result);
162
163 __printf_2_3 void __log(int, const char*, ...);
164
165 /**
166  * Write a log message to a dynamically allocated string.
167  *
168  * \param fmt Usual format string.
169  * \param p Result pointer.
170  *
171  * \sa printf(3). */
172 #define VSPRINTF(fmt, p) \
173 { \
174         int n; \
175         size_t size = 100; \
176         p = para_malloc(size); \
177         while (1) { \
178                 va_list ap; \
179                 /* Try to print in the allocated space. */ \
180                 va_start(ap, fmt); \
181                 n = vsnprintf(p, size, fmt, ap); \
182                 va_end(ap); \
183                 /* If that worked, return the string. */ \
184                 if (n > -1 && n < size) \
185                         break; \
186                 /* Else try again with more space. */ \
187                 if (n > -1) /* glibc 2.1 */ \
188                         size = n + 1; /* precisely what is needed */ \
189                 else /* glibc 2.0 */ \
190                         size *= 2; /* twice the old size */ \
191                 p = para_realloc(p, size); \
192         } \
193 }
194
195 /**
196  *  Return a random non-negative integer in an interval.
197  *
198  * \param max Determines maximal possible return value.
199  *
200  * \return An integer between zero and \p max - 1, inclusively.
201  */
202 static inline long int para_random(unsigned max)
203 {
204         return ((max + 0.0) * (random() / (RAND_MAX + 1.0)));
205 }
206
207 /** Round up x to a multiple of y */
208 #define ROUND_UP(x, y) (((x) + ((y) - 1) / (y)) * (y))
209
210 /** Get the size of an array */
211 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
212
213 /**
214  * Wrapper for isspace.
215  * NetBSD needs this.
216  */
217 /*
218  * The values should be cast to an unsigned char first, then to int.
219  * Why? Because the isdigit (as do all other is/to functions/macros)
220  * expect a number from 0 upto and including 255 as their (int) argument.
221  * Because char is signed on most systems, casting it to int immediately
222  * gives the functions an argument between -128 and 127 (inclusive),
223  * which they will use as an array index, and which will thus fail
224  * horribly for characters which have their most significant bit set.
225  */
226 #define para_isspace(c) isspace((int)(unsigned char)(c))