s4/samba-tool/drs: Make use of Command.message() method instead of using 'print'
[Samba.git] / source3 / libads / util.c
blob44b4c78038e7f74a9558d3d10ac2463d2d93f86e
1 /*
2 Unix SMB/CIFS implementation.
3 krb5 set password implementation
4 Copyright (C) Remus Koos 2001 (remuskoos@yahoo.com)
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "ads.h"
22 #include "secrets.h"
24 #ifdef HAVE_KRB5
26 ADS_STATUS ads_change_trust_account_password(ADS_STRUCT *ads, char *host_principal)
28 char *password;
29 char *new_password;
30 ADS_STATUS ret;
31 enum netr_SchannelType sec_channel_type;
33 if ((password = secrets_fetch_machine_password(lp_workgroup(), NULL, &sec_channel_type)) == NULL) {
34 DEBUG(1,("Failed to retrieve password for principal %s\n", host_principal));
35 return ADS_ERROR_SYSTEM(ENOENT);
38 new_password = generate_random_str(talloc_tos(), DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
40 ret = kerberos_set_password(ads->auth.kdc_server, host_principal, password, host_principal, new_password, ads->auth.time_offset);
42 if (!ADS_ERR_OK(ret)) {
43 goto failed;
46 if (!secrets_store_machine_password(new_password, lp_workgroup(), sec_channel_type)) {
47 DEBUG(1,("Failed to save machine password\n"));
48 ret = ADS_ERROR_SYSTEM(EACCES);
49 goto failed;
52 failed:
53 SAFE_FREE(password);
54 return ret;
57 ADS_STATUS ads_guess_service_principal(ADS_STRUCT *ads,
58 char **returned_principal)
60 char *princ = NULL;
62 if (ads->server.realm && ads->server.ldap_server) {
63 char *server, *server_realm;
65 server = SMB_STRDUP(ads->server.ldap_server);
66 server_realm = SMB_STRDUP(ads->server.realm);
68 if (!server || !server_realm) {
69 SAFE_FREE(server);
70 SAFE_FREE(server_realm);
71 return ADS_ERROR(LDAP_NO_MEMORY);
74 strlower_m(server);
75 strupper_m(server_realm);
76 if (asprintf(&princ, "ldap/%s@%s", server, server_realm) == -1) {
77 SAFE_FREE(server);
78 SAFE_FREE(server_realm);
79 return ADS_ERROR(LDAP_NO_MEMORY);
82 SAFE_FREE(server);
83 SAFE_FREE(server_realm);
85 if (!princ) {
86 return ADS_ERROR(LDAP_NO_MEMORY);
88 } else if (ads->config.realm && ads->config.ldap_server_name) {
89 char *server, *server_realm;
91 server = SMB_STRDUP(ads->config.ldap_server_name);
92 server_realm = SMB_STRDUP(ads->config.realm);
94 if (!server || !server_realm) {
95 SAFE_FREE(server);
96 SAFE_FREE(server_realm);
97 return ADS_ERROR(LDAP_NO_MEMORY);
100 strlower_m(server);
101 strupper_m(server_realm);
102 if (asprintf(&princ, "ldap/%s@%s", server, server_realm) == -1) {
103 SAFE_FREE(server);
104 SAFE_FREE(server_realm);
105 return ADS_ERROR(LDAP_NO_MEMORY);
108 SAFE_FREE(server);
109 SAFE_FREE(server_realm);
111 if (!princ) {
112 return ADS_ERROR(LDAP_NO_MEMORY);
116 if (!princ) {
117 return ADS_ERROR(LDAP_PARAM_ERROR);
120 *returned_principal = princ;
122 return ADS_SUCCESS;
125 #endif