comparison src/mail/ngx_mail_auth_http_module.c @ 1487:f69493e8faab

ngx_mail_pop3_module, ngx_mail_imap_module, and ngx_mail_smtp_module
author Igor Sysoev <igor@sysoev.ru>
date Sat, 15 Sep 2007 16:51:16 +0000
parents d0cce8369848
children fe11e2a3946d
comparison
equal deleted inserted replaced
1486:0e7074ef7303 1487:f69493e8faab
109 ngx_null_command 109 ngx_null_command
110 }; 110 };
111 111
112 112
113 static ngx_mail_module_t ngx_mail_auth_http_module_ctx = { 113 static ngx_mail_module_t ngx_mail_auth_http_module_ctx = {
114 NULL, /* protocol */
115
114 NULL, /* create main configuration */ 116 NULL, /* create main configuration */
115 NULL, /* init main configuration */ 117 NULL, /* init main configuration */
116 118
117 ngx_mail_auth_http_create_conf, /* create server configuration */ 119 ngx_mail_auth_http_create_conf, /* create server configuration */
118 ngx_mail_auth_http_merge_conf /* merge server configuration */ 120 ngx_mail_auth_http_merge_conf /* merge server configuration */
133 NULL, /* exit master */ 135 NULL, /* exit master */
134 NGX_MODULE_V1_PADDING 136 NGX_MODULE_V1_PADDING
135 }; 137 };
136 138
137 139
138 static char *ngx_mail_auth_http_protocol[] = { "pop3", "imap", "smtp" };
139 static ngx_str_t ngx_mail_auth_http_method[] = { 140 static ngx_str_t ngx_mail_auth_http_method[] = {
140 ngx_string("plain"), 141 ngx_string("plain"),
141 ngx_string("plain"), 142 ngx_string("plain"),
142 ngx_string("apop"), 143 ngx_string("apop"),
143 ngx_string("cram-md5") 144 ngx_string("cram-md5")
144 }; 145 };
145 146
146 static ngx_str_t ngx_mail_smtp_errcode = ngx_string("535 5.7.0"); 147 static ngx_str_t ngx_mail_smtp_errcode = ngx_string("535 5.7.0");
147
148 static ngx_uint_t ngx_mail_start_states[] = {
149 ngx_pop3_start,
150 ngx_imap_start,
151 ngx_smtp_start
152 };
153
154 static ngx_mail_auth_state_pt ngx_mail_auth_states[] = {
155 ngx_mail_pop3_auth_state,
156 ngx_mail_imap_auth_state,
157 ngx_mail_smtp_auth_state
158 };
159 148
160 149
161 void 150 void
162 ngx_mail_auth_http_init(ngx_mail_session_t *s) 151 ngx_mail_auth_http_init(ngx_mail_session_t *s)
163 { 152 {
760 ngx_destroy_pool(ctx->pool); 749 ngx_destroy_pool(ctx->pool);
761 ngx_mail_session_internal_server_error(s); 750 ngx_mail_session_internal_server_error(s);
762 return; 751 return;
763 } 752 }
764 753
765 if (s->passwd.data == NULL && s->protocol != NGX_MAIL_SMTP_PROTOCOL) 754 if (s->passwd.data == NULL
755 && s->protocol != NGX_MAIL_SMTP_PROTOCOL)
766 { 756 {
767 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, 757 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
768 "auth http server %V did not send password", 758 "auth http server %V did not send password",
769 ctx->peer.name); 759 ctx->peer.name);
770 ngx_destroy_pool(ctx->pool); 760 ngx_destroy_pool(ctx->pool);
879 s->auth_wait = 0; 869 s->auth_wait = 0;
880 ngx_mail_auth_http_init(s); 870 ngx_mail_auth_http_init(s);
881 return; 871 return;
882 } 872 }
883 873
884 s->mail_state = ngx_mail_start_states[s->protocol]; 874 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
885 rev->handler = ngx_mail_auth_states[s->protocol]; 875
886 876 rev->handler = cscf->protocol->auth_state;
877
878 s->mail_state = 0;
887 s->auth_method = NGX_MAIL_AUTH_PLAIN; 879 s->auth_method = NGX_MAIL_AUTH_PLAIN;
888 880
889 c->log->action = "in auth state"; 881 c->log->action = "in auth state";
890 882
891 ngx_mail_send(c->write); 883 ngx_mail_send(c->write);
892 884
893 if (c->destroyed) { 885 if (c->destroyed) {
894 return; 886 return;
895 } 887 }
896
897 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
898 888
899 ngx_add_timer(rev, cscf->timeout); 889 ngx_add_timer(rev, cscf->timeout);
900 890
901 if (rev->ready) { 891 if (rev->ready) {
902 rev->handler(rev); 892 rev->handler(rev);
1143 1133
1144 static ngx_buf_t * 1134 static ngx_buf_t *
1145 ngx_mail_auth_http_create_request(ngx_mail_session_t *s, ngx_pool_t *pool, 1135 ngx_mail_auth_http_create_request(ngx_mail_session_t *s, ngx_pool_t *pool,
1146 ngx_mail_auth_http_conf_t *ahcf) 1136 ngx_mail_auth_http_conf_t *ahcf)
1147 { 1137 {
1148 size_t len; 1138 size_t len;
1149 ngx_buf_t *b; 1139 ngx_buf_t *b;
1150 ngx_str_t login, passwd; 1140 ngx_str_t login, passwd;
1141 ngx_mail_core_srv_conf_t *cscf;
1151 1142
1152 if (ngx_mail_auth_http_escape(pool, &s->login, &login) != NGX_OK) { 1143 if (ngx_mail_auth_http_escape(pool, &s->login, &login) != NGX_OK) {
1153 return NULL; 1144 return NULL;
1154 } 1145 }
1155 1146
1156 if (ngx_mail_auth_http_escape(pool, &s->passwd, &passwd) != NGX_OK) { 1147 if (ngx_mail_auth_http_escape(pool, &s->passwd, &passwd) != NGX_OK) {
1157 return NULL; 1148 return NULL;
1158 } 1149 }
1150
1151 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
1159 1152
1160 len = sizeof("GET ") - 1 + ahcf->uri.len + sizeof(" HTTP/1.0" CRLF) - 1 1153 len = sizeof("GET ") - 1 + ahcf->uri.len + sizeof(" HTTP/1.0" CRLF) - 1
1161 + sizeof("Host: ") - 1 + ahcf->host_header.len + sizeof(CRLF) - 1 1154 + sizeof("Host: ") - 1 + ahcf->host_header.len + sizeof(CRLF) - 1
1162 + sizeof("Auth-Method: ") - 1 1155 + sizeof("Auth-Method: ") - 1
1163 + ngx_mail_auth_http_method[s->auth_method].len 1156 + ngx_mail_auth_http_method[s->auth_method].len
1164 + sizeof(CRLF) - 1 1157 + sizeof(CRLF) - 1
1165 + sizeof("Auth-User: ") - 1 + login.len + sizeof(CRLF) - 1 1158 + sizeof("Auth-User: ") - 1 + login.len + sizeof(CRLF) - 1
1166 + sizeof("Auth-Pass: ") - 1 + passwd.len + sizeof(CRLF) - 1 1159 + sizeof("Auth-Pass: ") - 1 + passwd.len + sizeof(CRLF) - 1
1167 + sizeof("Auth-Salt: ") - 1 + s->salt.len 1160 + sizeof("Auth-Salt: ") - 1 + s->salt.len
1168 + sizeof("Auth-Protocol: imap" CRLF) - 1 1161 + sizeof("Auth-Protocol: ") - 1 + cscf->protocol->name.len
1162 + sizeof(CRLF) - 1
1169 + sizeof("Auth-Login-Attempt: ") - 1 + NGX_INT_T_LEN 1163 + sizeof("Auth-Login-Attempt: ") - 1 + NGX_INT_T_LEN
1170 + sizeof(CRLF) - 1 1164 + sizeof(CRLF) - 1
1171 + sizeof("Client-IP: ") - 1 + s->connection->addr_text.len 1165 + sizeof("Client-IP: ") - 1 + s->connection->addr_text.len
1172 + sizeof(CRLF) - 1 1166 + sizeof(CRLF) - 1
1173 + ahcf->header.len 1167 + ahcf->header.len
1210 s->passwd.data = NULL; 1204 s->passwd.data = NULL;
1211 } 1205 }
1212 1206
1213 b->last = ngx_cpymem(b->last, "Auth-Protocol: ", 1207 b->last = ngx_cpymem(b->last, "Auth-Protocol: ",
1214 sizeof("Auth-Protocol: ") - 1); 1208 sizeof("Auth-Protocol: ") - 1);
1215 b->last = ngx_cpymem(b->last, ngx_mail_auth_http_protocol[s->protocol], 1209 b->last = ngx_cpymem(b->last, cscf->protocol->name.data,
1216 sizeof("imap") - 1); 1210 cscf->protocol->name.len);
1217 *b->last++ = CR; *b->last++ = LF; 1211 *b->last++ = CR; *b->last++ = LF;
1218 1212
1219 b->last = ngx_sprintf(b->last, "Auth-Login-Attempt: %ui" CRLF, 1213 b->last = ngx_sprintf(b->last, "Auth-Login-Attempt: %ui" CRLF,
1220 s->login_attempt); 1214 s->login_attempt);
1221 1215