1 #ifndef _MAIL_QUEUE_H_INCLUDED_
2 #define _MAIL_QUEUE_H_INCLUDED_
3 
4 /*++
5 /* NAME
6 /*	mail_queue 3h
7 /* SUMMARY
8 /*	mail queue access
9 /* SYNOPSIS
10 /*	#include <mail_queue.h>
11 /* DESCRIPTION
12 /* .nf
13 
14  /*
15   * System library.
16   */
17 #include <sys/time.h>
18 
19  /*
20   * Utility library.
21   */
22 #include <vstring.h>
23 #include <vstream.h>
24 
25  /*
26   * Mail queue names.
27   */
28 #define MAIL_QUEUE_MAILDROP	"maildrop"
29 #define MAIL_QUEUE_HOLD		"hold"
30 #define MAIL_QUEUE_INCOMING	"incoming"
31 #define MAIL_QUEUE_ACTIVE	"active"
32 #define MAIL_QUEUE_DEFERRED	"deferred"
33 #define MAIL_QUEUE_TRACE	"trace"
34 #define MAIL_QUEUE_DEFER	"defer"
35 #define MAIL_QUEUE_BOUNCE	"bounce"
36 #define MAIL_QUEUE_CORRUPT	"corrupt"
37 #define MAIL_QUEUE_FLUSH	"flush"
38 #define MAIL_QUEUE_SAVED	"saved"
39 
40  /*
41   * Queue file modes.
42   *
43   * 4.4BSD-like systems don't allow (sticky AND executable) together, so we use
44   * group read permission bits instead. These are more portable, but they
45   * also are more likely to be turned on by accident. It would not be the end
46   * of the world.
47   */
48 #define MAIL_QUEUE_STAT_READY	(S_IRUSR | S_IWUSR | S_IXUSR)
49 #define MAIL_QUEUE_STAT_CORRUPT	(S_IRUSR)
50 #ifndef MAIL_QUEUE_STAT_UNTHROTTLE
51 #define MAIL_QUEUE_STAT_UNTHROTTLE (S_IRGRP)
52 #define MAIL_QUEUE_STAT_EXPIRE	(S_IXGRP)
53 #endif
54 
55 extern struct VSTREAM *mail_queue_enter(const char *, mode_t, struct timeval *);
56 extern struct VSTREAM *mail_queue_open(const char *, const char *, int, mode_t);
57 extern int mail_queue_rename(const char *, const char *, const char *);
58 extern int mail_queue_remove(const char *, const char *);
59 extern const char *mail_queue_dir(VSTRING *, const char *, const char *);
60 extern const char *mail_queue_path(VSTRING *, const char *, const char *);
61 extern int mail_queue_mkdirs(const char *);
62 extern int mail_queue_name_ok(const char *);
63 extern int mail_queue_id_ok(const char *);
64 
65  /*
66   * MQID - Mail Queue ID format definitions. Needed only by code that creates
67   * or parses queue ID strings.
68   */
69 #ifdef MAIL_QUEUE_INTERNAL
70 
71  /*
72   * System library.
73   */
74 #include <errno.h>
75 
76  /*
77   * Global library.
78   */
79 #include <safe_ultostr.h>
80 
81  /*
82   * The long non-repeating queue ID is encoded in an alphabet of 10 digits,
83   * 21 upper-case characters, and 21 or fewer lower-case characters. The
84   * alphabet is made "safe" by removing all the vowels (AEIOUaeiou). The ID
85   * is the concatenation of:
86   *
87   * - the time in seconds (base 52 encoded, six or more chars),
88   *
89   * - the time in microseconds (base 52 encoded, exactly four chars),
90   *
91   * - the 'z' character to separate the time and inode information,
92   *
93   * - the inode number (base 51 encoded so that it contains no 'z').
94   */
95 #define MQID_LG_SEC_BASE	52	/* seconds safe alphabet base */
96 #define MQID_LG_SEC_PAD	6	/* seconds minimum field width */
97 #define MQID_LG_USEC_BASE	52	/* microseconds safe alphabet base */
98 #define MQID_LG_USEC_PAD	4	/* microseconds exact field width */
99 #define MQID_LG_TIME_PAD	(MQID_LG_SEC_PAD + MQID_LG_USEC_PAD)
100 #define MQID_LG_INUM_SEP	'z'	/* time-inode separator */
101 #define MQID_LG_INUM_BASE	51	/* inode safe alphabet base */
102 #define MQID_LG_INUM_PAD	0	/* no padding needed */
103 
104 #define MQID_FIND_LG_INUM_SEPARATOR(cp, path) \
105 	(((cp) = strrchr((path), MQID_LG_INUM_SEP)) != 0 \
106 	    && ((cp) - (path) >= MQID_LG_TIME_PAD))
107 
108 #define MQID_GET_INUM(path, inum, long_form, error) do { \
109 	char *_cp; \
110 	if (((long_form) = MQID_FIND_LG_INUM_SEPARATOR(_cp, (path))) != 0) { \
111 	    MQID_LG_DECODE_INUM(_cp + 1, (inum), (error)); \
112 	} else { \
113 	    MQID_SH_DECODE_INUM((path) + MQID_SH_USEC_PAD, (inum), (error)); \
114 	} \
115     } while (0)
116 
117 #define MQID_LG_ENCODE_SEC(buf, val) \
118 	MQID_LG_ENCODE((buf), (val), MQID_LG_SEC_BASE, MQID_LG_SEC_PAD)
119 
120 #define MQID_LG_ENCODE_USEC(buf, val) \
121 	MQID_LG_ENCODE((buf), (val), MQID_LG_USEC_BASE, MQID_LG_USEC_PAD)
122 
123 #define MQID_LG_ENCODE_INUM(buf, val) \
124 	MQID_LG_ENCODE((buf), (val), MQID_LG_INUM_BASE, MQID_LG_INUM_PAD)
125 
126 #define MQID_LG_DECODE_USEC(str, ulval, error) \
127 	MQID_LG_DECODE((str), (ulval), MQID_LG_USEC_BASE, (error))
128 
129 #define MQID_LG_DECODE_INUM(str, ulval, error) \
130 	MQID_LG_DECODE((str), (ulval), MQID_LG_INUM_BASE, (error))
131 
132 #define MQID_LG_ENCODE(buf, val, base, padlen) \
133 	safe_ultostr((buf), (unsigned long) (val), (base), (padlen), '0')
134 
135 #define MQID_LG_DECODE(str, ulval, base, error) do { \
136 	char *_end; \
137 	errno = 0; \
138 	(ulval) = safe_strtoul((str), &_end, (base)); \
139 	(error) = (*_end != 0 || ((ulval) == ULONG_MAX && errno == ERANGE)); \
140     } while (0)
141 
142 #define MQID_LG_GET_HEX_USEC(bp, zp) do { \
143 	int _error; \
144 	unsigned long _us_val; \
145 	vstring_strncpy((bp), (zp) - MQID_LG_USEC_PAD, MQID_LG_USEC_PAD); \
146 	MQID_LG_DECODE_USEC(STR(bp), _us_val, _error); \
147 	if (_error) \
148 	    _us_val = 0; \
149 	(void) MQID_SH_ENCODE_USEC((bp), _us_val); \
150     } while (0)
151 
152  /*
153   * The short repeating queue ID is encoded in upper-case hexadecimal, and is
154   * the concatenation of:
155   *
156   * - the time in microseconds (exactly five chars),
157   *
158   * - the inode number.
159   */
160 #define MQID_SH_USEC_PAD	5	/* microseconds exact field width */
161 
162 #define MQID_SH_ENCODE_USEC(buf, usec) \
163 	vstring_str(vstring_sprintf((buf), "%05X", (int) (usec)))
164 
165 #define MQID_SH_ENCODE_INUM(buf, inum) \
166 	vstring_str(vstring_sprintf((buf), "%lX", (unsigned long) (inum)))
167 
168 #define MQID_SH_DECODE_INUM(str, ulval, error) do { \
169         char *_end; \
170 	errno = 0; \
171 	(ulval) = strtoul((str), &_end, 16); \
172 	(error) = (*_end != 0 || ((ulval) == ULONG_MAX && errno == ERANGE)); \
173     } while (0)
174 
175 #endif					/* MAIL_QUEUE_INTERNAL */
176 
177 /* LICENSE
178 /* .ad
179 /* .fi
180 /*	The Secure Mailer license must be distributed with this software.
181 /* AUTHOR(S)
182 /*	Wietse Venema
183 /*	IBM T.J. Watson Research
184 /*	P.O. Box 704
185 /*	Yorktown Heights, NY 10598, USA
186 /*
187 /*	Wietse Venema
188 /*	Google, Inc.
189 /*	111 8th Avenue
190 /*	New York, NY 10011, USA
191 /*--*/
192 
193 #endif					/* _MAIL_QUEUE_H_INCLUDED_ */
194