r884: convert samba4 to use [u]int32_t instead of [u]int32
[bbaumbach/samba-autobuild/.git] / source4 / libads / util.c
1 /* 
2    Unix SMB/CIFS implementation.
3    krb5 set password implementation
4    Copyright (C) Remus Koos 2001 (remuskoos@yahoo.com)
5    
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 2 of the License, or
9    (at your option) any later version.
10    
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.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 #ifdef HAVE_KRB5
24
25 ADS_STATUS ads_change_trust_account_password(ADS_STRUCT *ads, char *host_principal)
26 {
27         char *tmp_password;
28         char *password;
29         char *new_password;
30         char *service_principal = NULL;
31         ADS_STATUS ret;
32         uint32_t sec_channel_type;
33
34         if ((password = secrets_fetch_machine_password(lp_workgroup(), NULL, &sec_channel_type)) == NULL) {
35                 DEBUG(1,("Failed to retrieve password for principal %s\n", host_principal));
36                 return ADS_ERROR_SYSTEM(ENOENT);
37         }
38
39         tmp_password = generate_random_str(DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
40         new_password = strdup(tmp_password);
41
42         asprintf(&service_principal, "HOST/%s", host_principal);
43
44         if (!service_principal) {
45                 DEBUG(1,("asprintf() failed principal %s\n", host_principal));
46                 return ADS_ERROR_SYSTEM(ENOMEM);
47         }
48
49         ret = kerberos_set_password(ads->auth.kdc_server, service_principal, password, service_principal, new_password, ads->auth.time_offset);
50
51         if (!ADS_ERR_OK(ret)) goto failed;
52
53         if (!secrets_store_machine_password(new_password, lp_workgroup(), sec_channel_type)) {
54                 DEBUG(1,("Failed to save machine password\n"));
55                 return ADS_ERROR_SYSTEM(EACCES);
56         }
57
58 failed:
59         SAFE_FREE(service_principal);
60         SAFE_FREE(new_password);
61
62         return ret;
63 }
64
65
66
67 #endif