source: trunk/server/source3/libnet/libnet_samsync_keytab.c@ 745

Last change on this file since 745 was 745, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.0

File size: 8.1 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 dump the remote SAM using rpc samsync operations
4
5 Copyright (C) Guenther Deschner 2008.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21#include "includes.h"
22#include "smb_krb5.h"
23#include "ads.h"
24#include "libnet/libnet_keytab.h"
25#include "libnet/libnet_samsync.h"
26#include "krb5_env.h"
27
28#if defined(HAVE_ADS)
29
30/****************************************************************
31****************************************************************/
32
33static NTSTATUS keytab_ad_connect(TALLOC_CTX *mem_ctx,
34 const char *domain_name,
35 const char *dc,
36 const char *username,
37 const char *password,
38 struct libnet_keytab_context *ctx)
39{
40 ADS_STATUS ad_status;
41 ADS_STRUCT *ads;
42
43 ads = ads_init(NULL, domain_name, dc);
44 NT_STATUS_HAVE_NO_MEMORY(ads);
45
46 if (getenv(KRB5_ENV_CCNAME) == NULL) {
47 setenv(KRB5_ENV_CCNAME, "MEMORY:libnet_samsync_keytab", 1);
48 }
49
50 ads->auth.user_name = SMB_STRDUP(username);
51 ads->auth.password = SMB_STRDUP(password);
52
53 ad_status = ads_connect_user_creds(ads);
54 if (!ADS_ERR_OK(ad_status)) {
55 return NT_STATUS_UNSUCCESSFUL;
56 }
57
58 ctx->ads = ads;
59
60 ctx->dns_domain_name = talloc_strdup_upper(mem_ctx, ads->config.realm);
61 NT_STATUS_HAVE_NO_MEMORY(ctx->dns_domain_name);
62
63 return NT_STATUS_OK;
64}
65
66/****************************************************************
67****************************************************************/
68
69static NTSTATUS fetch_sam_entry_keytab(TALLOC_CTX *mem_ctx,
70 enum netr_SamDatabaseID database_id,
71 uint32_t rid,
72 struct netr_DELTA_USER *r,
73 struct libnet_keytab_context *ctx)
74{
75 NTSTATUS status;
76 uint32_t kvno = 0;
77 DATA_BLOB blob;
78
79 if (memcmp(r->ntpassword.hash, ctx->zero_buf, 16) == 0) {
80 return NT_STATUS_OK;
81 }
82
83 kvno = ads_get_kvno(ctx->ads, r->account_name.string);
84 blob = data_blob_const(r->ntpassword.hash, 16);
85
86 status = libnet_keytab_add_to_keytab_entries(mem_ctx, ctx,
87 kvno,
88 r->account_name.string,
89 NULL,
90 ENCTYPE_ARCFOUR_HMAC,
91 blob);
92 if (!NT_STATUS_IS_OK(status)) {
93 return status;
94 }
95
96 return NT_STATUS_OK;
97}
98
99/****************************************************************
100****************************************************************/
101
102static NTSTATUS init_keytab(TALLOC_CTX *mem_ctx,
103 struct samsync_context *ctx,
104 enum netr_SamDatabaseID database_id,
105 uint64_t *sequence_num)
106{
107 krb5_error_code ret = 0;
108 NTSTATUS status;
109 struct libnet_keytab_context *keytab_ctx = NULL;
110 struct libnet_keytab_entry *entry;