Description: Fix buffer overflow in SMTP DIGEST-MD5 negotiation
 When negotiating SMTP DIGEST-MD5 authentication, the function
 smtp_state_authdigest_resp() uses the data provided from the
 server without doing the proper length checks and that data is then
 appended to a local fixed-size buffer on the stack.
Origin: vendor, adapted from http://curl.haxx.se/curl-sasl.patch
Bug: http://curl.haxx.se/docs/adv_20130206.html
Bug-Debian: http://bugs.debian.org/700002
Forwarded: not-needed
Author: Alessandro Ghedini <ghedo@debian.org>
Last-Update: 2013-02-10

--- a/lib/smtp.c
+++ b/lib/smtp.c
@@ -879,7 +879,8 @@
   char cnonce[]     = "12345678"; /* will be changed */
   char method[]     = "AUTHENTICATE";
   char qop[]        = "auth";
-  char uri[128]     = "smtp/";
+  char service[]    = "smtp";
+  char uri[128];
   char response[512];
 
   (void)instate; /* no use for this yet */
@@ -963,8 +964,8 @@
   for(i = 0; i < MD5_DIGEST_LEN; i++)
     snprintf(&HA1_hex[2 * i], 3, "%02x", digest[i]);
 
-  /* Orepare URL string, append realm to the protocol */
-  strcat(uri, realm);
+  /* Prepare the URL string */
+  snprintf(uri, sizeof(uri), "%s/%s", service, realm);
 
   /* Calculate H(A2) */
   ctxt = Curl_MD5_init(Curl_DIGEST_MD5);
@@ -1008,20 +1009,11 @@
   for(i = 0; i < MD5_DIGEST_LEN; i++)
     snprintf(&resp_hash_hex[2 * i], 3, "%02x", digest[i]);
 
-  strcpy(response, "username=\"");
-  strcat(response, conn->user);
-  strcat(response, "\",realm=\"");
-  strcat(response, realm);
-  strcat(response, "\",nonce=\"");
-  strcat(response, nonce);
-  strcat(response, "\",cnonce=\"");
-  strcat(response, cnonce);
-  strcat(response, "\",nc=");
-  strcat(response, nonceCount);
-  strcat(response, ",digest-uri=\"");
-  strcat(response, uri);
-  strcat(response, "\",response=");
-  strcat(response, resp_hash_hex);
+  snprintf(response, sizeof(response),
+           "username=\"%s\",realm=\"%s\",nonce=\"%s\","
+           "cnonce=\"%s\",nc=\"%s\",digest-uri=\"%s\",response=%s",
+           conn->user, realm, nonce,
+           cnonce, nonceCount, uri, resp_hash_hex);
 
   /* Encode it to base64 and send it */
   result = Curl_base64_encode(data, response, 0, &rplyb64, &len);

