Patch from Nicholas Brealey <nick@brealey.org> to distinguish between WinXP and WinXP64.
[ira/wip.git] / source3 / lib / util.c
index f51761ad7a6364f00afb32f1806d8778219c4437..00062b82a9d257f45ead64768a256c8b6cd2cfc1 100644 (file)
@@ -57,9 +57,6 @@ extern unsigned int global_clobber_region_line;
 
 enum protocol_types Protocol = PROTOCOL_COREPLUS;
 
-/* a default finfo structure to ensure all fields are sensible */
-file_info def_finfo;
-
 /* this is used by the chaining code */
 int chain_size = 0;
 
@@ -186,11 +183,12 @@ void gfree_names(void)
        SAFE_FREE( smb_myworkgroup );
        SAFE_FREE( smb_scope );
        free_netbios_names_array();
+       free_local_machine_name();
 }
 
 void gfree_all( void )
 {
-       gfree_names();  
+       gfree_names();
        gfree_loadparm();
        gfree_case_tables();
        gfree_debugsyms();
@@ -282,6 +280,118 @@ bool init_names(void)
        return( True );
 }
 
+/**************************************************************************n
+  Code to cope with username/password auth options from the commandline.
+  Used mainly in client tools.
+****************************************************************************/
+
+static struct user_auth_info cmdline_auth_info = {
+       NULL,   /* username */
+       NULL,   /* password */
+       false,  /* got_pass */
+       false,  /* use_kerberos */
+       Undefined, /* signing state */
+       false   /* smb_encrypt */
+};
+
+const char *get_cmdline_auth_info_username(void)
+{
+       if (!cmdline_auth_info.username) {
+               return "";
+       }
+       return cmdline_auth_info.username;
+}
+
+void set_cmdline_auth_info_username(const char *username)
+{
+       SAFE_FREE(cmdline_auth_info.username);
+       cmdline_auth_info.username = SMB_STRDUP(username);
+       if (!cmdline_auth_info.username) {
+               exit(ENOMEM);
+       }
+}
+
+const char *get_cmdline_auth_info_password(void)
+{
+       if (!cmdline_auth_info.password) {
+               return "";
+       }
+       return cmdline_auth_info.password;
+}
+
+void set_cmdline_auth_info_password(const char *password)
+{
+       SAFE_FREE(cmdline_auth_info.password);
+       cmdline_auth_info.password = SMB_STRDUP(password);
+       if (!cmdline_auth_info.password) {
+               exit(ENOMEM);
+       }
+       cmdline_auth_info.got_pass = true;
+}
+
+bool set_cmdline_auth_info_signing_state(const char *arg)
+{
+       cmdline_auth_info.signing_state = -1;
+       if (strequal(arg, "off") || strequal(arg, "no") ||
+                       strequal(arg, "false")) {
+               cmdline_auth_info.signing_state = false;
+       } else if (strequal(arg, "on") || strequal(arg, "yes") ||
+                       strequal(arg, "true") || strequal(arg, "auto")) {
+               cmdline_auth_info.signing_state = true;
+       } else if (strequal(arg, "force") || strequal(arg, "required") ||
+                       strequal(arg, "forced")) {
+               cmdline_auth_info.signing_state = Required;
+       } else {
+               return false;
+       }
+       return true;
+}
+
+int get_cmdline_auth_info_signing_state(void)
+{
+       return cmdline_auth_info.signing_state;
+}
+
+bool get_cmdline_auth_info_use_kerberos(void)
+{
+       return cmdline_auth_info.use_kerberos;
+}
+
+/* This should only be used by lib/popt_common.c JRA */
+void set_cmdline_auth_info_use_krb5_ticket(void)
+{
+       cmdline_auth_info.use_kerberos = true;
+       cmdline_auth_info.got_pass = true;
+}
+
+/* This should only be used by lib/popt_common.c JRA */
+void set_cmdline_auth_info_smb_encrypt(void)
+{
+       cmdline_auth_info.smb_encrypt = true;
+}
+
+bool get_cmdline_auth_info_got_pass(void)
+{
+       return cmdline_auth_info.got_pass;
+}
+
+bool get_cmdline_auth_info_smb_encrypt(void)
+{
+       return cmdline_auth_info.smb_encrypt;
+}
+
+bool get_cmdline_auth_info_copy(struct user_auth_info *info)
+{
+       *info = cmdline_auth_info;
+       /* Now re-alloc the strings. */
+       info->username = SMB_STRDUP(get_cmdline_auth_info_username());
+       info->password = SMB_STRDUP(get_cmdline_auth_info_password());
+       if (!info->username || !info->password) {
+               return false;
+       }
+       return true;
+}
+
 /**************************************************************************n
  Find a suitable temporary directory. The result should be copied immediately
  as it may be overwritten by a subsequent call.
@@ -395,6 +505,19 @@ bool file_exist(const char *fname,SMB_STRUCT_STAT *sbuf)
        return((S_ISREG(sbuf->st_mode)) || (S_ISFIFO(sbuf->st_mode)));
 }
 
+/*******************************************************************
+ Check if a unix domain socket exists - call vfs_file_exist for samba files.
+********************************************************************/
+
+bool socket_exist(const char *fname)
+{
+       SMB_STRUCT_STAT st;
+       if (sys_stat(fname,&st) != 0) 
+               return(False);
+
+       return S_ISSOCK(st.st_mode);
+}
+
 /*******************************************************************
  Check a files mod time.
 ********************************************************************/
