diff options
author | Andrew Tridgell <tridge@samba.org> | 2002-07-15 10:35:28 +0000 |
---|---|---|
committer | Andrew Tridgell <tridge@samba.org> | 2002-07-15 10:35:28 +0000 |
commit | e90b65284812aaa5ff9e9935ce9bbad7791cbbcd (patch) | |
tree | 9e744d1dc2f93934a4b49166a37383d3cb2b2139 /source3/lib | |
parent | ec167dc9cc0ec2ee461837c25a371d2981744208 (diff) | |
download | samba-e90b65284812aaa5ff9e9935ce9bbad7791cbbcd.tar.gz samba-e90b65284812aaa5ff9e9935ce9bbad7791cbbcd.tar.bz2 samba-e90b65284812aaa5ff9e9935ce9bbad7791cbbcd.zip |
updated the 3.0 branch from the head branch - ready for alpha18
(This used to be commit 03ac082dcb375b6f3ca3d810a6a6367542bc23ce)
Diffstat (limited to 'source3/lib')
34 files changed, 2106 insertions, 1625 deletions
diff --git a/source3/lib/access.c b/source3/lib/access.c index f12ee92799..4e524735e4 100644 --- a/source3/lib/access.c +++ b/source3/lib/access.c @@ -30,7 +30,7 @@ static int masked_match(char *tok, char *slash, char *s) if (strlen(slash + 1) > 2) { mask = interpret_addr(slash + 1); } else { - mask = (uint32)((ALLONES >> atoi(slash + 1)) ^ ALLONES); + mask = (uint32)((ALLONES << atoi(slash + 1)) ^ ALLONES); } if (net == INADDR_NONE || mask == INADDR_NONE) { @@ -188,7 +188,7 @@ static int list_match(char **list,char *item, int (*match_fn)(char *, char *)) /* return true if access should be allowed */ -BOOL allow_access(char **deny_list,char **allow_list, +static BOOL allow_access_internal(char **deny_list,char **allow_list, char *cname,char *caddr) { char *client[2]; @@ -240,6 +240,22 @@ BOOL allow_access(char **deny_list,char **allow_list, return (True); } +/* return true if access should be allowed */ +BOOL allow_access(char **deny_list,char **allow_list, + const char *cname, const char *caddr) +{ + BOOL ret; + + char *nc_cname = smb_xstrdup(cname); + char *nc_caddr = smb_xstrdup(caddr); + + ret = allow_access_internal(deny_list, allow_list, nc_cname, nc_caddr); + + SAFE_FREE(nc_cname); + SAFE_FREE(nc_caddr); + return ret; +} + /* return true if the char* contains ip addrs only. Used to avoid gethostbyaddr() calls */ static BOOL only_ipaddrs_in_list(char** list) diff --git a/source3/lib/charcnv.c b/source3/lib/charcnv.c index cdfca8eb97..d42dc994b0 100644 --- a/source3/lib/charcnv.c +++ b/source3/lib/charcnv.c @@ -143,7 +143,7 @@ size_t convert_string(charset_t from, charset_t to, switch(errno) { case EINVAL: reason="Incomplete multibyte sequence"; break; case E2BIG: reason="No more room"; - DEBUG(0, ("Required %d, available %d\n", + DEBUG(0, ("convert_string: Required %d, available %d\n", srclen, destlen)); /* we are not sure we need srclen bytes, may be more, may be less. @@ -354,7 +354,15 @@ int pull_ascii(char *dest, const void *src, int dest_len, int src_len, int flags dest_len = sizeof(pstring); } - if (flags & STR_TERMINATE) src_len = strlen(src)+1; + if (flags & STR_TERMINATE) { + if (src_len == -1) { + src_len = strlen(src) + 1; + } else { + int len = strnlen(src, src_len); + if (len < src_len) len++; + src_len = len; + } + } ret = convert_string(CH_DOS, CH_UNIX, src, src_len, dest, dest_len); @@ -525,7 +533,7 @@ copy a string from a ucs2 source to a unix char* destination flags can have: STR_TERMINATE means the string in src is null terminated STR_NOALIGN means don't try to align -if STR_TERMINATE is set then src_len is ignored +if STR_TERMINATE is set then src_len is ignored if it is -1 src_len is the length of the source area in bytes return the number of bytes occupied by the string in src the resulting string in "dest" is always null terminated @@ -543,7 +551,15 @@ int pull_ucs2(const void *base_ptr, char *dest, const void *src, int dest_len, i if (src_len > 0) src_len--; } - if (flags & STR_TERMINATE) src_len = strlen_w(src)*2+2; + if (flags & STR_TERMINATE) { + if (src_len == -1) { + src_len = strlen_w(src)*2 + 2; + } else { + int len = strnlen_w(src, src_len/2); + if (len < src_len/2) len++; + src_len = len*2; + } + } /* ucs2 is always a multiple of 2 bytes */ src_len &= ~1; @@ -609,7 +625,15 @@ int pull_utf8(char *dest, const void *src, int dest_len, int src_len, int flags) dest_len = sizeof(pstring); } - if (flags & STR_TERMINATE) src_len = strlen(src)+1; + if (flags & STR_TERMINATE) { + if (src_len == -1) { + src_len = strlen(src) + 1; + } else { + int len = strnlen(src, src_len); + if (len < src_len) len++; + src_len = len; + } + } ret = convert_string(CH_UTF8, CH_UNIX, src, src_len, dest, dest_len); if (dest_len) dest[MIN(ret, dest_len-1)] = 0; @@ -687,7 +711,7 @@ flags can have: STR_UNICODE means to force as unicode STR_ASCII use ascii even with unicode packet STR_NOALIGN means don't do alignment -if STR_TERMINATE is set then src_len is ignored +if STR_TERMINATE is set then src_len is ignored is it is -1 src_len is the length of the source area in bytes return the number of bytes occupied by the string in src the resulting string in "dest" is always null terminated diff --git a/source3/lib/debug.c b/source3/lib/debug.c index 7960c32b58..f41c3b6497 100644 --- a/source3/lib/debug.c +++ b/source3/lib/debug.c @@ -2,6 +2,8 @@ Unix SMB/CIFS implementation. Samba utility functions Copyright (C) Andrew Tridgell 1992-1998 + Copyright (C) Elrond 2002 + Copyright (C) Simo Sorce 2002 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -81,11 +83,23 @@ XFILE *dbf = NULL; pstring debugf = ""; BOOL append_log = False; +BOOL debug_warn_unknown_class = True; +BOOL debug_auto_add_unknown_class = True; +BOOL AllowDebugChange = True; -int DEBUGLEVEL_CLASS[DBGC_LAST]; -BOOL DEBUGLEVEL_CLASS_ISSET[DBGC_LAST]; -int DEBUGLEVEL = DEBUGLEVEL_CLASS; -BOOL AllowDebugChange = True; +/* + * This is to allow assignment to DEBUGLEVEL before the debug + * system has been initialised. + */ +static int debug_all_class_hack = 1; +static BOOL debug_all_class_isset_hack = True; + +static int debug_num_classes = 0; +int *DEBUGLEVEL_CLASS = &debug_all_class_hack; +BOOL *DEBUGLEVEL_CLASS_ISSET = &debug_all_class_isset_hack; + +/* DEBUGLEVEL is #defined to *debug_level */ +int DEBUGLEVEL = &debug_all_class_hack; /* -------------------------------------------------------------------------- ** @@ -129,40 +143,112 @@ static BOOL log_overflow = False; * white space. There must be one name for each DBGC_<class name>, and they * must be in the table in the order of DBGC_<class name>.. */ -char *classname_table[] = { +static const char *default_classname_table[] = { "all", /* DBGC_ALL; index refs traditional DEBUGLEVEL */ "tdb", /* DBGC_TDB */ "printdrivers", /* DBGC_PRINTDRIVERS */ "lanman", /* DBGC_LANMAN */ "smb", /* DBGC_SMB */ - "rpc", /* DBGC_RPC */ - "rpc_hdr", /* DBGC_RPC_HDR */ - "bdc", /* DBGC_BDC */ + "rpc_parse", /* DBGC_RPC_PARSE */ + "rpc_srv", /* DBGC_RPC_SRV */ + "rpc_cli", /* DBGC_RPC_CLI */ + "passdb", /* DBGC_PASSDB */ + "auth", /* DBGC_AUTH */ + "winbind", /* DBGC_WINBIND */ + NULL }; +static char **classname_table = NULL; + /* -------------------------------------------------------------------------- ** * Functions... */ + +/**************************************************************************** +utility lists registered debug class names's +****************************************************************************/ + +#define MAX_CLASS_NAME_SIZE 1024 + +static char *debug_list_class_names_and_levels(void) +{ + int i, dim; + char **list; + char *buf = NULL; + char *b; + BOOL err = False; + + if (DEBUGLEVEL_CLASS == &debug_all_class_hack) + return NULL; + + list = calloc(debug_num_classes + 1, sizeof(char *)); + if (!list) + return NULL; + + /* prepare strings */ + for (i = 0, dim = 0; i < debug_num_classes; i++) { + int l = asprintf(&list[i], + "%s:%d ", + classname_table[i], + DEBUGLEVEL_CLASS_ISSET[i]?DEBUGLEVEL_CLASS[i]:DEBUGLEVEL); + if (l < 0 || l > MAX_CLASS_NAME_SIZE) { + err = True; + goto done; + } + dim += l; + } + + /* create single string list */ + b = buf = malloc(dim); + if (!buf) { + err = True; + goto done; + } + for (i = 0; i < debug_num_classes; i++) { + int l = strlen(list[i]); + strncpy(b, list[i], l); + b = b + l; + } + b[-1] = '\0'; + +done: + /* free strings list */ + for (i = 0; i < debug_num_classes; i++) + if (list[i]) free(list[i]); + free(list); + + if (err) { + if (buf) + free(buf); + return NULL; + } else { + return buf; + } +} + /**************************************************************************** utility access to debug class names's ****************************************************************************/ -char* debug_classname_from_index(int ndx) +const char *debug_classname_from_index(int ndx) { - return classname_table[ndx]; + if (ndx < 0 || ndx >= debug_num_classes) + return NULL; + else + return classname_table[ndx]; } /**************************************************************************** -utility to translate names to debug class index's +utility to translate names to debug class index's (internal version) ****************************************************************************/ -int debug_lookup_classname(char* classname) +static int debug_lookup_classname_int(const char* classname) { int i; if (!classname) return -1; - for (i=0; i<DBGC_LAST; i++) { + for (i=0; i < debug_num_classes; i++) { if (strcmp(classname, classname_table[i])==0) return i; } @@ -170,6 +256,118 @@ int debug_lookup_classname(char* classname) } /**************************************************************************** +Add a new debug class to the system +****************************************************************************/ +int debug_add_class(const char *classname) +{ + int ndx; + void *new_ptr; + + if (!classname) + return -1; + + /* check the init has yet been called */ + debug_init(); + + ndx = debug_lookup_classname_int(classname); + if (ndx >= 0) + return ndx; + ndx = debug_num_classes; + + new_ptr = DEBUGLEVEL_CLASS; + if (DEBUGLEVEL_CLASS == &debug_all_class_hack) + { + /* Initial loading... */ + new_ptr = NULL; + } + new_ptr = Realloc(new_ptr, + sizeof(int) * (debug_num_classes + 1)); + if (!new_ptr) + return -1; + DEBUGLEVEL_CLASS = new_ptr; + DEBUGLEVEL_CLASS[ndx] = 0; + + /* debug_level is the pointer used for the DEBUGLEVEL-thingy */ + if (ndx==0) + { + /* Transfer the initial level from debug_all_class_hack */ + DEBUGLEVEL_CLASS[ndx] = DEBUGLEVEL; + } + debug_level = DEBUGLEVEL_CLASS; + + new_ptr = DEBUGLEVEL_CLASS_ISSET; + if (new_ptr == &debug_all_class_isset_hack) + { + new_ptr = NULL; + } + new_ptr = Realloc(new_ptr, + sizeof(BOOL) * (debug_num_classes + 1)); + if (!new_ptr) + return -1; + DEBUGLEVEL_CLASS_ISSET = new_ptr; + DEBUGLEVEL_CLASS_ISSET[ndx] = False; + + new_ptr = Realloc(classname_table, + sizeof(char *) * (debug_num_classes + 1)); + if (!new_ptr) + return -1; + classname_table = new_ptr; + + classname_table[ndx] = strdup(classname); + if (! classname_table[ndx]) + return -1; + + debug_num_classes++; + + return ndx; +} + +/**************************************************************************** +utility to translate names to debug class index's (public version) +****************************************************************************/ +int debug_lookup_classname(const char *classname) +{ + int ndx; + + if (!classname || !*classname) return -1; + + ndx = debug_lookup_classname_int(classname); + + if (ndx != -1) + return ndx; + + if (debug_warn_unknown_class) + { + DEBUG(0, ("debug_lookup_classname(%s): Unknown class\n", + classname)); + } + if (debug_auto_add_unknown_class) + { + return debug_add_class(classname); + } + return -1; +} + + +/**************************************************************************** +dump the current registered denug levels +****************************************************************************/ +static void debug_dump_status(int level) +{ + int q; + + DEBUG(level, ("INFO: Current debug levels:\n")); + for (q = 0; q < debug_num_classes; q++) + { + DEBUGADD(level, (" %s: %s/%d\n", + classname_table[q], + (DEBUGLEVEL_CLASS_ISSET[q] + ? "True" : "False"), + DEBUGLEVEL_CLASS[q])); + } +} + +/**************************************************************************** parse the debug levels from smbcontrol. Example debug level parameter: printdrivers:7 ****************************************************************************/ @@ -179,9 +377,9 @@ BOOL debug_parse_params(char **params, int *debuglevel_class, int i, ndx; char *class_name; char *class_level; - - /* Set the new debug level array to the current DEBUGLEVEL array */ - memcpy(debuglevel_class, DEBUGLEVEL_CLASS, sizeof(DEBUGLEVEL_CLASS)); + + if (!params) + return False; /* Allow DBGC_ALL to be specified w/o requiring its class name e.g."10" * v.s. "all:10", this is the traditional way to set DEBUGLEVEL @@ -195,7 +393,7 @@ BOOL debug_parse_params(char **params, int *debuglevel_class, i = 0; /* DBGC_ALL not specified OR class name was included */ /* Fill in new debug class levels */ - for (; i < DBGC_LAST && params[i]; i++) { + for (; i < debug_num_classes && params[i]; i++) { if ((class_name=strtok(params[i],":")) && (class_level=strtok(NULL, "\0")) && ((ndx = debug_lookup_classname(class_name)) != -1)) { @@ -215,83 +413,102 @@ parse the debug levels from smb.conf. Example debug level string: 3 tdb:5 printdrivers:7 Note: the 1st param has no "name:" preceeding it. ****************************************************************************/ -BOOL debug_parse_levels(char *params_str) +BOOL debug_parse_levels(const char *params_str) { - int i; - char *params[DBGC_LAST]; - int debuglevel_class[DBGC_LAST]; - BOOL debuglevel_class_isset[DBGC_LAST]; + char **params; + + /* Just in case */ + debug_init(); if (AllowDebugChange == False) return True; - ZERO_ARRAY(params); - ZERO_ARRAY(debuglevel_class); - ZERO_ARRAY(debuglevel_class_isset); - - if ((params[0]=strtok(params_str," ,"))) { - for (i=1; i<DBGC_LAST;i++) { - if ((params[i]=strtok(NULL," ,"))==NULL) - break; - } - } - else - return False; - - if (debug_parse_params(params, debuglevel_class, - debuglevel_class_isset)) { - debug_message(0, sys_getpid(), (void*)debuglevel_class, sizeof(debuglevel_class)); - - memcpy(DEBUGLEVEL_CLASS, debuglevel_class, - sizeof(debuglevel_class)); - memcpy(DEBUGLEVEL_CLASS_ISSET, debuglevel_class_isset, - sizeof(debuglevel_class_isset)); - - { - int q; - - for (q = 0; q < DBGC_LAST; q++) - DEBUG(5, ("%s: %d/%d\n", - classname_table[q], - DEBUGLEVEL_CLASS[q], - DEBUGLEVEL_CLASS_ISSET[q])); - } + params = str_list_make(params_str); + if (debug_parse_params(params, DEBUGLEVEL_CLASS, + DEBUGLEVEL_CLASS_ISSET)) + { + debug_dump_status(5); + str_list_free(¶ms); return True; - } else + } else { + str_list_free(¶ms); return False; + } } /**************************************************************************** receive a "set debug level" message ****************************************************************************/ -void debug_message(int msg_type, pid_t src, void *buf, size_t len) +static void debug_message(int msg_type, pid_t src, void *buf, size_t len) { - struct debuglevel_message *dm = (struct debuglevel_message *)buf; - int i; + const char *params_str = buf; - /* Set the new DEBUGLEVEL_CLASS array from the passed message */ - memcpy(DEBUGLEVEL_CLASS, dm->debuglevel_class, sizeof(dm->debuglevel_class)); - memcpy(DEBUGLEVEL_CLASS_ISSET, dm->debuglevel_class_isset, sizeof(dm->debuglevel_class_isset)); + /* Check, it's a proper string! */ + if (params_str[len-1] != '\0') + { + DEBUG(1, ("Invalid debug message from pid %u to pid %u\n", + (unsigned int)src, (unsigned int)getpid())); + return; + } - DEBUG(3,("INFO: Debug class %s level = %d (pid %u from pid %u)\n", - classname_table[DBGC_ALL], - DEBUGLEVEL_CLASS[DBGC_ALL], (unsigned int)sys_getpid(), (unsigned int)src)); + DEBUG(3, ("INFO: Remote set of debug to `%s' (pid %u from pid %u)\n", + params_str, (unsigned int)getpid(), (unsigned int)src)); - for (i=1; i<DBGC_LAST; i++) { - if (DEBUGLEVEL_CLASS[i]) - DEBUGADD(3,("INFO: Debug class %s level = %d\n", - classname_table[i], DEBUGLEVEL_CLASS[i])); - } + debug_parse_levels(params_str); } /**************************************************************************** send a "set debug level" message ****************************************************************************/ -void debug_message_send(pid_t pid, int level) +void debug_message_send(pid_t pid, const char *params_str) { - message_send_pid(pid, MSG_DEBUG, &level, sizeof(int), False); + if (!params_str) + return; + message_send_pid(pid, MSG_DEBUG, params_str, strlen(params_str) + 1, + False); +} + + +/**************************************************************************** + Return current debug level. +****************************************************************************/ + +static void debuglevel_message(int msg_type, pid_t src, void *buf, size_t len) +{ + char *debug_level_classes; + DEBUG(1,("INFO: Received REQ_DEBUGLEVEL message from PID %u\n",(unsigned int)src)); + + if ((debug_level_classes = debug_list_class_names_and_levels())) { + /*{ debug_level_classes = "test:1000";*/ + message_send_pid(src, MSG_DEBUGLEVEL, debug_level_classes, strlen(debug_level_classes) + 1, True); + SAFE_FREE(debug_level_classes); + } else { + DEBUG(0, ("debuglevel_message: error retrieving class levels!\n")); + } +} + +/**************************************************************************** +Init debugging (one time stuff) +****************************************************************************/ +void debug_init(void) +{ + static BOOL initialised = False; + const char **p; + + if (initialised) + return; + + initialised = True; + + message_register(MSG_DEBUG, debug_message); + message_register(MSG_REQ_DEBUGLEVEL, debuglevel_message); + + for(p = default_classname_table; *p; p++) + { + debug_add_class(*p); + } } @@ -299,9 +516,9 @@ void debug_message_send(pid_t pid, int level) * get ready for syslog stuff * ************************************************************************** ** */ -void setup_logging(char *pname, BOOL interactive) +void setup_logging(const char *pname, BOOL interactive) { - message_register(MSG_DEBUG, debug_message); + debug_init(); /* reset to allow multiple setup calls, going from interactive to non-interactive */ @@ -314,7 +531,7 @@ void setup_logging(char *pname, BOOL interactive) } #ifdef WITH_SYSLOG else { - char *p = strrchr_m( pname,'/' ); + const char *p = strrchr_m( pname,'/' ); if (p) pname = p + 1; #ifdef LOG_DAEMON diff --git a/source3/lib/dprintf.c b/source3/lib/dprintf.c index dadebdb3b4..f0f09e199d 100644 --- a/source3/lib/dprintf.c +++ b/source3/lib/dprintf.c @@ -36,12 +36,15 @@ int d_vfprintf(FILE *f, const char *format, va_list ap) char *p, *p2; int ret, maxlen, clen; const char *msgstr; + va_list ap2; /* do any message translations */ msgstr = lang_msg(format); if (!msgstr) return -1; - ret = vasprintf(&p, msgstr, ap); + VA_COPY(ap2, ap); + + ret = vasprintf(&p, msgstr, ap2); lang_msg_free(msgstr); diff --git a/source3/lib/error.c b/source3/lib/error.c index 22ac1cb2d7..608d2b89ba 100644 --- a/source3/lib/error.c +++ b/source3/lib/error.c @@ -25,8 +25,8 @@ const struct unix_error_map unix_dos_nt_errmap[] = { { EPERM, ERRDOS, ERRnoaccess, NT_STATUS_ACCESS_DENIED }, { EACCES, ERRDOS, ERRnoaccess, NT_STATUS_ACCESS_DENIED }, - { ENOENT, ERRDOS, ERRbadfile, NT_STATUS_NO_SUCH_FILE }, - { ENOTDIR, ERRDOS, ERRbadpath, NT_STATUS_NOT_A_DIRECTORY }, + { ENOENT, ERRDOS, ERRbadfile, NT_STATUS_OBJECT_NAME_NOT_FOUND }, + { ENOTDIR, ERRDOS, ERRbadpath, NT_STATUS_OBJECT_PATH_NOT_FOUND }, { EIO, ERRHRD, ERRgeneral, NT_STATUS_IO_DEVICE_ERROR }, { EBADF, ERRSRV, ERRsrverror, NT_STATUS_INVALID_HANDLE }, { EINVAL, ERRSRV, ERRsrverror, NT_STATUS_INVALID_HANDLE }, diff --git a/source3/lib/genrand.c b/source3/lib/genrand.c index 6296ead726..ee8bc0b1d5 100644 --- a/source3/lib/genrand.c +++ b/source3/lib/genrand.c @@ -24,8 +24,8 @@ static unsigned char hash[258]; static uint32 counter; -unsigned char *reseed_data; -size_t reseed_data_size; +static unsigned char *reseed_data; +static size_t reseed_data_size; /**************************************************************** Copy any user given reseed data. diff --git a/source3/lib/interface.c b/source3/lib/interface.c index d43001342e..0d751a9c7c 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -276,19 +276,6 @@ int iface_count(void) } /**************************************************************************** - True if we have two or more interfaces. - **************************************************************************/ -BOOL we_are_multihomed(void) -{ - static int multi = -1; - - if(multi == -1) - multi = (iface_count() > 1 ? True : False); - - return multi; -} - -/**************************************************************************** return the Nth interface **************************************************************************/ struct interface *get_interface(int n) @@ -331,40 +318,21 @@ struct in_addr *iface_n_bcast(int n) } -/**************************************************************************** -this function provides a simple hash of the configured interfaces. It is -used to detect a change in interfaces to tell us whether to discard -the current wins.dat file. -Note that the result is independent of the order of the interfaces - **************************************************************************/ -unsigned iface_hash(void) -{ - unsigned ret = 0; - struct interface *i; - - for (i=local_interfaces;i;i=i->next) { - unsigned x1 = (unsigned)str_checksum(inet_ntoa(i->ip)); - unsigned x2 = (unsigned)str_checksum(inet_ntoa(i->nmask)); - ret ^= (x1 ^ x2); - } - - return ret; -} - - /* these 3 functions return the ip/bcast/nmask for the interface most appropriate for the given ip address. If they can't find an appropriate interface they return the requested field of the first known interface. */ -struct in_addr *iface_bcast(struct in_addr ip) +struct in_addr *iface_ip(struct in_addr ip) { struct interface *i = iface_find(ip, True); - return(i ? &i->bcast : &local_interfaces->bcast); + return(i ? &i->ip : &local_interfaces->ip); } -struct in_addr *iface_ip(struct in_addr ip) +/* + return True if a IP is directly reachable on one of our interfaces +*/ +BOOL iface_local(struct in_addr ip) { - struct interface *i = iface_find(ip, True); - return(i ? &i->ip : &local_interfaces->ip); + return iface_find(ip, True) ? True : False; } diff --git a/source3/lib/interfaces.c b/source3/lib/interfaces.c index 7b8ef0d0c1..96f4b4cd94 100644 --- a/source3/lib/interfaces.c +++ b/source3/lib/interfaces.c @@ -41,10 +41,6 @@ #include <sys/time.h> #include <net/if.h> -#ifndef SIOCGIFCONF -#include <sys/sockio.h> -#endif - #ifdef AUTOCONF_TEST struct iface_struct { char name[16]; @@ -56,6 +52,16 @@ struct iface_struct { #include "interfaces.h" #endif +#ifdef HAVE_SYS_TIME_H +#include <sys/time.h> +#endif + +#ifndef SIOCGIFCONF +#ifdef HAVE_SYS_SOCKIO_H +#include <sys/sockio.h> +#endif +#endif + #ifdef HAVE_STDLIB_H #include <stdlib.h> #endif diff --git a/source3/lib/md5.c b/source3/lib/md5.c index 627725bb25..2121b17047 100644 --- a/source3/lib/md5.c +++ b/source3/lib/md5.c @@ -22,6 +22,8 @@ #include "md5.h" +static void MD5Transform(uint32 buf[4], uint32 const in[16]); + /* * Note: this code is harmless on little-endian machines. */ @@ -161,7 +163,7 @@ void MD5Final(unsigned char digest[16], struct MD5Context *ctx) * reflect the addition of 16 longwords of new data. MD5Update blocks * the data and converts bytes into longwords for this routine. */ -void MD5Transform(uint32 buf[4], uint32 const in[16]) +static void MD5Transform(uint32 buf[4], uint32 const in[16]) { register uint32 a, b, c, d; diff --git a/source3/lib/messages.c b/source3/lib/messages.c index 8602c2f32d..e6d2de4a58 100644 --- a/source3/lib/messages.c +++ b/source3/lib/messages.c @@ -79,7 +79,7 @@ static void sig_usr1(void) A useful function for testing the message system. ****************************************************************************/ -void ping_message(int msg_type, pid_t src, void *buf, size_t len) +static void ping_message(int msg_type, pid_t src, void *buf, size_t len) { char *msg = buf ? buf : "none"; DEBUG(1,("INFO: Received PING message from PID %u [%s]\n",(unsigned int)src, msg)); @@ -87,16 +87,6 @@ void ping_message(int msg_type, pid_t src, void *buf, size_t len) } /**************************************************************************** - Return current debug level. -****************************************************************************/ - -void debuglevel_message(int msg_type, pid_t src, void *buf, size_t len) -{ - DEBUG(1,("INFO: Received REQ_DEBUGLEVEL message from PID %u\n",(unsigned int)src)); - message_send_pid(src, MSG_DEBUGLEVEL, DEBUGLEVEL_CLASS, sizeof(DEBUGLEVEL_CLASS), True); -} - -/**************************************************************************** Initialise the messaging functions. ****************************************************************************/ @@ -116,7 +106,6 @@ BOOL message_init(void) CatchSignal(SIGUSR1, SIGNAL_CAST sig_usr1); message_register(MSG_PING, ping_message); - message_register(MSG_REQ_DEBUGLEVEL, debuglevel_message); return True; } @@ -340,8 +329,8 @@ void message_dispatch(void) } } if (!n_handled) { - DEBUG(5,("message_dispatch: warning: no handlers registed for " - "msg_type %d in pid%d\n", + DEBUG(5,("message_dispatch: warning: no handlers registered for " + "msg_type %d in pid %d\n", msg_type, sys_getpid())); } SAFE_FREE(buf); @@ -464,7 +453,7 @@ BOOL message_send_all(TDB_CONTEXT *conn_tdb, int msg_type, return True; } -static VOLATILE sig_atomic_t gotalarm; +static SIG_ATOMIC_T gotalarm; /*************************************************************** Signal function to tell us we timed out. @@ -486,6 +475,7 @@ BOOL message_named_mutex(char *name, unsigned int timeout) { TDB_DATA key; int ret; + void (*oldsig_handler)(int) = NULL; if (!message_init()) return False; @@ -495,7 +485,7 @@ BOOL message_named_mutex(char *name, unsigned int timeout) if (timeout) { gotalarm = 0; - CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig); + oldsig_handler = CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig); alarm(timeout); } @@ -503,7 +493,7 @@ BOOL message_named_mutex(char *name, unsigned int timeout) if (timeout) { alarm(0); - CatchSignal(SIGALRM, SIGNAL_CAST SIG_IGN); + CatchSignal(SIGALRM, SIGNAL_CAST oldsig_handler); if (gotalarm) return False; } diff --git a/source3/lib/pam_errors.c b/source3/lib/pam_errors.c index f74e4bf176..e1d02151a6 100644 --- a/source3/lib/pam_errors.c +++ b/source3/lib/pam_errors.c @@ -67,6 +67,7 @@ const static struct { {NT_STATUS_WRONG_PASSWORD, PAM_AUTH_ERR}, {NT_STATUS_LOGON_FAILURE, PAM_AUTH_ERR}, {NT_STATUS_ACCOUNT_EXPIRED, PAM_ACCT_EXPIRED}, + {NT_STATUS_PASSWORD_EXPIRED, PAM_AUTHTOK_EXPIRED}, {NT_STATUS_PASSWORD_MUST_CHANGE, PAM_NEW_AUTHTOK_REQD}, {NT_STATUS_OK, PAM_SUCCESS} }; diff --git a/source3/lib/pidfile.c b/source3/lib/pidfile.c index 28fd959b54..b98259fe5e 100644 --- a/source3/lib/pidfile.c +++ b/source3/lib/pidfile.c @@ -35,7 +35,7 @@ pid_t pidfile_pid(char *name) unsigned ret; pstring pidFile; - slprintf(pidFile, sizeof(pidFile)-1, "%s/%s.pid", lp_lockdir(), name); + slprintf(pidFile, sizeof(pidFile)-1, "%s/%s.pid", lp_piddir(), name); fd = sys_open(pidFile, O_NONBLOCK | O_RDONLY, 0644); if (fd == -1) { @@ -68,10 +68,7 @@ pid_t pidfile_pid(char *name) return 0; } -/* Create a pid file in the lock directory. open it and leave it locked. - This must be done after a call to lp_load() as it uses the lp_lockdir() - function to generate the path to the pidfile. */ - +/* create a pid file in the pid directory. open it and leave it locked */ void pidfile_create(char *name) { int fd; @@ -79,7 +76,7 @@ void pidfile_create(char *name) pstring pidFile; pid_t pid; - slprintf(pidFile, sizeof(pidFile)-1, "%s/%s.pid", lp_lockdir(), name); + slprintf(pidFile, sizeof(pidFile)-1, "%s/%s.pid", lp_piddir(), name); pid = pidfile_pid(name); if (pid != 0) { diff --git a/source3/lib/replace.c b/source3/lib/replace.c index dd50ff035e..2cc7d48adb 100644 --- a/source3/lib/replace.c +++ b/source3/lib/replace.c @@ -414,3 +414,17 @@ char *rep_inet_ntoa(struct in_addr ip) return setvbuf(stream, (char *)NULL, _IOLBF, 0); } #endif /* HAVE_SETLINEBUF */ + +#ifndef HAVE_VSYSLOG +#ifdef HAVE_SYSLOG + void vsyslog (int facility_priority, char *format, va_list arglist) +{ + char *msg = NULL; + vasprintf(&msg, format, arglist); + if (!msg) + return; + syslog(facility_priority, "%s", msg); + SAFE_FREE(msg); +} +#endif /* HAVE_SYSLOG */ +#endif /* HAVE_VSYSLOG */ diff --git a/source3/lib/select.c b/source3/lib/select.c index 550941ba77..f88ad52de6 100644 --- a/source3/lib/select.c +++ b/source3/lib/select.c @@ -1,5 +1,6 @@ /* - Unix SMB/CIFS implementation. + Unix SMB/Netbios implementation. + Version 3.0 Samba select/poll implementation Copyright (C) Andrew Tridgell 1992-1998 @@ -101,6 +102,12 @@ int sys_select(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *errorfds, s } if (FD_ISSET(select_pipe[0], readfds2)) { + char c; + saved_errno = errno; + if (read(select_pipe[0], &c, 1) == 1) { + pipe_read++; + } + errno = saved_errno; FD_CLR(select_pipe[0], readfds2); ret--; if (ret == 0) { @@ -109,18 +116,6 @@ int sys_select(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *errorfds, s } } - saved_errno = errno; - - while (pipe_written != pipe_read) { - char c; - /* Due to the linux kernel bug in 2.0.x, we - * always increment here even if the read failed... */ - read(select_pipe[0], &c, 1); - pipe_read++; - } - - errno = saved_errno; - return ret; } @@ -133,10 +128,12 @@ int sys_select_intr(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *errorf { int ret; fd_set *readfds2, readfds_buf, *writefds2, writefds_buf, *errorfds2, errorfds_buf; + struct timeval tval2, *ptval; readfds2 = (readfds ? &readfds_buf : NULL); writefds2 = (writefds ? &writefds_buf : NULL); errorfds2 = (errorfds ? &errorfds_buf : NULL); + ptval = (tval ? &tval2 : NULL); do { if (readfds) @@ -145,7 +142,10 @@ int sys_select_intr(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *errorf writefds_buf = *writefds; if (errorfds) errorfds_buf = *errorfds; - ret = sys_select(maxfd, readfds2, writefds2, errorfds2, tval); + if (tval) + tval2 = *tval; + + ret = sys_select(maxfd, readfds2, writefds2, errorfds2, ptval); } while (ret == -1 && errno == EINTR); if (readfds) diff --git a/source3/lib/signal.c b/source3/lib/signal.c index 99f908235c..dceb3b53bc 100644 --- a/source3/lib/signal.c +++ b/source3/lib/signal.c @@ -96,10 +96,11 @@ void BlockSignals(BOOL block,int signum) 2) The signal should be blocked during handler execution. ********************************************************************/ -void CatchSignal(int signum,void (*handler)(int )) +void (*CatchSignal(int signum,void (*handler)(int )))(int) { #ifdef HAVE_SIGACTION struct sigaction act; + struct sigaction oldact; ZERO_STRUCT(act); @@ -113,10 +114,11 @@ void CatchSignal(int signum,void (*handler)(int )) #endif sigemptyset(&act.sa_mask); sigaddset(&act.sa_mask,signum); - sigaction(signum,&act,NULL); + sigaction(signum,&act,&oldact); + return oldact.sa_handler; #else /* !HAVE_SIGACTION */ /* FIXME: need to handle sigvec and systems with broken signal() */ - signal(signum, handler); + return signal(signum, handler); #endif } diff --git a/source3/lib/snprintf.c b/source3/lib/snprintf.c index 9a9dcdbae1..cd1e63ce59 100644 --- a/source3/lib/snprintf.c +++ b/source3/lib/snprintf.c @@ -57,6 +57,12 @@ #ifndef NO_CONFIG_H /* for some tests */ #include "config.h" +#else +#define NULL 0 +#endif + +#ifdef TEST_SNPRINTF /* need math library headers for testing */ +#include <math.h> #endif #ifdef HAVE_STRING_H @@ -94,8 +100,21 @@ #define LLONG long #endif +/* free memory if the pointer is valid and zero the pointer */ +#ifndef SAFE_FREE +#define SAFE_FREE(x) do { if ((x) != NULL) {free((x)); (x)=NULL;} } while(0) +#endif + +#ifndef VA_COPY +#ifdef HAVE_VA_COPY +#define VA_COPY(dest, src) __va_copy(dest, src) +#else +#define VA_COPY(dest, src) (dest) = (src) +#endif +#endif + static size_t dopr(char *buffer, size_t maxlen, const char *format, - va_list args); + va_list args_in); static void fmtstr(char *buffer, size_t *currlen, size_t maxlen, char *value, int flags, int min, int max); static void fmtint(char *buffer, size_t *currlen, size_t maxlen, @@ -138,7 +157,7 @@ static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c); #define MAX(p,q) (((p) >= (q)) ? (p) : (q)) #endif -static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args) +static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args_in) { char ch; LLONG value; @@ -150,6 +169,9 @@ static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args int flags; int cflags; size_t currlen; + va_list args; + + VA_COPY(args, args_in); state = DP_S_DEFAULT; currlen = flags = cflags = min = 0; @@ -656,9 +678,8 @@ static void fmtfp (char *buffer, size_t *currlen, size_t maxlen, /* Convert integer part */ do { - temp = intpart; - my_modf(intpart*0.1, &intpart); - temp = temp*0.1; + temp = intpart*0.1; + my_modf(temp, &intpart); index = (int) ((temp -intpart +0.05)* 10.0); /* index = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */ /* printf ("%llf, %f, %x\n", temp, intpart, index); */ @@ -672,9 +693,8 @@ static void fmtfp (char *buffer, size_t *currlen, size_t maxlen, if (fracpart) { do { - temp = fracpart; - my_modf(fracpart*0.1, &fracpart); - temp = temp*0.1; + temp = fracpart*0.1; + my_modf(temp, &fracpart); index = (int) ((temp -fracpart +0.05)* 10.0); /* index = (int) ((((temp/10) -fracpart) +0.05) *10); */ /* printf ("%lf, %lf, %ld\n", temp, fracpart, index); */ @@ -726,14 +746,14 @@ static void fmtfp (char *buffer, size_t *currlen, size_t maxlen, if (max > 0) { dopr_outch (buffer, currlen, maxlen, '.'); + while (zpadlen > 0) { + dopr_outch (buffer, currlen, maxlen, '0'); + --zpadlen; + } + while (fplace > 0) dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]); } - - while (zpadlen > 0) { - dopr_outch (buffer, currlen, maxlen, '0'); - --zpadlen; - } while (padlen < 0) { dopr_outch (buffer, currlen, maxlen, ' '); @@ -784,13 +804,19 @@ static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c) int vasprintf(char **ptr, const char *format, va_list ap) { int ret; + va_list ap2; + + VA_COPY(ap2, ap); - ret = vsnprintf(NULL, 0, format, ap); + ret = vsnprintf(NULL, 0, format, ap2); if (ret <= 0) return ret; (*ptr) = (char *)malloc(ret+1); if (!*ptr) return -1; - ret = vsnprintf(*ptr, ret+1, format, ap); + + VA_COPY(ap2, ap); + + ret = vsnprintf(*ptr, ret+1, format, ap2); return ret; } @@ -812,20 +838,6 @@ static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c) } #endif -#ifndef HAVE_VSYSLOG -#ifdef HAVE_SYSLOG - void vsyslog (int facility_priority, char *format, va_list arglist) -{ - char *msg = NULL; - vasprintf(&msg, format, arglist); - if (!msg) - return; - syslog(facility_priority, "%s", msg); - free(msg); -} -#endif /* HAVE_SYSLOG */ -#endif /* HAVE_VSYSLOG */ - #ifdef TEST_SNPRINTF int sprintf(char *str,const char *fmt,...); @@ -853,7 +865,7 @@ static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c) NULL }; double fp_nums[] = { 6442452944.1234, -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996, - 0.9996, 1.996, 4.136, 0}; + 0.9996, 1.996, 4.136, 5.030201, 0}; char *int_fmt[] = { "%-1.5d", "%1.5d", @@ -948,8 +960,10 @@ static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c) { double v0 = 0.12345678901234567890123456789012345678901; for (x=0; x<100; x++) { - snprintf(buf1, sizeof(buf1), "%1.1f", v0*pow(10, x)); - sprintf(buf2, "%1.1f", v0*pow(10, x)); + double p = pow(10, x); + double r = v0*p; + snprintf(buf1, sizeof(buf1), "%1.1f", r); + sprintf(buf2, "%1.1f", r); if (strcmp(buf1, buf2)) { printf("we seem to support %d digits\n", x-1); break; diff --git a/source3/lib/substitute.c b/source3/lib/substitute.c index e878ee8cbf..6d96a1820f 100644 --- a/source3/lib/substitute.c +++ b/source3/lib/substitute.c @@ -84,6 +84,66 @@ static size_t expand_env_var(char *p, int len) } /******************************************************************* + Given a pointer to a %$(NAME) in p and the whole string in str + expand it as an environment variable. + Return a new allocated and expanded string. + Based on code by Branko Cibej <branko.cibej@hermes.si> + When this is called p points at the '%' character. + May substitute multiple occurrencies of the same env var. +********************************************************************/ + + +static char * realloc_expand_env_var(char *str, char *p) +{ + char *envname; + char *envval; + char *q, *r; + int copylen; + + if (p[0] != '%' || p[1] != '$' || p[2] != '(') + return str; + + /* + * Look for the terminating ')'. + */ + + if ((q = strchr_m(p,')')) == NULL) { + DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p)); + return str; + } + + /* + * Extract the name from within the %$(NAME) string. + */ + + r = p + 3; + copylen = q - r; + envname = (char *)malloc(copylen + 1 + 4); /* reserve space for use later add %$() chars */ + if (envname == NULL) return NULL; + strncpy(envname,r,copylen); + envname[copylen] = '\0'; + + if ((envval = getenv(envname)) == NULL) { + DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname)); + SAFE_FREE(envname); + return str; + } + + /* + * Copy the full %$(NAME) into envname so it + * can be replaced. + */ + + copylen = q + 1 - p; + strncpy(envname,p,copylen); + envname[copylen] = '\0'; + r = realloc_string_sub(str, envname, envval); + SAFE_FREE(envname); + if (r == NULL) return NULL; + return r; +} + +/******************************************************************* Patch from jkf@soton.ac.uk Added this to implement %p (NIS auto-map version of %H) *******************************************************************/ @@ -160,9 +220,11 @@ static char *automount_server(const char *user_name) /**************************************************************************** Do some standard substitutions in a string. + len is the length in bytes of the space allowed in string str. If zero means + don't allow expansions. ****************************************************************************/ -void standard_sub_basic(const char *smb_name, char *str) +void standard_sub_basic(const char *smb_name, char *str,size_t len) { char *p, *s; fstring pidstr; @@ -171,7 +233,10 @@ void standard_sub_basic(const char *smb_name, char *str) for (s=str; (p=strchr_m(s, '%'));s=p) { fstring tmp_str; - int l = sizeof(pstring) - (int)(p-str); + int l = (int)len - (int)(p-str); + + if (l < 0) + l = 0; switch (*(p+1)) { case 'U' : @@ -192,26 +257,43 @@ void standard_sub_basic(const char *smb_name, char *str) strupper(tmp_str); string_sub(p,"%D", tmp_str,l); break; - case 'I' : string_sub(p,"%I", client_addr(),l); break; + case 'I' : + string_sub(p,"%I", client_addr(),l); + break; case 'L' : - if (*local_machine) { + if (*local_machine) string_sub(p,"%L", local_machine,l); - } else { + else string_sub(p,"%L", global_myname,l); - } break; - case 'M' : string_sub(p,"%M", client_name(),l); break; - case 'R' : string_sub(p,"%R", remote_proto,l); break; - case 'T' : string_sub(p,"%T", timestring(False),l); break; - case 'a' : string_sub(p,"%a", remote_arch,l); break; + case 'M' : + string_sub(p,"%M", client_name(),l); + break; + case 'R' : + string_sub(p,"%R", remote_proto,l); + break; + case 'T' : + string_sub(p,"%T", timestring(False),l); + break; + case 'a' : + string_sub(p,"%a", remote_arch,l); + break; case 'd' : slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid()); string_sub(p,"%d", pidstr,l); break; - case 'h' : string_sub(p,"%h", myhostname(),l); break; - case 'm' : string_sub(p,"%m", remote_machine,l); break; - case 'v' : string_sub(p,"%v", VERSION,l); break; - case '$' : p += expand_env_var(p,l); break; /* Expand environment variables */ + case 'h' : + string_sub(p,"%h", myhostname(),l); + break; + case 'm' : + string_sub(p,"%m", remote_machine,l); + break; + case 'v' : + string_sub(p,"%v", VERSION,l); + break; + case '$' : + p += expand_env_var(p,l); + break; /* Expand environment variables */ case '\0': p++; break; /* don't run off the end of the string */ @@ -222,34 +304,34 @@ void standard_sub_basic(const char *smb_name, char *str) } } -/**************************************************************************** - Do some standard substitutions in a string. -****************************************************************************/ - -void standard_sub_advanced(int snum, const char *user, const char *connectpath, gid_t gid, const char *smb_name, char *str) +static void standard_sub_advanced(int snum, const char *user, + const char *connectpath, gid_t gid, + const char *smb_name, char *str, size_t len) { char *p, *s, *home; for (s=str; (p=strchr_m(s, '%'));s=p) { - int l = sizeof(pstring) - (int)(p-str); - + int l = (int)len - (int)(p-str); + + if (l < 0) + l = 0; + switch (*(p+1)) { - case 'N' : string_sub(p,"%N", automount_server(user),l); break; + case 'N' : + string_sub(p,"%N", automount_server(user),l); + break; case 'H': - if ((home = get_user_home_dir(user))) { + if ((home = get_user_home_dir(user))) string_sub(p,"%H",home, l); - } else { + else p += 2; - } break; case 'P': string_sub(p,"%P", connectpath, l); break; - case 'S': string_sub(p,"%S", lp_servicename(snum), l); break; - case 'g': string_sub(p,"%g", gidtoname(gid), l); break; @@ -276,54 +358,293 @@ void standard_sub_advanced(int snum, const char *user, const char *connectpath, } } - standard_sub_basic(smb_name, str); + standard_sub_basic(smb_name, str, len); } /**************************************************************************** Do some standard substitutions in a string. + This function will return an allocated string that have to be freed. ****************************************************************************/ -void standard_sub_conn(connection_struct *conn, char *str) +char *talloc_sub_basic(TALLOC_CTX *mem_ctx, const char *smb_name, const char *str) { - standard_sub_advanced(SNUM(conn), conn->user, conn->connectpath, conn->gid, current_user_info.smb_name, str); + char *a, *t; + a = alloc_sub_basic(smb_name, str); + if (!a) return NULL; + t = talloc_strdup(mem_ctx, a); + SAFE_FREE(a); + return t; +} + +char *alloc_sub_basic(const char *smb_name, const char *str) +{ + char *b, *p, *s, *t, *r, *a_string; + fstring pidstr; + struct passwd *pass; + + a_string = strdup(str); + if (a_string == NULL) { + DEBUG(0, ("alloc_sub_specified: Out of memory!\n")); + return NULL; + } + + for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) { + + r = NULL; + b = t = a_string; + + switch (*(p+1)) { + case 'U' : + r = strdup_lower(smb_name); + if (r == NULL) goto error; + t = realloc_string_sub(t, "%U", r); + break; + case 'G' : + r = strdup(smb_name); + if (r == NULL) goto error; + if ((pass = Get_Pwnam(r))!=NULL) { + t = realloc_string_sub(t, "%G", gidtoname(pass->pw_gid)); + } + break; + case 'D' : + r = strdup_upper(current_user_info.domain); + if (r == NULL) goto error; + t = realloc_string_sub(t, "%D", r); + break; + case 'I' : + t = realloc_string_sub(t, "%I", client_addr()); + break; + case 'L' : + if (*local_machine) + t = realloc_string_sub(t, "%L", local_machine); + else + t = realloc_string_sub(t, "%L", global_myname); + break; + case 'M' : + t = realloc_string_sub(t, "%M", client_name()); + break; + case 'R' : + t = realloc_string_sub(t, "%R", remote_proto); + break; + case 'T' : + t = realloc_string_sub(t, "%T", timestring(False)); + break; + case 'a' : + t = realloc_string_sub(t, "%a", remote_arch); + break; + case 'd' : + slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid()); + t = realloc_string_sub(t, "%d", pidstr); + break; + case 'h' : + t = realloc_string_sub(t, "%h", myhostname()); + break; + case 'm' : + t = realloc_string_sub(t, "%m", remote_machine); + break; + case 'v' : + t = realloc_string_sub(t, "%v", VERSION); + break; + case '$' : + t = realloc_expand_env_var(t, p); /* Expand environment variables */ + break; + + default: + break; + } + + p++; + SAFE_FREE(r); + if (t == NULL) goto error; + a_string = t; + } + + return a_string; +error: + SAFE_FREE(a_string); + return NULL; } /**************************************************************************** - Like standard_sub but for a homes share where snum still points to the [homes] - share. No user specific snum created yet so servicename should be the username. + Do some specific substitutions in a string. + This function will return an allocated string that have to be freed. ****************************************************************************/ -void standard_sub_home(int snum, const char *user, char *str) +char *talloc_sub_specified(TALLOC_CTX *mem_ctx, + const char *input_string, + const char *username, + const char *domain, + uid_t uid, + gid_t gid) { - char *p, *s; + char *a, *t; + a = alloc_sub_specified(input_string, username, domain, uid, gid); + if (!a) return NULL; + t = talloc_strdup(mem_ctx, a); + SAFE_FREE(a); + return t; +} - for (s=str; (p=strchr_m(s, '%'));s=p) { - int l = sizeof(pstring) - (int)(p-str); +char *alloc_sub_specified(const char *input_string, + const char *username, + const char *domain, + uid_t uid, + gid_t gid) +{ + char *a_string, *ret_string; + char *b, *p, *s, *t; + + a_string = strdup(input_string); + if (a_string == NULL) { + DEBUG(0, ("alloc_sub_specified: Out of memory!\n")); + return NULL; + } + + for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) { + + b = t = a_string; + + switch (*(p+1)) { + case 'U' : + t = realloc_string_sub(t, "%U", username); + break; + case 'u' : + t = realloc_string_sub(t, "%u", username); + break; + case 'G' : + if (gid != -1) { + t = realloc_string_sub(t, "%G", gidtoname(gid)); + } else { + t = realloc_string_sub(t, "%G", "NO_GROUP"); + } + break; + case 'g' : + if (gid != -1) { + t = realloc_string_sub(t, "%g", gidtoname(gid)); + } else { + t = realloc_string_sub(t, "%g", "NO_GROUP"); + } + break; + case 'D' : + t = realloc_string_sub(t, "%D", domain); + break; + case 'N' : + t = realloc_string_sub(t, "%N", automount_server(username)); + break; + default: + break; + } + + p++; + if (t == NULL) { + SAFE_FREE(a_string); + return NULL; + } + a_string = t; + } + + ret_string = alloc_sub_basic(username, a_string); + SAFE_FREE(a_string); + return ret_string; +} + +char *talloc_sub_advanced(TALLOC_CTX *mem_ctx, + int snum, + const char *user, + const char *connectpath, + gid_t gid, + const char *smb_name, + char *str) +{ + char *a, *t; + a = alloc_sub_advanced(snum, user, connectpath, gid, smb_name, str); + if (!a) return NULL; + t = talloc_strdup(mem_ctx, a); + SAFE_FREE(a); + return t; +} + +char *alloc_sub_advanced(int snum, const char *user, + const char *connectpath, gid_t gid, + const char *smb_name, char *str) +{ + char *a_string, *ret_string; + char *b, *p, *s, *t, *h; + + a_string = strdup(str); + if (a_string == NULL) { + DEBUG(0, ("alloc_sub_specified: Out of memory!\n")); + return NULL; + } + + for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) { + + b = t = a_string; switch (*(p+1)) { + case 'N' : + t = realloc_string_sub(t, "%N", automount_server(user)); + break; + case 'H': + if ((h = get_user_home_dir(user))) + t = realloc_string_sub(t, "%H", h); + break; + case 'P': + t = realloc_string_sub(t, "%P", connectpath); + break; case 'S': - string_sub(p,"%S", user, l); + t = realloc_string_sub(t, "%S", lp_servicename(snum)); + break; + case 'g': + t = realloc_string_sub(t, "%g", gidtoname(gid)); + break; + case 'u': + t = realloc_string_sub(t, "%u", user); break; + + /* Patch from jkf@soton.ac.uk Left the %N (NIS + * server name) in standard_sub_basic as it is + * a feature for logon servers, hence uses the + * username. The %p (NIS server path) code is + * here as it is used instead of the default + * "path =" string in [homes] and so needs the + * service name, not the username. */ case 'p': - string_sub(p,"%p", automount_path(user), l); + t = realloc_string_sub(t, "%p", automount_path(lp_servicename(snum))); break; - case '\0': - p++; - break; /* don't run off the end of the string */ - default: p+=2; + default: break; } + + p++; + if (t == NULL) { + SAFE_FREE(a_string); + return NULL; + } + a_string = t; } - standard_sub_advanced(snum, user, "", -1, current_user_info.smb_name, str); + ret_string = alloc_sub_basic(smb_name, a_string); + SAFE_FREE(a_string); + return ret_string; +} + +/**************************************************************************** + Do some standard substitutions in a string. +****************************************************************************/ + +void standard_sub_conn(connection_struct *conn, char *str, size_t len) +{ + standard_sub_advanced(SNUM(conn), conn->user, conn->connectpath, + conn->gid, current_user_info.smb_name, str, len); } /**************************************************************************** Like standard_sub but by snum. ****************************************************************************/ -void standard_sub_snum(int snum, char *str) +void standard_sub_snum(int snum, char *str, size_t len) { extern struct current_user current_user; static uid_t cached_uid = -1; @@ -336,23 +657,6 @@ void standard_sub_snum(int snum, char *str) cached_uid = current_user.uid; } - standard_sub_advanced(snum, cached_user, "", -1, current_user_info.smb_name, str); -} - -/******************************************************************* - Substitute strings with useful parameters. -********************************************************************/ - -void standard_sub_vuser(char *str, user_struct *vuser) -{ - standard_sub_advanced(-1, vuser->user.unix_name, "", -1, current_user_info.smb_name, str); -} - -/******************************************************************* - Substitute strings with useful parameters. -********************************************************************/ - -void standard_sub_vsnum(char *str, user_struct *vuser, int snum) -{ - standard_sub_advanced(snum, vuser->user.unix_name, "", -1, current_user_info.smb_name, str); + standard_sub_advanced(snum, cached_user, "", -1, + current_user_info.smb_name, str, len); } diff --git a/source3/lib/sysacls.c b/source3/lib/sysacls.c index 22245992f5..00d06e4a5a 100644 --- a/source3/lib/sysacls.c +++ b/source3/lib/sysacls.c @@ -644,13 +644,7 @@ char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p) break; case SMB_ACL_USER: - if ((pw = sys_getpwuid(ap->a_id)) == NULL) { - slprintf(idbuf, sizeof(idbuf)-1, "%ld", - (long)ap->a_id); - id = idbuf; - } else { - id = pw->pw_name; - } + id = uidtoname(ap->a_id); case SMB_ACL_USER_OBJ: tag = "user"; break; @@ -1281,13 +1275,7 @@ char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p) break; case SMB_ACL_USER: - if ((pw = sys_getpwuid(ap->a_id)) == NULL) { - slprintf(idbuf, sizeof(idbuf)-1, "%ld", - (long)ap->a_id); - id = idbuf; - } else { - id = pw->pw_name; - } + id = uidtoname(ap->a_id); case SMB_ACL_USER_OBJ: tag = "user"; break; diff --git a/source3/lib/system.c b/source3/lib/system.c index 8c7eec939e..8b2ba800f5 100644 --- a/source3/lib/system.c +++ b/source3/lib/system.c @@ -73,6 +73,104 @@ int sys_usleep(long usecs) } /******************************************************************* +A read wrapper that will deal with EINTR. +********************************************************************/ + +ssize_t sys_read(int fd, void *buf, size_t count) +{ + ssize_t ret; + + do { + ret = read(fd, buf, count); + } while (ret == -1 && errno == EINTR); + return ret; +} + +/******************************************************************* +A write wrapper that will deal with EINTR. +********************************************************************/ + +ssize_t sys_write(int fd, const void *buf, size_t count) +{ + ssize_t ret; + + do { + ret = write(fd, buf, count); + } while (ret == -1 && errno == EINTR); + return ret; +} + +/******************************************************************* +A send wrapper that will deal with EINTR. +********************************************************************/ + +ssize_t sys_send(int s, const void *msg, size_t len, int flags) +{ + ssize_t ret; + + do { + ret = send(s, msg, len, flags); + } while (ret == -1 && errno == EINTR); + return ret; +} + +/******************************************************************* +A sendto wrapper that will deal with EINTR. +********************************************************************/ + +ssize_t sys_sendto(int s, const void *msg, size_t len, int flags, const struct sockaddr *to, socklen_t tolen) +{ + ssize_t ret; + + do { + ret = sendto(s, msg, len, flags, to, tolen); + } while (ret == -1 && errno == EINTR); + return ret; +} + +/******************************************************************* +A recvfrom wrapper that will deal with EINTR. +********************************************************************/ + +ssize_t sys_recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen) +{ + ssize_t ret; + + do { + ret = recvfrom(s, buf, len, flags, from, fromlen); + } while (ret == -1 && errno == EINTR); + return ret; +} + +/******************************************************************* +A fcntl wrapper that will deal with EINTR. +********************************************************************/ + +int sys_fcntl_ptr(int fd, int cmd, void *arg) +{ + int ret; + + do { + ret = fcntl(fd, cmd, arg); + } while (ret == -1 && errno == EINTR); + return ret; +} + +/******************************************************************* +A fcntl wrapper that will deal with EINTR. +********************************************************************/ + +int sys_fcntl_long(int fd, int cmd, long arg) +{ + int ret; + + do { + ret = fcntl(fd, cmd, arg); + } while (ret == -1 && errno == EINTR); + return ret; +} + +/******************************************************************* A stat() wrapper that will deal with 64 bit filesizes. ********************************************************************/ @@ -646,131 +744,47 @@ int sys_setgroups(int setlen, gid_t *gidset) #endif /* HAVE_SETGROUPS */ -/* - * We only wrap pw_name and pw_passwd for now as these - * are the only potentially modified fields. - */ - -/************************************************************************** - Helper function for getpwnam/getpwuid wrappers. -****************************************************************************/ - -struct saved_pw { - fstring pw_name; - fstring pw_passwd; - fstring pw_gecos; - pstring pw_dir; - pstring pw_shell; - struct passwd pass; -}; - -static struct saved_pw pw_mod; /* This is the structure returned - can be modified. */ -static struct saved_pw pw_cache; /* This is the structure saved - used to check cache. */ - -static int num_lookups; /* Counter so we don't always use cache. */ -#ifndef PW_RET_CACHE_MAX_LOOKUPS -#define PW_RET_CACHE_MAX_LOOKUPS 100 -#endif - -static void copy_pwent(struct saved_pw *dst, struct passwd *pass) -{ - memcpy((char *)&dst->pass, pass, sizeof(struct passwd)); - - fstrcpy(dst->pw_name, pass->pw_name); - dst->pass.pw_name = dst->pw_name; - - fstrcpy(dst->pw_passwd, pass->pw_passwd); - dst->pass.pw_passwd = dst->pw_passwd; - - fstrcpy(dst->pw_gecos, pass->pw_gecos); - dst->pass.pw_gecos = dst->pw_gecos; - - pstrcpy(dst->pw_dir, pass->pw_dir); - dst->pass.pw_dir = dst->pw_dir; - - pstrcpy(dst->pw_shell, pass->pw_shell); - dst->pass.pw_shell = dst->pw_shell; -} - -static struct passwd *setup_pwret(struct passwd *pass) -{ - if (pass == NULL) { - /* Clear the caches. */ - memset(&pw_cache, '\0', sizeof(struct saved_pw)); - memset(&pw_mod, '\0', sizeof(struct saved_pw)); - num_lookups = 0; - return NULL; - } - - copy_pwent( &pw_mod, pass); - - if (pass != &pw_cache.pass) { - - /* If it's a cache miss we must also refill the cache. */ - - copy_pwent( &pw_cache, pass); - num_lookups = 1; - - } else { - - /* Cache hit. */ - - num_lookups++; - num_lookups = (num_lookups % PW_RET_CACHE_MAX_LOOKUPS); - } - - return &pw_mod.pass; -} - /************************************************************************** Wrappers for setpwent(), getpwent() and endpwent() ****************************************************************************/ void sys_setpwent(void) { - setup_pwret(NULL); /* Clear cache. */ setpwent(); } struct passwd *sys_getpwent(void) { - return setup_pwret(getpwent()); + return getpwent(); } void sys_endpwent(void) { - setup_pwret(NULL); /* Clear cache. */ endpwent(); } /************************************************************************** - Wrapper for getpwnam(). Always returns a static that can be modified. + Wrappers for getpwnam(), getpwuid(), getgrnam(), getgrgid() ****************************************************************************/ struct passwd *sys_getpwnam(const char *name) { - if (!name || !name[0]) - return NULL; - - /* check for a cache hit first */ - if (num_lookups && pw_cache.pass.pw_name && !strcmp(name, pw_cache.pass.pw_name)) { - return setup_pwret(&pw_cache.pass); - } + return getpwnam(name); +} - return setup_pwret(getpwnam(name)); +struct passwd *sys_getpwuid(uid_t uid) +{ + return getpwuid(uid); } -/************************************************************************** - Wrapper for getpwuid(). Always returns a static that can be modified. -****************************************************************************/ +struct group *sys_getgrnam(const char *name) +{ + return getgrnam(name); +} -struct passwd *sys_getpwuid(uid_t uid) +struct group *sys_getgrgid(gid_t gid) { - if (num_lookups && pw_cache.pass.pw_name && (uid == pw_cache.pass.pw_uid)) { - return setup_pwret(&pw_cache.pass); - } - - return setup_pwret(getpwuid(uid)); + return getgrgid(gid); } #if 0 /* NOT CURRENTLY USED - JRA */ @@ -1171,7 +1185,7 @@ int sys_pclose(int fd) void *sys_dlopen(const char *name, int flags) { -#if defined(HAVE_LIBDL) || defined(HAVE_DLOPEN) +#if defined(HAVE_DLOPEN) return dlopen(name, flags); #else return NULL; @@ -1180,7 +1194,7 @@ void *sys_dlopen(const char *name, int flags) void *sys_dlsym(void *handle, char *symbol) { -#if defined(HAVE_LIBDL) || defined(HAVE_DLSYM) +#if defined(HAVE_DLSYM) return dlsym(handle, symbol); #else return NULL; @@ -1189,7 +1203,7 @@ void *sys_dlsym(void *handle, char *symbol) int sys_dlclose (void *handle) { -#if defined(HAVE_LIBDL) || defined(HAVE_DLCLOSE) +#if defined(HAVE_DLCLOSE) return dlclose(handle); #else return 0; @@ -1198,7 +1212,7 @@ int sys_dlclose (void *handle) const char *sys_dlerror(void) { -#if defined(HAVE_LIBDL) || defined(HAVE_DLERROR) +#if defined(HAVE_DLERROR) return dlerror(); #else return NULL; diff --git a/source3/lib/talloc.c b/source3/lib/talloc.c index 6ac784a929..0f293e1725 100644 --- a/source3/lib/talloc.c +++ b/source3/lib/talloc.c @@ -82,7 +82,7 @@ struct talloc_ctx { * @todo We should turn the global list off when using Insure++, * otherwise all the memory will be seen as still reachable. **/ -TALLOC_CTX *list_head = NULL; +static TALLOC_CTX *list_head = NULL; /** @@ -287,6 +287,15 @@ char *talloc_strdup(TALLOC_CTX *t, const char *p) return NULL; } +/** strdup_w with a talloc */ +smb_ucs2_t *talloc_strdup_w(TALLOC_CTX *t, const smb_ucs2_t *p) +{ + if (p) + return talloc_memdup(t, p, (strlen_w(p) + 1) * sizeof(smb_ucs2_t)); + else + return NULL; +} + /** * Perform string formatting, and return a pointer to newly allocated * memory holding the result, inside a memory pool. @@ -307,12 +316,17 @@ char *talloc_strdup(TALLOC_CTX *t, const char *p) { int len; char *ret; + va_list ap2; - len = vsnprintf(NULL, 0, fmt, ap); + VA_COPY(ap2, ap); + + len = vsnprintf(NULL, 0, fmt, ap2); ret = talloc(t, len+1); - if (ret) - vsnprintf(ret, len+1, fmt, ap); + if (ret) { + VA_COPY(ap2, ap); + vsnprintf(ret, len+1, fmt, ap2); + } return ret; } @@ -345,14 +359,19 @@ char *talloc_strdup(TALLOC_CTX *t, const char *p) const char *fmt, va_list ap) { int len, s_len; + va_list ap2; + + VA_COPY(ap2, ap); s_len = strlen(s); - len = vsnprintf(NULL, 0, fmt, ap); + len = vsnprintf(NULL, 0, fmt, ap2); s = talloc_realloc(t, s, s_len + len+1); if (!s) return NULL; - vsnprintf(s+s_len, len+1, fmt, ap); + VA_COPY(ap2, ap); + + vsnprintf(s+s_len, len+1, fmt, ap2); return s; } diff --git a/source3/lib/time.c b/source3/lib/time.c index 5fc43612dd..9d87414aea 100644 --- a/source3/lib/time.c +++ b/source3/lib/time.c @@ -44,11 +44,6 @@ int extra_time_offset = 0; External access to time_t_min and time_t_max. ********************************************************************/ -time_t get_time_t_min(void) -{ - return TIME_T_MIN; -} - time_t get_time_t_max(void) { return TIME_T_MAX; @@ -413,7 +408,7 @@ void unix_to_nt_time(NTTIME *nt, time_t t) } /* this converts GMT to kludge-GMT */ - t -= LocTimeDiff(t) - get_serverzone(); + t -= TimeDiff(t) - get_serverzone(); d = (double)(t); d += TIME_FIXUP_CONSTANT; diff --git a/source3/lib/username.c b/source3/lib/username.c index da603949bc..4813c8fd19 100644 --- a/source3/lib/username.c +++ b/source3/lib/username.c @@ -30,7 +30,7 @@ static struct passwd *uname_string_combinations2(char *s, int offset, struct pas *local* people, there's nothing for you here...). *****************************************************************/ -BOOL name_is_local(const char *name) +static BOOL name_is_local(const char *name) { return !(strchr_m(name, *lp_winbind_separator())); } @@ -55,9 +55,10 @@ BOOL split_domain_and_name(const char *name, char *domain, char* username) } else if (lp_winbind_use_default_domain()) { fstrcpy(username, name); fstrcpy(domain, lp_workgroup()); - } else + } else { return False; - + } + DEBUG(10,("split_domain_and_name: all is fine, domain is |%s| and name is |%s|\n", domain, username)); return True; } @@ -81,40 +82,6 @@ char *get_user_home_dir(const char *user) return(pass->pw_dir); } -/**************************************************************************** - Get a users service home directory. -****************************************************************************/ - -char *get_user_service_home_dir(const char *user) -{ - static struct passwd *pass; - int snum; - - /* Ensure the user exists. */ - - pass = Get_Pwnam(user); - - if (!pass) - return(NULL); - - /* If a path is specified in [homes] then use it instead of the - user's home directory from struct passwd. */ - - if ((snum = lp_servicenumber(HOMES_NAME)) != -1) { - static pstring home_dir; - - pstrcpy(home_dir, lp_pathname(snum)); - standard_sub_home(snum, user, home_dir); - - if (home_dir[0]) - return home_dir; - } - - /* Return home directory from struct passwd. */ - - return(pass->pw_dir); -} - /******************************************************************* Map a username from a dos name to a unix name by looking in the username map. Note that this modifies the name in place. @@ -196,7 +163,7 @@ BOOL map_username(char *user) } } - dosuserlist = lp_list_make(dosname); + dosuserlist = str_list_make(dosname); if (!dosuserlist) { DEBUG(0,("Unable to build user list\n")); return False; @@ -209,13 +176,13 @@ BOOL map_username(char *user) sscanf(unixname,"%s",user); fstrcpy(last_to,user); if(return_if_mapped) { - lp_list_free (&dosuserlist); + str_list_free (&dosuserlist); x_fclose(f); return True; } } - lp_list_free (&dosuserlist); + str_list_free (&dosuserlist); } x_fclose(f); @@ -238,6 +205,8 @@ BOOL map_username(char *user) * - using lp_usernamelevel() for permutations. ****************************************************************************/ +static struct passwd *Get_Pwnam_ret = NULL; + static struct passwd *Get_Pwnam_internals(const char *user, char *user2) { struct passwd *ret = NULL; @@ -252,14 +221,14 @@ static struct passwd *Get_Pwnam_internals(const char *user, char *user2) common case on UNIX systems */ strlower(user2); DEBUG(5,("Trying _Get_Pwnam(), username as lowercase is %s\n",user2)); - ret = sys_getpwnam(user2); + ret = getpwnam_alloc(user2); if(ret) goto done; /* Try as given, if username wasn't originally lowercase */ if(strcmp(user, user2) != 0) { DEBUG(5,("Trying _Get_Pwnam(), username as given is %s\n", user)); - ret = sys_getpwnam(user); + ret = getpwnam_alloc(user); if(ret) goto done; } @@ -268,7 +237,7 @@ static struct passwd *Get_Pwnam_internals(const char *user, char *user2) strupper(user2); if(strcmp(user, user2) != 0) { DEBUG(5,("Trying _Get_Pwnam(), username as uppercase is %s\n", user2)); - ret = sys_getpwnam(user2); + ret = getpwnam_alloc(user2); if(ret) goto done; } @@ -276,10 +245,31 @@ static struct passwd *Get_Pwnam_internals(const char *user, char *user2) /* Try all combinations up to usernamelevel */ strlower(user2); DEBUG(5,("Checking combinations of %d uppercase letters in %s\n", lp_usernamelevel(), user2)); - ret = uname_string_combinations(user2, sys_getpwnam, lp_usernamelevel()); + ret = uname_string_combinations(user2, getpwnam_alloc, lp_usernamelevel()); done: DEBUG(5,("Get_Pwnam_internals %s find user [%s]!\n",ret ? "did":"didn't", user)); + + /* This call used to just return the 'passwd' static buffer. + This could then have accidental reuse implications, so + we now malloc a copy, and free it in the next use. + + This should cause the (ab)user to segfault if it + uses an old struct. + + This is better than useing the wrong data in security + critical operations. + + The real fix is to make the callers free the returned + malloc'ed data. + */ + + if (Get_Pwnam_ret) { + passwd_free(&Get_Pwnam_ret); + } + + Get_Pwnam_ret = ret; + return ret; } @@ -288,7 +278,7 @@ done: NOTE: This can potentially modify 'user'! ****************************************************************************/ -struct passwd *Get_Pwnam_Modify(char *user) +struct passwd *Get_Pwnam_Modify(fstring user) { fstring user2; struct passwd *ret; @@ -320,8 +310,6 @@ struct passwd *Get_Pwnam(const char *user) ret = Get_Pwnam_internals(user, user2); - DEBUG(5,("Get_Pwnam %s find user [%s]!\n",ret ? "did":"didn't", user)); - return ret; } diff --git a/source3/lib/util.c b/source3/lib/util.c index 7e2ad49639..be108aa405 100644 --- a/source3/lib/util.c +++ b/source3/lib/util.c @@ -52,13 +52,6 @@ #endif /* WITH_NISPLUS_HOME */ #endif /* HAVE_NETGROUP && WITH_AUTOMOUNT */ -#ifdef WITH_SSL -#include <openssl/ssl.h> -#undef Realloc /* SSLeay defines this and samba has a function of this name */ -extern SSL *ssl; -extern int sslFd; -#endif /* WITH_SSL */ - int Protocol = PROTOCOL_COREPLUS; /* a default finfo structure to ensure all fields are sensible */ @@ -91,9 +84,10 @@ char **my_netbios_names; /**************************************************************************** - find a suitable temporary directory. The result should be copied immediately + Find a suitable temporary directory. The result should be copied immediately as it may be overwritten by a subsequent call. - ****************************************************************************/ +****************************************************************************/ + char *tmpdir(void) { char *p; @@ -124,7 +118,7 @@ BOOL in_group(gid_t group, gid_t current_gid, int ngroups, gid_t *groups) Like atoi but gets the value up to the separator character. ****************************************************************************/ -char *Atoic(char *p, int *n, char *c) +static char *Atoic(char *p, int *n, char *c) { if (!isdigit((int)*p)) { DEBUG(5, ("Atoic: malformed number\n")); @@ -190,7 +184,7 @@ BOOL file_exist(const char *fname,SMB_STRUCT_STAT *sbuf) if (sys_stat(fname,sbuf) != 0) return(False); - return(S_ISREG(sbuf->st_mode)); + return((S_ISREG(sbuf->st_mode)) || (S_ISFIFO(sbuf->st_mode))); } /******************************************************************* @@ -552,13 +546,13 @@ int set_blocking(int fd, BOOL set) #endif #endif - if((val = fcntl(fd, F_GETFL, 0)) == -1) + if((val = sys_fcntl_long(fd, F_GETFL, 0)) == -1) return -1; if(set) /* Turn blocking on - ie. clear nonblock flag */ val &= ~FLAG_TO_SET; else val |= FLAG_TO_SET; - return fcntl( fd, F_SETFL, val); + return sys_fcntl_long( fd, F_SETFL, val); #undef FLAG_TO_SET } @@ -620,7 +614,7 @@ ssize_t transfer_file_internal(int infd, int outfd, size_t n, ssize_t (*read_fn) SMB_OFF_T transfer_file(int infd,int outfd,SMB_OFF_T n) { - return (SMB_OFF_T)transfer_file_internal(infd, outfd, (size_t)n, read, write); + return (SMB_OFF_T)transfer_file_internal(infd, outfd, (size_t)n, sys_read, sys_write); } /******************************************************************* @@ -689,7 +683,7 @@ void become_daemon(void) /**************************************************************************** -put up a yes/no prompt + Put up a yes/no prompt ****************************************************************************/ BOOL yesno(char *p) { @@ -862,7 +856,7 @@ struct in_addr *interpret_addr2(const char *str) } /******************************************************************* - check if an IP is the 0.0.0.0 + Check if an IP is the 0.0.0.0 ******************************************************************/ BOOL is_zero_ip(struct in_addr ip) { @@ -871,7 +865,9 @@ BOOL is_zero_ip(struct in_addr ip) return(a == 0); } -/* Set an IP to 0.0.0.0 */ +/******************************************************************* + Set an IP to 0.0.0.0 + ******************************************************************/ void zero_ip(struct in_addr *ip) { @@ -1056,15 +1052,19 @@ BOOL process_exists(pid_t pid) Convert a uid into a user name. ********************************************************************/ -char *uidtoname(uid_t uid) +const char *uidtoname(uid_t uid) { static fstring name; struct passwd *pass; - pass = sys_getpwuid(uid); - if (pass) return(pass->pw_name); - slprintf(name, sizeof(name) - 1, "%d",(int)uid); - return(name); + pass = getpwuid_alloc(uid); + if (pass) { + fstrcpy(name, pass->pw_name); + passwd_free(&pass); + } else { + slprintf(name, sizeof(name) - 1, "%ld",(long int)uid); + } + return name; } @@ -1078,13 +1078,14 @@ char *gidtoname(gid_t gid) struct group *grp; grp = getgrgid(gid); - if (grp) return(grp->gr_name); + if (grp) + return(grp->gr_name); slprintf(name,sizeof(name) - 1, "%d",(int)gid); return(name); } /******************************************************************* - Convert a user name into a uid. If winbindd is present uses this. + Convert a user name into a uid. ********************************************************************/ uid_t nametouid(char *name) @@ -1093,21 +1094,22 @@ uid_t nametouid(char *name) char *p; uid_t u; - u = (uid_t)strtol(name, &p, 0); - if ((p != name) && (*p == '\0')) - return u; - pass = getpwnam_alloc(name); if (pass) { - return(pass->pw_uid); + u = pass->pw_uid; passwd_free(&pass); + return u; } + + u = (uid_t)strtol(name, &p, 0); + if ((p != name) && (*p == '\0')) + return u; + return (uid_t)-1; } /******************************************************************* - Convert a name to a gid_t if possible. Return -1 if not a group. If winbindd - is present does a shortcut lookup... + Convert a name to a gid_t if possible. Return -1 if not a group. ********************************************************************/ gid_t nametogid(const char *name) @@ -1120,7 +1122,7 @@ gid_t nametogid(const char *name) if ((p != name) && (*p == '\0')) return g; - grp = getgrnam(name); + grp = sys_getgrnam(name); if (grp) return(grp->gr_gid); return (gid_t)-1; @@ -1353,11 +1355,9 @@ BOOL fcntl_lock(int fd, int op, SMB_OFF_T offset, SMB_OFF_T count, int type) lock.l_len = count; lock.l_pid = 0; - errno = 0; - - ret = fcntl(fd,op,&lock); + ret = sys_fcntl_ptr(fd,op,&lock); - if (errno != 0) + if (ret == -1 && errno != 0) DEBUG(3,("fcntl_lock: fcntl lock gave errno %d (%s)\n",errno,strerror(errno))); /* a lock query */ @@ -1391,20 +1391,39 @@ BOOL fcntl_lock(int fd, int op, SMB_OFF_T offset, SMB_OFF_T count, int type) } /******************************************************************* -is the name specified one of my netbios names -returns true is it is equal, false otherwise + Is the name specified one of my netbios names. + Returns true if it is equal, false otherwise. ********************************************************************/ + BOOL is_myname(char *s) { - int n; - BOOL ret = False; + int n; + BOOL ret = False; - for (n=0; my_netbios_names[n]; n++) { - if (strequal(my_netbios_names[n], s)) - ret=True; - } - DEBUG(8, ("is_myname(\"%s\") returns %d\n", s, ret)); - return(ret); + for (n=0; my_netbios_names[n]; n++) { + if (strequal(my_netbios_names[n], s)) + ret=True; + } + DEBUG(8, ("is_myname(\"%s\") returns %d\n", s, ret)); + return(ret); +} + +/******************************************************************** + Return only the first IP address of our configured interfaces + as a string + *******************************************************************/ + +const char* get_my_primary_ip (void) +{ + static fstring ip_string; + int n; + struct iface_struct nics[MAX_INTERFACES]; + + if ((n=get_interfaces(nics, MAX_INTERFACES)) <= 0) + return NULL; + + fstrcpy(ip_string, inet_ntoa(nics[0].ip)); + return ip_string; } BOOL is_myname_or_ipaddr(char *s) @@ -1416,8 +1435,7 @@ BOOL is_myname_or_ipaddr(char *s) return True; /* maybe its an IP address? */ - if (is_ipaddress(s)) - { + if (is_ipaddress(s)) { struct iface_struct nics[MAX_INTERFACES]; int i, n; uint32 ip; @@ -1435,59 +1453,56 @@ BOOL is_myname_or_ipaddr(char *s) /* check for an alias */ ptr = lp_netbios_aliases(); - for ( ; *ptr; ptr++ ) - { + for ( ; *ptr; ptr++ ) { if (StrCaseCmp(s, *ptr) == 0) return True; } - /* no match */ return False; - } - /******************************************************************* -set the horrid remote_arch string based on an enum. + Set the horrid remote_arch string based on an enum. ********************************************************************/ + void set_remote_arch(enum remote_arch_types type) { - extern fstring remote_arch; - ra_type = type; - switch( type ) - { - case RA_WFWG: - fstrcpy(remote_arch, "WfWg"); - return; - case RA_OS2: - fstrcpy(remote_arch, "OS2"); - return; - case RA_WIN95: - fstrcpy(remote_arch, "Win95"); - return; - case RA_WINNT: - fstrcpy(remote_arch, "WinNT"); - return; - case RA_WIN2K: - fstrcpy(remote_arch, "Win2K"); - return; - case RA_SAMBA: - fstrcpy(remote_arch,"Samba"); - return; - default: - ra_type = RA_UNKNOWN; - fstrcpy(remote_arch, "UNKNOWN"); - break; - } + extern fstring remote_arch; + ra_type = type; + switch( type ) { + case RA_WFWG: + fstrcpy(remote_arch, "WfWg"); + return; + case RA_OS2: + fstrcpy(remote_arch, "OS2"); + return; + case RA_WIN95: + fstrcpy(remote_arch, "Win95"); + return; + case RA_WINNT: + fstrcpy(remote_arch, "WinNT"); + return; + case RA_WIN2K: + fstrcpy(remote_arch, "Win2K"); + return; + case RA_SAMBA: + fstrcpy(remote_arch,"Samba"); + return; + default: + ra_type = RA_UNKNOWN; + fstrcpy(remote_arch, "UNKNOWN"); + break; + } } /******************************************************************* Get the remote_arch type. ********************************************************************/ + enum remote_arch_types get_remote_arch(void) { - return ra_type; + return ra_type; } @@ -1495,42 +1510,35 @@ void out_ascii(FILE *f, unsigned char *buf,int len) { int i; for (i=0;i<len;i++) - { fprintf(f, "%c", isprint(buf[i])?buf[i]:'.'); - } } void out_data(FILE *f,char *buf1,int len, int per_line) { unsigned char *buf = (unsigned char *)buf1; int i=0; - if (len<=0) - { + if (len<=0) { return; } fprintf(f, "[%03X] ",i); - for (i=0;i<len;) - { + for (i=0;i<len;) { fprintf(f, "%02X ",(int)buf[i]); i++; if (i%(per_line/2) == 0) fprintf(f, " "); - if (i%per_line == 0) - { + if (i%per_line == 0) { out_ascii(f,&buf[i-per_line ],per_line/2); fprintf(f, " "); out_ascii(f,&buf[i-per_line/2],per_line/2); fprintf(f, "\n"); if (i<len) fprintf(f, "[%03X] ",i); } } - if ((i%per_line) != 0) - { + if ((i%per_line) != 0) { int n; n = per_line - (i%per_line); fprintf(f, " "); if (n>(per_line/2)) fprintf(f, " "); - while (n--) - { + while (n--) { fprintf(f, " "); } n = MIN(per_line/2,i%per_line); @@ -1769,10 +1777,10 @@ int smb_mkstemp(char *template) #endif } - -/** +/***************************************************************** malloc that aborts with smb_panic on fail or zero size. -**/ + *****************************************************************/ + void *smb_xmalloc(size_t size) { void *p; @@ -1811,7 +1819,11 @@ char *smb_xstrdup(const char *s) int smb_xvasprintf(char **ptr, const char *format, va_list ap) { int n; - n = vasprintf(ptr, format, ap); + va_list ap2; + + VA_COPY(ap2, ap); + + n = vasprintf(ptr, format, ap2); if (n == -1 || ! *ptr) { smb_panic("smb_xvasprintf: out of memory"); } @@ -1847,7 +1859,7 @@ char *myhostname(void) /***************************************************************** a useful function for returning a path in the Samba lock directory *****************************************************************/ -char *lock_path(char *name) +char *lock_path(const char *name) { static pstring fname; @@ -1864,6 +1876,26 @@ char *lock_path(char *name) return fname; } +/***************************************************************** +a useful function for returning a path in the Samba pid directory + *****************************************************************/ +char *pid_path(const char *name) +{ + static pstring fname; + + pstrcpy(fname,lp_piddir()); + trim_string(fname,"","/"); + + if (!directory_exist(fname,NULL)) { + mkdir(fname,0755); + } + + pstrcat(fname,"/"); + pstrcat(fname,name); + + return fname; +} + /** * @brief Returns an absolute path to a file in the Samba lib directory. @@ -1872,7 +1904,7 @@ char *lock_path(char *name) * * @retval Pointer to a static #pstring containing the full path. **/ -char *lib_path(char *name) +char *lib_path(const char *name) { static pstring fname; snprintf(fname, sizeof(fname), "%s/%s", dyn_LIBDIR, name); @@ -2092,92 +2124,6 @@ BOOL unix_wild_match(char *pattern, char *string) return unix_do_match(p2, s2) == 0; } -/******************************************************************* - free() a data blob -*******************************************************************/ -static void free_data_blob(DATA_BLOB *d) -{ - if ((d) && (d->free)) { - SAFE_FREE(d->data); - } -} - -/******************************************************************* - construct a data blob, must be freed with data_blob_free() - you can pass NULL for p and get a blank data blob -*******************************************************************/ -DATA_BLOB data_blob(const void *p, size_t length) -{ - DATA_BLOB ret; - - if (!length) { - ZERO_STRUCT(ret); - return ret; - } - - if (p) { - ret.data = smb_xmemdup(p, length); - } else { - ret.data = smb_xmalloc(length); - } - ret.length = length; - ret.free = free_data_blob; - return ret; -} - -/******************************************************************* - construct a data blob, using supplied TALLOC_CTX -*******************************************************************/ -DATA_BLOB data_blob_talloc(TALLOC_CTX *mem_ctx, const void *p, size_t length) -{ - DATA_BLOB ret; - - if (!p || !length) { - ZERO_STRUCT(ret); - return ret; - } - - ret.data = talloc_memdup(mem_ctx, p, length); - if (ret.data == NULL) - smb_panic("data_blob_talloc: talloc_memdup failed.\n"); - - ret.length = length; - ret.free = NULL; - return ret; -} - -/******************************************************************* -free a data blob -*******************************************************************/ -void data_blob_free(DATA_BLOB *d) -{ - if (d) { - if (d->free) { - (d->free)(d); - } - ZERO_STRUCTP(d); - } -} - -/******************************************************************* -clear a DATA_BLOB's contents -*******************************************************************/ -void data_blob_clear(DATA_BLOB *d) -{ - if (d->data) { - memset(d->data, 0, d->length); - } -} - -/******************************************************************* -free a data blob and clear its contents -*******************************************************************/ -void data_blob_clear_free(DATA_BLOB *d) -{ - data_blob_clear(d); - data_blob_free(d); -} - #ifdef __INSURE__ /******************************************************************* diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index e80267f84b..611e0e40be 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -40,9 +40,10 @@ BOOL do_file_lock(int fd, int waitsecs, int type) { SMB_STRUCT_FLOCK lock; int ret; + void (*oldsig_handler)(int); gotalarm = 0; - CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig); + oldsig_handler = CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig); lock.l_type = type; lock.l_whence = SEEK_SET; @@ -51,9 +52,10 @@ BOOL do_file_lock(int fd, int waitsecs, int type) lock.l_pid = 0; alarm(waitsecs); + /* Note we must *NOT* use sys_fcntl here ! JRA */ ret = fcntl(fd, SMB_F_SETLKW, &lock); alarm(0); - CatchSignal(SIGALRM, SIGNAL_CAST SIG_IGN); + CatchSignal(SIGALRM, SIGNAL_CAST oldsig_handler); if (gotalarm) { DEBUG(0, ("do_file_lock: failed to %s file.\n", @@ -448,7 +450,8 @@ void *map_file(char *fname, size_t size) #endif if (!p) { p = file_load(fname, &s2); - if (!p || s2 != size) { + if (!p) return NULL; + if (s2 != size) { DEBUG(1,("incorrect size for %s - got %d expected %d\n", fname, s2, size)); if (p) free(p); diff --git a/source3/lib/util_getent.c b/source3/lib/util_getent.c index 02e4b932de..2e76121aae 100644 --- a/source3/lib/util_getent.c +++ b/source3/lib/util_getent.c @@ -277,20 +277,24 @@ struct sys_userlist *get_users_in_group(const char *gname) DOM_SID sid; enum SID_NAME_USE name_type; - (void) split_domain_and_name(gname, domain, groupname); - - /* - * If we're doing this via winbindd, don't do the - * entire group list enumeration as we know this is - * pointless (and slow). - */ - - if (winbind_lookup_name(domain, groupname, &sid, &name_type) && name_type == SID_NAME_DOM_GRP) { - if ((gptr = (struct group *)getgrnam(gname)) == NULL) - return NULL; - return add_members_to_userlist(list_head, gptr); + /* No point using winbind if we can't split it in the + first place */ + if (split_domain_and_name(gname, domain, groupname)) { + + /* + * If we're doing this via winbindd, don't do the + * entire group list enumeration as we know this is + * pointless (and slow). + */ + + if (winbind_lookup_name(domain, groupname, &sid, &name_type) + && name_type == SID_NAME_DOM_GRP) { + if ((gptr = (struct group *)getgrnam(gname)) == NULL) + return NULL; + return add_members_to_userlist(list_head, gptr); + } } - + setgrent(); while((gptr = getgrent()) != NULL) { if (strequal(gname, gptr->gr_name)) { diff --git a/source3/lib/util_pw.c b/source3/lib/util_pw.c index 259649a064..9d075a05e8 100644 --- a/source3/lib/util_pw.c +++ b/source3/lib/util_pw.c @@ -22,50 +22,6 @@ #include "includes.h" -struct passwd *make_modifyable_passwd(const struct passwd *from) -{ - struct passwd *ret = smb_xmalloc(sizeof(*ret)); -/* This is the assumed shape of the members by certain parts of the code... - fstring pw_name; - fstring pw_passwd; - fstring pw_gecos; - pstring pw_dir; - pstring pw_shell; -*/ - char *pw_name = smb_xmalloc(sizeof(fstring)); - char *pw_passwd = smb_xmalloc(sizeof(fstring)); - char *pw_gecos = smb_xmalloc(sizeof(fstring)); - char *pw_dir = smb_xmalloc(sizeof(pstring)); - char *pw_shell = smb_xmalloc(sizeof(pstring)); - - ZERO_STRUCTP(ret); - - /* - * Now point the struct's members as the - * newly allocated buffers: - */ - - ret->pw_name = pw_name; - fstrcpy(ret->pw_name, from->pw_name); - - ret->pw_passwd = pw_passwd; - fstrcpy(ret->pw_passwd, from->pw_passwd); - - ret->pw_uid = from->pw_uid; - ret->pw_gid = from->pw_gid; - - ret->pw_gecos = pw_gecos; - fstrcpy(ret->pw_gecos, from->pw_gecos); - - ret->pw_dir = pw_dir; - pstrcpy(ret->pw_dir, from->pw_dir); - - ret->pw_shell = pw_shell; - pstrcpy(ret->pw_shell, from->pw_shell); - - return ret; -} - static struct passwd *alloc_copy_passwd(const struct passwd *from) { struct passwd *ret = smb_xmalloc(sizeof(struct passwd)); @@ -100,7 +56,7 @@ struct passwd *getpwnam_alloc(const char *name) { struct passwd *temp; - temp = getpwnam(name); + temp = sys_getpwnam(name); if (!temp) { #if 0 @@ -118,7 +74,7 @@ struct passwd *getpwuid_alloc(uid_t uid) { struct passwd *temp; - temp = getpwuid(uid); + temp = sys_getpwuid(uid); if (!temp) { #if 0 diff --git a/source3/lib/util_seaccess.c b/source3/lib/util_seaccess.c index 8ed266aced..9fdf03adfc 100644 --- a/source3/lib/util_seaccess.c +++ b/source3/lib/util_seaccess.c @@ -20,8 +20,6 @@ */ #include "includes.h" -#include "nterr.h" -#include "sids.h" /********************************************************************************** Check if this ACE has a SID in common with the token. diff --git a/source3/lib/util_sec.c b/source3/lib/util_sec.c index d59b1b0471..132748ce13 100644 --- a/source3/lib/util_sec.c +++ b/source3/lib/util_sec.c @@ -227,6 +227,7 @@ void set_effective_gid(gid_t gid) } static uid_t saved_euid, saved_ruid; +static gid_t saved_egid, saved_rgid; /**************************************************************************** save the real and effective uid for later restoration. Used by the quotas @@ -264,6 +265,41 @@ void restore_re_uid(void) assert_uid(saved_ruid, saved_euid); } + +/**************************************************************************** + save the real and effective gid for later restoration. Used by the + getgroups code +****************************************************************************/ +void save_re_gid(void) +{ + saved_rgid = getgid(); + saved_egid = getegid(); +} + +/**************************************************************************** + and restore them! +****************************************************************************/ +void restore_re_gid(void) +{ +#if USE_SETRESUID + setresgid(saved_rgid, saved_egid, -1); +#elif USE_SETREUID + setregid(saved_rgid, -1); + setregid(-1,saved_egid); +#elif USE_SETUIDX + setgidx(ID_REAL, saved_rgid); + setgidx(ID_EFFECTIVE, saved_egid); +#else + set_effective_gid(saved_egid); + if (getgid() != saved_rgid) + setgid(saved_rgid); + set_effective_gid(saved_egid); +#endif + + assert_gid(saved_rgid, saved_egid); +} + + /**************************************************************************** set the real AND effective uid to the current effective uid in a way that allows root to be regained. diff --git a/source3/lib/util_sid.c b/source3/lib/util_sid.c index cd7b64bb70..5dd1d75c70 100644 --- a/source3/lib/util_sid.c +++ b/source3/lib/util_sid.c @@ -1,10 +1,11 @@ /* Unix SMB/CIFS implementation. Samba utility functions - Copyright (C) Andrew Tridgell 1992-1998 - Copyright (C) Luke Kenneth Caseson Leighton 1998-1999 - Copyright (C) Jeremy Allison 1999 - + Copyright (C) Andrew Tridgell 1992-1998 + Copyright (C) Luke Kenneth Caseson Leighton 1998-1999 + Copyright (C) Jeremy Allison 1999 + Copyright (C) Stefan (metze) Metzmacher 2002 + This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or @@ -22,10 +23,6 @@ #include "includes.h" -/* NOTE! the global_sam_sid is the SID of our local SAM. This is only - equal to the domain SID when we are a DC, otherwise its our - workstation SID */ -DOM_SID global_sam_sid; extern pstring global_myname; extern fstring global_myworkgroup; @@ -37,66 +34,15 @@ DOM_SID global_sid_Builtin; /* Local well-known domain */ DOM_SID global_sid_World_Domain; /* Everyone domain */ DOM_SID global_sid_World; /* Everyone */ DOM_SID global_sid_Creator_Owner_Domain; /* Creator Owner domain */ -DOM_SID global_sid_Creator_Owner; /* Creator Owner */ -DOM_SID global_sid_Creator_Group; /* Creator Group */ DOM_SID global_sid_NT_Authority; /* NT Authority */ DOM_SID global_sid_NULL; /* NULL sid */ DOM_SID global_sid_Builtin_Guests; /* Builtin guest users */ DOM_SID global_sid_Authenticated_Users; /* All authenticated rids */ DOM_SID global_sid_Network; /* Network rids */ -DOM_SID global_sid_Anonymous; /* Anonymous login */ - -const DOM_SID *global_sid_everyone = &global_sid_World; -typedef struct _known_sid_users { - uint32 rid; - enum SID_NAME_USE sid_name_use; - char *known_user_name; -} known_sid_users; - -/* static known_sid_users no_users[] = {{0, 0, NULL}}; */ - -static known_sid_users everyone_users[] = { - { 0, SID_NAME_WKN_GRP, "Everyone" }, - {0, (enum SID_NAME_USE)0, NULL}}; - -static known_sid_users creator_owner_users[] = { - { 0, SID_NAME_ALIAS, "Creator Owner" }, - {0, (enum SID_NAME_USE)0, NULL}}; - -static known_sid_users nt_authority_users[] = { - { 1, SID_NAME_ALIAS, "Dialup" }, - { 2, SID_NAME_ALIAS, "Network"}, - { 3, SID_NAME_ALIAS, "Batch"}, - { 4, SID_NAME_ALIAS, "Interactive"}, - { 6, SID_NAME_ALIAS, "Service"}, - { 7, SID_NAME_ALIAS, "AnonymousLogon"}, - { 8, SID_NAME_ALIAS, "Proxy"}, - { 9, SID_NAME_ALIAS, "ServerLogon"}, - { 11, SID_NAME_ALIAS, "Authenticated Users"}, - { 18, SID_NAME_ALIAS, "SYSTEM"}, - { 0, (enum SID_NAME_USE)0, NULL}}; - -static known_sid_users builtin_groups[] = { - { BUILTIN_ALIAS_RID_ADMINS, SID_NAME_ALIAS, "Administrators" }, - { BUILTIN_ALIAS_RID_USERS, SID_NAME_ALIAS, "Users" }, - { BUILTIN_ALIAS_RID_GUESTS, SID_NAME_ALIAS, "Guests" }, - { BUILTIN_ALIAS_RID_ACCOUNT_OPS, SID_NAME_ALIAS, "Account Operators" }, - { BUILTIN_ALIAS_RID_SYSTEM_OPS, SID_NAME_ALIAS, "Server Operators" }, - { BUILTIN_ALIAS_RID_PRINT_OPS, SID_NAME_ALIAS, "Print Operators" }, - { BUILTIN_ALIAS_RID_BACKUP_OPS, SID_NAME_ALIAS, "Backup Operators" }, - { 0, (enum SID_NAME_USE)0, NULL}}; - -#define MAX_SID_NAMES 7 - -static struct sid_name_map_info -{ - DOM_SID *sid; - char *name; - known_sid_users *known_users; -} sid_name_map[MAX_SID_NAMES]; - -static BOOL sid_name_map_initialized = False; +static DOM_SID global_sid_Creator_Owner; /* Creator Owner */ +static DOM_SID global_sid_Creator_Group; /* Creator Group */ +static DOM_SID global_sid_Anonymous; /* Anonymous login */ /* * An NT compatible anonymous token. @@ -109,65 +55,43 @@ NT_USER_TOKEN anonymous_token = { anon_sid_array }; -/************************************************************************** - quick init function - *************************************************************************/ -static void init_sid_name_map (void) +/**************************************************************************** + Lookup string names for SID types. +****************************************************************************/ + +const static struct { + enum SID_NAME_USE sid_type; + char *string; +} sid_name_type[] = { + {SID_NAME_USER, "user"}, + {SID_NAME_DOM_GRP, "domain group"}, + {SID_NAME_DOMAIN, "domain"}, + {SID_NAME_ALIAS, "local group"}, + {SID_NAME_WKN_GRP, "well-known group"}, + {SID_NAME_DELETED, "deleted account"}, + {SID_NAME_INVALID, "invalid account"}, + {SID_NAME_UNKNOWN, "UNKNOWN"}, + + {SID_NAME_USE_NONE, NULL} +}; + +const char *sid_type_lookup(uint32 sid_type) { int i = 0; - - if (sid_name_map_initialized) return; - - if ((lp_security() == SEC_USER) && lp_domain_logons()) { - sid_name_map[i].sid = &global_sam_sid; - sid_name_map[i].name = global_myworkgroup; - sid_name_map[i].known_users = NULL; - i++; - sid_name_map[i].sid = &global_sam_sid; - sid_name_map[i].name = global_myname; - sid_name_map[i].known_users = NULL; - i++; - } - else { - sid_name_map[i].sid = &global_sam_sid; - sid_name_map[i].name = global_myname; - sid_name_map[i].known_users = NULL; + /* Look through list */ + while(sid_name_type[i].sid_type != 0) { + if (sid_name_type[i].sid_type == sid_type) + return sid_name_type[i].string; i++; } - sid_name_map[i].sid = &global_sid_Builtin; - sid_name_map[i].name = "BUILTIN"; - sid_name_map[i].known_users = &builtin_groups[0]; - i++; + /* Default return */ + return "SID *TYPE* is INVALID"; - sid_name_map[i].sid = &global_sid_World_Domain; - sid_name_map[i].name = ""; - sid_name_map[i].known_users = &everyone_users[0]; - i++; - - sid_name_map[i].sid = &global_sid_Creator_Owner_Domain; - sid_name_map[i].name = ""; - sid_name_map[i].known_users = &creator_owner_users[0]; - i++; - - sid_name_map[i].sid = &global_sid_NT_Authority; - sid_name_map[i].name = "NT Authority"; - sid_name_map[i].known_users = &nt_authority_users[0]; - i++; - - - /* end of array */ - sid_name_map[i].sid = NULL; - sid_name_map[i].name = NULL; - sid_name_map[i].known_users = NULL; - - sid_name_map_initialized = True; - - return; - } + /**************************************************************************** Creates some useful well known sids ****************************************************************************/ @@ -194,115 +118,6 @@ void generate_wellknown_sids(void) } /************************************************************************** - Turns a domain SID into a name, returned in the nt_domain argument. -***************************************************************************/ - -BOOL map_domain_sid_to_name(DOM_SID *sid, char *nt_domain) -{ - fstring sid_str; - int i = 0; - - sid_to_string(sid_str, sid); - - if (!sid_name_map_initialized) - init_sid_name_map(); - - DEBUG(5,("map_domain_sid_to_name: %s\n", sid_str)); - - if (nt_domain == NULL) - return False; - - while (sid_name_map[i].sid != NULL) { - sid_to_string(sid_str, sid_name_map[i].sid); - DEBUG(5,("map_domain_sid_to_name: compare: %s\n", sid_str)); - if (sid_equal(sid_name_map[i].sid, sid)) { - fstrcpy(nt_domain, sid_name_map[i].name); - DEBUG(5,("map_domain_sid_to_name: found '%s'\n", nt_domain)); - return True; - } - i++; - } - - DEBUG(5,("map_domain_sid_to_name: mapping for %s not found\n", sid_str)); - - return False; -} - -/************************************************************************** - Looks up a known username from one of the known domains. -***************************************************************************/ - -BOOL lookup_known_rid(DOM_SID *sid, uint32 rid, char *name, enum SID_NAME_USE *psid_name_use) -{ - int i = 0; - struct sid_name_map_info *psnm; - - if (!sid_name_map_initialized) - init_sid_name_map(); - - for(i = 0; sid_name_map[i].sid != NULL; i++) { - psnm = &sid_name_map[i]; - if(sid_equal(psnm->sid, sid)) { - int j; - for(j = 0; psnm->known_users && psnm->known_users[j].known_user_name != NULL; j++) { - if(rid == psnm->known_users[j].rid) { - DEBUG(5,("lookup_builtin_rid: rid = %u, domain = '%s', user = '%s'\n", - (unsigned int)rid, psnm->name, psnm->known_users[j].known_user_name )); - fstrcpy( name, psnm->known_users[j].known_user_name); - *psid_name_use = psnm->known_users[j].sid_name_use; - return True; - } - } - } - } - - return False; -} - -/************************************************************************** - Turns a domain name into a SID. - *** side-effect: if the domain name is NULL, it is set to our domain *** -***************************************************************************/ - -BOOL map_domain_name_to_sid(DOM_SID *sid, char *nt_domain) -{ - int i = 0; - - if (nt_domain == NULL) { - DEBUG(5,("map_domain_name_to_sid: mapping NULL domain to our SID.\n")); - sid_copy(sid, &global_sam_sid); - return True; - } - - if (nt_domain[0] == 0) { - fstrcpy(nt_domain, global_myname); - DEBUG(5,("map_domain_name_to_sid: overriding blank name to %s\n", nt_domain)); - sid_copy(sid, &global_sam_sid); - return True; - } - - DEBUG(5,("map_domain_name_to_sid: %s\n", nt_domain)); - - if (!sid_name_map_initialized) - init_sid_name_map(); - - while (sid_name_map[i].name != NULL) { - DEBUG(5,("map_domain_name_to_sid: compare: %s\n", sid_name_map[i].name)); - if (strequal(sid_name_map[i].name, nt_domain)) { - fstring sid_str; - sid_copy(sid, sid_name_map[i].sid); - sid_to_string(sid_str, sid_name_map[i].sid); - DEBUG(5,("map_domain_name_to_sid: found %s\n", sid_str)); - return True; - } - i++; - } - - DEBUG(0,("map_domain_name_to_sid: mapping to %s not found.\n", nt_domain)); - return False; -} - -/************************************************************************** Splits a name of format \DOMAIN\name or name into its two components. Sets the DOMAIN name to global_myname if it has not been specified. ***************************************************************************/ @@ -340,15 +155,22 @@ void split_domain_name(const char *fullname, char *domain, char *name) Convert a SID to an ascii string. *****************************************************************/ -char *sid_to_string(fstring sidstr_out, DOM_SID *sid) +char *sid_to_string(fstring sidstr_out, const DOM_SID *sid) { char subauth[16]; int i; + uint32 ia; + + if (!sid) { + fstrcpy(sidstr_out, "(NULL SID)"); + return sidstr_out; + } + /* BIG NOTE: this function only does SIDS where the identauth is not >= 2^32 */ - uint32 ia = (sid->id_auth[5]) + - (sid->id_auth[4] << 8 ) + - (sid->id_auth[3] << 16) + - (sid->id_auth[2] << 24); + ia = (sid->id_auth[5]) + + (sid->id_auth[4] << 8 ) + + (sid->id_auth[3] << 16) + + (sid->id_auth[2] << 24); slprintf(sidstr_out, sizeof(fstring) - 1, "S-%u-%lu", (unsigned int)sid->sid_rev_num, (unsigned long)ia); @@ -363,7 +185,7 @@ char *sid_to_string(fstring sidstr_out, DOM_SID *sid) /* useful function for debug lines */ -const char *sid_string_static(DOM_SID *sid) +const char *sid_string_static(const DOM_SID *sid) { static fstring sid_str; sid_to_string(sid_str, sid); @@ -466,8 +288,11 @@ BOOL sid_split_rid(DOM_SID *sid, uint32 *rid) Return the last rid from the end of a sid *****************************************************************/ -BOOL sid_peek_rid(DOM_SID *sid, uint32 *rid) +BOOL sid_peek_rid(const DOM_SID *sid, uint32 *rid) { + if (!sid || !rid) + return False; + if (sid->num_auths > 0) { *rid = sid->sub_auths[sid->num_auths - 1]; return True; @@ -476,6 +301,25 @@ BOOL sid_peek_rid(DOM_SID *sid, uint32 *rid) } /***************************************************************** + Return the last rid from the end of a sid + and check the sid against the exp_dom_sid +*****************************************************************/ + +BOOL sid_peek_check_rid(const DOM_SID *exp_dom_sid, const DOM_SID *sid, uint32 *rid) +{ + if (!exp_dom_sid || !sid || !rid) + return False; + + + if (sid_compare_domain(exp_dom_sid, sid)!=0){ + *rid=(-1); + return False; + } + + return sid_peek_rid(sid, rid); +} + +/***************************************************************** Copies a sid *****************************************************************/ @@ -483,7 +327,7 @@ void sid_copy(DOM_SID *dst, const DOM_SID *src) { int i; - memset((char *)dst, '\0', sizeof(DOM_SID)); + ZERO_STRUCTP(dst); dst->sid_rev_num = src->sid_rev_num; dst->num_auths = src->num_auths; @@ -494,24 +338,6 @@ void sid_copy(DOM_SID *dst, const DOM_SID *src) dst->sub_auths[i] = src->sub_auths[i]; } -/***************************************************************** - Duplicates a sid - mallocs the target. -*****************************************************************/ - -DOM_SID *sid_dup(DOM_SID *src) -{ - DOM_SID *dst; - - if(!src) - return NULL; - - if((dst = malloc(sizeof(DOM_SID))) != NULL) { - memset(dst, '\0', sizeof(DOM_SID)); - sid_copy( dst, src); - } - - return dst; -} /***************************************************************** Write a sid out into on-the-wire format. @@ -553,7 +379,7 @@ BOOL sid_parse(char *inbuf, size_t len, DOM_SID *sid) /***************************************************************** Compare the auth portion of two sids. *****************************************************************/ -int sid_compare_auth(const DOM_SID *sid1, const DOM_SID *sid2) +static int sid_compare_auth(const DOM_SID *sid1, const DOM_SID *sid2) { int i; @@ -619,14 +445,6 @@ BOOL sid_equal(const DOM_SID *sid1, const DOM_SID *sid2) } -/***************************************************************** - Check if the SID is our domain SID (S-1-5-21-x-y-z). -*****************************************************************/ -BOOL sid_check_is_domain(const DOM_SID *sid) -{ - return sid_equal(sid, &global_sam_sid); -} - /***************************************************************** Check if the SID is the builtin SID (S-1-5-32). @@ -640,20 +458,6 @@ BOOL sid_check_is_builtin(const DOM_SID *sid) /***************************************************************** Check if the SID is our domain SID (S-1-5-21-x-y-z). *****************************************************************/ -BOOL sid_check_is_in_our_domain(const DOM_SID *sid) -{ - DOM_SID dom_sid; - uint32 rid; - - sid_copy(&dom_sid, sid); - sid_split_rid(&dom_sid, &rid); - - return sid_equal(&dom_sid, &global_sam_sid); -} - -/***************************************************************** - Check if the SID is our domain SID (S-1-5-21-x-y-z). -*****************************************************************/ BOOL sid_check_is_in_builtin(const DOM_SID *sid) { DOM_SID dom_sid; diff --git a/source3/lib/util_sock.c b/source3/lib/util_sock.c index af3182264d..4f1f2a1470 100644 --- a/source3/lib/util_sock.c +++ b/source3/lib/util_sock.c @@ -21,13 +21,6 @@ #include "includes.h" -#ifdef WITH_SSL -#include <openssl/ssl.h> -#undef Realloc /* SSLeay defines this and samba has a function of this name */ -extern SSL *ssl; -extern int sslFd; -#endif /* WITH_SSL */ - /* the last IP received from */ struct in_addr lastip; @@ -42,23 +35,22 @@ int smb_read_error = 0; BOOL is_a_socket(int fd) { - int v,l; - l = sizeof(int); - return(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0); + int v,l; + l = sizeof(int); + return(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0); } enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON}; -typedef struct smb_socket_option -{ - char *name; - int level; - int option; - int value; - int opttype; +typedef struct smb_socket_option { + char *name; + int level; + int option; + int value; + int opttype; } smb_socket_option; -smb_socket_option socket_options[] = { +static const smb_socket_option socket_options[] = { {"SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE, 0, OPT_BOOL}, {"SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR, 0, OPT_BOOL}, {"SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, 0, OPT_BOOL}, @@ -97,10 +89,11 @@ smb_socket_option socket_options[] = { /**************************************************************************** Print socket options. ****************************************************************************/ + static void print_socket_options(int s) { int value, vlen = 4; - smb_socket_option *p = &socket_options[0]; + const smb_socket_option *p = &socket_options[0]; for (; p->name != NULL; p++) { if (getsockopt(s, p->level, p->option, (void *)&value, &vlen) == -1) { @@ -178,7 +171,7 @@ ssize_t read_udp_socket(int fd,char *buf,size_t len) memset((char *)&sock,'\0',socklen); memset((char *)&lastip,'\0',sizeof(lastip)); - ret = (ssize_t)recvfrom(fd,buf,len,0,(struct sockaddr *)&sock,&socklen); + ret = (ssize_t)sys_recvfrom(fd,buf,len,0,(struct sockaddr *)&sock,&socklen); if (ret <= 0) { DEBUG(2,("read socket failed. ERRNO=%s\n",strerror(errno))); return(0); @@ -196,7 +189,7 @@ ssize_t read_udp_socket(int fd,char *buf,size_t len) /******************************************************************* checks if read data is outstanding. ********************************************************************/ -int read_data_outstanding(int fd, unsigned int time_out) +static int read_data_outstanding(int fd, unsigned int time_out) { int selrtn; fd_set fds; @@ -243,15 +236,7 @@ static ssize_t read_socket_with_timeout(int fd,char *buf,size_t mincnt,size_t ma if (mincnt == 0) mincnt = maxcnt; while (nread < mincnt) { -#ifdef WITH_SSL - if (fd == sslFd) { - readret = SSL_read(ssl, buf + nread, maxcnt - nread); - } else { - readret = read(fd, buf + nread, maxcnt - nread); - } -#else /* WITH_SSL */ - readret = read(fd, buf + nread, maxcnt - nread); -#endif /* WITH_SSL */ + readret = sys_read(fd, buf + nread, maxcnt - nread); if (readret == 0) { DEBUG(5,("read_socket_with_timeout: blocking read. EOF from client.\n")); @@ -300,15 +285,7 @@ static ssize_t read_socket_with_timeout(int fd,char *buf,size_t mincnt,size_t ma return -1; } -#ifdef WITH_SSL - if (fd == sslFd) { - readret = SSL_read(ssl, buf + nread, maxcnt - nread); - }else{ - readret = read(fd, buf + nread, maxcnt - nread); - } -#else /* WITH_SSL */ - readret = read(fd, buf+nread, maxcnt-nread); -#endif /* WITH_SSL */ + readret = sys_read(fd, buf+nread, maxcnt-nread); if (readret == 0) { /* we got EOF on the file descriptor */ @@ -353,15 +330,7 @@ ssize_t read_with_timeout(int fd, char *buf, size_t mincnt, size_t maxcnt, if (mincnt == 0) mincnt = maxcnt; while (nread < mincnt) { -#ifdef WITH_SSL - if(fd == sslFd){ - readret = SSL_read(ssl, buf + nread, maxcnt - nread); - }else{ - readret = read(fd, buf + nread, maxcnt - nread); - } -#else /* WITH_SSL */ - readret = read(fd, buf + nread, maxcnt - nread); -#endif /* WITH_SSL */ + readret = sys_read(fd, buf + nread, maxcnt - nread); if (readret <= 0) return readret; @@ -383,15 +352,7 @@ ssize_t read_with_timeout(int fd, char *buf, size_t mincnt, size_t maxcnt, if(selrtn <= 0) return selrtn; -#ifdef WITH_SSL - if(fd == sslFd){ - readret = SSL_read(ssl, buf + nread, maxcnt - nread); - }else{ - readret = read(fd, buf + nread, maxcnt - nread); - } -#else /* WITH_SSL */ - readret = read(fd, buf+nread, maxcnt-nread); -#endif /* WITH_SSL */ + readret = sys_read(fd, buf+nread, maxcnt-nread); if (readret <= 0) return readret; @@ -404,57 +365,33 @@ ssize_t read_with_timeout(int fd, char *buf, size_t mincnt, size_t maxcnt, } /**************************************************************************** -send a keepalive packet (rfc1002) -****************************************************************************/ - -BOOL send_keepalive(int client) -{ - unsigned char buf[4]; - - buf[0] = SMBkeepalive; - buf[1] = buf[2] = buf[3] = 0; - - return(write_socket_data(client,(char *)buf,4) == 4); -} - -/**************************************************************************** read data from the client, reading exactly N bytes. ****************************************************************************/ ssize_t read_data(int fd,char *buffer,size_t N) { - ssize_t ret; - size_t total=0; + ssize_t ret; + size_t total=0; - smb_read_error = 0; - - while (total < N) - { -#ifdef WITH_SSL - if(fd == sslFd){ - ret = SSL_read(ssl, buffer + total, N - total); - }else{ - ret = read(fd,buffer + total,N - total); - } -#else /* WITH_SSL */ - ret = read(fd,buffer + total,N - total); -#endif /* WITH_SSL */ - - if (ret == 0) - { - DEBUG(10,("read_data: read of %d returned 0. Error = %s\n", (int)(N - total), strerror(errno) )); - smb_read_error = READ_EOF; - return 0; - } - if (ret == -1) - { - DEBUG(0,("read_data: read failure for %d. Error = %s\n", (int)(N - total), strerror(errno) )); - smb_read_error = READ_ERROR; - return -1; - } - total += ret; - } - return (ssize_t)total; + smb_read_error = 0; + + while (total < N) { + ret = sys_read(fd,buffer + total,N - total); + + if (ret == 0) { + DEBUG(10,("read_data: read of %d returned 0. Error = %s\n", (int)(N - total), strerror(errno) )); + smb_read_error = READ_EOF; + return 0; + } + + if (ret == -1) { + DEBUG(0,("read_data: read failure for %d. Error = %s\n", (int)(N - total), strerror(errno) )); + smb_read_error = READ_ERROR; + return -1; + } + total += ret; + } + return (ssize_t)total; } /**************************************************************************** @@ -463,38 +400,28 @@ ssize_t read_data(int fd,char *buffer,size_t N) static ssize_t read_socket_data(int fd,char *buffer,size_t N) { - ssize_t ret; - size_t total=0; + ssize_t ret; + size_t total=0; - smb_read_error = 0; - - while (total < N) - { -#ifdef WITH_SSL - if(fd == sslFd){ - ret = SSL_read(ssl, buffer + total, N - total); - }else{ - ret = read(fd,buffer + total,N - total); - } -#else /* WITH_SSL */ - ret = read(fd,buffer + total,N - total); -#endif /* WITH_SSL */ - - if (ret == 0) - { - DEBUG(10,("read_socket_data: recv of %d returned 0. Error = %s\n", (int)(N - total), strerror(errno) )); - smb_read_error = READ_EOF; - return 0; - } - if (ret == -1) - { - DEBUG(0,("read_socket_data: recv failure for %d. Error = %s\n", (int)(N - total), strerror(errno) )); - smb_read_error = READ_ERROR; - return -1; - } - total += ret; - } - return (ssize_t)total; + smb_read_error = 0; + + while (total < N) { + ret = sys_read(fd,buffer + total,N - total); + + if (ret == 0) { + DEBUG(10,("read_socket_data: recv of %d returned 0. Error = %s\n", (int)(N - total), strerror(errno) )); + smb_read_error = READ_EOF; + return 0; + } + + if (ret == -1) { + DEBUG(0,("read_socket_data: recv failure for %d. Error = %s\n", (int)(N - total), strerror(errno) )); + smb_read_error = READ_ERROR; + return -1; + } + total += ret; + } + return (ssize_t)total; } /**************************************************************************** @@ -503,62 +430,46 @@ static ssize_t read_socket_data(int fd,char *buffer,size_t N) ssize_t write_data(int fd,char *buffer,size_t N) { - size_t total=0; - ssize_t ret; - - while (total < N) - { -#ifdef WITH_SSL - if(fd == sslFd){ - ret = SSL_write(ssl,buffer + total,N - total); - }else{ - ret = write(fd,buffer + total,N - total); - } -#else /* WITH_SSL */ - ret = write(fd,buffer + total,N - total); -#endif /* WITH_SSL */ - - if (ret == -1) { - DEBUG(0,("write_data: write failure. Error = %s\n", strerror(errno) )); - return -1; - } - if (ret == 0) return total; + size_t total=0; + ssize_t ret; - total += ret; - } - return (ssize_t)total; + while (total < N) { + ret = sys_write(fd,buffer + total,N - total); + + if (ret == -1) { + DEBUG(0,("write_data: write failure. Error = %s\n", strerror(errno) )); + return -1; + } + if (ret == 0) + return total; + + total += ret; + } + return (ssize_t)total; } /**************************************************************************** Write data to a socket - use send rather than write. ****************************************************************************/ -ssize_t write_socket_data(int fd,char *buffer,size_t N) +static ssize_t write_socket_data(int fd,char *buffer,size_t N) { - size_t total=0; - ssize_t ret; - - while (total < N) - { -#ifdef WITH_SSL - if(fd == sslFd){ - ret = SSL_write(ssl,buffer + total,N - total); - }else{ - ret = send(fd,buffer + total,N - total, 0); - } -#else /* WITH_SSL */ - ret = send(fd,buffer + total,N - total,0); -#endif /* WITH_SSL */ - - if (ret == -1) { - DEBUG(0,("write_socket_data: write failure. Error = %s\n", strerror(errno) )); - return -1; - } - if (ret == 0) return total; + size_t total=0; + ssize_t ret; - total += ret; - } - return (ssize_t)total; + while (total < N) { + ret = sys_send(fd,buffer + total,N - total,0); + + if (ret == -1) { + DEBUG(0,("write_socket_data: write failure. Error = %s\n", strerror(errno) )); + return -1; + } + if (ret == 0) + return total; + + total += ret; + } + return (ssize_t)total; } /**************************************************************************** @@ -567,19 +478,34 @@ write to a socket ssize_t write_socket(int fd,char *buf,size_t len) { - ssize_t ret=0; + ssize_t ret=0; - DEBUG(6,("write_socket(%d,%d)\n",fd,(int)len)); - ret = write_socket_data(fd,buf,len); + DEBUG(6,("write_socket(%d,%d)\n",fd,(int)len)); + ret = write_socket_data(fd,buf,len); - DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,(int)len,(int)ret)); - if(ret <= 0) - DEBUG(0,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n", - (int)len, fd, strerror(errno) )); + DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,(int)len,(int)ret)); + if(ret <= 0) + DEBUG(0,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n", + (int)len, fd, strerror(errno) )); + + return(ret); +} + +/**************************************************************************** +send a keepalive packet (rfc1002) +****************************************************************************/ - return(ret); +BOOL send_keepalive(int client) +{ + unsigned char buf[4]; + + buf[0] = SMBkeepalive; + buf[1] = buf[2] = buf[3] = 0; + + return(write_socket_data(client,(char *)buf,4) == 4); } + /**************************************************************************** read 4 bytes of a smb packet and return the smb length of the packet store the result in the buffer @@ -590,30 +516,29 @@ timeout is in milliseconds. static ssize_t read_smb_length_return_keepalive(int fd,char *inbuf,unsigned int timeout) { - ssize_t len=0; - int msg_type; - BOOL ok = False; + ssize_t len=0; + int msg_type; + BOOL ok = False; - while (!ok) - { - if (timeout > 0) - ok = (read_socket_with_timeout(fd,inbuf,4,4,timeout) == 4); - else - ok = (read_socket_data(fd,inbuf,4) == 4); + while (!ok) { + if (timeout > 0) + ok = (read_socket_with_timeout(fd,inbuf,4,4,timeout) == 4); + else + ok = (read_socket_data(fd,inbuf,4) == 4); - if (!ok) - return(-1); + if (!ok) + return(-1); - len = smb_len(inbuf); - msg_type = CVAL(inbuf,0); + len = smb_len(inbuf); + msg_type = CVAL(inbuf,0); - if (msg_type == SMBkeepalive) - DEBUG(5,("Got keepalive packet\n")); - } + if (msg_type == SMBkeepalive) + DEBUG(5,("Got keepalive packet\n")); + } - DEBUG(10,("got smb length of %d\n",len)); + DEBUG(10,("got smb length of %d\n",len)); - return(len); + return(len); } /**************************************************************************** @@ -625,23 +550,22 @@ timeout is in milliseconds. ssize_t read_smb_length(int fd,char *inbuf,unsigned int timeout) { - ssize_t len; + ssize_t len; - for(;;) - { - len = read_smb_length_return_keepalive(fd, inbuf, timeout); + for(;;) { + len = read_smb_length_return_keepalive(fd, inbuf, timeout); - if(len < 0) - return len; + if(len < 0) + return len; - /* Ignore session keepalives. */ - if(CVAL(inbuf,0) != SMBkeepalive) - break; - } + /* Ignore session keepalives. */ + if(CVAL(inbuf,0) != SMBkeepalive) + break; + } - DEBUG(10,("read_smb_length: got smb length of %d\n",len)); + DEBUG(10,("read_smb_length: got smb length of %d\n",len)); - return len; + return len; } /**************************************************************************** @@ -709,40 +633,6 @@ BOOL receive_smb(int fd,char *buffer, unsigned int timeout) } /**************************************************************************** - read an smb from a fd ignoring all keepalive packets. Note that the buffer - *MUST* be of size BUFFER_SIZE+SAFETY_MARGIN. - The timeout is in milliseconds - - This is exactly the same as receive_smb except that it never returns - a session keepalive packet (just as receive_smb used to do). - receive_smb was changed to return keepalives as the oplock processing means this call - should never go into a blocking read. -****************************************************************************/ - -BOOL client_receive_smb(int fd,char *buffer, unsigned int timeout) -{ - BOOL ret; - - for(;;) - { - ret = receive_smb(fd, buffer, timeout); - - if (!ret) - { - DEBUG(10,("client_receive_smb failed\n")); - show_msg(buffer); - return ret; - } - - /* Ignore session keepalive packets. */ - if(CVAL(buffer,0) != SMBkeepalive) - break; - } - show_msg(buffer); - return ret; -} - -/**************************************************************************** send an smb to a fd ****************************************************************************/ @@ -767,45 +657,6 @@ BOOL send_smb(int fd,char *buffer) } /**************************************************************************** -send a single packet to a port on another machine -****************************************************************************/ - -BOOL send_one_packet(char *buf,int len,struct in_addr ip,int port,int type) -{ - BOOL ret; - int out_fd; - struct sockaddr_in sock_out; - - /* create a socket to write to */ - out_fd = socket(AF_INET, type, 0); - if (out_fd == -1) - { - DEBUG(0,("socket failed")); - return False; - } - - /* set the address and port */ - memset((char *)&sock_out,'\0',sizeof(sock_out)); - putip((char *)&sock_out.sin_addr,(char *)&ip); - sock_out.sin_port = htons( port ); - sock_out.sin_family = AF_INET; - - if (DEBUGLEVEL > 0) - DEBUG(3,("sending a packet of len %d to (%s) on port %d of type %s\n", - len,inet_ntoa(ip),port,type==SOCK_DGRAM?"DGRAM":"STREAM")); - - /* send it */ - ret = (sendto(out_fd,buf,len,0,(struct sockaddr *)&sock_out,sizeof(sock_out)) >= 0); - - if (!ret) - DEBUG(0,("Packet send to %s(%d) failed ERRNO=%s\n", - inet_ntoa(ip),port,strerror(errno))); - - close(out_fd); - return(ret); -} - -/**************************************************************************** Open a socket of the specified type, port, and address for incoming data. ****************************************************************************/ @@ -866,7 +717,7 @@ int open_socket_in( int type, int port, int dlevel, uint32 socket_addr, BOOL reb return( -1 ); } - DEBUG( 3, ( "bind succeeded on port %d\n", port ) ); + DEBUG( 10, ( "bind succeeded on port %d\n", port ) ); return( res ); } @@ -1113,37 +964,6 @@ char *get_socket_addr(int fd) return addr_buf; } -/******************************************************************* - opens and connects to a unix pipe socket - ******************************************************************/ -int open_pipe_sock(char *path) -{ - int sock; - struct sockaddr_un sa; - - sock = socket(AF_UNIX, SOCK_STREAM, 0); - - if (sock < 0) - { - DEBUG(0, ("unix socket open failed\n")); - return sock; - } - - ZERO_STRUCT(sa); - sa.sun_family = AF_UNIX; - safe_strcpy(sa.sun_path, path, sizeof(sa.sun_path)-1); - - DEBUG(10, ("socket open succeeded. file name: %s\n", sa.sun_path)); - - if (connect(sock, (struct sockaddr*) &sa, sizeof(sa)) < 0) - { - DEBUG(0,("socket connect to %s failed\n", sa.sun_path)); - close(sock); - return -1; - } - - return sock; -} /******************************************************************* Create protected unix domain socket. @@ -1153,8 +973,8 @@ int open_pipe_sock(char *path) permissions, instead. ******************************************************************/ int create_pipe_sock(const char *socket_dir, - const char *socket_name, - mode_t dir_perms) + const char *socket_name, + mode_t dir_perms) { struct sockaddr_un sunaddr; struct stat st; diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 6fdca658cd..88a72f1536 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -650,23 +650,27 @@ This routine looks for pattern in s and replaces it with insert. It may do multiple replacements. any of " ; ' $ or ` in the insert string are replaced with _ -if len==0 then no length check is performed +if len==0 then the string cannot be extended. This is different from the old +use of len==0 which was for no length checks to be done. ****************************************************************************/ -void string_sub(char *s,const char *pattern,const char *insert, size_t len) + +void string_sub(char *s,const char *pattern, const char *insert, size_t len) { char *p; ssize_t ls,lp,li, i; - if (!insert || !pattern || !s) return; + if (!insert || !pattern || !*pattern || !s) + return; ls = (ssize_t)strlen(s); lp = (ssize_t)strlen(pattern); li = (ssize_t)strlen(insert); - if (!*pattern) return; - + if (len == 0) + len = ls; + while (lp <= ls && (p = strstr(s,pattern))) { - if (len && (ls + (li-lp) >= len)) { + if (ls + (li-lp) >= len) { DEBUG(0,("ERROR: string overflow by %d in string_sub(%.50s, %d)\n", (int)(ls + (li-lp) - len), pattern, (int)len)); @@ -706,26 +710,98 @@ void pstring_sub(char *s,const char *pattern,const char *insert) string_sub(s, pattern, insert, sizeof(pstring)); } +/* similar to string_sub, but it will accept only allocated strings + * and may realloc them so pay attention at what you pass on no + * pointers inside strings, no pstrings or const must be passed + * as string. + */ + +char *realloc_string_sub(char *string, const char *pattern, const char *insert) +{ + char *p, *in; + char *s; + ssize_t ls,lp,li,ld, i; + + if (!insert || !pattern || !*pattern || !string || !*string) + return NULL; + + s = string; + + in = strdup(insert); + if (!in) { + DEBUG(0, ("realloc_string_sub: out of memory!\n")); + return NULL; + } + ls = (ssize_t)strlen(s); + lp = (ssize_t)strlen(pattern); + li = (ssize_t)strlen(insert); + ld = li - lp; + for (i=0;i<li;i++) { + switch (in[i]) { + case '`': + case '"': + case '\'': + case ';': + case '$': + case '%': + case '\r': + case '\n': + in[i] = '_'; + default: + /* ok */ + break; + } + } + + while ((p = strstr(s,pattern))) { + if (ld > 0) { + char *t = Realloc(string, ls + ld + 1); + if (!t) { + DEBUG(0, ("realloc_string_sub: out of memory!\n")); + SAFE_FREE(in); + return NULL; + } + string = t; + p = t + (p - s); + } + if (li != lp) { + memmove(p+li,p+lp,strlen(p+lp)+1); + } + memcpy(p, in, li); + s = p + li; + ls += ld; + } + SAFE_FREE(in); + return string; +} + /**************************************************************************** similar to string_sub() but allows for any character to be substituted. Use with caution! -if len==0 then no length check is performed +if len==0 then the string cannot be extended. This is different from the old +use of len==0 which was for no length checks to be done. ****************************************************************************/ + void all_string_sub(char *s,const char *pattern,const char *insert, size_t len) { char *p; ssize_t ls,lp,li; - if (!insert || !pattern || !s) return; + if (!insert || !pattern || !s) + return; ls = (ssize_t)strlen(s); lp = (ssize_t)strlen(pattern); li = (ssize_t)strlen(insert); - if (!*pattern) return; + if (!*pattern) + return; + + if (len == 0) + len = ls; while (lp <= ls && (p = strstr(s,pattern))) { - if (len && (ls + (li-lp) >= len)) { + if (ls + (li-lp) >= len) { DEBUG(0,("ERROR: string overflow by %d in all_string_sub(%.50s, %d)\n", (int)(ls + (li-lp) - len), pattern, (int)len)); @@ -743,10 +819,8 @@ void all_string_sub(char *s,const char *pattern,const char *insert, size_t len) /**************************************************************************** similar to all_string_sub but for unicode strings. return a new allocate unicode string. -len is the number of bytes, not chars similar to string_sub() but allows for any character to be substituted. Use with caution! - if len==0 then no length check is performed ****************************************************************************/ smb_ucs2_t *all_string_sub_w(const smb_ucs2_t *s, const smb_ucs2_t *pattern, @@ -916,6 +990,20 @@ void strlower_m(char *s) } /******************************************************************* + duplicate convert a string to lower case +********************************************************************/ +char *strdup_lower(const char *s) +{ + char *t = strdup(s); + if (t == NULL) { + DEBUG(0, ("strdup_lower: Out of memory!\n")); + return NULL; + } + strlower_m(t); + return t; +} + +/******************************************************************* convert a string to upper case ********************************************************************/ void strupper_m(char *s) @@ -935,6 +1023,20 @@ void strupper_m(char *s) unix_strupper(s,strlen(s)+1,s,strlen(s)+1); } +/******************************************************************* + convert a string to upper case +********************************************************************/ +char *strdup_upper(const char *s) +{ + char *t = strdup(s); + if (t == NULL) { + DEBUG(0, ("strdup_upper: Out of memory!\n")); + return NULL; + } + strupper_m(t); + return t; +} + /* return a RFC2254 binary string representation of a buffer used in LDAP filters @@ -991,13 +1093,207 @@ some platforms don't have strndup char *strndup(const char *s, size_t n) { char *ret; - int i; - for (i=0;s[i] && i<n;i++) ; - - ret = malloc(i+1); + + n = strnlen(s, n); + ret = malloc(n+1); if (!ret) return NULL; - memcpy(ret, s, i); - ret[i] = 0; + memcpy(ret, s, n); + ret[n] = 0; + return ret; } #endif + +#ifndef HAVE_STRNLEN +/******************************************************************* +some platforms don't have strnlen +********************************************************************/ + size_t strnlen(const char *s, size_t n) +{ + int i; + for (i=0; s[i] && i<n; i++) /* noop */ ; + return i; +} +#endif + + + +/*********************************************************** + List of Strings manipulation functions +***********************************************************/ + +#define S_LIST_ABS 16 /* List Allocation Block Size */ + +char **str_list_make(const char *string) +{ + char **list, **rlist; + char *str, *s; + int num, lsize; + pstring tok; + + if (!string || !*string) return NULL; + s = strdup(string); + if (!s) { + DEBUG(0,("str_list_make: Unable to allocate memory")); + return NULL; + } + + num = lsize = 0; + list = NULL; + + str = s; + while (next_token(&str, tok, LIST_SEP, sizeof(tok))) + { + if (num == lsize) { + lsize += S_LIST_ABS; + rlist = (char **)Realloc(list, ((sizeof(char **)) * (lsize +1))); + if (!rlist) { + DEBUG(0,("str_list_make: Unable to allocate memory")); + str_list_free(&list); + SAFE_FREE(s); + return NULL; + } + else list = rlist; + memset (&list[num], 0, ((sizeof(char**)) * (S_LIST_ABS +1))); + } + + list[num] = strdup(tok); + if (!list[num]) { + DEBUG(0,("str_list_make: Unable to allocate memory")); + str_list_free(&list); + SAFE_FREE(s); + return NULL; + } + + num++; + } + + SAFE_FREE(s); + return list; +} + +BOOL str_list_copy(char ***dest, char **src) +{ + char **list, **rlist; + int num, lsize; + + *dest = NULL; + if (!src) return False; + + num = lsize = 0; + list = NULL; + + while (src[num]) + { + if (num == lsize) { + lsize += S_LIST_ABS; + rlist = (char **)Realloc(list, ((sizeof(char **)) * (lsize +1))); + if (!rlist) { + DEBUG(0,("str_list_copy: Unable to allocate memory")); + str_list_free(&list); + return False; + } + else list = rlist; + memset (&list[num], 0, ((sizeof(char **)) * (S_LIST_ABS +1))); + } + + list[num] = strdup(src[num]); + if (!list[num]) { + DEBUG(0,("str_list_copy: Unable to allocate memory")); + str_list_free(&list); + return False; + } + + num++; + } + + *dest = list; + return True; +} + +/* return true if all the elemnts of the list matches exactly */ +BOOL str_list_compare(char **list1, char **list2) +{ + int num; + + if (!list1 || !list2) return (list1 == list2); + + for (num = 0; list1[num]; num++) { + if (!list2[num]) return False; + if (!strcsequal(list1[num], list2[num])) return False; + } + if (list2[num]) return False; /* if list2 has more elements than list1 fail */ + + return True; +} + +void str_list_free(char ***list) +{ + char **tlist; + + if (!list || !*list) return; + tlist = *list; + for(; *tlist; tlist++) SAFE_FREE(*tlist); + SAFE_FREE(*list); +} + +BOOL str_list_substitute(char **list, const char *pattern, const char *insert) +{ + char *p, *s, *t; + ssize_t ls, lp, li, ld, i, d; + + if (!list) return False; + if (!pattern) return False; + if (!insert) return False; + + lp = (ssize_t)strlen(pattern); + li = (ssize_t)strlen(insert); + ld = li -lp; + + while (*list) + { + s = *list; + ls = (ssize_t)strlen(s); + + while ((p = strstr(s, pattern))) + { + t = *list; + d = p -t; + if (ld) + { + t = (char *) malloc(ls +ld +1); + if (!t) { + DEBUG(0,("str_list_substitute: Unable to allocate memory")); + return False; + } + memcpy(t, *list, d); + memcpy(t +d +li, p +lp, ls -d -lp +1); + SAFE_FREE(*list); + *list = t; + ls += ld; + s = t +d +li; + } + + for (i = 0; i < li; i++) { + switch (insert[i]) { + case '`': + case '"': + case '\'': + case ';': + case '$': + case '%': + case '\r': + case '\n': + t[d +i] = '_'; + break; + default: + t[d +i] = insert[i]; + } + } + } + + list++; + } + + return True; +} diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index a1cff26169..ba02819bdc 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -775,3 +775,44 @@ int unistrcpy(uint16 *dst, uint16 *src) return num_wchars; } + +/** + * Samba ucs2 type to UNISTR2 conversion + * + * @param ctx Talloc context to create the dst strcture (if null) and the + * contents of the unicode string. + * @param dst UNISTR2 destination. If equals null, then it's allocated. + * @param src smb_ucs2_t source. + * @param max_len maximum number of unicode characters to copy. If equals + * null, then null-termination of src is taken + * + * @return copied UNISTR2 destination + **/ +UNISTR2* ucs2_to_unistr2(TALLOC_CTX *ctx, UNISTR2* dst, smb_ucs2_t* src) +{ + size_t len; + + if (!src) return NULL; + len = strlen_w(src); + + /* allocate UNISTR2 destination if not given */ + if (!dst) { + dst = (UNISTR2*) talloc(ctx, sizeof(UNISTR2)); + if (!dst) return NULL; + } + if (!dst->buffer) { + dst->buffer = (uint16*) talloc(ctx, sizeof(uint16) * (len + 1)); + if (!dst->buffer) return NULL; + } + + /* set UNISTR2 parameters */ + dst->uni_max_len = len + 1; + dst->undoc = 0; + dst->uni_str_len = len; + + /* copy the actual unicode string */ + strncpy_w(dst->buffer, src, dst->uni_max_len); + + return dst; +}; + diff --git a/source3/lib/wins_srv.c b/source3/lib/wins_srv.c index 0181ee0956..adf405ae7e 100644 --- a/source3/lib/wins_srv.c +++ b/source3/lib/wins_srv.c @@ -1,7 +1,7 @@ /* Unix SMB/CIFS implementation. - Samba utility functions - Copyright (C) Andrew Tridgell 1992-1998 + Samba wins server helper functions + Copyright (C) Andrew Tridgell 1992-2002 Copyright (C) Christopher R. Hertel 2000 This program is free software; you can redistribute it and/or modify @@ -21,319 +21,330 @@ #include "includes.h" -/* -------------------------------------------------------------------------- ** - * Discussion... - * - * This module implements WINS failover. - * - * Microsoft's WINS servers provide a feature called WINS replication, - * which synchronises the WINS name databases between two or more servers. - * This means that the two servers can be used interchangably (more or - * less). WINS replication is particularly useful if you are trying to - * synchronise the WINS namespace between servers in remote locations, or - * if your WINS servers tend to crash a lot. - * - * WINS failover allows the client to 'switch' to a different WINS server - * if the current WINS server mysteriously disappears. On Windows - * systems, this is typically represented as 'primary' and 'secondary' - * WINS servers. - * - * Failover only works if the WINS servers are synced. If they are not, - * then - * a) if the primary WINS server never fails the client will never 'see' - * the secondary (or tertiary or...) WINS server name space. - * b) if the primary *does* fail, the client will be entering an - * unfamiliar namespace. The client itself will not be registered in - * that namespace and any names which match names in the previous - * space will likely resolve to different host IP addresses. - * - * One key thing to remember regarding WINS failover is that Samba does - * not (yet) implement WINS replication. For those interested, sniff port - * 42 (TCP? UDP? ...dunno off hand) and see what two MS WINS servers do. - * - * At this stage, only failover is implemented. The next thing is to add - * support for multi-WINS server registration and query (multi-membership). - * - * Multi-membership is a little wierd. The idea is that the client can - * register itself with multiple non-replicated WINS servers, and query - * all of those servers (in a prescribed sequence) to resolve a name. - * - * The implications of multi-membership are not quite clear. Worth - * trying, I suppose. Changes will be needed in the name query and - * registration code to accomodate this feature. Also, there will need to - * be some sort of syntax extension for the 'wins server' parameter in - * smb.conf. I'm thinking that a colon could be used as a separator. - * - * Of course, for each WINS namespace there might be multiple, synced WINS - * servers. The change to this module would likely be the addition of a - * linked list of linked lists. - * - * crh@samba.org - */ - -/* -------------------------------------------------------------------------- ** - * Defines... - * - * NECROMANCYCLE - The dead server retry period, in seconds. When a WINS - * server is declared dead, wait this many seconds before - * attempting to communicate with it. - */ - -#define NECROMANCYCLE 600 /* 600 seconds == 10 minutes. */ - -/* -------------------------------------------------------------------------- ** - * Typedefs... - */ - -typedef struct - { - ubi_slNode node; /* Linked list node structure. */ - time_t mourning; /* If > current time then server is dead, Jim. */ - char *server; /* DNS name or IP of NBNS server to query. */ - struct in_addr ip_addr; /* Cache translated IP. */ - } list_entry; - -/* -------------------------------------------------------------------------- ** - * Private, static variables. - */ - -static ubi_slNewList( wins_srv_list ); - -/* -------------------------------------------------------------------------- ** - * Functions... - */ - - -BOOL wins_srv_load_list( char *src ) - /* ------------------------------------------------------------------------ ** - * Create or recreate the linked list of failover WINS servers. - * - * Input: src - String of DNS names and/or IP addresses delimited by the - * characters listed in LIST_SEP (see include/local.h). - * - * Output: True if at least one name or IP could be parsed out of the - * list, else False. - * - * Notes: There is no syntax checking done on the names or IPs. We do - * check to see if the field is an IP, in which case we copy it - * to the ip_addr field of the entry. Don't bother to to a host - * name lookup on all names now. They're done as needed in - * wins_srv_ip(). - * ------------------------------------------------------------------------ ** - */ - { - list_entry *entry; - char *p = src; - pstring wins_id_bufr; - unsigned long count; - - /* Empty the list. */ - while( NULL != (entry =(list_entry *)ubi_slRemHead( wins_srv_list )) ) - { - SAFE_FREE( entry->server ); - SAFE_FREE( entry ); - } - (void)ubi_slInitList( wins_srv_list ); /* shouldn't be needed */ - - /* Parse out the DNS names or IP addresses of the WINS servers. */ - DEBUG( 4, ("wins_srv_load_list(): Building WINS server list:\n") ); - while( next_token( &p, wins_id_bufr, LIST_SEP, sizeof( wins_id_bufr ) ) ) - { - entry = (list_entry *)malloc( sizeof( list_entry ) ); - if( NULL == entry ) - { - DEBUG( 0, ("wins_srv_load_list(): malloc(list_entry) failed.\n") ); - } - else - { - entry->mourning = 0; - /* Create a copy of the server name and store it in the list. */ - if( NULL == (entry->server = strdup( wins_id_bufr )) ) - { - SAFE_FREE( entry ); - DEBUG( 0, - ("wins_srv_load_list(): strdup(\"%s\") failed.\n", wins_id_bufr) ); - } - else - { - /* Add server to list. - * If the server name was actually an IP address we will store that - * too. It it was a DNS name, we will wait until we need to use - * the WINS server before doing the DNS lookup. Since this may be - * a list, and since we will reload the list whenever smb.conf is - * reloaded, there's no point in trying to look up names until we - * need them. ...of course, once we do the lookup we will cache - * the result. See wins_srv_ip(). - */ - if( is_ipaddress( wins_id_bufr ) ) - entry->ip_addr = *interpret_addr2( wins_id_bufr ); - else - entry->ip_addr = *interpret_addr2( "0.0.0.0" ); - (void)ubi_slAddTail( wins_srv_list, entry ); - DEBUGADD( 4, ("%s,\n", wins_id_bufr) ); - } - } - } - - count = ubi_slCount( wins_srv_list ); - DEBUGADD( 4, - ( "%d WINS server%s listed.\n", (int)count, (1==count)?"":"s" ) ); - - return( (count > 0) ? True : False ); - } /* wins_srv_load_list */ - - -struct in_addr wins_srv_ip( void ) - /* ------------------------------------------------------------------------ ** - * Return the IP address of an NBNS (WINS) server thought to be active. - * - * Input: none. - * - * Output: An IP address in struct in_addr form. - * - * Notes: This function will return the IP address of the first available - * NBNS (WINS) server. The order of the list is determined in - * smb.conf. If all of the WINS servers have been marked 'dead' - * then the zero IP (0.0.0.0) is returned. The zero IP is not a - * valid Unicast address on any system. - * - * ------------------------------------------------------------------------ ** - */ - { - time_t now = time(NULL); - list_entry *entry = (list_entry *)ubi_slFirst( wins_srv_list ); - - while( NULL != entry ) - { - if( now >= entry->mourning ) /* Found a live one. */ - { - /* If we don't have the IP, look it up. */ - if( is_zero_ip( entry->ip_addr ) ) - entry->ip_addr = *interpret_addr2( entry->server ); - - /* If we still don't have the IP then kill it, else return it. */ - if( is_zero_ip( entry->ip_addr ) ) - entry->mourning = now + NECROMANCYCLE; - else - return( entry->ip_addr ); - } - entry = (list_entry *)ubi_slNext( entry ); - } - - /* If there are no live entries, return the zero IP. */ - return( *interpret_addr2( "0.0.0.0" ) ); - } /* wins_srv_ip */ - - -char *wins_srv_name( void ) - /* ------------------------------------------------------------------------ ** - * Return the name of first live WINS server in the list. - * - * Input: none. - * - * Output: A pointer to a string containing either the DNS name or IP - * address of the WINS server as given in the WINS SERVER - * parameter in smb.conf, or NULL if no (live) WINS servers are - * in the list. - * - * Notes: This function will return the name of the first available - * NBNS (WINS) server. The order of the list is determined in - * smb.conf. If all of the WINS servers have been marked 'dead' - * then NULL is returned. - * - * - This function does not verify that the name can be mapped to - * an IP address, or that the WINS server is running. - * - * ------------------------------------------------------------------------ ** - */ - { - time_t now = time(NULL); - list_entry *entry = (list_entry *)ubi_slFirst( wins_srv_list ); - - while( NULL != entry ) - { - if( now >= entry->mourning ) - return( entry->server ); /* Found a live one. */ - entry = (list_entry *)ubi_slNext( entry ); - } - - /* If there are no live entries, return NULL. */ - return( NULL ); - } /* wins_srv_name */ - - -void wins_srv_died( struct in_addr boothill_ip ) - /* ------------------------------------------------------------------------ ** - * Called to indicate that a specific WINS server has died. - * - * Input: boothill_ip - IP address of an NBNS (WINS) server that has - * failed. - * - * Notes: This function marks the record 'dead' for NECROMANCYCLE - * seconds. - * - * ------------------------------------------------------------------------ ** - */ - { - list_entry *entry; - - if( is_zero_ip( boothill_ip ) ) - { - DEBUG( 4, ("wins_srv_died(): Invalid request to mark zero IP down.\n") ); - return; - } - - entry = (list_entry *)ubi_slFirst( wins_srv_list ); - while( NULL != entry ) - { - /* Match based on IP. */ - if( ip_equal( boothill_ip, entry->ip_addr ) ) - { - entry->mourning = time(NULL) + NECROMANCYCLE; - entry->ip_addr.s_addr = 0; /* Force a re-lookup at re-birth. */ - DEBUG( 2, ( "wins_srv_died(): WINS server %s appears to be down.\n", - entry->server ) ); - return; - } - entry = (list_entry *)ubi_slNext( entry ); - } - - if( DEBUGLVL( 1 ) ) - { - dbgtext( "wins_srv_died(): Could not mark WINS server %s down.\n", - inet_ntoa( boothill_ip ) ); - dbgtext( "Address not found in server list.\n" ); - } - } /* wins_srv_died */ - - -unsigned long wins_srv_count( void ) - /* ------------------------------------------------------------------------ ** - * Return the total number of entries in the list, dead or alive. - * ------------------------------------------------------------------------ ** - */ - { - unsigned long count = ubi_slCount( wins_srv_list ); - - if( DEBUGLVL( 8 ) ) - { - list_entry *entry = (list_entry *)ubi_slFirst( wins_srv_list ); - time_t now = time(NULL); - - dbgtext( "wins_srv_count: WINS status: %ld servers.\n", count ); - while( NULL != entry ) - { - dbgtext( " %s <%s>: ", entry->server, inet_ntoa( entry->ip_addr ) ); - if( now >= entry->mourning ) - dbgtext( "alive\n" ); - else - dbgtext( "dead for %d more seconds\n", (int)(entry->mourning - now) ); - - entry = (list_entry *)ubi_slNext( entry ); - } - } - - return( count ); - } /* wins_srv_count */ - -/* ========================================================================== */ +/* + this is pretty much a complete rewrite of the earlier code. The main + aim of the rewrite is to add support for having multiple wins server + lists, so Samba can register with multiple groups of wins servers + and each group has a failover list of wins servers. + + Central to the way it all works is the idea of a wins server + 'tag'. A wins tag is a label for a group of wins servers. For + example if you use + + wins server = fred:192.168.2.10 mary:192.168.3.199 fred:192.168.2.61 + + then you would have two groups of wins servers, one tagged with the + name 'fred' and the other with the name 'mary'. I would usually + recommend using interface names instead of 'fred' and 'mary' but + they can be any alpha string. + + Now, how does it all work. Well, nmbd needs to register each of its + IPs with each of its names once with each group of wins servers. So + it tries registering with the first one mentioned in the list, then + if that fails it marks that WINS server dead and moves onto the next + one. + + In the client code things are a bit different. As each of the groups + of wins servers is a separate name space we need to try each of the + groups until we either succeed or we run out of wins servers to + try. If we get a negative response from a wins server then that + means the name doesn't exist in that group, so we give up on that + group and move to the next group. If we don't get a response at all + then maybe the wins server is down, in which case we need to + failover to the next one for that group. + + confused yet? (tridge) +*/ + + +/* how long a server is marked dead for */ +#define DEATH_TIME 600 + +/* a list of wins server that are marked dead from the point of view + of a given source address. We keep a separate dead list for each src address + to cope with multiple interfaces that are not routable to each other + */ +static struct wins_dead { + struct in_addr dest_ip; + struct in_addr src_ip; + time_t revival; /* when it will be revived */ + struct wins_dead *next, *prev; +} *dead_servers; + +/* an internal convenience structure for an IP with a short string tag + attached */ +struct tagged_ip { + fstring tag; + struct in_addr ip; +}; + +/* + see if an ip is on the dead list +*/ +BOOL wins_srv_is_dead(struct in_addr wins_ip, struct in_addr src_ip) +{ + struct wins_dead *d; + for (d=dead_servers; d; d=d->next) { + if (ip_equal(wins_ip, d->dest_ip) && ip_equal(src_ip, d->src_ip)) { + /* it might be due for revival */ + if (d->revival <= time(NULL)) { + fstring src_name; + fstrcpy(src_name, inet_ntoa(src_ip)); + DEBUG(4,("Reviving wins server %s for source %s\n", + inet_ntoa(wins_ip), src_name)); + DLIST_REMOVE(dead_servers, d); + free(d); + return False; + } + return True; + } + } + return False; +} + + +/* + mark a wins server as being alive (for the moment) +*/ +void wins_srv_alive(struct in_addr wins_ip, struct in_addr src_ip) +{ + struct wins_dead *d; + for (d=dead_servers; d; d=d->next) { + if (ip_equal(wins_ip, d->dest_ip) && ip_equal(src_ip, d->src_ip)) { + fstring src_name; + fstrcpy(src_name, inet_ntoa(src_ip)); + DEBUG(4,("Reviving wins server %s for source %s\n", + inet_ntoa(wins_ip), src_name)); + DLIST_REMOVE(dead_servers, d); + return; + } + } +} + + +/* + mark a wins server as temporarily dead +*/ +void wins_srv_died(struct in_addr wins_ip, struct in_addr src_ip) +{ + struct wins_dead *d; + fstring src_name; + + if (is_zero_ip(wins_ip) || wins_srv_is_dead(wins_ip, src_ip)) { + return; + } + + d = (struct wins_dead *)malloc(sizeof(*d)); + if (!d) return; + + d->dest_ip = wins_ip; + d->src_ip = src_ip; + d->revival = time(NULL) + DEATH_TIME; + + fstrcpy(src_name, inet_ntoa(src_ip)); + + DEBUG(4,("Marking wins server %s dead for %u seconds from source %s\n", + inet_ntoa(wins_ip), DEATH_TIME, src_name)); + + DLIST_ADD(dead_servers, d); +} + +/* + return the total number of wins servers, dead or not +*/ +unsigned wins_srv_count(void) +{ + char **list; + int count = 0; + + if (lp_wins_support()) { + /* simple - just talk to ourselves */ + return 1; + } + + list = lp_wins_server_list(); + for (count=0; list && list[count]; count++) /* nop */ ; + + return count; +} + +/* + parse an IP string that might be in tagged format + the result is a tagged_ip structure containing the tag + and the ip in in_addr format. If there is no tag then + use the tag '*' +*/ +static void parse_ip(struct tagged_ip *ip, const char *str) +{ + char *s = strchr(str, ':'); + if (!s) { + fstrcpy(ip->tag, "*"); + ip->ip = *interpret_addr2(str); + return; + } + + ip->ip = *interpret_addr2(s+1); + fstrcpy(ip->tag, str); + s = strchr(ip->tag, ':'); + if (s) *s = 0; +} + + + +/* + return the list of wins server tags. A 'tag' is used to distinguish + wins server as either belonging to the same name space or a separate + name space. Usually you would setup your 'wins server' option to + list one or more wins server per interface and use the interface + name as your tag, but you are free to use any tag you like. +*/ +char **wins_srv_tags(void) +{ + char **ret = NULL; + int count=0, i, j; + char **list; + + if (lp_wins_support()) { + /* give the caller something to chew on. This makes + the rest of the logic simpler (ie. less special cases) */ + ret = (char **)malloc(sizeof(char *)*2); + if (!ret) return NULL; + ret[0] = strdup("*"); + ret[1] = NULL; + return ret; + } + + list = lp_wins_server_list(); + if (!list) return NULL; + + /* yes, this is O(n^2) but n is very small */ + for (i=0;list[i];i++) { + struct tagged_ip t_ip; + + parse_ip(&t_ip, list[i]); + + /* see if we already have it */ + for (j=0;j<count;j++) { + if (strcmp(ret[j], t_ip.tag) == 0) { + break; + } + } + + if (j != count) { + /* we already have it. Move along */ + continue; + } + + /* add it to the list */ + ret = (char **)Realloc(ret, (count+1) * sizeof(char *)); + ret[count] = strdup(t_ip.tag); + if (!ret[count]) break; + count++; + } + + if (count) { + /* make sure we null terminate */ + ret[count] = NULL; + } + + return ret; +} + +/* free a list of wins server tags given by wins_srv_tags */ +void wins_srv_tags_free(char **list) +{ + int i; + if (!list) return; + for (i=0; list[i]; i++) { + free(list[i]); + } + free(list); +} + + +/* + return the IP of the currently active wins server for the given tag, + or the zero IP otherwise +*/ +struct in_addr wins_srv_ip_tag(const char *tag, struct in_addr src_ip) +{ + char **list; + int i; + struct tagged_ip t_ip; + + /* if we are a wins server then we always just talk to ourselves */ + if (lp_wins_support()) { + extern struct in_addr loopback_ip; + return loopback_ip; + } + + list = lp_wins_server_list(); + if (!list || !list[0]) { + struct in_addr ip; + zero_ip(&ip); + return ip; + } + + /* find the first live one for this tag */ + for (i=0; list[i]; i++) { + parse_ip(&t_ip, list[i]); + if (strcmp(tag, t_ip.tag) != 0) { + /* not for the right tag. Move along */ + continue; + } + if (!wins_srv_is_dead(t_ip.ip, src_ip)) { + fstring src_name; + fstrcpy(src_name, inet_ntoa(src_ip)); + DEBUG(6,("Current wins server for tag '%s' with source %s is %s\n", + tag, + src_name, + inet_ntoa(t_ip.ip))); + return t_ip.ip; + } + } + + /* they're all dead - try the first one until they revive */ + for (i=0; list[i]; i++) { + parse_ip(&t_ip, list[i]); + if (strcmp(tag, t_ip.tag) != 0) { + continue; + } + return t_ip.ip; + } + + /* this can't happen?? */ + zero_ip(&t_ip.ip); + return t_ip.ip; +} + + +/* + return a count of the number of IPs for a particular tag, including + dead ones +*/ +unsigned wins_srv_count_tag(const char *tag) +{ + char **list; + int i, count=0; + + /* if we are a wins server then we always just talk to ourselves */ + if (lp_wins_support()) { + return 1; + } + + list = lp_wins_server_list(); + if (!list || !list[0]) { + return 0; + } + + /* find the first live one for this tag */ + for (i=0; list[i]; i++) { + struct tagged_ip t_ip; + parse_ip(&t_ip, list[i]); + if (strcmp(tag, t_ip.tag) == 0) { + count++; + } + } + + return count; +} diff --git a/source3/lib/xfile.c b/source3/lib/xfile.c index 00ea6e5cac..903dfb1ae0 100644 --- a/source3/lib/xfile.c +++ b/source3/lib/xfile.c @@ -31,16 +31,16 @@ #include "includes.h" -static XFILE _x_stdin = { 0, NULL, NULL, 0, 0, O_RDONLY, X_IOFBF, 0 }; -static XFILE _x_stdout = { 1, NULL, NULL, 0, 0, O_WRONLY, X_IOLBF, 0 }; +#define XBUFSIZE BUFSIZ + +static XFILE _x_stdin = { 0, NULL, NULL, XBUFSIZE, 0, O_RDONLY, X_IOFBF, 0 }; +static XFILE _x_stdout = { 1, NULL, NULL, XBUFSIZE, 0, O_WRONLY, X_IOLBF, 0 }; static XFILE _x_stderr = { 2, NULL, NULL, 0, 0, O_WRONLY, X_IONBF, 0 }; XFILE *x_stdin = &_x_stdin; XFILE *x_stdout = &_x_stdout; XFILE *x_stderr = &_x_stderr; -#define XBUFSIZE BUFSIZ - #define X_FLAG_EOF 1 #define X_FLAG_ERROR 2 @@ -187,7 +187,11 @@ int x_vfprintf(XFILE *f, const char *format, va_list ap) { char *p; int len, ret; - len = vasprintf(&p, format, ap); + va_list ap2; + + VA_COPY(ap2, ap); + + len = vasprintf(&p, format, ap2); if (len <= 0) return len; ret = x_fwrite(p, 1, len, f); SAFE_FREE(p); |