eda9320b4a4a147fed42dc16de203f1dfea97902
[apps/pfixtools.git] / postlicyd / query.h
1 /******************************************************************************/
2 /*          pfixtools: a collection of postfix related tools                  */
3 /*          ~~~~~~~~~                                                         */
4 /*  ________________________________________________________________________  */
5 /*                                                                            */
6 /*  Redistribution and use in source and binary forms, with or without        */
7 /*  modification, are permitted provided that the following conditions        */
8 /*  are met:                                                                  */
9 /*                                                                            */
10 /*  1. Redistributions of source code must retain the above copyright         */
11 /*     notice, this list of conditions and the following disclaimer.          */
12 /*  2. Redistributions in binary form must reproduce the above copyright      */
13 /*     notice, this list of conditions and the following disclaimer in the    */
14 /*     documentation and/or other materials provided with the distribution.   */
15 /*  3. The names of its contributors may not be used to endorse or promote    */
16 /*     products derived from this software without specific prior written     */
17 /*     permission.                                                            */
18 /*                                                                            */
19 /*  THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS   */
20 /*  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED         */
21 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE    */
22 /*  DISCLAIMED.  IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY         */
23 /*  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL        */
24 /*  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS   */
25 /*  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)     */
26 /*  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,       */
27 /*  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN  */
28 /*  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
29 /*  POSSIBILITY OF SUCH DAMAGE.                                               */
30 /*                                                                            */
31 /*   Copyright (c) 2006-2008 the Authors                                      */
32 /*   see AUTHORS and source files for details                                 */
33 /******************************************************************************/
34
35 /*
36  * Copyright © 2007 Pierre Habouzit
37  * Copyright © 2008 Florent Bruneau
38  */
39
40 #ifndef PFIXTOOLS_QUERY_H
41 #define PFIXTOOLS_QUERY_H
42
43 #include "mem.h"
44 #include "common.h"
45 #include "buffer.h"
46 #include "policy_tokens.h"
47
48 enum smtp_state {
49     SMTP_CONNECT,
50     SMTP_EHLO,
51     SMTP_HELO = SMTP_EHLO,
52     SMTP_MAIL,
53     SMTP_RCPT,
54     SMTP_DATA,
55     SMTP_END_OF_MESSAGE,
56     SMTP_VRFY,
57     SMTP_ETRN,
58     SMTP_count,
59     SMTP_UNKNOWN,
60 };
61
62 extern const static_str_t smtp_state_names[SMTP_count];
63
64 /* \see http://www.postfix.org/SMTPD_POLICY_README.html */
65 typedef struct query_t {
66     unsigned state : 4;
67     unsigned esmtp : 1;
68
69     static_str_t helo_name;
70     static_str_t queue_id;
71     static_str_t sender;
72     static_str_t recipient;
73     static_str_t recipient_count;
74     static_str_t client_address;
75     static_str_t client_name;
76     static_str_t reverse_client_name;
77     static_str_t instance;
78
79     /* useful data extracted from previous ones */
80     static_str_t sender_domain;
81     static_str_t recipient_domain;
82
83     /* postfix 2.2+ */
84     static_str_t sasl_method;
85     static_str_t sasl_username;
86     static_str_t sasl_sender;
87     static_str_t size;
88     static_str_t ccert_subject;
89     static_str_t ccert_issuer;
90     static_str_t ccert_fingerprint;
91
92     /* postfix 2.3+ */
93     static_str_t encryption_protocol;
94     static_str_t encryption_cipher;
95     static_str_t encryption_keysize;
96     static_str_t etrn_domain;
97
98     /* postfix 2.5+ */
99     static_str_t stress;
100
101     const char *eoq;
102 } query_t;
103
104 /** Parse the content of the text to fill the query.
105  * The text pointed by \p p is segmented (and modified to add
106  * a \0 at the end of each segment) and used to fill the query
107  * object.
108  */
109 __attribute__((nonnull(1,2)))
110 bool query_parse(query_t *query, char *p);
111
112 /** Return the value of the field with the given name.
113  */
114 __attribute__((nonnull(1,2)))
115 const static_str_t *query_field_for_name(const query_t *query, const char *name);
116
117 /** Returns the value of the field with the given id.
118  */
119 __attribute__((nonnull))
120 const static_str_t *query_field_for_id(const query_t *query, postlicyd_token id);
121
122 /** Formats the given string by replacing ${field_name} with the content
123  * of the query.
124  * Unknown and empty fields are filled with (null).
125  */
126 __attribute__((nonnull(3)))
127 ssize_t query_format(char *dest, size_t len, const char* fmt, const query_t *query);
128
129 /** Writes a query-formated string in a buffer.
130  */
131 __attribute__((nonnull(1,2)))
132 bool query_format_buffer(buffer_t *buf, const char *fmt, const query_t *query);
133
134 /** Check the query-format string.
135  */
136 #define query_format_check(fmt) (query_format(NULL, 0, fmt, NULL) >= 0)
137
138 #endif