Beispiel #1
0
static SMB_INO_T stream_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
{
	MD5_CTX ctx;
        unsigned char hash[16];
	SMB_INO_T result;
	char *upper_sname;

	DEBUG(10, ("stream_inode called for %lu/%lu [%s]\n",
		   (unsigned long)sbuf->st_ex_dev,
		   (unsigned long)sbuf->st_ex_ino, sname));

	upper_sname = talloc_strdup_upper(talloc_tos(), sname);
	SMB_ASSERT(upper_sname != NULL);

        MD5Init(&ctx);
        MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
		  sizeof(sbuf->st_ex_dev));
        MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
		  sizeof(sbuf->st_ex_ino));
        MD5Update(&ctx, (unsigned char *)upper_sname,
		  talloc_get_size(upper_sname)-1);
        MD5Final(hash, &ctx);

	TALLOC_FREE(upper_sname);

        /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
	memcpy(&result, hash, sizeof(result));

	DEBUG(10, ("stream_inode returns %lu\n", (unsigned long)result));

	return result;
}
Beispiel #2
0
static NTSTATUS libnet_dssync_lookup_nc(TALLOC_CTX *mem_ctx,
					struct dssync_context *ctx)
{
	NTSTATUS status;
	WERROR werr;
	int32_t level = 1;
	union drsuapi_DsNameRequest req;
	int32_t level_out;
	struct drsuapi_DsNameString names[1];
	union drsuapi_DsNameCtr ctr;

	names[0].str = talloc_asprintf(mem_ctx, "%s\\", ctx->domain_name);
	NT_STATUS_HAVE_NO_MEMORY(names[0].str);

	req.req1.codepage	= 1252; /* german */
	req.req1.language	= 0x00000407; /* german */
	req.req1.count		= 1;
	req.req1.names		= names;
	req.req1.format_flags	= DRSUAPI_DS_NAME_FLAG_NO_FLAGS;
	req.req1.format_offered	= DRSUAPI_DS_NAME_FORMAT_UNKNOWN;
	req.req1.format_desired	= DRSUAPI_DS_NAME_FORMAT_FQDN_1779;

	status = rpccli_drsuapi_DsCrackNames(ctx->cli, mem_ctx,
					     &ctx->bind_handle,
					     level,
					     &req,
					     &level_out,
					     &ctr,
					     &werr);
	if (!NT_STATUS_IS_OK(status)) {
		ctx->error_message = talloc_asprintf(ctx,
			"Failed to lookup DN for domain name: %s",
			get_friendly_werror_msg(werr));
		return status;
	}

	if (!W_ERROR_IS_OK(werr)) {
		return werror_to_ntstatus(werr);
	}

	if (ctr.ctr1->count != 1) {
		return NT_STATUS_UNSUCCESSFUL;
	}

	if (ctr.ctr1->array[0].status != DRSUAPI_DS_NAME_STATUS_OK) {
		return NT_STATUS_UNSUCCESSFUL;
	}

	ctx->nc_dn = talloc_strdup(mem_ctx, ctr.ctr1->array[0].result_name);
	NT_STATUS_HAVE_NO_MEMORY(ctx->nc_dn);

	if (!ctx->dns_domain_name) {
		ctx->dns_domain_name = talloc_strdup_upper(mem_ctx,
			ctr.ctr1->array[0].dns_domain_name);
		NT_STATUS_HAVE_NO_MEMORY(ctx->dns_domain_name);
	}

