~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Nginx/mail/ngx_mail_imap_module.c

Version: ~ [ nginx-0.6.26 ] ~ [ nginx-0.5.35 ] ~ [ nginx-0.5.20 ] ~ [ nginx-0.5.19 ] ~

  1 
  2 /*
  3  * Copyright (C) Igor Sysoev
  4  */
  5 
  6 
  7 #include <ngx_config.h>
  8 #include <ngx_core.h>
  9 #include <ngx_event.h>
 10 #include <ngx_mail.h>
 11 #include <ngx_mail_imap_module.h>
 12 
 13 
 14 static void *ngx_mail_imap_create_srv_conf(ngx_conf_t *cf);
 15 static char *ngx_mail_imap_merge_srv_conf(ngx_conf_t *cf, void *parent,
 16     void *child);
 17 
 18 
 19 static ngx_str_t  ngx_mail_imap_default_capabilities[] = {
 20     ngx_string("IMAP4"),
 21     ngx_string("IMAP4rev1"),
 22     ngx_string("UIDPLUS"),
 23     ngx_null_string
 24 };
 25 
 26 
 27 static ngx_conf_bitmask_t  ngx_mail_imap_auth_methods[] = {
 28     { ngx_string("plain"), NGX_MAIL_AUTH_PLAIN_ENABLED },
 29     { ngx_string("login"), NGX_MAIL_AUTH_LOGIN_ENABLED },
 30     { ngx_string("cram-md5"), NGX_MAIL_AUTH_CRAM_MD5_ENABLED },
 31     { ngx_null_string, 0 }
 32 };
 33 
 34 
 35 static ngx_str_t  ngx_mail_imap_auth_methods_names[] = {
 36     ngx_string("AUTH=PLAIN"),
 37     ngx_string("AUTH=LOGIN"),
 38     ngx_null_string,  /* APOP */
 39     ngx_string("AUTH=CRAM-MD5")
 40 };
 41 
 42 
 43 static ngx_mail_protocol_t  ngx_mail_imap_protocol = {
 44     ngx_string("imap"),
 45     { 143, 993, 0, 0 },
 46     NGX_MAIL_IMAP_PROTOCOL,
 47 
 48     ngx_mail_imap_init_session,
 49     ngx_mail_imap_init_protocol,
 50     ngx_mail_imap_parse_command,
 51     ngx_mail_imap_auth_state,
 52 
 53     ngx_string("* BAD internal server error" CRLF)
 54 };
 55 
 56 
 57 static ngx_command_t  ngx_mail_imap_commands[] = {
 58 
 59     { ngx_string("imap_client_buffer"),
 60       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
 61       ngx_conf_set_size_slot,
 62       NGX_MAIL_SRV_CONF_OFFSET,
 63       offsetof(ngx_mail_imap_srv_conf_t, client_buffer_size),
 64       NULL },
 65 
 66     { ngx_string("imap_capabilities"),
 67       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
 68       ngx_mail_capabilities,
 69       NGX_MAIL_SRV_CONF_OFFSET,
 70       offsetof(ngx_mail_imap_srv_conf_t, capabilities),
 71       NULL },
 72 
 73     { ngx_string("imap_auth"),
 74       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
 75       ngx_conf_set_bitmask_slot,
 76       NGX_MAIL_SRV_CONF_OFFSET,
 77       offsetof(ngx_mail_imap_srv_conf_t, auth_methods),
 78       &ngx_mail_imap_auth_methods },
 79 
 80       ngx_null_command
 81 };
 82 
 83 
 84 static ngx_mail_module_t  ngx_mail_imap_module_ctx = {
 85     &ngx_mail_imap_protocol,               /* protocol */
 86 
 87     NULL,                                  /* create main configuration */
 88     NULL,                                  /* init main configuration */
 89 
 90     ngx_mail_imap_create_srv_conf,         /* create server configuration */
 91     ngx_mail_imap_merge_srv_conf           /* merge server configuration */
 92 };
 93 
 94 
 95 ngx_module_t  ngx_mail_imap_module = {
 96     NGX_MODULE_V1,
 97     &ngx_mail_imap_module_ctx,             /* module context */
 98     ngx_mail_imap_commands,                /* module directives */
 99     NGX_MAIL_MODULE,                       /* module type */
100     NULL,                                  /* init master */
101     NULL,                                  /* init module */
102     NULL,                                  /* init process */
103     NULL,                                  /* init thread */
104     NULL,                                  /* exit thread */
105     NULL,                                  /* exit process */
106     NULL,                                  /* exit master */
107     NGX_MODULE_V1_PADDING
108 };
109 
110 
111 static void *
112 ngx_mail_imap_create_srv_conf(ngx_conf_t *cf)
113 {
114     ngx_mail_imap_srv_conf_t  *iscf;
115 
116     iscf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_imap_srv_conf_t));
117     if (iscf == NULL) {
118         return NULL;
119     }
120 
121     iscf->client_buffer_size = NGX_CONF_UNSET_SIZE;
122 
123     if (ngx_array_init(&iscf->capabilities, cf->pool, 4, sizeof(ngx_str_t))
124         != NGX_OK)
125     {
126         return NULL;
127     }
128 
129     return iscf;
130 }
131 
132 
133 static char *
134 ngx_mail_imap_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
135 {
136     ngx_mail_imap_srv_conf_t *prev = parent;
137     ngx_mail_imap_srv_conf_t *conf = child;
138 
139     u_char      *p, *auth;
140     size_t       size;
141     ngx_str_t   *c, *d;
142     ngx_uint_t   i, m;
143 
144     ngx_conf_merge_size_value(conf->client_buffer_size,
145                               prev->client_buffer_size,
146                               (size_t) ngx_pagesize);
147 
148     ngx_conf_merge_bitmask_value(conf->auth_methods,
149                               prev->auth_methods,
150                               (NGX_CONF_BITMASK_SET
151                                |NGX_MAIL_AUTH_PLAIN_ENABLED));
152 
153 
154     if (conf->capabilities.nelts == 0) {
155         conf->capabilities = prev->capabilities;
156     }
157 
158     if (conf->capabilities.nelts == 0) {
159 
160         for (d = ngx_mail_imap_default_capabilities; d->len; d++) {
161             c = ngx_array_push(&conf->capabilities);
162             if (c == NULL) {
163                 return NGX_CONF_ERROR;
164             }
165 
166             *c = *d;
167         }
168     }
169 
170     size = sizeof("* CAPABILITY" CRLF) - 1;
171 
172     c = conf->capabilities.elts;
173     for (i = 0; i < conf->capabilities.nelts; i++) {
174         size += 1 + c[i].len;
175     }
176 
177     for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
178          m <= NGX_MAIL_AUTH_CRAM_MD5_ENABLED;
179          m <<= 1, i++)
180     {
181         if (m & conf->auth_methods) {
182             size += 1 + ngx_mail_imap_auth_methods_names[i].len;
183         }
184     }
185 
186     p = ngx_palloc(cf->pool, size);
187     if (p == NULL) {
188         return NGX_CONF_ERROR;
189     }
190 
191     conf->capability.len = size;
192     conf->capability.data = p;
193 
194     p = ngx_cpymem(p, "* CAPABILITY", sizeof("* CAPABILITY") - 1);
195 
196     for (i = 0; i < conf->capabilities.nelts; i++) {
197         *p++ = ' ';
198         p = ngx_cpymem(p, c[i].data, c[i].len);
199     }
200 
201     auth = p;
202 
203     for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
204          m <= NGX_MAIL_AUTH_CRAM_MD5_ENABLED;
205          m <<= 1, i++)
206     {
207         if (m & conf->auth_methods) {
208             *p++ = ' ';
209             p = ngx_cpymem(p, ngx_mail_imap_auth_methods_names[i].data,
210                            ngx_mail_imap_auth_methods_names[i].len);
211         }
212     }
213 
214     *p++ = CR; *p = LF;
215 
216 
217     size += sizeof(" STARTTLS") - 1;
218 
219     p = ngx_palloc(cf->pool, size);
220     if (p == NULL) {
221         return NGX_CONF_ERROR;
222     }
223 
224     conf->starttls_capability.len = size;
225     conf->starttls_capability.data = p;
226 
227     p = ngx_cpymem(p, conf->capability.data,
228                    conf->capability.len - (sizeof(CRLF) - 1));
229     p = ngx_cpymem(p, " STARTTLS", sizeof(" STARTTLS") - 1);
230     *p++ = CR; *p = LF;
231 
232 
233     size = (auth - conf->capability.data) + sizeof(CRLF) - 1
234             + sizeof(" STARTTLS LOGINDISABLED") - 1;
235 
236     p = ngx_palloc(cf->pool, size);
237     if (p == NULL) {
238         return NGX_CONF_ERROR;
239     }
240 
241     conf->starttls_only_capability.len = size;
242     conf->starttls_only_capability.data = p;
243 
244     p = ngx_cpymem(p, conf->capability.data,
245                    auth - conf->capability.data);
246     p = ngx_cpymem(p, " STARTTLS LOGINDISABLED",
247                    sizeof(" STARTTLS LOGINDISABLED") - 1);
248     *p++ = CR; *p = LF;
249 
250     return NGX_CONF_OK;
251 }
252 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.