Merge in lp:789740
[awesomized/libmemcached] / libmemcached / sasl.c
index ef52c7c31db0fd6c80d1e52d699a5b0f27a5e570..29939a348c3059e6cf8f1fe9d18e3c69466015ec 100644 (file)
@@ -1,15 +1,41 @@
-/* LibMemcached
- * Copyright (C) 2006-2010 Brian Aker
- * All rights reserved.
+/*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ * 
+ *  Libmemcached library
  *
- * Use and distribution licensed under the BSD license.  See
- * the COPYING file in the parent directory for full text.
+ *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ *  Copyright (C) 2006-2009 Brian Aker All rights reserved.
  *
- * Summary: interface for memcached server
- * Description: main include file for libmemcached
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are
+ *  met:
+ *
+ *      * Redistributions of source code must retain the above copyright
+ *  notice, this list of conditions and the following disclaimer.
+ *
+ *      * Redistributions in binary form must reproduce the above
+ *  copyright notice, this list of conditions and the following disclaimer
+ *  in the documentation and/or other materials provided with the
+ *  distribution.
+ *
+ *      * The names of its contributors may not be used to endorse or
+ *  promote products derived from this software without specific prior
+ *  written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  */
-#include "common.h"
+
+#include <libmemcached/common.h>
 
 void memcached_set_sasl_callbacks(memcached_st *ptr,
                                   const sasl_callback_t *callbacks)
@@ -30,7 +56,7 @@ const sasl_callback_t *memcached_get_sasl_callbacks(memcached_st *ptr)
  * @param raddr remote address (out)
  * @return true on success false otherwise (errno contains more info)
  */
-static bool resolve_names(int fd, char *laddr, char *raddr)
+static bool resolve_names(int fd, char *laddr, size_t laddr_length, char *raddr, size_t raddr_length)
 {
   char host[NI_MAXHOST];
   char port[NI_MAXSERV];
@@ -44,7 +70,7 @@ static bool resolve_names(int fd, char *laddr, char *raddr)
     return false;
   }
 
-  (void)sprintf(laddr, "%s;%s", host, port);
+  (void)snprintf(laddr, laddr_length, "%s;%s", host, port);
   salen= sizeof(saddr);
 
   if ((getpeername(fd, (struct sockaddr *)&saddr, &salen) < 0) ||
@@ -54,7 +80,7 @@ static bool resolve_names(int fd, char *laddr, char *raddr)
     return false;
   }
 
-  (void)sprintf(raddr, "%s;%s", host, port);
+  (void)snprintf(raddr, raddr_length, "%s;%s", host, port);
 
   return true;
 }
@@ -64,7 +90,7 @@ memcached_return_t memcached_sasl_authenticate_connection(memcached_server_st *s
   memcached_return_t rc;
 
   /* SANITY CHECK: SASL can only be used with the binary protocol */
-  unlikely (!server->root->flags.binary_protocol)
+  if (!server->root->flags.binary_protocol)
     return MEMCACHED_FAILURE;
 
   /* Try to get the supported mech from the server. Servers without SASL
@@ -108,7 +134,7 @@ memcached_return_t memcached_sasl_authenticate_connection(memcached_server_st *s
   char laddr[NI_MAXHOST + NI_MAXSERV];
   char raddr[NI_MAXHOST + NI_MAXSERV];
 
-  unlikely (!resolve_names(server->fd, laddr, raddr))
+  unlikely (!resolve_names(server->fd, laddr, sizeof(laddr), raddr, sizeof(raddr)))
   {
     server->cached_errno= errno;
     return MEMCACHED_ERRNO;
@@ -223,33 +249,36 @@ memcached_return_t memcached_set_sasl_auth_data(memcached_st *ptr,
     return MEMCACHED_FAILURE;
   }
 
-  sasl_callback_t *cb= libmemcached_calloc(ptr, 4, sizeof(sasl_callback_t));
-  char *name= libmemcached_malloc(ptr, strlen(username) + 1);
-  sasl_secret_t *secret= libmemcached_malloc(ptr, strlen(password) + 1 + sizeof(*secret))
-;
-  if (cb == NULL || name == NULL || secret == NULL)
+  sasl_callback_t *callbacks= libmemcached_calloc(ptr, 4, sizeof(sasl_callback_t));
+  size_t password_length= strlen(password);
+  size_t username_length= strlen(username);
+  char *name= libmemcached_malloc(ptr, username_length +1);
+  sasl_secret_t *secret= libmemcached_malloc(ptr, password_length +1 + sizeof(sasl_secret_t));
+
+  if (callbacks == NULL || name == NULL || secret == NULL)
   {
-    libmemcached_free(ptr, cb);
+    libmemcached_free(ptr, callbacks);
     libmemcached_free(ptr, name);
     libmemcached_free(ptr, secret);
     return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
   }
 
-  secret->len= strlen(password);
-  strcpy((void*)secret->data, password);
-
-  cb[0].id= SASL_CB_USER;
-  cb[0].proc= get_username;
-  cb[0].context= strcpy(name, username);
-  cb[1].id= SASL_CB_AUTHNAME;
-  cb[1].proc= get_username;
-  cb[1].context= name;
-  cb[2].id= SASL_CB_PASS;
-  cb[2].proc= get_password;
-  cb[2].context= secret;
-  cb[3].id= SASL_CB_LIST_END;
-
-  ptr->sasl.callbacks= cb;
+  secret->len= password_length;
+  memcpy(secret->data, password, password_length);
+  secret->data[password_length]= 0;
+
+  callbacks[0].id= SASL_CB_USER;
+  callbacks[0].proc= get_username;
+  callbacks[0].context= strncpy(name, username, username_length +1);
+  callbacks[1].id= SASL_CB_AUTHNAME;
+  callbacks[1].proc= get_username;
+  callbacks[1].context= name;
+  callbacks[2].id= SASL_CB_PASS;
+  callbacks[2].proc= get_password;
+  callbacks[2].context= secret;
+  callbacks[3].id= SASL_CB_LIST_END;
+
+  ptr->sasl.callbacks= callbacks;
   ptr->sasl.is_allocated= true;
 
   return MEMCACHED_SUCCESS;
@@ -346,7 +375,7 @@ memcached_return_t memcached_clone_sasl(memcached_st *clone, const  memcached_st
         libmemcached_free(clone, cb);
         return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
       }
-      strcpy(cb[x].context, source->sasl.callbacks[x].context);
+      strncpy(cb[x].context, source->sasl.callbacks[x].context, sizeof(cb[x].context));
     }
     else
     {