	return NT_STATUS_OK;
}
Beispiel #3
0
static bool lp_scan_idmap_found_domain(const char *string,
				       regmatch_t matches[],
				       void *private_data)
{
	bool ok = false;

	if (matches[1].rm_so == -1) {
		fprintf(stderr, "Found match, but no name - invalid idmap config");
		return false;
	}
	if (matches[1].rm_eo <= matches[1].rm_so) {
		fprintf(stderr, "Invalid match - invalid idmap config");
		return false;
	}

	{
		struct idmap_domains *d = private_data;
		struct idmap_config *c = &d->c[d->count];
		regoff_t len = matches[1].rm_eo - matches[1].rm_so;
		char domname[len + 1];

		if (d->count >= d->size) {
			return false;
		}

		memcpy(domname, string + matches[1].rm_so, len);
		domname[len] = '\0';

		c->domain_name = talloc_strdup_upper(d->c, domname);
		if (c->domain_name == NULL) {
			return false;
		}
		c->backend = talloc_strdup(d->c, lp_idmap_backend(domname));
		if (c->backend == NULL) {
			return false;
		}

		ok = lp_idmap_range(domname, &c->low, &c->high);
		if (!ok) {
			fprintf(stderr,
				"ERROR: Invalid idmap range for domain "
				"%s!\n\n",
				c->domain_name);
			return false;
		}

		d->count++;
	}

	return false; /* Keep scanning */
}
Beispiel #4
0
void stat_cache_delete(const char *name)
{
	char *lname = talloc_strdup_upper(talloc_tos(), name);

	if (!lname) {
		return;
	}
	DEBUG(10,("stat_cache_delete: deleting name [%s] -> %s\n",
			lname, name ));

	memcache_delete(smbd_memcache(), STAT_CACHE,
			data_blob_const(lname, talloc_get_size(lname)-1));
	TALLOC_FREE(lname);
}
Beispiel #5
0
NTSTATUS dbwrap_delete_bystring_upper(struct db_context *db, const char *key)
{
	char *key_upper;
	NTSTATUS status;

	key_upper = talloc_strdup_upper(talloc_tos(), key);
	if (key_upper == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	status = dbwrap_delete_bystring(db, key_upper);

	talloc_free(key_upper);
	return status;
}
Beispiel #6
0
NTSTATUS dbwrap_fetch_bystring_upper(struct db_context *db, TALLOC_CTX *mem_ctx,
				     const char *key, TDB_DATA *value)
{
	char *key_upper;
	NTSTATUS status;

	key_upper = talloc_strdup_upper(talloc_tos(), key);
	if (key_upper == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	status = dbwrap_fetch_bystring(db, mem_ctx, key_upper, value);

	talloc_free(key_upper);
	return status;
}
Beispiel #7
0
NTSTATUS dbwrap_store_bystring_upper(struct db_context *db, const char *key,
				     TDB_DATA data, int flags)
{
	char *key_upper;
	NTSTATUS status;

	key_upper = talloc_strdup_upper(talloc_tos(), key);
	if (key_upper == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	status = dbwrap_store_bystring(db, key_upper, data, flags);

	talloc_free(key_upper);
	return status;
}
Beispiel #8
0
TDB_DATA dbwrap_fetch_bystring_upper(struct db_context *db, TALLOC_CTX *mem_ctx,
				     const char *key)
{
	char *key_upper;
	TDB_DATA result;

	key_upper = talloc_strdup_upper(talloc_tos(), key);
	if (key_upper == NULL) {
		return make_tdb_data(NULL, 0);
	}

	result = dbwrap_fetch_bystring(db, mem_ctx, key_upper);

	talloc_free(key_upper);
	return result;
}
Beispiel #9
0
NTSTATUS gp_find_file(TALLOC_CTX *mem_ctx,
		      uint32_t flags,
		      const char *filename,
		      const char *suffix,
		      const char **filename_out)
{
	const char *tmp = NULL;
	struct stat sbuf;
	const char *path = NULL;

	if (flags & GPO_LIST_FLAG_MACHINE) {
		path = "Machine";
	} else {
		path = "User";
	}

	tmp = talloc_asprintf(mem_ctx, "%s/%s/%s", filename,
			      path, suffix);
	NT_STATUS_HAVE_NO_MEMORY(tmp);

	if (stat(tmp, &sbuf) == 0) {
		*filename_out = tmp;
		return NT_STATUS_OK;
	}

	path = talloc_strdup_upper(mem_ctx, path);
	NT_STATUS_HAVE_NO_MEMORY(path);

	tmp = talloc_asprintf(mem_ctx, "%s/%s/%s", filename,
			      path, suffix);
	NT_STATUS_HAVE_NO_MEMORY(tmp);

	if (stat(tmp, &sbuf) == 0) {
		*filename_out = tmp;
		return NT_STATUS_OK;
	}

	return NT_STATUS_NO_SUCH_FILE;
}
Beispiel #10
0
static NTSTATUS process_dc_netbios(TALLOC_CTX *mem_ctx,
				   struct messaging_context *msg_ctx,
				   const char *domain_name,
				   uint32_t flags,
				   struct ip_service_name *dclist,
				   int num_dcs,
				   struct netr_DsRGetDCNameInfo **info)
{
	struct sockaddr_storage ss;
	struct ip_service ip_list;
	enum nbt_name_type name_type = NBT_NAME_LOGON;
	NTSTATUS status;
	int i;
	const char *dc_name = NULL;
	fstring tmp_dc_name;
	struct netlogon_samlogon_response *r = NULL;
	bool store_cache = false;
	uint32_t nt_version = NETLOGON_NT_VERSION_1 |
			      NETLOGON_NT_VERSION_5 |
			      NETLOGON_NT_VERSION_5EX_WITH_IP;

	if (!msg_ctx) {
		msg_ctx = msg_context(mem_ctx);
	}

	if (flags & DS_PDC_REQUIRED) {
		name_type = NBT_NAME_PDC;
	}

	nt_version |= map_ds_flags_to_nt_version(flags);

	DEBUG(10,("process_dc_netbios\n"));

	for (i=0; i<num_dcs; i++) {

		ip_list.ss = dclist[i].ss;
		ip_list.port = 0;

		if (!interpret_string_addr(&ss, dclist[i].hostname, AI_NUMERICHOST)) {
			return NT_STATUS_UNSUCCESSFUL;
		}

		if (send_getdc_request(mem_ctx, msg_ctx,
				       &dclist[i].ss, domain_name,
				       NULL, nt_version))
		{
			int k;
			smb_msleep(300);
			for (k=0; k<5; k++) {
				if (receive_getdc_response(mem_ctx,
							   &dclist[i].ss,
							   domain_name,
							   &nt_version,
							   &dc_name,
							   &r)) {
					store_cache = true;
					namecache_store(dc_name, NBT_NAME_SERVER, 1, &ip_list);
					goto make_reply;
				}
				smb_msleep(1500);
			}
		}

		if (name_status_find(domain_name,
				     name_type,
				     NBT_NAME_SERVER,
				     &dclist[i].ss,
				     tmp_dc_name))
		{
			struct NETLOGON_SAM_LOGON_RESPONSE_NT40 logon1;

			r = TALLOC_ZERO_P(mem_ctx, struct netlogon_samlogon_response);
			NT_STATUS_HAVE_NO_MEMORY(r);

			ZERO_STRUCT(logon1);

			nt_version = NETLOGON_NT_VERSION_1;

			logon1.nt_version = nt_version;
			logon1.server = tmp_dc_name;
			logon1.domain = talloc_strdup_upper(mem_ctx, domain_name);
			NT_STATUS_HAVE_NO_MEMORY(logon1.domain);

			r->data.nt4 = logon1;
			r->ntver = nt_version;

			map_netlogon_samlogon_response(r);

			namecache_store(tmp_dc_name, NBT_NAME_SERVER, 1, &ip_list);

			goto make_reply;
		}
	}

	return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;

 make_reply:

	status = make_dc_info_from_cldap_reply(mem_ctx, flags, &dclist[i].ss,
					       &r->data.nt5_ex, info);
	if (NT_STATUS_IS_OK(status) && store_cache) {
		return store_cldap_reply(mem_ctx, flags, &dclist[i].ss,
					 nt_version, &r->data.nt5_ex);
	}

	return status;
}
Beispiel #11
0
NTSTATUS ntlm_password_check(TALLOC_CTX *mem_ctx,
			     bool lanman_auth,
			     bool ntlm_auth,
			     uint32_t logon_parameters,
			     const DATA_BLOB *challenge,
			     const DATA_BLOB *lm_response,
			     const DATA_BLOB *nt_response,
			     const char *username, 
			     const char *client_username, 
			     const char *client_domain,
			     const struct samr_Password *stored_lanman, 
			     const struct samr_Password *stored_nt, 
			     DATA_BLOB *user_sess_key, 
			     DATA_BLOB *lm_sess_key)
{
	const static uint8_t zeros[8];
	DATA_BLOB tmp_sess_key;
	const char *upper_client_domain = NULL;

	if (client_domain != NULL) {
		upper_client_domain = talloc_strdup_upper(mem_ctx, client_domain);
		if (upper_client_domain == NULL) {
			return NT_STATUS_NO_MEMORY;
		}
	}

	if (stored_nt == NULL) {
		DEBUG(3,("ntlm_password_check: NO NT password stored for user %s.\n", 
			 username));
	}

	*lm_sess_key = data_blob(NULL, 0);
	*user_sess_key = data_blob(NULL, 0);

	/* Check for cleartext netlogon. Used by Exchange 5.5. */
	if ((logon_parameters & MSV1_0_CLEARTEXT_PASSWORD_ALLOWED)
	    && challenge->length == sizeof(zeros) 
	    && (memcmp(challenge->data, zeros, challenge->length) == 0 )) {
		struct samr_Password client_nt;
		struct samr_Password client_lm;
		char *unix_pw = NULL;
		bool lm_ok;
		size_t converted_size = 0;

		DEBUG(4,("ntlm_password_check: checking plaintext passwords for user %s\n",
			 username));
		mdfour(client_nt.hash, nt_response->data, nt_response->length);

		if (lm_response->length && 
		    (convert_string_talloc(mem_ctx, CH_DOS, CH_UNIX, 
					  lm_response->data, lm_response->length, 
					   (void *)&unix_pw, &converted_size))) {
			if (E_deshash(unix_pw, client_lm.hash)) {
				lm_ok = true;
			} else {
				lm_ok = false;
			}
		} else {
			lm_ok = false;
		}
		return hash_password_check(mem_ctx, 
					   lanman_auth,
					   lm_ok ? &client_lm : NULL, 
					   nt_response->length ? &client_nt : NULL, 
					   username,  
					   stored_lanman, stored_nt);
	}

	if (nt_response->length != 0 && nt_response->length < 24) {
		DEBUG(2,("ntlm_password_check: invalid NT password length (%lu) for user %s\n", 
			 (unsigned long)nt_response->length, username));		
	}

	if (nt_response->length > 24 && stored_nt) {
		/* We have the NT MD4 hash challenge available - see if we can
		   use it 
		*/
		DEBUG(4,("ntlm_password_check: Checking NTLMv2 password with domain [%s]\n",
			client_domain ? client_domain : "<NULL>"));
		if (smb_pwd_check_ntlmv2(mem_ctx,
					 nt_response, 
					 stored_nt->hash, challenge, 
					 client_username, 
					 client_domain,
					 user_sess_key)) {
			if (user_sess_key->length) {
				*lm_sess_key = data_blob_talloc(mem_ctx, user_sess_key->data, MIN(8, user_sess_key->length));
			}
			return NT_STATUS_OK;
		}

		DEBUG(4,("ntlm_password_check: Checking NTLMv2 password with uppercased version of domain [%s]\n",
			upper_client_domain ? upper_client_domain : "<NULL>"));
		if (smb_pwd_check_ntlmv2(mem_ctx,
					 nt_response, 
					 stored_nt->hash, challenge, 
					 client_username, 
					 upper_client_domain,
					 user_sess_key)) {
			if (user_sess_key->length) {
				*lm_sess_key = data_blob_talloc(mem_ctx, user_sess_key->data, MIN(8, user_sess_key->length));
			}
			return NT_STATUS_OK;
		}

		DEBUG(4,("ntlm_password_check: Checking NTLMv2 password without a domain\n"));
		if (smb_pwd_check_ntlmv2(mem_ctx,
					 nt_response, 
					 stored_nt->hash, challenge, 
					 client_username, 
					 "",
					 user_sess_key)) {
			if (user_sess_key->length) {
				*lm_sess_key = data_blob_talloc(mem_ctx, user_sess_key->data, MIN(8, user_sess_key->length));
			}
			return NT_STATUS_OK;
		} else {
			DEBUG(3,("ntlm_password_check: NTLMv2 password check failed\n"));
		}
	} else if (nt_response->length == 24 && stored_nt) {
		if (ntlm_auth) {		
			/* We have the NT MD4 hash challenge available - see if we can
			   use it (ie. does it exist in the smbpasswd file).
			*/
			DEBUG(4,("ntlm_password_check: Checking NT MD4 password\n"));
			if (smb_pwd_check_ntlmv1(mem_ctx, 
						 nt_response, 
						 stored_nt->hash, challenge,
						 user_sess_key)) {
				/* The LM session key for this response is not very secure, 
				   so use it only if we otherwise allow LM authentication */

				if (lanman_auth && stored_lanman) {
					*lm_sess_key = data_blob_talloc(mem_ctx, stored_lanman->hash, MIN(8, user_sess_key->length));
				}
				return NT_STATUS_OK;
			} else {
				DEBUG(3,("ntlm_password_check: NT MD4 password check failed for user %s\n",
					 username));
				return NT_STATUS_WRONG_PASSWORD;
			}
		} else {
			DEBUG(2,("ntlm_password_check: NTLMv1 passwords NOT PERMITTED for user %s\n",
				 username));			
			/* no return, because we might pick up LMv2 in the LM field */
		}
	}

	if (lm_response->length == 0) {
		DEBUG(3,("ntlm_password_check: NEITHER LanMan nor NT password supplied for user %s\n",
			 username));
		return NT_STATUS_WRONG_PASSWORD;
	}

	if (lm_response->length < 24) {
		DEBUG(2,("ntlm_password_check: invalid LanMan password length (%lu) for user %s\n", 
			 (unsigned long)nt_response->length, username));		
		return NT_STATUS_WRONG_PASSWORD;
	}

	if (!lanman_auth) {
		DEBUG(3,("ntlm_password_check: Lanman passwords NOT PERMITTED for user %s\n",
			 username));
	} else if (!stored_lanman) {
		DEBUG(3,("ntlm_password_check: NO LanMan password set for user %s (and no NT password supplied)\n",
			 username));
	} else if (strchr_m(username, '@')) {
		DEBUG(3,("ntlm_password_check: NO LanMan password allowed for username@realm logins (user: %s)\n",
			 username));
	} else {
		DEBUG(4,("ntlm_password_check: Checking LM password\n"));
		if (smb_pwd_check_ntlmv1(mem_ctx,
					 lm_response, 
					 stored_lanman->hash, challenge,
					 NULL)) {
			/* The session key for this response is still very odd.  
			   It not very secure, so use it only if we otherwise 
			   allow LM authentication */

			if (lanman_auth && stored_lanman) {
				uint8_t first_8_lm_hash[16];
				memcpy(first_8_lm_hash, stored_lanman->hash, 8);
				memset(first_8_lm_hash + 8, '\0', 8);
				*user_sess_key = data_blob_talloc(mem_ctx, first_8_lm_hash, 16);
				*lm_sess_key = data_blob_talloc(mem_ctx, stored_lanman->hash, 8);
			}
			return NT_STATUS_OK;
		}
	}

	if (!stored_nt) {
		DEBUG(4,("ntlm_password_check: LM password check failed for user, no NT password %s\n",username));
		return NT_STATUS_WRONG_PASSWORD;
	}

	/* This is for 'LMv2' authentication.  almost NTLMv2 but limited to 24 bytes.
	   - related to Win9X, legacy NAS pass-though authentication
	*/
	DEBUG(4,("ntlm_password_check: Checking LMv2 password with domain %s\n",
		client_domain ? client_domain : "<NULL>"));
	if (smb_pwd_check_ntlmv2(mem_ctx,
				 lm_response, 
				 stored_nt->hash, challenge, 
				 client_username,
				 client_domain,
				 &tmp_sess_key)) {
		if (nt_response->length > 24) {
			/* If NTLMv2 authentication has preceded us
			 * (even if it failed), then use the session
			 * key from that.  See the RPC-SAMLOGON
			 * torture test */
			smb_sess_key_ntlmv2(mem_ctx,
					    nt_response, 
					    stored_nt->hash, challenge, 
					    client_username,
					    client_domain,
					    user_sess_key);
		} else {
			/* Otherwise, use the LMv2 session key */
			*user_sess_key = tmp_sess_key;
		}
		if (user_sess_key->length) {
			*lm_sess_key = data_blob_talloc(mem_ctx, user_sess_key->data, MIN(8, user_sess_key->length));
		}
		return NT_STATUS_OK;
	}

	DEBUG(4,("ntlm_password_check: Checking LMv2 password with upper-cased version of domain %s\n",
		upper_client_domain ? upper_client_domain : "<NULL>"));
	if (smb_pwd_check_ntlmv2(mem_ctx,
				 lm_response, 
				 stored_nt->hash, challenge, 
				 client_username,
				 upper_client_domain,
				 &tmp_sess_key)) {
		if (nt_response->length > 24) {
			/* If NTLMv2 authentication has preceded us
			 * (even if it failed), then use the session
			 * key from that.  See the RPC-SAMLOGON
			 * torture test */
			smb_sess_key_ntlmv2(mem_ctx,
					    nt_response, 
					    stored_nt->hash, challenge, 
					    client_username,
					    upper_client_domain,
					    user_sess_key);
		} else {
			/* Otherwise, use the LMv2 session key */
			*user_sess_key = tmp_sess_key;
		}
		if (user_sess_key->length) {
			*lm_sess_key = data_blob_talloc(mem_ctx, user_sess_key->data, MIN(8, user_sess_key->length));
		}
		return NT_STATUS_OK;
	}

	DEBUG(4,("ntlm_password_check: Checking LMv2 password without a domain\n"));
	if (smb_pwd_check_ntlmv2(mem_ctx,
				 lm_response, 
				 stored_nt->hash, challenge, 
				 client_username,
				 "",
				 &tmp_sess_key)) {
		if (nt_response->length > 24) {
			/* If NTLMv2 authentication has preceded us
			 * (even if it failed), then use the session
			 * key from that.  See the RPC-SAMLOGON
			 * torture test */
			smb_sess_key_ntlmv2(mem_ctx,
					    nt_response, 
					    stored_nt->hash, challenge, 
					    client_username,
					    "",
					    user_sess_key);
		} else {
			/* Otherwise, use the LMv2 session key */
			*user_sess_key = tmp_sess_key;
		}
		if (user_sess_key->length) {
			*lm_sess_key = data_blob_talloc(mem_ctx, user_sess_key->data, MIN(8, user_sess_key->length));
		}
		return NT_STATUS_OK;
	}

	/* Apparently NT accepts NT responses in the LM field
	   - I think this is related to Win9X pass-though authentication
	*/
	DEBUG(4,("ntlm_password_check: Checking NT MD4 password in LM field\n"));
	if (ntlm_auth) {
		if (smb_pwd_check_ntlmv1(mem_ctx, 
					 lm_response, 
					 stored_nt->hash, challenge,
					 NULL)) {
			/* The session key for this response is still very odd.  
			   It not very secure, so use it only if we otherwise 
			   allow LM authentication */

			if (lanman_auth && stored_lanman) {
				uint8_t first_8_lm_hash[16];
				memcpy(first_8_lm_hash, stored_lanman->hash, 8);
				memset(first_8_lm_hash + 8, '\0', 8);
				*user_sess_key = data_blob_talloc(mem_ctx, first_8_lm_hash, 16);
				*lm_sess_key = data_blob_talloc(mem_ctx, stored_lanman->hash, 8);
			}
			return NT_STATUS_OK;
		}
		DEBUG(3,("ntlm_password_check: LM password, NT MD4 password in LM field and LMv2 failed for user %s\n",username));
	} else {
		DEBUG(3,("ntlm_password_check: LM password and LMv2 failed for user %s, and NT MD4 password in LM field not permitted\n",username));
	}

	/* Try and match error codes */
	if (strchr_m(username, '@')) {
		return NT_STATUS_NOT_FOUND;
	}
	return NT_STATUS_WRONG_PASSWORD;
}
Beispiel #12
0
void stat_cache_add( const char *full_orig_name,
		char *translated_path,
		bool case_sensitive)
{
	size_t translated_path_length;
	char *original_path;
	size_t original_path_length;
	char saved_char;
	TALLOC_CTX *ctx = talloc_tos();

	if (!lp_stat_cache()) {
		return;
	}

	/*
	 * Don't cache trivial valid directory entries such as . and ..
	 */

	if ((*full_orig_name == '\0')
	    || ISDOT(full_orig_name) || ISDOTDOT(full_orig_name)) {
		return;
	}

	/*
	 * If we are in case insentive mode, we don't need to
	 * store names that need no translation - else, it
	 * would be a waste.
	 */

	if (case_sensitive && (strcmp(full_orig_name, translated_path) == 0)) {
		return;
	}

	/*
	 * Remove any trailing '/' characters from the
	 * translated path.
	 */

	translated_path_length = strlen(translated_path);

	if(translated_path[translated_path_length-1] == '/') {
		translated_path_length--;
	}

	if(case_sensitive) {
		original_path = talloc_strdup(ctx,full_orig_name);
	} else {
		original_path = talloc_strdup_upper(ctx,full_orig_name);
	}

	if (!original_path) {
		return;
	}

	original_path_length = strlen(original_path);

	if(original_path[original_path_length-1] == '/') {
		original_path[original_path_length-1] = '\0';
		original_path_length--;
	}

	if (original_path_length != translated_path_length) {
		if (original_path_length < translated_path_length) {
			DEBUG(0, ("OOPS - tried to store stat cache entry "
			"for weird length paths [%s] %lu and [%s] %lu)!\n",
				  original_path,
				  (unsigned long)original_path_length,
				  translated_path,
				  (unsigned long)translated_path_length));
			TALLOC_FREE(original_path);
			return;
		}

		/* we only want to index by the first part of original_path,
			up to the length of translated_path */

		original_path[translated_path_length] = '\0';
		original_path_length = translated_path_length;
	}

	/* Ensure we're null terminated. */
	saved_char = translated_path[translated_path_length];
	translated_path[translated_path_length] = '\0';

	/*
	 * New entry or replace old entry.
	 */

	memcache_add(
		smbd_memcache(), STAT_CACHE,
		data_blob_const(original_path, original_path_length),
		data_blob_const(translated_path, translated_path_length + 1));

	DEBUG(5,("stat_cache_add: Added entry (%lx:size %x) %s -> %s\n",
		 (unsigned long)translated_path,
		 (unsigned int)translated_path_length,
		 original_path,
		 translated_path));

	translated_path[translated_path_length] = saved_char;
	TALLOC_FREE(original_path);
}
Beispiel #13
0
bool stat_cache_lookup(connection_struct *conn,
			bool posix_paths,
			char **pp_name,
			char **pp_dirpath,
			char **pp_start,
			SMB_STRUCT_STAT *pst)
{
	char *chk_name;
	size_t namelen;
	bool sizechanged = False;
	unsigned int num_components = 0;
	char *translated_path;
	size_t translated_path_length;
	DATA_BLOB data_val;
	char *name;
	TALLOC_CTX *ctx = talloc_tos();
	struct smb_filename smb_fname;
	int ret;

	*pp_dirpath = NULL;
	*pp_start = *pp_name;

	if (!lp_stat_cache()) {
		return False;
	}

	name = *pp_name;
	namelen = strlen(name);

	DO_PROFILE_INC(statcache_lookups);

	/*
	 * Don't lookup trivial valid directory entries.
	 */
	if ((*name == '\0') || ISDOT(name) || ISDOTDOT(name)) {
		return False;
	}

	if (conn->case_sensitive) {
		chk_name = talloc_strdup(ctx,name);
		if (!chk_name) {
			DEBUG(0, ("stat_cache_lookup: strdup failed!\n"));
			return False;
		}

	} else {
		chk_name = talloc_strdup_upper(ctx,name);
		if (!chk_name) {
			DEBUG(0, ("stat_cache_lookup: talloc_strdup_upper failed!\n"));
			return False;
		}

		/*
		 * In some language encodings the length changes
		 * if we uppercase. We need to treat this differently
		 * below.
		 */
		if (strlen(chk_name) != namelen) {
			sizechanged = True;
		}
	}

	while (1) {
		char *sp;

		data_val = data_blob_null;

		if (memcache_lookup(
			    smbd_memcache(), STAT_CACHE,
			    data_blob_const(chk_name, strlen(chk_name)),
			    &data_val)) {
			break;
		}

		DEBUG(10,("stat_cache_lookup: lookup failed for name [%s]\n",
				chk_name ));
		/*
		 * Didn't find it - remove last component for next try.
		 */
		if (!(sp = strrchr_m(chk_name, '/'))) {
			/*
			 * We reached the end of the name - no match.
			 */
			DO_PROFILE_INC(statcache_misses);
			TALLOC_FREE(chk_name);
			return False;
		}

		*sp = '\0';

		/*
		 * Count the number of times we have done this, we'll
		 * need it when reconstructing the string.
		 */

		if (sizechanged) {
			num_components++;
		}

		if ((*chk_name == '\0')
		    || ISDOT(chk_name) || ISDOTDOT(chk_name)) {
			DO_PROFILE_INC(statcache_misses);
			TALLOC_FREE(chk_name);
			return False;
		}
	}

	translated_path = talloc_strdup(ctx,(char *)data_val.data);
	if (!translated_path) {
		smb_panic("talloc failed");
	}
	translated_path_length = data_val.length - 1;

	DEBUG(10,("stat_cache_lookup: lookup succeeded for name [%s] "
		  "-> [%s]\n", chk_name, translated_path ));
	DO_PROFILE_INC(statcache_hits);

	ZERO_STRUCT(smb_fname);
	smb_fname.base_name = translated_path;

	if (posix_paths) {
		ret = SMB_VFS_LSTAT(conn, &smb_fname);
	} else {
		ret = SMB_VFS_STAT(conn, &smb_fname);
	}

	if (ret != 0) {
		/* Discard this entry - it doesn't exist in the filesystem. */
		memcache_delete(smbd_memcache(), STAT_CACHE,
				data_blob_const(chk_name, strlen(chk_name)));
		TALLOC_FREE(chk_name);
		TALLOC_FREE(translated_path);
		return False;
	}
	*pst = smb_fname.st;

	if (!sizechanged) {
		memcpy(*pp_name, translated_path,
		       MIN(namelen, translated_path_length));
	} else {
		if (num_components == 0) {
			name = talloc_strndup(ctx, translated_path,
					   translated_path_length);
		} else {
			char *sp;

			sp = strnrchr_m(name, '/', num_components);
			if (sp) {
				name = talloc_asprintf(ctx,"%.*s%s",
					 (int)translated_path_length,
					 translated_path, sp);
			} else {
				name = talloc_strndup(ctx,
						translated_path,
						translated_path_length);
			}
		}
		if (name == NULL) {
			/*
			 * TODO: Get us out of here with a real error message
			 */
			smb_panic("talloc failed");
		}
		TALLOC_FREE(*pp_name);
		*pp_name = name;
	}


	/* set pointer for 'where to start' on fixing the rest of the name */
	*pp_start = &name[translated_path_length];
	if (**pp_start == '/') {
		++*pp_start;
	}

	*pp_dirpath = translated_path;
	TALLOC_FREE(chk_name);
	return (namelen == translated_path_length);
}