Imported Upstream version 2.6.0p2
[debian/amanda] / common-src / amanda.h
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1999 University of Maryland at College Park
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of U.M. not be used in advertising or
11  * publicity pertaining to distribution of the software without specific,
12  * written prior permission.  U.M. makes no representations about the
13  * suitability of this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  *
16  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  * Authors: the Amanda Development Team.  Its members are listed in a
24  * file named AUTHORS, in the root directory of this distribution.
25  */
26 /*
27  * $Id: amanda.h,v 1.131 2006/07/25 18:27:56 martinea Exp $
28  *
29  * the central header file included by all amanda sources
30  */
31 #ifndef AMANDA_H
32 #define AMANDA_H
33
34 #include <glib.h>
35 #include <glib/gprintf.h>
36
37 #include "amflock.h"
38
39 #ifdef HAVE_CONFIG_H
40 /* use a relative path here to avoid conflicting with Perl's config.h. */
41 #include "../config/config.h"
42 #endif
43
44 /*
45  * Force large file source even if configure guesses wrong.
46  */
47 #ifndef _LARGE_FILE_SOURCE
48 #define _LARGE_FILES 1
49 #endif
50
51 #ifndef _LARGEFILE64_SOURCE
52 #define _LARGEFILE64_SOURCE 1
53 #endif
54
55 #ifndef  _FILE_OFFSET_BITS
56 #define _FILE_OFFSET_BITS 64
57 #endif
58
59 #ifdef HAVE_SYS_TYPES_H
60 #  include <sys/types.h>
61 #endif
62
63 /* gnulib creates this header locally if the system doesn't provide it */
64 #include <stdint.h>
65
66 /*
67  * I would prefer that each Amanda module include only those system headers
68  * that are locally needed, but on most Unixes the system header files are not
69  * protected against multiple inclusion, so this can lead to problems.
70  *
71  * Also, some systems put key files in different places, so by including 
72  * everything here the rest of the system is isolated from such things.
73  */
74 #ifdef HAVE_ALLOCA_H
75 #  include <alloca.h>
76 #endif
77
78 /* from the autoconf documentation */
79 #ifdef HAVE_DIRENT_H
80 #  include <dirent.h>
81 #  define NAMLEN(dirent) strlen((dirent)->d_name)
82 #else
83 #  define dirent direct
84 #  define NAMLEN(dirent) (dirent)->d_namlen
85 #  if HAVE_SYS_NDIR_H
86 #    include <sys/ndir.h>
87 #  endif
88 #  if HAVE_SYS_DIR_H
89 #    include <sys/dir.h>
90 #  endif
91 #  if HAVE_NDIR_H
92 #    include <ndir.h>
93 #  endif
94 #endif
95
96 #ifdef ENABLE_NLS
97 #  include <libintl.h>
98 #  include <locale.h>
99 #  define  plural(String1, String2, Count)                              \
100                 (((Count) == 1) ? (String1) : (String2))
101 #else
102 #  define plural(String1, String2, Count)                               \
103                 (((Count) == 1) ? (String1) : (String2))
104 #  define setlocale(Which, Locale)
105 #  define textdomain(Domain)
106 #  define bindtextdomain(Package, Directory)
107 #  define gettext(String)                       String
108 #  define dgettext(Domain, String)              String
109 #  define dcgettext(Domain, String, Catagory)   String
110 #  define ngettext(String1, String2, Count)                             \
111                 plural((String1), (String2), (Count))
112 #  define dngettext(Domain, String1, String2, Count)                    \
113                 plural((String1), (String2), (Count))
114 #  define dcngettext(Domain, String1, String2, Count, Catagory)         \
115                 plural((String1), (String2), (Count))
116 #endif
117 #define T_(String)                      String
118 #ifndef SWIG /* TODO: make this go away */
119 #define _(String)                       dgettext("amanda", (String))
120 #endif
121
122 #ifdef HAVE_FCNTL_H
123 #  include <fcntl.h>
124 #endif
125
126 #ifdef HAVE_GRP_H
127 #  include <grp.h>
128 #endif
129
130 #if defined(USE_DB_H)
131 #  include <db.h>
132 #else
133 #if defined(USE_DBM_H)
134 #  include <dbm.h>
135 #else
136 #if defined(USE_GDBM_H)
137 #  include <gdbm.h>
138 #else
139 #if defined(USE_NDBM_H)
140 #  include <ndbm.h>
141 #endif
142 #endif
143 #endif
144 #endif
145
146 #ifdef HAVE_NETDB_H
147 #  include <netdb.h>
148 #endif
149
150 #ifdef TIME_WITH_SYS_TIME
151 #  include <sys/time.h>
152 #  include <time.h>
153 #else
154 #  ifdef HAVE_SYS_TIME_H
155 #    include <sys/time.h>
156 #  else
157 #    include <time.h>
158 #  endif
159 #endif
160
161 #ifdef HAVE_LIBC_H
162 #  include <libc.h>
163 #endif
164
165 #ifdef HAVE_STDLIB_H
166 #  include <stdlib.h>
167 #endif
168
169 #ifdef HAVE_LIBGEN_H
170 #  include <libgen.h>
171 #endif
172
173 #ifdef HAVE_STRING_H
174 #  include <string.h>
175 #endif
176
177 #ifdef HAVE_STRINGS_H
178 #  include <strings.h>
179 #endif
180
181 #ifdef HAVE_SYSLOG_H
182 #  include <syslog.h>
183 #endif
184
185 #ifdef HAVE_MATH_H
186 #  include <math.h>
187 #endif
188
189 #ifdef HAVE_SYS_FILE_H
190 #  include <sys/file.h>
191 #endif
192
193 #ifdef HAVE_SYS_IOCTL_H
194 #  include <sys/ioctl.h>
195 #endif
196
197 #ifdef HAVE_LIMITS_H
198 #include <limits.h>
199 #endif
200
201 #ifdef HAVE_SYS_PARAM_H
202 #  include <sys/param.h>
203 #endif
204
205 #if defined(HAVE_SYS_IPC_H) && defined(HAVE_SYS_SHM_H)
206 #  include <sys/ipc.h>
207 #  include <sys/shm.h>
208 #else
209 #  ifdef HAVE_SYS_MMAN_H
210 #    include <sys/mman.h>
211 #  endif
212 #endif
213
214 #ifdef HAVE_SYS_SELECT_H
215 #  include <sys/select.h>
216 #endif
217
218 #ifdef HAVE_SYS_STAT_H
219 #  include <sys/stat.h>
220 #endif
221
222 #ifdef HAVE_SYS_UIO_H
223 #  include <sys/uio.h>
224 #else
225 struct iovec {
226     void *iov_base;
227     int iov_len;
228 };
229 #endif
230
231 #ifdef HAVE_WAIT_H
232 #  include <wait.h>
233 #endif
234
235 #ifdef HAVE_SYS_WAIT_H
236 #  include <sys/wait.h>
237 #endif
238
239 #ifdef HAVE_STDARG_H
240 #include <stdarg.h>
241 #endif
242
243 #ifdef WAIT_USES_INT
244   typedef int amwait_t;
245 # ifndef WEXITSTATUS
246 #  define WEXITSTATUS(stat_val) (*(unsigned*)&(stat_val) >> 8)
247 # endif
248 # ifndef WTERMSIG
249 #  define WTERMSIG(stat_val) (*(unsigned*)&(stat_val) & 0x7F)
250 # endif
251 # ifndef WIFEXITED
252 #  define WIFEXITED(stat_val) ((*(unsigned*)&(stat_val) & 255) == 0)
253 # endif
254 #else
255 # ifdef WAIT_USES_UNION
256    typedef union wait amwait_t;
257 #  ifndef WEXITSTATUS
258 #  define WEXITSTATUS(stat_val) (((amwait_t*)&(stat_val))->w_retcode)
259 #  endif
260 #  ifndef WTERMSIG
261 #   define WTERMSIG(stat_val) (((amwait_t*)&(stat_val))->w_termsig)
262 #  endif
263 #  ifndef WIFEXITED
264 #   define WIFEXITED(stat_val) (WTERMSIG(stat_val) == 0)
265 #  endif
266 # else
267    typedef int amwait_t;
268 #  ifndef WEXITSTATUS
269 #   define WEXITSTATUS(stat_val) (*(unsigned*)&(stat_val) >> 8)
270 #  endif
271 #  ifndef WTERMSIG
272 #   define WTERMSIG(stat_val) (*(unsigned*)&(stat_val) & 0x7F)
273 #  endif
274 #  ifndef WIFEXITED
275 #   define WIFEXITED(stat_val) ((*(unsigned*)&(stat_val) & 255) == 0)
276 #  endif
277 # endif
278 #endif
279
280 #ifndef WIFSIGNALED
281 # define WIFSIGNALED(stat_val)  (WTERMSIG(stat_val) != 0)
282 #endif
283
284 #ifdef HAVE_UNISTD_H
285 #  include <unistd.h>
286 #endif
287
288 #ifdef HAVE_NETINET_IN_H
289 #  include <netinet/in.h>
290 #endif
291
292 #include <ctype.h>
293 #include <errno.h>
294 #include <pwd.h>
295 #include <signal.h>
296 #include <setjmp.h>
297 #include <stdio.h>
298 #include <sys/resource.h>
299 #include <sys/socket.h>
300
301 #ifdef HAVE_ARPA_INET_H
302 #include <arpa/inet.h>
303 #endif
304
305 #ifdef WORKING_IPV6
306 #define INET6
307 #endif
308
309 #ifndef INET_ADDRSTRLEN
310 #define INET_ADDRSTRLEN 16
311 #endif
312
313 #if !defined(HAVE_SIGACTION) && defined(HAVE_SIGVEC)
314 /* quick'n'dirty hack for NextStep31 */
315 #  define sa_flags sv_flags
316 #  define sa_handler sv_handler
317 #  define sa_mask sv_mask
318 #  define sigaction sigvec
319 #  define sigemptyset(mask) /* no way to clear pending signals */
320 #endif
321
322 /*
323  * Most Unixen declare errno in <errno.h>, some don't.  Some multithreaded
324  * systems have errno as a per-thread macro.  So, we have to be careful.
325  */
326 #ifndef errno
327 extern int errno;
328 #endif
329
330 /*
331  * Some compilers have int for type of sizeof() some use size_t.
332  * size_t is the one we want...
333  */
334 #define SIZEOF(x)       (size_t)sizeof(x)
335
336
337 /*
338  * Some older BSD systems don't have these FD_ macros, so if not, provide them.
339  */
340 #if !defined(FD_SET) || defined(LINT) || defined(__lint)
341 #  undef FD_SETSIZE
342 #  define FD_SETSIZE      (int)(SIZEOF(fd_set) * CHAR_BIT)
343
344 #  undef FD_SET
345 #  define FD_SET(n, p)    (((fd_set *)(p))->fds_bits[(n)/WORD_BIT] |= (int)((1 << ((n) % WORD_BIT))))
346
347 #  undef FD_CLR
348 #  define FD_CLR(n, p)    (((fd_set *)(p))->fds_bits[(n)/WORD_BIT] &= (int)(~(1 << ((n) % WORD_BIT))))
349
350 #  undef FD_ISSET
351 #  define FD_ISSET(n, p)  (((fd_set *)(p))->fds_bits[(n)/WORD_BIT] & (1 << ((n) % WORD_BIT)))
352
353 #  undef FD_ZERO
354 #  define FD_ZERO(p)      memset((p), 0, SIZEOF(*(p)))
355 #endif
356
357 #ifndef FD_COPY
358 #  define FD_COPY(p, q)   memcpy((q), (p), SIZEOF(*(p)))
359 #endif
360
361
362 /*
363  * Define MAX_HOSTNAME_LENGTH as the size of arrays to hold hostname's.
364  */
365 #undef  MAX_HOSTNAME_LENGTH
366 #define MAX_HOSTNAME_LENGTH 1025
367
368 /*
369  * If void is broken, substitute char.
370  */
371 #ifdef BROKEN_VOID
372 #  define void char
373 #endif
374
375 #define stringize(x) #x
376 #define stringconcat(x, y) x ## y
377
378 /* amanda #days calculation, with roundoff */
379
380 #define SECS_PER_DAY    (24*60*60)
381 #define days_diff(a, b) (int)(((b) - (a) + SECS_PER_DAY/2) / SECS_PER_DAY)
382
383 /* Global constants.  */
384 #ifndef AMANDA_SERVICE_NAME
385 #define AMANDA_SERVICE_NAME "amanda"
386 #endif
387 #ifndef KAMANDA_SERVICE_NAME
388 #define KAMANDA_SERVICE_NAME "kamanda"
389 #endif
390 #ifndef SERVICE_SUFFIX
391 #define SERVICE_SUFFIX ""
392 #endif
393 #ifndef AMANDA_SERVICE_DEFAULT
394 #define AMANDA_SERVICE_DEFAULT  ((in_port_t)10080)
395 #endif
396 #ifndef KAMANDA_SERVICE_DEFAULT
397 #define KAMANDA_SERVICE_DEFAULT ((in_port_t)10081)
398 #endif
399
400 #define am_round(v,u)   ((((v) + (u) - 1) / (u)) * (u))
401 #define am_floor(v,u)   (((v) / (u)) * (u))
402
403 /* Holding disk block size.  Do not even think about changint this!  :-) */
404 #define DISK_BLOCK_KB           32
405 #define DISK_BLOCK_BYTES        (DISK_BLOCK_KB * 1024)
406
407 /* Maximum size of a tape block */
408 /* MAX_TAPE_BLOCK_KB is defined in config.h */
409 /* by configure --with-maxtapeblocksize     */
410 #define MAX_TAPE_BLOCK_BYTES (MAX_TAPE_BLOCK_KB*1024)
411
412 /* Maximum length of tape label, plus one for null-terminator. */
413 #define MAX_TAPE_LABEL_LEN (10240)
414 #define MAX_TAPE_LABEL_BUF (MAX_TAPE_LABEL_LEN+1)
415 #define MAX_TAPE_LABEL_FMT "%10240s"
416
417 /* Unfortunately, the system-level sockaddr_storage definition can lead to
418  * C aliasing errors (where the optimizer doesn't notice that two operations
419  * affect the same datum).  We define our own similar type as a union.
420  */
421 typedef union sockaddr_union {
422     struct sockaddr         sa;
423     struct sockaddr_in      sin;
424 #ifdef WORKING_IPV6
425     struct sockaddr_in6     sin6;
426 #endif
427 #ifdef HAVE_SOCKADDR_STORAGE
428     struct sockaddr_storage ss; /* not used; just here to make the union full-size */
429 #endif
430 } sockaddr_union;
431
432 #include "debug.h"
433 #include "file.h"
434
435 void *debug_alloc(const char *file, int line, size_t size);
436 void *debug_newalloc(const char *file, int line, void *old, size_t size);
437 char *debug_stralloc(const char *file, int line, const char *str);
438 char *debug_newstralloc(const char *file, int line,
439                 char *oldstr, const char *newstr);
440 char *debug_vstralloc(const char *file, int line, const char *str, ...);
441 char *debug_newvstralloc(const char *file, int line,
442                 char *oldstr, const char *str, ...);
443 char *debug_vstrallocf(const char *file, int line, const char *fmt,
444                 ...) G_GNUC_PRINTF(3, 4);
445 char *debug_newvstrallocf(const char *file, int line, char *oldstr,
446                 const char *fmt, ...) G_GNUC_PRINTF(4, 5);
447
448 /* Usage: vstrextend(foo, "bar, "baz", NULL). Extends the existing 
449  * string, or allocates a brand new one. */
450 char *debug_vstrextend(const char *file, int line, char **oldstr, ...);
451
452 #define alloc(s)                debug_alloc(__FILE__, __LINE__, (s))
453 #define newalloc(p,s)           debug_newalloc(__FILE__, __LINE__, (p), (s))
454 #define stralloc(s)             debug_stralloc(__FILE__, __LINE__, (s))
455 #define newstralloc(p,s)        debug_newstralloc(__FILE__, __LINE__, (p), (s))
456 #define vstralloc(...)          debug_vstralloc(__FILE__,__LINE__,__VA_ARGS__)
457 #define newvstralloc(...)       debug_newvstralloc(__FILE__,__LINE__,__VA_ARGS__)
458 #define vstrallocf(...)         debug_vstrallocf(__FILE__,__LINE__,__VA_ARGS__)
459 #define newvstrallocf(...)      debug_newvstrallocf(__FILE__,__LINE__,__VA_ARGS__)
460 #define vstrextend(...)         debug_vstrextend(__FILE__,__LINE__,__VA_ARGS__)
461
462 #define stralloc2(s1,s2)        vstralloc((s1),(s2),NULL)
463 #define newstralloc2(p,s1,s2)   newvstralloc((p),(s1),(s2),NULL)
464
465 #define vstrallocf(...)         debug_vstrallocf(__FILE__,__LINE__,__VA_ARGS__)
466
467 /*@only@*/ /*@null@*/ char *debug_agets(const char *file, int line, FILE *f);
468 /*@only@*/ /*@null@*/ char *debug_areads(const char *file, int line, int fd);
469 #define agets(f)              debug_agets(__FILE__,__LINE__,(f))
470 #define areads(f)             debug_areads(__FILE__,__LINE__,(f))
471
472 extern int debug_amtable_alloc(const char *file,
473                                   int line,
474                                   void **table,
475                                   size_t *current,
476                                   size_t elsize,
477                                   size_t count,
478                                   int bump,
479                                   void (*init_func)(void *));
480
481 #define amtable_alloc(t,c,s,n,b,f) debug_amtable_alloc(__FILE__,      \
482                                                      __LINE__,        \
483                                                      (t),             \
484                                                      (c),             \
485                                                      (s),             \
486                                                      (n),             \
487                                                      (b),             \
488                                                      (f))
489
490 extern void amtable_free(void **, size_t *);
491
492 char ** safe_env(void);
493 char *  validate_regexp(const char *regex);
494 char *  validate_glob(const char *glob);
495 char *  clean_regex(const char *regex);
496 int     match(const char *regex, const char *str);
497 int     match_glob(const char *glob, const char *str);
498 char *  glob_to_regex(const char *glob);
499 int     match_tar(const char *glob, const char *str);
500 char *  tar_to_regex(const char *glob);
501 int     match_host(const char *glob, const char *host);
502 int     match_disk(const char *glob, const char *disk);
503 int     match_datestamp(const char *dateexp, const char *datestamp);
504 int     match_level(const char *levelexp, const char *level);
505 time_t  unctime(char *timestr);
506
507 /*
508  * amfree(ptr) -- if allocated, release space and set ptr to NULL.
509  *
510  * In general, this should be called instead of just free(), unless
511  * the very next source line sets the pointer to a new value.
512  */
513
514 #define amfree(ptr) do {                                                \
515     if((ptr) != NULL) {                                                 \
516         int e__errno = errno;                                           \
517         free(ptr);                                                      \
518         (ptr) = NULL;                                                   \
519         errno = e__errno;                                               \
520         (void)(ptr);  /* Fix value never used warning at end of routines */ \
521     }                                                                   \
522 } while (0)
523
524 #define strappend(s1,s2) do {                                           \
525     char *t_t_t = (s1) ? stralloc2((s1),(s2)) : stralloc((s2));         \
526     amfree((s1));                                                       \
527     (s1) = t_t_t;                                                       \
528 } while(0)
529
530 /*
531  * Return the number of elements in an array.
532  */
533 #define am_countof(a)   (int)(SIZEOF(a) / SIZEOF((a)[0]))
534
535 /*
536  * min/max.  Don't do something like
537  *
538  *    x = min(y++, z);
539  *
540  * because the increment will be duplicated.
541  */
542 #undef min
543 #undef max
544 #define min(a, b)       ((a) < (b) ? (a) : (b))
545 #define max(a, b)       ((a) > (b) ? (a) : (b))
546
547 /*
548  * Utility bitmask manipulation macros.
549  */
550 #define SET(t, f)       ((t) |= (f))
551 #define CLR(t, f)       ((t) &= ~((unsigned)(f)))
552 #define ISSET(t, f)     ((t) & (f))
553
554 /*
555  * Utility string macros.  All assume a variable holds the current
556  * character and the string pointer points to the next character to
557  * be processed.  Typical setup is:
558  *
559  *  s = buffer;
560  *  ch = *s++;
561  *  skip_whitespace(s, ch);
562  *  ...
563  *
564  * If you advance the pointer "by hand" to skip over something, do
565  * it like this:
566  *
567  *  s += some_amount;
568  *  ch = s[-1];
569  *
570  * Note that ch has the character at the end of the just skipped field.
571  * It is often useful to terminate a string, make a copy, then restore
572  * the input like this:
573  *
574  *  skip_whitespace(s, ch);
575  *  fp = s-1;                   ## save the start
576  *  skip_nonwhitespace(s, ch);  ## find the end
577  *  p[-1] = '\0';               ## temporary terminate
578  *  field = stralloc(fp);       ## make a copy
579  *  p[-1] = ch;                 ## restore the input
580  *
581  * The scanning macros are:
582  *
583  *  skip_whitespace (ptr, var)
584  *    -- skip whitespace, but stops at a newline
585  *  skip_non_whitespace (ptr, var)
586  *    -- skip non whitespace
587  *  skip_non_whitespace_cs (ptr, var)
588  *    -- skip non whitespace, stop at comment
589  *  skip_integer (ptr, var)
590  *    -- skip an integer field
591  *  skip_line (ptr, var)
592  *    -- skip just past the next newline
593  *  strncmp_const (str, const_str)
594  *    -- compare str to const_str, a string constant
595  *  strncmp_const_skip (str, const_var, ptr, var)
596  *    -- like strncmp_const, but skip the string if a match is
597  *       found; this macro only tests for equality, discarding
598  *       ordering information.
599  *
600  * where:
601  *
602  *  ptr -- string pointer
603  *  var -- current character
604  *
605  * These macros copy a non-whitespace field to a new buffer, and should
606  * only be used if dynamic allocation is impossible (fixed size buffers
607  * are asking for trouble):
608  *
609  *  copy_string (ptr, var, field, len, fldptr)
610  *    -- copy a non-whitespace field
611  *  copy_string_cs (ptr, var, field, len, fldptr)
612  *    -- copy a non-whitespace field, stop at comment
613  *
614  * where:
615  *
616  *  ptr -- string pointer
617  *  var -- current character
618  *  field -- area to copy to
619  *  len -- length of area (needs room for null byte)
620  *  fldptr -- work pointer used in move
621  *            if NULL on exit, the field was too small for the input
622  */
623
624 #define STR_SIZE        4096            /* a generic string buffer size */
625 #define NUM_STR_SIZE    128             /* a generic number buffer size */
626
627 #define skip_whitespace(ptr,c) do {                                     \
628     while((c) != '\n' && isspace((int)c)) (c) = *(ptr)++;               \
629 } while(0)
630
631 #define skip_non_whitespace(ptr,c) do {                                 \
632     while((c) != '\0' && !isspace((int)c)) (c) = *(ptr)++;              \
633 } while(0)
634
635 #define skip_non_whitespace_cs(ptr,c) do {                              \
636     while((c) != '\0' && (c) != '#' && !isspace((int)c)) (c) = *(ptr)++;\
637 } while(0)
638
639 #define skip_non_integer(ptr,c) do {                                    \
640     while((c) != '\0' && !isdigit(c)) (c) = *(ptr)++;                   \
641 } while(0)
642
643 #define skip_integer(ptr,c) do {                                        \
644     if((c) == '+' || (c) == '-') (c) = *(ptr)++;                        \
645     while(isdigit(c)) (c) = *(ptr)++;                                   \
646 } while(0)
647
648 #define skip_quoted_string(ptr, c) do {                                 \
649     int iq = 0;                                                         \
650     while (((c) != '\0') && !((iq == 0) && isspace((int)c))) {          \
651         if ((c) == '"') {                                               \
652             iq = !iq;                                                   \
653         } else if (((c) == '\\') && (*(ptr) == '"')) {                  \
654             (ptr)++;                                                    \
655         }                                                               \
656         (c) = *(ptr)++;                                                 \
657     }                                                                   \
658 } while (0)
659
660 #define skip_quoted_line(ptr, c) do {                                   \
661     int iq = 0;                                                         \
662     while((c) && !((iq == 0) && ((c) == '\n'))) {                       \
663         if ((c) == '"')                                                 \
664             iq = !iq;                                                   \
665         (c) = *(ptr)++;                                                 \
666     }                                                                   \
667     if(c)                                                               \
668         (c) = *(ptr)++;                                                 \
669 } while(0)
670
671 #define skip_line(ptr,c) do {                                           \
672     while((c) && (c) != '\n')                                           \
673         (c) = *(ptr)++;                                                 \
674     if(c)                                                               \
675         (c) = *(ptr)++;                                                 \
676 } while(0)
677
678 #define copy_string(ptr,c,f,l,fp) do {                                  \
679     (fp) = (f);                                                         \
680     while((c) != '\0' && !isspace((int)c)) {                            \
681         if((fp) >= (f) + (l) - 1) {                                     \
682             *(fp) = '\0';                                               \
683             (fp) = NULL;                                                \
684             (void)(fp);  /* Fix value never used warning at end of routines */ \
685             break;                                                      \
686         }                                                               \
687         *(fp)++ = (c);                                                  \
688         (c) = *(ptr)++;                                                 \
689     }                                                                   \
690     if(fp)                                                              \
691         *fp = '\0';                                                     \
692 } while(0)
693
694 #define copy_string_cs(ptr,c,f,l,fp) do {                               \
695     (fp) = (f);                                                         \
696     while((c) != '\0' && (c) != '#' && !isspace((int)c)) {              \
697         if((fp) >= (f) + (l) - 1) {                                     \
698             *(fp) = '\0';                                               \
699             (fp) = NULL;                                                \
700             break;                                                      \
701         }                                                               \
702         *(fp)++ = (c);                                                  \
703         (c) = *(ptr)++;                                                 \
704     }                                                                   \
705     if(fp) *fp = '\0';                                                  \
706 } while(0)
707
708 #define is_dot_or_dotdot(s)                                             \
709     ((s)[0] == '.'                                                      \
710      && ((s)[1] == '\0'                                                 \
711          || ((s)[1] == '.' && (s)[2] == '\0')))
712
713 #define strncmp_const(str, cnst)                                        \
714         strncmp((str), (cnst), sizeof((cnst))-1)
715
716 /* (have to roll this up in an expression, so it can be used in if()) */
717 #define strncmp_const_skip(str, cnst, ptr, var)                         \
718         ((strncmp((str), (cnst), sizeof((cnst))-1) == 0)?               \
719                  ((ptr)+=sizeof((cnst))-1, (var)=(ptr)[-1], 0)          \
720                 :1)
721
722 /* from old bsd-security.c */
723 extern int debug;
724 extern int check_security(sockaddr_union *, char *, unsigned long, char **);
725
726 /*
727  * Handle functions which are not always declared on all systems.  This
728  * stops gcc -Wall and lint from complaining.
729  */
730
731 /* AIX #defines accept, and provides a prototype for the alternate name */
732 #if !defined(HAVE_ACCEPT_DECL) && !defined(accept)
733 extern int accept(int s, struct sockaddr *addr, socklen_t_equiv *addrlen);
734 #endif
735
736 #ifndef HAVE_ATOF_DECL
737 extern double atof(const char *ptr);
738 #endif
739
740 #ifndef HAVE_BCOPY
741 # define bcopy(from,to,n) ((void)memmove((to), (from), (n)))
742 #else
743 # ifndef HAVE_BCOPY_DECL
744 extern void bcopy(const void *s1, void *s2, size_t n);
745 # endif
746 #endif
747
748 #ifndef HAVE_BIND_DECL
749 extern int bind(int s, const struct sockaddr *name, socklen_t_equiv namelen);
750 #endif
751
752 #ifndef HAVE_BZERO
753 #define bzero(s,n) ((void)memset((s),0,(n)))
754 #else
755 # ifndef HAVE_BZERO_DECL
756 extern void bzero(void *s, size_t n);
757 # endif
758 #endif
759
760 #ifndef HAVE_CLOSELOG_DECL
761 extern void closelog(void);
762 #endif
763
764 #ifndef HAVE_CONNECT_DECL
765 extern int connect(int s, struct sockaddr *name, socklen_t_equiv namelen);
766 #endif
767
768 #ifndef HAVE_FCLOSE_DECL
769 extern int fclose(FILE *stream);
770 #endif
771
772 #ifndef HAVE_FFLUSH_DECL
773 extern int fflush(FILE *stream);
774 #endif
775
776 #ifndef HAVE_FPRINTF_DECL
777 extern int fprintf(FILE *stream, const char *format, ...);
778 #endif
779
780 #ifndef HAVE_FPUTC_DECL
781 extern int fputc(int c, FILE *stream);
782 #endif
783
784 #ifndef HAVE_FPUTS_DECL
785 extern int fputs(const char *s, FILE *stream);
786 #endif
787
788 #ifndef HAVE_FREAD_DECL
789 extern size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
790 #endif
791
792 #ifndef HAVE_FSEEK_DECL
793 extern int fseek(FILE *stream, long offset, int ptrname);
794 #endif
795
796 #ifndef HAVE_FWRITE_DECL
797 extern size_t fwrite(const void *ptr, size_t size, size_t nitems,
798                         FILE *stream);
799 #endif
800
801 #ifndef HAVE_GETHOSTNAME_DECL
802 extern int gethostname(char *name, int namelen);
803 #endif
804
805 #ifndef HAVE_GETOPT_DECL
806 extern char *optarg;
807 extern int getopt(int argc, char * const *argv, const char *optstring);
808 #endif
809
810 /* AIX #defines getpeername, and provides a prototype for the alternate name */
811 #if !defined(HAVE_GETPEERNAME_DECL) && !defined(getpeername)
812 extern int getpeername(int s, struct sockaddr *name, socklen_t_equiv *namelen);
813 #endif
814
815 /* AIX #defines getsockname, and provides a prototype for the alternate name */
816 #if !defined(HAVE_GETSOCKNAME_DECL) && !defined(getsockname)
817 extern int getsockname(int s, struct sockaddr *name, socklen_t_equiv *namelen);
818 #endif
819
820 #ifndef HAVE_GETSOCKOPT_DECL
821 extern int getsockopt(int s, int level, int optname, char *optval,
822                          socklen_t_equiv *optlen);
823 #endif
824
825 #ifndef HAVE_INITGROUPS
826 # define initgroups(name,basegid) 0
827 #else
828 # ifndef HAVE_INITGROUPS_DECL
829 extern int initgroups(const char *name, gid_t basegid);
830 # endif
831 #endif
832
833 #ifndef HAVE_IOCTL_DECL
834 extern int ioctl(int fildes, int request, ...);
835 #endif
836
837 #ifndef isnormal
838 #ifndef HAVE_ISNORMAL
839 #define isnormal(f) (((f) < 0.0) || ((f) > 0.0))
840 #endif
841 #endif
842
843 #ifndef HAVE_LISTEN_DECL
844 extern int listen(int s, int backlog);
845 #endif
846
847 #ifndef HAVE_LSTAT_DECL
848 extern int lstat(const char *path, struct stat *buf);
849 #endif
850
851 #ifndef HAVE_MALLOC_DECL
852 extern void *malloc (size_t size);
853 #endif
854
855 #ifndef HAVE_MEMMOVE_DECL
856 #ifdef HAVE_MEMMOVE
857 extern void *memmove(void *to, const void *from, size_t n);
858 #else
859 extern char *memmove(char *to, /*const*/ char *from, size_t n);
860 #endif
861 #endif
862
863 #ifndef HAVE_MEMSET_DECL
864 extern void *memset(void *s, int c, size_t n);
865 #endif
866
867 #ifndef HAVE_MKTEMP_DECL
868 extern char *mktemp(char *template);
869 #endif
870
871 #ifndef HAVE_MKSTEMP_DECL
872 extern int mkstemp(char *template);
873 #endif
874
875 #ifndef HAVE_MKTIME_DECL
876 extern time_t mktime(struct tm *timeptr);
877 #endif
878
879 #ifndef HAVE_OPENLOG_DECL
880 #ifdef LOG_AUTH
881 extern void openlog(const char *ident, int logopt, int facility);
882 #else
883 extern void openlog(const char *ident, int logopt);
884 #endif
885 #endif
886
887 #ifndef HAVE_PCLOSE_DECL
888 extern int pclose(FILE *stream);
889 #endif
890
891 #ifndef HAVE_PERROR_DECL
892 extern void perror(const char *s);
893 #endif
894
895 #ifndef HAVE_PRINTF_DECL
896 extern int printf(const char *format, ...);
897 #endif
898
899 #ifndef HAVE_PUTS_DECL
900 extern int puts(const char *s);
901 #endif
902
903 #ifndef HAVE_REALLOC_DECL
904 extern void *realloc(void *ptr, size_t size);
905 #endif
906
907 /* AIX #defines recvfrom, and provides a prototype for the alternate name */
908 #if !defined(HAVE_RECVFROM_DECL) && !defined(recvfrom)
909 extern int recvfrom(int s, char *buf, int len, int flags,
910                        struct sockaddr *from, socklen_t_equiv *fromlen);
911 #endif
912
913 #ifndef HAVE_REMOVE_DECL
914 extern int remove(const char *path);
915 #endif
916
917 #ifndef HAVE_RENAME_DECL
918 extern int rename(const char *old, const char *new);
919 #endif
920
921 #ifndef HAVE_REWIND_DECL
922 extern void rewind(FILE *stream);
923 #endif
924
925 #ifndef HAVE_RUSEROK_DECL
926 extern int ruserok(const char *rhost, int suser,
927                       const char *ruser, const char *luser);
928 #endif
929
930 #ifndef HAVE_SELECT_DECL
931 extern int select(int nfds,
932                      SELECT_ARG_TYPE *readfds,
933                      SELECT_ARG_TYPE *writefds,
934                      SELECT_ARG_TYPE *exceptfds,
935                      struct timeval *timeout);
936 #endif
937
938 #ifndef HAVE_SENDTO_DECL
939 extern int sendto(int s, const char *msg, int len, int flags,
940                      const struct sockaddr *to, int tolen);
941 #endif
942
943 #ifdef HAVE_SETRESGID
944 #define setegid(x)      setresgid((gid_t)-1,(x),(gid_t)-1)
945 #ifndef HAVE_SETRESGID_DECL
946 extern int setresgid(gid_t rgid, gid_t egid, gid_t sgid);
947 #endif
948 #else
949 #ifndef HAVE_SETEGID_DECL
950 extern int setegid(gid_t egid);
951 #endif
952 #endif
953
954 #ifdef HAVE_SETRESUID
955 #define seteuid(x)      setresuid((uid_t)-1,(x),(uid_t)-1)
956 #ifndef HAVE_SETRESUID_DECL
957 extern int setresuid(uid_t ruid, uid_t euid, uid_t suid);
958 #endif
959 #else
960 #ifndef HAVE_SETEUID_DECL
961 extern int seteuid(uid_t euid);
962 #endif
963 #endif
964
965 #ifndef HAVE_SETPGID_DECL
966 #ifdef HAVE_SETPGID
967 extern int setpgid(pid_t pid, pid_t pgid);
968 #endif
969 #endif
970
971 #ifndef HAVE_SETPGRP_DECL
972 #ifdef SETPGRP_VOID
973 extern pid_t setpgrp(void);
974 #else
975 extern pid_t setpgrp(pid_t pgrp, pid_t pid);
976 #endif
977 #endif
978
979 #ifndef HAVE_SETSOCKOPT_DECL
980 extern int setsockopt(int s, int level, int optname,
981                          const char *optval, int optlen);
982 #endif
983
984 #ifdef HAVE_SHMGET
985 #ifndef HAVE_SHMAT_DECL
986 extern void *shmat(int shmid, const SHM_ARG_TYPE *shmaddr, int shmflg);
987 #endif
988
989 #ifndef HAVE_SHMCTL_DECL
990 extern int shmctl(int shmid, int cmd, struct shmid_ds *buf);
991 #endif
992
993 #ifndef HAVE_SHMDT_DECL
994 extern int shmdt(SHM_ARG_TYPE *shaddr);
995 #endif
996
997 #ifndef HAVE_SHMGET_DECL
998 extern int shmget(key_t key, size_t size, int shmflg);
999 #endif
1000 #endif
1001
1002 #ifndef HAVE_SNPRINTF_DECL
1003 #include "arglist.h"
1004 int snprintf(char *buf, size_t len, const char *format,...)
1005      G_GNUC_PRINTF(3,4);
1006 #endif
1007 #ifndef HAVE_VSNPRINTF_DECL
1008 #include "arglist.h"
1009 int vsnprintf(char *buf, size_t len, const char *format, va_list ap);
1010 #endif
1011
1012 #ifndef HAVE_SOCKET_DECL
1013 extern int socket(int domain, int type, int protocol);
1014 #endif
1015
1016 #ifndef HAVE_SOCKETPAIR_DECL
1017 extern int socketpair(int domain, int type, int protocol, int sv[2]);
1018 #endif
1019
1020 #ifndef HAVE_SSCANF_DECL
1021 extern int sscanf(const char *s, const char *format, ...);
1022 #endif
1023
1024 #ifndef HAVE_STRCASECMP_DECL
1025 extern int strcasecmp(const char *s1, const char *s2);
1026 #endif
1027
1028 #ifndef HAVE_STRERROR_DECL
1029 extern char *strerror(int errnum);
1030 #endif
1031
1032 #ifndef HAVE_STRFTIME_DECL
1033 extern size_t strftime(char *s, size_t maxsize, const char *format,
1034                           const struct tm *timeptr);
1035 #endif
1036
1037 #ifndef HAVE_STRNCASECMP_DECL
1038 extern int strncasecmp(const char *s1, const char *s2, int n);
1039 #endif
1040
1041 #ifndef HAVE_SYSLOG_DECL
1042 extern void syslog(int priority, const char *logstring, ...)
1043      G_GNUC_PRINTF(2,3);
1044 #endif
1045
1046 #ifndef HAVE_SYSTEM_DECL
1047 extern int system(const char *string);
1048 #endif
1049
1050 #ifndef HAVE_TIME_DECL
1051 extern time_t time(time_t *tloc);
1052 #endif
1053
1054 #ifndef HAVE_TOLOWER_DECL
1055 extern int tolower(int c);
1056 #endif
1057
1058 #ifndef HAVE_TOUPPER_DECL
1059 extern int toupper(int c);
1060 #endif
1061
1062 #ifndef HAVE_UNGETC_DECL
1063 extern int ungetc(int c, FILE *stream);
1064 #endif
1065
1066 #ifndef HAVE_VFPRINTF_DECL
1067 #include "arglist.h"
1068 extern int vfprintf(FILE *stream, const char *format, va_list ap);
1069 #endif
1070
1071 #ifndef HAVE_VPRINTF_DECL
1072 #include "arglist.h"
1073 extern int vprintf(const char *format, va_list ap);
1074 #endif
1075
1076 /* gnulib-only includes (hence "" instead of <>) */
1077 #include "getaddrinfo.h"
1078 #include "inet_ntop.h"
1079
1080 #if !defined(S_ISCHR) && defined(_S_IFCHR) && defined(_S_IFMT)
1081 #define S_ISCHR(mode) (((mode) & _S_IFMT) == _S_IFCHR)
1082 #endif
1083
1084 #if !defined(S_ISREG) && defined(_S_IFREG) && defined(_S_IFMT)
1085 #define S_ISREG(mode) (((mode) & _S_IFMT) == _S_IFREG)
1086 #endif
1087
1088 #ifndef HAVE_WAITPID
1089 #ifdef HAVE_WAIT4
1090 #define waitpid(pid,status,options) wait4(pid,status,options,0)
1091 #else
1092 extern pid_t waitpid(pid_t pid, amwait_t *stat_loc, int options);
1093 #endif
1094 #endif
1095
1096 #ifndef HAVE_WRITEV_DECL
1097 extern ssize_t writev(int fd, const struct iovec *iov, int iovcnt);
1098 #endif
1099
1100 #ifndef STDIN_FILENO
1101 #define STDIN_FILENO 0
1102 #endif
1103
1104 #ifndef STDOUT_FILENO
1105 #define STDOUT_FILENO 1
1106 #endif
1107
1108 #ifndef STDERR_FILENO
1109 #define STDERR_FILENO 2
1110 #endif
1111
1112 /* S_ISDIR is not defined on Nextstep */
1113 #ifndef S_ISDIR
1114 #if defined(_S_IFMT) && defined(_S_IFDIR)
1115 #define S_ISDIR(mode)   (((mode) & (_S_IFMT)) == (_S_IFDIR))
1116 #else
1117 #error Don t know how to define S_ISDIR
1118 #endif
1119 #endif
1120
1121 #if SIZEOF_SIZE_T == SIZEOF_INT
1122 #  define        SIZE_T_ATOI    (size_t)atoi
1123 #  ifndef SIZE_MAX
1124 #    define      SIZE_MAX       G_MAXUINT
1125 #  endif
1126 #else
1127 #  define        SIZE_T_ATOI    (size_t)atol
1128 #  ifndef SIZE_MAX
1129 #    define      SIZE_MAX       ULONG_MAX
1130 #  endif
1131 #endif
1132
1133 #if SIZEOF_SSIZE_T == SIZEOF_INT
1134 #  define        SSIZE_T_ATOI   (ssize_t)atoi
1135 #  ifndef SSIZE_MAX
1136 #    define      SSIZE_MAX      INT_MAX
1137 #  endif
1138 #  ifndef SSIZE_MIN
1139 #    define      SSIZE_MIN      INT_MIN
1140 #  endif
1141 #else
1142 #  define        SSIZE_T_ATOI   (ssize_t)atol
1143 #  ifndef SSIZE_MAX
1144 #    define      SSIZE_MAX      LONG_MAX
1145 #  endif
1146 #  ifndef SSIZE_MIN
1147 #    define      SSIZE_MIN      LONG_MIN
1148 #  endif
1149 #endif
1150
1151 #if SIZEOF_TIME_T == SIZEOF_INT
1152 #  define        TIME_T_ATOI    (time_t)atoi
1153 #  ifndef TIME_MAX
1154 #    define      TIME_MAX       G_MAXUINT
1155 #  endif
1156 #else
1157 #  define        TIME_T_ATOI    (time_t)atol
1158 #  ifndef TIME_MAX
1159 #    define      TIME_MAX       ULONG_MAX
1160 #  endif
1161 #endif
1162
1163 #if SIZEOF_OFF_T > SIZEOF_LONG
1164 #  ifdef HAVE_ATOLL
1165 #    define        OFF_T_ATOI    (off_t)atoll
1166 #  else
1167 #    define        OFF_T_ATOI    (off_t)atol
1168 #  endif
1169 #  ifdef HAVE_STRTOLL
1170 #    define        OFF_T_STRTOL  (off_t)strtoll
1171 #  else
1172 #    define        OFF_T_STRTOL  (off_t)strtol
1173 #  endif
1174 #else
1175 #  if SIZEOF_OFF_T == SIZEOF_LONG
1176 #    define        OFF_T_ATOI    (off_t)atol
1177 #    define        OFF_T_STRTOL  (off_t)strtol
1178 #  else
1179 #    define        OFF_T_ATOI    (off_t)atoi
1180 #    define        OFF_T_STRTOL  (off_t)strtol
1181 #  endif
1182 #endif
1183
1184 #if SIZEOF_OFF_T == 8
1185 #  ifdef OFF_MAX
1186 #    define AM64_MAX (off_t)(OFF_MAX)
1187 #  else
1188 #    define AM64_MAX (off_t)(9223372036854775807LL)
1189 #  endif
1190 #  ifdef OFF_MIN
1191 #    define AM64_MIN (off_t)(OFF_MIN)
1192 #  else
1193 #    define AM64_MIN (off_t)(-9223372036854775807LL -1LL)
1194 #  endif
1195 #else
1196 #if SIZEOF_LONG == 8
1197 #  ifdef LONG_MAX
1198 #    define AM64_MAX (off_t)(LONG_MAX)
1199 #  else
1200 #    define AM64_MAX (off_t)9223372036854775807L
1201 #  endif
1202 #  ifdef LONG_MIN
1203 #    define AM64_MIN (off_t)(LONG_MIN)
1204 #  else
1205 #    define AM64_MIN (off_t)(-9223372036854775807L -1L)
1206 #  endif
1207 #else
1208 #if SIZEOF_LONG_LONG == 8
1209 #  ifdef LONG_LONG_MAX
1210 #    define AM64_MAX (off_t)(LONG_LONG_MAX)
1211 #  else
1212 #    define AM64_MAX (off_t)9223372036854775807LL
1213 #  endif
1214 #  ifdef LONG_LONG_MIN
1215 #    define AM64_MIN (off_t)(LONG_LONG_MIN)
1216 #  else
1217 #    define AM64_MIN (off_t)(-9223372036854775807LL -1LL)
1218 #  endif
1219 #else
1220 #if SIZEOF_INTMAX_T == 8
1221 #  ifdef INTMAX_MAX
1222 #    define AM64_MAX (off_t)(INTMAX_MAX)
1223 #  else
1224 #    define AM64_MAX (off_t)9223372036854775807LL
1225 #  endif
1226 #  ifdef INTMAX_MIN
1227 #    define AM64_MIN (off_t)(INTMAX_MIN)
1228 #  else
1229 #    define AM64_MIN (off_t)(-9223372036854775807LL -1LL)
1230 #  endif
1231 #else  /* no 64 bits type found, use long. */
1232 #  ifdef LONG_MAX
1233 #    define AM64_MAX (off_t)(LONG_MAX)
1234 #  else
1235 #    define AM64_MAX (off_t)2147483647
1236 #  endif
1237 #  ifdef LONG_MIN
1238 #    define AM64_MIN (off_t)(LONG_MIN)
1239 #  else
1240 #    define AM64_MIN (off_t)(-2147483647 -1)
1241 #  endif
1242 #endif
1243 #endif
1244 #endif
1245 #endif
1246
1247 #define BIND_CYCLE_RETRIES      120             /* Total of 30 minutes */
1248
1249 #define MAX_DUMPERS 63
1250
1251 #ifndef NI_MAXHOST
1252 #define NI_MAXHOST 1025
1253 #endif
1254
1255 typedef enum {
1256     KENCRYPT_NONE,      /* krb5 encryption not enabled */
1257     KENCRYPT_WILL_DO,   /* krb5 encryption will be enabled once amanda
1258                            protocol stream is closed */
1259     KENCRYPT_YES        /* krb5 encryption enabled on all stream */
1260 } kencrypt_type;
1261
1262 #endif  /* !AMANDA_H */