By request, SASL PLAIN for IMAP and SMTP.
[squirrelmail.git] / class / deliver / Deliver_SMTP.class.php
CommitLineData
e1ee60fe 1<?php
5b8fd093 2/**
3 * Deliver_SMTP.class.php
4 *
76911253 5 * Copyright (c) 1999-2003 The SquirrelMail Project Team
5b8fd093 6 * Licensed under the GNU GPL. For full terms see the file COPYING.
7 *
8 * Delivery backend for the Deliver class.
9 *
10 * $Id$
11 */
e1ee60fe 12
0f85ddf9 13require_once(SM_PATH . 'class/deliver/Deliver.class.php');
e1ee60fe 14
15class Deliver_SMTP extends Deliver {
16
5fe73b9f 17 function preWriteToStream(&$s) {
18 if ($s) {
19 if ($s{0} == '.') $s = '.' . $s;
20 $s = str_replace("\n.","\n..",$s);
21 }
22 }
23
24 function initStream($message, $domain, $length=0, $host='', $port='', $user='', $pass='', $authpop=false) {
47a29326 25 global $use_smtp_tls,$smtp_auth_mech,$username,$key,$onetimepad;
26
27 if ($authpop) {
28 $this->authPop($host, '', $username, $pass);
5fe73b9f 29 }
47a29326 30
31 $rfc822_header = $message->rfc822_header;
5fe73b9f 32 $from = $rfc822_header->from[0];
33 $to = $rfc822_header->to;
34 $cc = $rfc822_header->cc;
47a29326 35 $bcc = $rfc822_header->bcc;
36
37 if (($use_smtp_tls == true) and (check_php_version(4,3)) and (extension_loaded('openssl'))) {
38 $stream = fsockopen('tls://' . $host, $port, $errorNumber, $errorString);
39 } else {
40 $stream = fsockopen($host, $port, $errorNumber, $errorString);
41 }
5fe73b9f 42
5fe73b9f 43 if (!$stream) {
44 $this->dlv_msg = $errorString;
45 $this->dlv_ret_nr = $errorNumber;
46 return(0);
47 }
48 $tmp = fgets($stream, 1024);
49 if ($this->errorCheck($tmp, $stream)) {
50 return(0);
51 }
0c0e6024 52
22bd1baf 53 /*
54 * If $_SERVER['HTTP_HOST'] is set, use that in our HELO to the SMTP
55 * server. This should fix the DNS issues some people have had
56 */
0c0e6024 57 if (sqgetGlobalVar('HTTP_HOST', $HTTP_HOST, SQ_SERVER)) { // HTTP_HOST is set
22bd1baf 58 // optionally trim off port number
59 if($p = strrpos($HTTP_HOST, ':')) {
60 $HTTP_HOST = substr($HTTP_HOST, 0, $p);
61 }
0c0e6024 62 $helohost = $HTTP_HOST;
63 } else { // For some reason, HTTP_HOST is not set - revert to old behavior
64 $helohost = $domain;
65 }
66
5fe73b9f 67 /* Lets introduce ourselves */
33feaaec 68 fputs($stream, "EHLO $helohost\r\n");
69 $tmp = fgets($stream,1024);
70 if ($this->errorCheck($tmp,$stream)) {
71 return(0);
72 }
73
639c7164 74 if (( $smtp_auth_mech == 'cram-md5') or ( $smtp_auth_mech == 'digest-md5' )) {
47a29326 75 // Doing some form of non-plain auth
47a29326 76 if ($smtp_auth_mech == 'cram-md5') {
77 fputs($stream, "AUTH CRAM-MD5\r\n");
78 } elseif ($smtp_auth_mech == 'digest-md5') {
79 fputs($stream, "AUTH DIGEST-MD5\r\n");
80 }
22bd1baf 81
47a29326 82 $tmp = fgets($stream,1024);
83
84 if ($this->errorCheck($tmp,$stream)) {
85 return(0);
86 }
87
88 // At this point, $tmp should hold "334 <challenge string>"
89 $chall = substr($tmp,4);
90 // Depending on mechanism, generate response string
91 if ($smtp_auth_mech == 'cram-md5') {
92 $response = cram_md5_response($username,$pass,$chall);
93 } elseif ($smtp_auth_mech == 'digest-md5') {
94 $response = digest_md5_response($username,$pass,$chall,'smtp',$host);
95 }
96 fputs($stream, $response);
97
98 // Let's see what the server had to say about that
99 $tmp = fgets($stream,1024);
100 if ($this->errorCheck($tmp,$stream)) {
101 return(0);
102 }
103
104 // CRAM-MD5 is done at this point. If DIGEST-MD5, there's a bit more to go
105 if ($smtp_auth_mech == 'digest-md5')
106 {
107 // $tmp contains rspauth, but I don't store that yet. (No need yet)
108 fputs($stream,"\r\n");
109 $tmp = fgets($stream,1024);
110
111 if ($this->errorCheck($tmp,$stream)) {
112 return(0);
113 }
114 }
115 // CRAM-MD5 and DIGEST-MD5 code ends here
116 } elseif ($smtp_auth_mech == 'none') {
117 // No auth at all, just send helo and then send the mail
0c0e6024 118 fputs($stream, "HELO $helohost\r\n");
47a29326 119 $tmp = fgets($stream, 1024);
120 if ($this->errorCheck($tmp, $stream)) {
121 return(0);
122 }
fe0b18b3 123 } elseif ($smtp_auth_mech == 'login') {
124 // The LOGIN method
47a29326 125 fputs($stream, "AUTH LOGIN\r\n");
126 $tmp = fgets($stream, 1024);
5fe73b9f 127
47a29326 128 if ($this->errorCheck($tmp, $stream)) {
129 return(0);
130 }
131 fputs($stream, base64_encode ($username) . "\r\n");
132 $tmp = fgets($stream, 1024);
133 if ($this->errorCheck($tmp, $stream)) {
134 return(0);
135 }
5fe73b9f 136
47a29326 137 fputs($stream, base64_encode($pass) . "\r\n");
138 $tmp = fgets($stream, 1024);
139 if ($this->errorCheck($tmp, $stream)) {
140 return(0);
141 }
33feaaec 142 } elseif ($smtp_auth_mech == "plain") {
143 /* SASL Plain */
144 $auth = base64_encode("$username\0$username\0$pass");
145
146 $query = "AUTH PLAIN\r\n";
147 fputs($stream, $query);
148 $read=fgets($stream, 1024);
149
150 if (substr($read,0,3) == '334') { // OK so far..
151 fputs($stream, "$auth\r\n");
152 $read = fgets($stream, 1024);
153 }
154
155 $results=explode(" ",$read,3);
156 $response=$results[1];
157 $message=$results[2];
158
159
160 } else {
aa358b7e 161 /* Right here, they've reached an unsupported auth mechanism.
162 This is the ugliest hack I've ever done, but it'll do till I can fix
163 things up better tomorrow. So tired... */
164 if ($this->errorCheck("535 Unable to use this auth type",$stream)) {
165 return(0);
166 }
167 }
5fe73b9f 168
169 /* Ok, who is sending the message? */
064206f5 170 fputs($stream, 'MAIL FROM:<'.$from->mailbox.'@'.$from->host.">\r\n");
5fe73b9f 171 $tmp = fgets($stream, 1024);
172 if ($this->errorCheck($tmp, $stream)) {
173 return(0);
174 }
175
176 /* send who the recipients are */
177 for ($i = 0, $cnt = count($to); $i < $cnt; $i++) {
178 if (!$to[$i]->host) $to[$i]->host = $domain;
d1825c3c 179 if ($to[$i]->mailbox) {
064206f5 180 fputs($stream, 'RCPT TO:<'.$to[$i]->mailbox.'@'.$to[$i]->host.">\r\n");
d1825c3c 181 $tmp = fgets($stream, 1024);
182 if ($this->errorCheck($tmp, $stream)) {
183 return(0);
184 }
5fe73b9f 185 }
186 }
d1825c3c 187
5fe73b9f 188 for ($i = 0, $cnt = count($cc); $i < $cnt; $i++) {
d1825c3c 189 if (!$cc[$i]->host) $cc[$i]->host = $domain;
190 if ($cc[$i]->mailbox) {
064206f5 191 fputs($stream, 'RCPT TO:<'.$cc[$i]->mailbox.'@'.$cc[$i]->host.">\r\n");
d1825c3c 192 $tmp = fgets($stream, 1024);
193 if ($this->errorCheck($tmp, $stream)) {
194 return(0);
195 }
5fe73b9f 196 }
197 }
22bd1baf 198
5fe73b9f 199 for ($i = 0, $cnt = count($bcc); $i < $cnt; $i++) {
d1825c3c 200 if (!$bcc[$i]->host) $bcc[$i]->host = $domain;
201 if ($bcc[$i]->mailbox) {
064206f5 202 fputs($stream, 'RCPT TO:<'.$bcc[$i]->mailbox.'@'.$bcc[$i]->host.">\r\n");
d1825c3c 203 $tmp = fgets($stream, 1024);
204 if ($this->errorCheck($tmp, $stream)) {
205 return(0);
206 }
5fe73b9f 207 }
208 }
209 /* Lets start sending the actual message */
210 fputs($stream, "DATA\r\n");
211 $tmp = fgets($stream, 1024);
212 if ($this->errorCheck($tmp, $stream)) {
213 return(0);
214 }
215 return $stream;
216 }
217
218 function finalizeStream($stream) {
219 fputs($stream, ".\r\n"); /* end the DATA part */
220 $tmp = fgets($stream, 1024);
221 $this->errorCheck($tmp, $stream);
222 if ($this->dlv_ret_nr != 250) {
223 return(0);
224 }
225 fputs($stream, "QUIT\r\n"); /* log off */
226 fclose($stream);
227 return true;
228 }
229
ca71b2db 230 /* check if an SMTP reply is an error and set an error message) */
5fe73b9f 231 function errorCheck($line, $smtpConnection) {
ca71b2db 232
233 $err_num = substr($line, 0, 3);
234 $this->dlv_ret_nr = $err_num;
235 $server_msg = substr($line, 4);
236
237 while(substr($line, 0, 4) == ($err_num.'-')) {
238 $line = fgets($smtpConnection, 1024);
239 $server_msg .= substr($line, 4);
240 }
241
242 if ( ((int) $err_num{0}) < 4)
243 {
244 return false;
245 }
246
247 switch ($err_num) {
248 case '421': $message = _("Service not available, closing channel");
249 break;
250 case '432': $message = _("A password transition is needed");
251 break;
252 case '450': $message = _("Requested mail action not taken: mailbox unavailable");
253 break;
254 case '451': $message = _("Requested action aborted: error in processing");
255 break;
256 case '452': $message = _("Requested action not taken: insufficient system storage");
257 break;
258 case '454': $message = _("Temporary authentication failure");
259 break;
260 case '500': $message = _("Syntax error; command not recognized");
261 break;
262 case '501': $message = _("Syntax error in parameters or arguments");
263 break;
264 case '502': $message = _("Command not implemented");
265 break;
266 case '503': $message = _("Bad sequence of commands");
267 break;
268 case '504': $message = _("Command parameter not implemented");
269 break;
270 case '530': $message = _("Authentication required");
271 break;
272 case '534': $message = _("Authentication mechanism is too weak");
273 break;
274 case '535': $message = _("Authentication failed");
275 break;
276 case '538': $message = _("Encryption required for requested authentication mechanism");
277 break;
278 case '550': $message = _("Requested action not taken: mailbox unavailable");
279 break;
280 case '551': $message = _("User not local; please try forwarding");
281 break;
282 case '552': $message = _("Requested mail action aborted: exceeding storage allocation");
283 break;
284 case '553': $message = _("Requested action not taken: mailbox name not allowed");
285 break;
286 case '554': $message = _("Transaction failed");
287 break;
288 default: $message = _("Unknown response");
289 break;
290 }
291
292 $this->dlv_msg = $message;
293 $this->dlv_server_msg = nl2br(htmlspecialchars($server_msg));
294
5fe73b9f 295 return true;
296 }
297
298 function authPop($pop_server='', $pop_port='', $user, $pass) {
299 if (!$pop_port) {
300 $pop_port = 110;
301 }
302 if (!$pop_server) {
303 $pop_server = 'localhost';
304 }
305 $popConnection = fsockopen($pop_server, $pop_port, $err_no, $err_str);
306 if (!$popConnection) {
307 error_log("Error connecting to POP Server ($pop_server:$pop_port)"
308 . " $err_no : $err_str");
309 } else {
310 $tmp = fgets($popConnection, 1024); /* banner */
22bd1baf 311 if (substr($tmp, 0, 3) != '+OK') {
5fe73b9f 312 return(0);
313 }
314 fputs($popConnection, "USER $user\r\n");
315 $tmp = fgets($popConnection, 1024);
22bd1baf 316 if (substr($tmp, 0, 3) != '+OK') {
5fe73b9f 317 return(0);
318 }
319 fputs($popConnection, 'PASS ' . $pass . "\r\n");
320 $tmp = fgets($popConnection, 1024);
22bd1baf 321 if (substr($tmp, 0, 3) != '+OK') {
5fe73b9f 322 return(0);
323 }
324 fputs($popConnection, "QUIT\r\n"); /* log off */
325 fclose($popConnection);
326 }
327 }
e1ee60fe 328}
5fe73b9f 329
e1ee60fe 330?>