@@ -508,32 +631,30 @@ void show_msg(char *buf)
 }
 
 /*******************************************************************
- Set the length and marker of an smb packet.
+ Set the length and marker of an encrypted smb packet.
 ********************************************************************/
 
-void smb_setlen(char *buf,int len)
+void smb_set_enclen(char *buf,int len,uint16 enc_ctx_num)
 {
        _smb_setlen(buf,len);
 
        SCVAL(buf,4,0xFF);
-       SCVAL(buf,5,'S');
-       SCVAL(buf,6,'M');
-       SCVAL(buf,7,'B');
+       SCVAL(buf,5,'E');
+       SSVAL(buf,6,enc_ctx_num);
 }
 
 /*******************************************************************
- Setup the word count and byte count for a smb message.
+ Set the length and marker of an smb packet.
 ********************************************************************/
 
-int set_message(char *buf,int num_words,int num_bytes,bool zero)
+void smb_setlen(char *buf,int len)
 {
-       if (zero && (num_words || num_bytes)) {
-               memset(buf + smb_size,'\0',num_words*2 + num_bytes);
-       }
-       SCVAL(buf,smb_wct,num_words);
-       SSVAL(buf,smb_vwv + num_words*SIZEOFWORD,num_bytes);  
-       smb_setlen(buf,smb_size + num_words*2 + num_bytes - 4);
-       return (smb_size + num_words*2 + num_bytes);
+       _smb_setlen(buf,len);
+
+       SCVAL(buf,4,0xFF);
+       SCVAL(buf,5,'S');
+       SCVAL(buf,6,'M');
+       SCVAL(buf,7,'B');
 }
 
 /*******************************************************************
@@ -544,20 +665,10 @@ int set_message_bcc(char *buf,int num_bytes)
 {
        int num_words = CVAL(buf,smb_wct);
        SSVAL(buf,smb_vwv + num_words*SIZEOFWORD,num_bytes);
-       smb_setlen(buf,smb_size + num_words*2 + num_bytes - 4);
+       _smb_setlen(buf,smb_size + num_words*2 + num_bytes - 4);
        return (smb_size + num_words*2 + num_bytes);
 }
 
-/*******************************************************************
- Setup only the byte count for a smb message, using the end of the
- message as a marker.
-********************************************************************/
-
-int set_message_end(void *outbuf,void *end_ptr)
-{
-       return set_message_bcc((char *)outbuf,PTR_DIFF(end_ptr,smb_buf((char *)outbuf)));
-}
-
 /*******************************************************************
  Add a data blob to the end of a smb_buf, adjusting bcc and smb_len.
  Return the bytes added
@@ -692,21 +803,6 @@ char *clean_name(TALLOC_CTX *ctx, const char *s)
        return unix_clean_name(ctx, str);
 }
 
-/*******************************************************************
- Horrible temporary hack until pstring is dead.
-********************************************************************/
-
-char *pstring_clean_name(pstring s)
-{
-       char *str = clean_name(NULL,s);
-       if (!str) {
-               return NULL;
-       }
-       pstrcpy(s, str);
-       TALLOC_FREE(str);
-       return s;
-}
-
 /*******************************************************************
  Close the low 3 fd's and open dev/null in their place.
 ********************************************************************/
