summaryrefslogtreecommitdiff
path: root/source3/lib
diff options
context:
space:
mode:
Diffstat (limited to 'source3/lib')
-rw-r--r--source3/lib/charcnv.c20
-rw-r--r--source3/lib/dbwrap_tdb.c11
-rw-r--r--source3/lib/debug.c6
-rw-r--r--source3/lib/popt_common.c15
-rw-r--r--source3/lib/replace/libreplace_ld.m43
-rw-r--r--source3/lib/replace/system/network.h7
-rw-r--r--source3/lib/util_sock.c133
-rw-r--r--source3/lib/util_str.c177
8 files changed, 149 insertions, 223 deletions
diff --git a/source3/lib/charcnv.c b/source3/lib/charcnv.c
index eeff805459..d11620ecd7 100644
--- a/source3/lib/charcnv.c
+++ b/source3/lib/charcnv.c
@@ -524,7 +524,7 @@ size_t convert_string(charset_t from, charset_t to,
size_t convert_string_allocate(TALLOC_CTX *ctx, charset_t from, charset_t to,
void const *src, size_t srclen, void *dst, bool allow_bad_conv)
{
- size_t i_len, o_len, destlen = MAX(srclen, 512);
+ size_t i_len, o_len, destlen = (srclen * 3) / 2;
size_t retval;
const char *inbuf = (const char *)src;
char *outbuf = NULL, *ob = NULL;
@@ -550,7 +550,8 @@ size_t convert_string_allocate(TALLOC_CTX *ctx, charset_t from, charset_t to,
convert:
- if ((destlen*2) < destlen) {
+ /* +2 is for ucs2 null termination. */
+ if ((destlen*2)+2 < destlen) {
/* wrapped ! abort. */
if (!conv_silent)
DEBUG(0, ("convert_string_allocate: destlen wrapped !\n"));
@@ -561,10 +562,11 @@ size_t convert_string_allocate(TALLOC_CTX *ctx, charset_t from, charset_t to,
destlen = destlen * 2;
}
+ /* +2 is for ucs2 null termination. */
if (ctx) {
- ob = (char *)TALLOC_REALLOC(ctx, ob, destlen);
+ ob = (char *)TALLOC_REALLOC(ctx, ob, destlen + 2);
} else {
- ob = (char *)SMB_REALLOC(ob, destlen);
+ ob = (char *)SMB_REALLOC(ob, destlen + 2);
}
if (!ob) {
@@ -619,10 +621,11 @@ size_t convert_string_allocate(TALLOC_CTX *ctx, charset_t from, charset_t to,
* reallocs *cost*. JRA.
*/
if (o_len > 1024) {
+ /* We're shrinking here so we know the +2 is safe from wrap. */
if (ctx) {
- ob = (char *)TALLOC_REALLOC(ctx,ob,destlen);
+ ob = (char *)TALLOC_REALLOC(ctx,ob,destlen + 2);
} else {
- ob = (char *)SMB_REALLOC(ob,destlen);
+ ob = (char *)SMB_REALLOC(ob,destlen + 2);
}
}
@@ -632,6 +635,11 @@ size_t convert_string_allocate(TALLOC_CTX *ctx, charset_t from, charset_t to,
}
*dest = ob;
+
+ /* Must ucs2 null terminate in the extra space we allocated. */
+ ob[destlen] = '\0';
+ ob[destlen+1] = '\0';
+
return destlen;
use_as_is:
diff --git a/source3/lib/dbwrap_tdb.c b/source3/lib/dbwrap_tdb.c
index 18f9495931..da55049e52 100644
--- a/source3/lib/dbwrap_tdb.c
+++ b/source3/lib/dbwrap_tdb.c
@@ -196,8 +196,15 @@ static NTSTATUS db_tdb_delete(struct db_record *rec)
struct db_tdb_ctx *ctx = talloc_get_type_abort(rec->private_data,
struct db_tdb_ctx);
- return (tdb_delete(ctx->wtdb->tdb, rec->key) == 0) ?
- NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
+ if (tdb_delete(ctx->wtdb->tdb, rec->key) == 0) {
+ return NT_STATUS_OK;
+ }
+
+ if (tdb_error(ctx->wtdb->tdb) == TDB_ERR_NOEXIST) {
+ return NT_STATUS_NOT_FOUND;
+ }
+
+ return NT_STATUS_UNSUCCESSFUL;
}
struct db_tdb_traverse_ctx {
diff --git a/source3/lib/debug.c b/source3/lib/debug.c
index 51bb0d7541..9ff267b607 100644
--- a/source3/lib/debug.c
+++ b/source3/lib/debug.c
@@ -460,14 +460,14 @@ bool debug_parse_levels(const char *params_str)
if (AllowDebugChange == False)
return True;
- params = str_list_make(params_str, NULL);
+ params = str_list_make(talloc_tos(), params_str, NULL);
if (debug_parse_params(params)) {
debug_dump_status(5);
- str_list_free(&params);
+ TALLOC_FREE(params);
return True;
} else {
- str_list_free(&params);
+ TALLOC_FREE(params);
return False;
}
}
diff --git a/source3/lib/popt_common.c b/source3/lib/popt_common.c
index 5a9d39d181..7f7d23fa00 100644
--- a/source3/lib/popt_common.c
+++ b/source3/lib/popt_common.c
@@ -71,10 +71,19 @@ static void popt_common_callback(poptContext con,
}
if (reason == POPT_CALLBACK_REASON_POST) {
- if (!PrintSambaVersionString) return;
- printf( "Version %s\n", SAMBA_VERSION_STRING);
- exit(0);
+ if (PrintSambaVersionString) {
+ printf( "Version %s\n", SAMBA_VERSION_STRING);
+ exit(0);
+ }
+
+ if (is_default_dyn_CONFIGFILE()) {
+ if(getenv("SMB_CONF_PATH")) {
+ set_dyn_CONFIGFILE(getenv("SMB_CONF_PATH"));
+ }
+ }
+
+ /* Further 'every Samba program must do this' hooks here. */
return;
}
diff --git a/source3/lib/replace/libreplace_ld.m4 b/source3/lib/replace/libreplace_ld.m4
index 2aec698967..f0d10c1e3e 100644
--- a/source3/lib/replace/libreplace_ld.m4
+++ b/source3/lib/replace/libreplace_ld.m4
@@ -289,6 +289,9 @@ AC_DEFUN([AC_LIBREPLACE_RUNTIME_LIB_PATH_VAR],
*linux*)
LIB_PATH_VAR=LD_LIBRARY_PATH
;;
+ *netbsd*)
+ LIB_PATH_VAR=LD_LIBRARY_PATH
+ ;;
*solaris*)
LIB_PATH_VAR=LD_LIBRARY_PATH
;;
diff --git a/source3/lib/replace/system/network.h b/source3/lib/replace/system/network.h
index fe6e46817f..aff8a841da 100644
--- a/source3/lib/replace/system/network.h
+++ b/source3/lib/replace/system/network.h
@@ -137,8 +137,15 @@ const char *rep_inet_ntop(int af, const void *src, char *dst, socklen_t size);
#endif
#ifndef AI_ADDRCONFIG
+/*
+ * logic copied from AI_NUMERICHOST
+ */
+#if defined(HAVE_STRUCT_ADDRINFO) && defined(HAVE_GETADDRINFO)
+#define AI_ADDRCONFIG 0
+#else
#define AI_ADDRCONFIG 0x0020
#endif
+#endif
#ifndef AI_NUMERICSERV
/*
diff --git a/source3/lib/util_sock.c b/source3/lib/util_sock.c
index ced1130536..71d48d6053 100644
--- a/source3/lib/util_sock.c
+++ b/source3/lib/util_sock.c
@@ -112,7 +112,7 @@ static bool interpret_string_addr_internal(struct addrinfo **ppres,
&hints,
ppres);
if (ret) {
- DEBUG(3,("interpret_string_addr_interal: getaddrinfo failed "
+ DEBUG(3,("interpret_string_addr_internal: getaddrinfo failed "
"for name %s [%s]\n",
str,
gai_strerror(ret) ));
@@ -913,12 +913,10 @@ ssize_t read_udp_v4_socket(int fd,
time_out = timeout in milliseconds
****************************************************************************/
-ssize_t read_socket_with_timeout(int fd,
- char *buf,
- size_t mincnt,
- size_t maxcnt,
- unsigned int time_out,
- enum smb_read_errors *pre)
+NTSTATUS read_socket_with_timeout(int fd, char *buf,
+ size_t mincnt, size_t maxcnt,
+ unsigned int time_out,
+ size_t *size_ret)
{
fd_set fds;
int selrtn;
@@ -929,9 +927,7 @@ ssize_t read_socket_with_timeout(int fd,
/* just checking .... */
if (maxcnt <= 0)
- return(0);
-
- set_smb_read_error(pre,SMB_READ_OK);
+ return NT_STATUS_OK;
/* Blocking read */
if (time_out == 0) {
@@ -945,8 +941,7 @@ ssize_t read_socket_with_timeout(int fd,
if (readret == 0) {
DEBUG(5,("read_socket_with_timeout: "
"blocking read. EOF from client.\n"));
- set_smb_read_error(pre,SMB_READ_EOF);
- return -1;
+ return NT_STATUS_END_OF_FILE;
}
if (readret == -1) {
@@ -962,12 +957,11 @@ ssize_t read_socket_with_timeout(int fd,
"read error = %s.\n",
strerror(errno) ));
}
- set_smb_read_error(pre,SMB_READ_ERROR);
- return -1;
+ return map_nt_error_from_unix(errno);
}
nread += readret;
}
- return((ssize_t)nread);
+ goto done;
}
/* Most difficult - timeout read */
@@ -1001,16 +995,14 @@ ssize_t read_socket_with_timeout(int fd,
"read. select error = %s.\n",
strerror(errno) ));
}
- set_smb_read_error(pre,SMB_READ_ERROR);
- return -1;
+ return map_nt_error_from_unix(errno);
}
/* Did we timeout ? */
if (selrtn == 0) {
DEBUG(10,("read_socket_with_timeout: timeout read. "
"select timed out.\n"));
- set_smb_read_error(pre,SMB_READ_TIMEOUT);
- return -1;
+ return NT_STATUS_IO_TIMEOUT;
}
readret = sys_read(fd, buf+nread, maxcnt-nread);
@@ -1019,8 +1011,7 @@ ssize_t read_socket_with_timeout(int fd,
/* we got EOF on the file descriptor */
DEBUG(5,("read_socket_with_timeout: timeout read. "
"EOF from client.\n"));
- set_smb_read_error(pre,SMB_READ_EOF);
- return -1;
+ return NT_STATUS_END_OF_FILE;
}
if (readret == -1) {
@@ -1037,24 +1028,27 @@ ssize_t read_socket_with_timeout(int fd,
"read. read error = %s.\n",
strerror(errno) ));
}
- set_smb_read_error(pre,SMB_READ_ERROR);
- return -1;
+ return map_nt_error_from_unix(errno);
}
nread += readret;
}
+ done:
/* Return the number we got */
- return (ssize_t)nread;
+ if (size_ret) {
+ *size_ret = nread;
+ }
+ return NT_STATUS_OK;
}
/****************************************************************************
Read data from the client, reading exactly N bytes.
****************************************************************************/
-ssize_t read_data(int fd,char *buffer,size_t N, enum smb_read_errors *pre)
+NTSTATUS read_data(int fd, char *buffer, size_t N)
{
- return read_socket_with_timeout(fd, buffer, N, N, 0, pre);
+ return read_socket_with_timeout(fd, buffer, N, N, 0, NULL);
}
/****************************************************************************
@@ -1116,28 +1110,29 @@ bool send_keepalive(int client)
Timeout is in milliseconds.
****************************************************************************/
-ssize_t read_smb_length_return_keepalive(int fd,
- char *inbuf,
- unsigned int timeout,
- enum smb_read_errors *pre)
+NTSTATUS read_smb_length_return_keepalive(int fd, char *inbuf,
+ unsigned int timeout,
+ size_t *len)
{
- ssize_t len=0;
int msg_type;
+ NTSTATUS status;
- if (read_socket_with_timeout(fd, inbuf, 4, 4, timeout, pre) != 4) {
- return -1;
+ status = read_socket_with_timeout(fd, inbuf, 4, 4, timeout, NULL);
+
+ if (!NT_STATUS_IS_OK(status)) {
+ return status;
}
- len = smb_len(inbuf);
+ *len = smb_len(inbuf);
msg_type = CVAL(inbuf,0);
if (msg_type == SMBkeepalive) {
DEBUG(5,("Got keepalive packet\n"));
}
- DEBUG(10,("got smb length of %lu\n",(unsigned long)len));
+ DEBUG(10,("got smb length of %lu\n",(unsigned long)(*len)));
- return len;
+ return NT_STATUS_OK;
}
/****************************************************************************
@@ -1147,24 +1142,27 @@ ssize_t read_smb_length_return_keepalive(int fd,
Timeout is in milliseconds.
****************************************************************************/
-ssize_t read_smb_length(int fd, char *inbuf, unsigned int timeout, enum smb_read_errors *pre)
+NTSTATUS read_smb_length(int fd, char *inbuf, unsigned int timeout,
+ size_t *len)
{
- ssize_t len;
uint8_t msgtype = SMBkeepalive;
while (msgtype == SMBkeepalive) {
- len = read_smb_length_return_keepalive(fd, inbuf, timeout,
- pre);
- if (len < 0) {
- return len;
+ NTSTATUS status;
+
+ status = read_smb_length_return_keepalive(fd, inbuf, timeout,
+ len);
+ if (!NT_STATUS_IS_OK(status)) {
+ return status;
}
+
msgtype = CVAL(inbuf, 0);
}
DEBUG(10,("read_smb_length: got smb length of %lu\n",
(unsigned long)len));
- return len;
+ return NT_STATUS_OK;
}
/****************************************************************************
@@ -1177,28 +1175,17 @@ ssize_t read_smb_length(int fd, char *inbuf, unsigned int timeout, enum smb_read
Doesn't check the MAC on signed packets.
****************************************************************************/
-ssize_t receive_smb_raw(int fd,
- char *buffer,
- unsigned int timeout,
- size_t maxlen,
- enum smb_read_errors *pre)
+NTSTATUS receive_smb_raw(int fd, char *buffer, unsigned int timeout,
+ size_t maxlen, size_t *p_len)
{
- ssize_t len,ret;
-
- set_smb_read_error(pre,SMB_READ_OK);
-
- len = read_smb_length_return_keepalive(fd,buffer,timeout,pre);
- if (len < 0) {
- DEBUG(10,("receive_smb_raw: length < 0!\n"));
+ size_t len;
+ NTSTATUS status;
- /*
- * Correct fix. smb_read_error may have already been
- * set. Only set it here if not already set. Global
- * variables still suck :-). JRA.
- */
+ status = read_smb_length_return_keepalive(fd,buffer,timeout,&len);
- cond_set_smb_read_error(pre,SMB_READ_ERROR);
- return -1;
+ if (!NT_STATUS_IS_OK(status)) {
+ DEBUG(10, ("receive_smb_raw: %s!\n", nt_errstr(status)));
+ return status;
}
/*
@@ -1210,15 +1197,7 @@ ssize_t receive_smb_raw(int fd,
DEBUG(0,("Invalid packet length! (%lu bytes).\n",
(unsigned long)len));
if (len > BUFFER_SIZE + (SAFETY_MARGIN/2)) {
-
- /*
- * Correct fix. smb_read_error may have already been
- * set. Only set it here if not already set. Global
- * variables still suck :-). JRA.
- */
-
- cond_set_smb_read_error(pre,SMB_READ_ERROR);
- return -1;
+ return NT_STATUS_INVALID_PARAMETER;
}
}
@@ -1227,12 +1206,11 @@ ssize_t receive_smb_raw(int fd,
len = MIN(len,maxlen);
}
- ret = read_socket_with_timeout(fd, buffer+4, len, len, timeout,
- pre);
+ status = read_socket_with_timeout(
+ fd, buffer+4, len, len, timeout, &len);
- if (ret != len) {
- cond_set_smb_read_error(pre,SMB_READ_ERROR);
- return -1;
+ if (!NT_STATUS_IS_OK(status)) {
+ return status;
}
/* not all of samba3 properly checks for packet-termination
@@ -1241,7 +1219,8 @@ ssize_t receive_smb_raw(int fd,
SSVAL(buffer+4,len, 0);
}
- return len;
+ *p_len = len;
+ return NT_STATUS_OK;
}
/****************************************************************************
diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c
index bcb9197141..93ecad728d 100644
--- a/source3/lib/util_str.c
+++ b/source3/lib/util_str.c
@@ -1841,149 +1841,93 @@ int fstr_sprintf(fstring s, const char *fmt, ...)
#define S_LIST_ABS 16 /* List Allocation Block Size */
-static char **str_list_make_internal(TALLOC_CTX *mem_ctx,
- const char *string,
- const char *sep)
+char **str_list_make(TALLOC_CTX *mem_ctx, const char *string, const char *sep)
{
- char **list, **rlist;
+ char **list;
const char *str;
char *s;
int num, lsize;
char *tok;
- TALLOC_CTX *frame = NULL;
if (!string || !*string)
return NULL;
- if (mem_ctx) {
- s = talloc_strdup(mem_ctx, string);
- } else {
- s = SMB_STRDUP(string);
+
+ list = TALLOC_ARRAY(mem_ctx, char *, S_LIST_ABS+1);
+ if (list == NULL) {
+ return NULL;
}
- if (!s) {
+ lsize = S_LIST_ABS;
+
+ s = talloc_strdup(list, string);
+ if (s == NULL) {
DEBUG(0,("str_list_make: Unable to allocate memory"));
+ TALLOC_FREE(list);
return NULL;
}
if (!sep) sep = LIST_SEP;
- num = lsize = 0;
- list = NULL;
-
+ num = 0;
str = s;
- frame = talloc_stackframe();
- while (next_token_talloc(frame, &str, &tok, sep)) {
+
+ while (next_token_talloc(list, &str, &tok, sep)) {
+
if (num == lsize) {
+ char **tmp;
+
lsize += S_LIST_ABS;
- if (mem_ctx) {
- rlist = TALLOC_REALLOC_ARRAY(mem_ctx, list,
- char *, lsize +1);
- } else {
- /* We need to keep the old list on
- * error so we can free the elements
- if the realloc fails. */
- rlist =SMB_REALLOC_ARRAY_KEEP_OLD_ON_ERROR(list,
- char *, lsize +1);
- }
- if (!rlist) {
+
+ tmp = TALLOC_REALLOC_ARRAY(mem_ctx, list, char *,
+ lsize + 1);
+ if (tmp == NULL) {
DEBUG(0,("str_list_make: "
"Unable to allocate memory"));
- str_list_free(&list);
- if (mem_ctx) {
- TALLOC_FREE(s);
- } else {
- SAFE_FREE(s);
- }
- TALLOC_FREE(frame);
+ TALLOC_FREE(list);
return NULL;
- } else {
- list = rlist;
}
- memset (&list[num], 0,
- ((sizeof(char**)) * (S_LIST_ABS +1)));
- }
- if (mem_ctx) {
- list[num] = talloc_strdup(mem_ctx, tok);
- } else {
- list[num] = SMB_STRDUP(tok);
- }
+ list = tmp;
- if (!list[num]) {
- DEBUG(0,("str_list_make: Unable to allocate memory"));
- str_list_free(&list);
- if (mem_ctx) {
- TALLOC_FREE(s);
- } else {
- SAFE_FREE(s);
- }
- TALLOC_FREE(frame);
- return NULL;
+ memset (&list[num], 0,
+ ((sizeof(char**)) * (S_LIST_ABS +1)));
}
- num++;
+ list[num] = tok;
+ num += 1;
}
- TALLOC_FREE(frame);
-
- if (mem_ctx) {
- TALLOC_FREE(s);
- } else {
- SAFE_FREE(s);
- }
+ list[num] = NULL;
+ TALLOC_FREE(s);
return list;
}
-char **str_list_make_talloc(TALLOC_CTX *mem_ctx,
- const char *string,
- const char *sep)
-{
- return str_list_make_internal(mem_ctx, string, sep);
-}
-
-char **str_list_make(const char *string, const char *sep)
-{
- return str_list_make_internal(NULL, string, sep);
-}
-
-bool str_list_copy(char ***dest, const char **src)
+bool str_list_copy(TALLOC_CTX *mem_ctx, char ***dest, const char **src)
{
- char **list, **rlist;
- int num, lsize;
+ char **list;
+ int i, num;
*dest = NULL;
if (!src)
return false;
- num = lsize = 0;
- list = NULL;
+ num = 0;
+ while (src[num] != NULL) {
+ num += 1;
+ }
- while (src[num]) {
- if (num == lsize) {
- lsize += S_LIST_ABS;
- rlist = SMB_REALLOC_ARRAY_KEEP_OLD_ON_ERROR(list,
- char *, lsize +1);
- if (!rlist) {
- DEBUG(0,("str_list_copy: "
- "Unable to re-allocate memory"));
- str_list_free(&list);
- return false;
- } else {
- list = rlist;
- }
- memset (&list[num], 0,
- ((sizeof(char **)) * (S_LIST_ABS +1)));
- }
+ list = TALLOC_ARRAY(mem_ctx, char *, num+1);
+ if (list == NULL) {
+ return false;
+ }
- list[num] = SMB_STRDUP(src[num]);
- if (!list[num]) {
- DEBUG(0,("str_list_copy: Unable to allocate memory"));
- str_list_free(&list);
+ for (i=0; i<num; i++) {
+ list[i] = talloc_strdup(list, src[i]);
+ if (list[i] == NULL) {
+ TALLOC_FREE(list);
return false;
}
-
- num++;
}
-
+ list[i] = NULL;
*dest = list;
return true;
}
@@ -2010,37 +1954,6 @@ bool str_list_compare(char **list1, char **list2)
return true;
}
-static void str_list_free_internal(TALLOC_CTX *mem_ctx, char ***list)
-{
- char **tlist;
-
- if (!list || !*list)
- return;
- tlist = *list;
- for(; *tlist; tlist++) {
- if (mem_ctx) {
- TALLOC_FREE(*tlist);
- } else {
- SAFE_FREE(*tlist);
- }
- }
- if (mem_ctx) {
- TALLOC_FREE(*tlist);
- } else {
- SAFE_FREE(*list);
- }
-}
-
-void str_list_free_talloc(TALLOC_CTX *mem_ctx, char ***list)
-{
- str_list_free_internal(mem_ctx, list);
-}
-
-void str_list_free(char ***list)
-{
- str_list_free_internal(NULL, list);
-}
-
/******************************************************************************
*****************************************************************************/