@@ -718,7 +814,7 @@ void close_low_fds(bool stderr_too)
        int i;
 
        close(0);
-       close(1); 
+       close(1);
 
        if (stderr_too)
                close(2);
@@ -814,67 +910,6 @@ int set_blocking(int fd, bool set)
 #undef FLAG_TO_SET
 }
 
-/****************************************************************************
- Transfer some data between two fd's.
-****************************************************************************/
-
-#ifndef TRANSFER_BUF_SIZE
-#define TRANSFER_BUF_SIZE 65536
-#endif
-
-ssize_t transfer_file_internal(int infd, int outfd, size_t n, ssize_t (*read_fn)(int, void *, size_t),
-                                               ssize_t (*write_fn)(int, const void *, size_t))
-{
-       char *buf;
-       size_t total = 0;
-       ssize_t read_ret;
-       ssize_t write_ret;
-       size_t num_to_read_thistime;
-       size_t num_written = 0;
-
-       if ((buf = SMB_MALLOC_ARRAY(char, TRANSFER_BUF_SIZE)) == NULL)
-               return -1;
-
-       while (total < n) {
-               num_to_read_thistime = MIN((n - total), TRANSFER_BUF_SIZE);
-
-               read_ret = (*read_fn)(infd, buf, num_to_read_thistime);
-               if (read_ret == -1) {
-                       DEBUG(0,("transfer_file_internal: read failure. Error = %s\n", strerror(errno) ));
-                       SAFE_FREE(buf);
-                       return -1;
-               }
-               if (read_ret == 0)
-                       break;
-
-               num_written = 0;
-               while (num_written < read_ret) {
-                       write_ret = (*write_fn)(outfd,buf + num_written, read_ret - num_written);
-                       if (write_ret == -1) {
-                               DEBUG(0,("transfer_file_internal: write failure. Error = %s\n", strerror(errno) ));
-                               SAFE_FREE(buf);
-                               return -1;
-                       }
-                       if (write_ret == 0)
-                               return (ssize_t)total;
-                       num_written += (size_t)write_ret;
-               }
-
-               total += (size_t)read_ret;
-       }
-
-       SAFE_FREE(buf);
-       return (ssize_t)total;          
-}
-
-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, sys_read, sys_write);
-}
-
 /*******************************************************************
  Sleep for a specified number of milliseconds.
 ********************************************************************/
@@ -1384,7 +1419,7 @@ char *automount_lookup(TALLOC_CTX *ctx, const char *user_name)
 
        if ((nis_error = yp_get_default_domain(&nis_domain)) != 0) {
                DEBUG(3, ("YP Error: %s\n", yperr_string(nis_error)));
-               return last_value;
+               return NULL;
        }
 
        DEBUG(5, ("NIS Domain: %s\n", nis_domain));
@@ -2022,7 +2057,7 @@ void ra_lanman_string( const char *native_lanman )
        if ( strcmp( native_lanman, "Windows 2002 5.1" ) == 0 )
                set_remote_arch( RA_WINXP );
        else if ( strcmp( native_lanman, "Windows XP 5.2" ) == 0 )
-               set_remote_arch( RA_WINXP );
+               set_remote_arch( RA_WINXP64 );
        else if ( strcmp( native_lanman, "Windows Server 2003 5.2" ) == 0 )
                set_remote_arch( RA_WIN2K3 );
 }
@@ -2063,6 +2098,9 @@ void set_remote_arch(enum remote_arch_types type)
        case RA_WINXP:
                remote_arch_str = "WinXP";
                break;
+       case RA_WINXP64:
+               remote_arch_str = "WinXP64";
+               break;
        case RA_WIN2K3:
                remote_arch_str = "Win2K3";
                break;
@@ -2129,9 +2167,9 @@ void dump_data(int level, const unsigned char *buf1,int len)
                n = MIN(8,i%16);
                print_asc(level,&buf[i-(i%16)],n); DEBUGADD(level,( " " ));
                n = (i%16) - n;
-               if (n>0) print_asc(level,&buf[i-n],n); 
-               DEBUGADD(level,("\n"));    
-       }       
+               if (n>0) print_asc(level,&buf[i-n],n);
+               DEBUGADD(level,("\n"));
+       }
 }
 
 void dump_data_pw(const char *msg, const uchar * data, size_t len)
@@ -2145,17 +2183,12 @@ void dump_data_pw(const char *msg, const uchar * data, size_t len)
 #endif
 }
 
-char *tab_depth(int depth)
+const char *tab_depth(int level, int depth)
 {
-       static pstring spaces;
-       size_t len = depth * 4;
-       if (len > sizeof(pstring)-1) {
-               len = sizeof(pstring)-1;
+       if( CHECK_DEBUGLVL(level) ) {
+               dbgtext("%*s", depth*4, "");
        }
-
-       memset(spaces, ' ', len);
-       spaces[len] = 0;
-       return spaces;
+       return "";
 }
 
 /*****************************************************************************
@@ -2172,7 +2205,7 @@ int str_checksum(const char *s)
        int res = 0;
        int c;
        int i=0;
-       
+
        while(*s) {
                c = *s;
                res ^= (c << (i % 15)) ^ (c >> (15-(i%15)));
@@ -2401,6 +2434,7 @@ char *smb_xstrndup(const char *s, size_t n)
        if (n == -1 || ! *ptr) {
                smb_panic("smb_xvasprintf: out of memory");
        }
+       va_end(ap2);
        return n;
 }
 
@@ -2482,12 +2516,12 @@ char *pid_path(const char *name)
  *
  * @param name File to find, relative to LIBDIR.
  *
- * @retval Pointer to a static #pstring containing the full path.
+ * @retval Pointer to a string containing the full path.
  **/
 
 char *lib_path(const char *name)
 {
-       return talloc_asprintf(talloc_tos(), "%s/%s", dyn_LIBDIR, name);
+       return talloc_asprintf(talloc_tos(), "%s/%s", get_dyn_LIBDIR(), name);
 }
 
 /**
@@ -2500,7 +2534,7 @@ char *lib_path(const char *name)
 
 char *data_path(const char *name)
 {
-       return talloc_asprintf(talloc_tos(), "%s/%s", dyn_CODEPAGEDIR, name);
+       return talloc_asprintf(talloc_tos(), "%s/%s", get_dyn_CODEPAGEDIR(), name);
 }
 
 /*****************************************************************
@@ -2509,18 +2543,18 @@ a useful function for returning a path in the Samba state directory
 
 char *state_path(const char *name)
 {
-       return xx_path(name, dyn_STATEDIR());
+       return xx_path(name, get_dyn_STATEDIR());
 }
 
 /**
  * @brief Returns the platform specific shared library extension.
  *
- * @retval Pointer to a static #fstring containing the extension.
+ * @retval Pointer to a const char * containing the extension.
  **/
 
 const char *shlib_ext(void)
 {
-       return dyn_SHLIBEXT;
+       return get_dyn_SHLIBEXT();
 }
 
 /*******************************************************************
@@ -3115,6 +3149,30 @@ int get_safe_IVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, i
        return IVAL(ptr,off);
 }
 
+/****************************************************************
+ Split DOM\user into DOM and user. Do not mix with winbind variants of that
+ call (they take care of winbind separator and other winbind specific settings).
+****************************************************************/
+
+void split_domain_user(TALLOC_CTX *mem_ctx,
+                      const char *full_name,
+                      char **domain,
+                      char **user)
+{
+       const char *p = NULL;
+
+       p = strchr_m(full_name, '\\');
+
+       if (p != NULL) {
+               *domain = talloc_strndup(mem_ctx, full_name,
+                                        PTR_DIFF(p, full_name));
+               *user = talloc_strdup(mem_ctx, p+1);
+       } else {
+               *domain = talloc_strdup(mem_ctx, "");
+               *user = talloc_strdup(mem_ctx, full_name);
+       }
+}
+
 #if 0
 
 Disable these now we have checked all code paths and ensured
@@ -3219,3 +3277,93 @@ void *talloc_zeronull(const void *context, size_t size, const char *name)
        return talloc_named_const(context, size, name);
 }
 #endif
+
+/* Split a path name into filename and stream name components. Canonicalise
+ * such that an implicit $DATA token is always explicit.
+ *
+ * The "specification" of this function can be found in the
+ * run_local_stream_name() function in torture.c, I've tried those
+ * combinations against a W2k3 server.
+ */
+
+NTSTATUS split_ntfs_stream_name(TALLOC_CTX *mem_ctx, const char *fname,
+                               char **pbase, char **pstream)
+{
+       char *base = NULL;
+       char *stream = NULL;
+       char *sname; /* stream name */
+       const char *stype; /* stream type */
+
+       DEBUG(10, ("split_ntfs_stream_name called for [%s]\n", fname));
+
+       sname = strchr_m(fname, ':');
+
+       if (lp_posix_pathnames() || (sname == NULL)) {
+               if (pbase != NULL) {
+                       base = talloc_strdup(mem_ctx, fname);
+                       NT_STATUS_HAVE_NO_MEMORY(base);
+               }
+               goto done;
+       }
+
+       if (pbase != NULL) {
+               base = talloc_strndup(mem_ctx, fname, PTR_DIFF(sname, fname));
+               NT_STATUS_HAVE_NO_MEMORY(base);
+       }
+
+       sname += 1;
+
+       stype = strchr_m(sname, ':');
+
+       if (stype == NULL) {
+               sname = talloc_strdup(mem_ctx, sname);
+               stype = "$DATA";
+       }
+       else {
+               if (StrCaseCmp(stype, ":$DATA") != 0) {
+                       /*
+                        * If there is an explicit stream type, so far we only
+                        * allow $DATA. Is there anything else allowed? -- vl
+                        */
+                       DEBUG(10, ("[%s] is an invalid stream type\n", stype));
+                       TALLOC_FREE(base);
+                       return NT_STATUS_OBJECT_NAME_INVALID;
+               }
+               sname = talloc_strndup(mem_ctx, sname, PTR_DIFF(stype, sname));
+               stype += 1;
+       }
+
+       if (sname == NULL) {
+               TALLOC_FREE(base);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       if (sname[0] == '\0') {
+               /*
+                * no stream name, so no stream
+                */
+               goto done;
+       }
+
+       if (pstream != NULL) {
+               stream = talloc_asprintf(mem_ctx, "%s:%s", sname, stype);
+               if (stream == NULL) {
+                       TALLOC_FREE(sname);
+                       TALLOC_FREE(base);
+                       return NT_STATUS_NO_MEMORY;
+               }
+               /*
+                * upper-case the type field
+                */
+               strupper_m(strchr_m(stream, ':')+1);
+       }
+
+ done:
+       if (pbase != NULL) {
+               *pbase = base;
+       }
+       if (pstream != NULL) {
+               *pstream = stream;
+       }
+       return NT_STATUS_OK;
+}