Fix more asprintf warnings and some error path errors.
[jra/samba/.git] / source3 / 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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21
22 #ifdef HAVE_KRB5
23
24 ADS_STATUS ads_change_trust_account_password(ADS_STRUCT *ads, char *host_principal)
25 {
26         char *password;
27         char *new_password;
28         ADS_STATUS ret;
29         uint32 sec_channel_type;
30     
31         if ((password = secrets_fetch_machine_password(lp_workgroup(), NULL, &sec_channel_type)) == NULL) {
32                 DEBUG(1,("Failed to retrieve password for principal %s\n", host_principal));
33                 return ADS_ERROR_SYSTEM(ENOENT);
34         }
35
36         new_password = generate_random_str(talloc_tos(), DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
37     
38         ret = kerberos_set_password(ads->auth.kdc_server, host_principal, password, host_principal, new_password, ads->auth.time_offset);
39
40         if (!ADS_ERR_OK(ret)) {
41                 goto failed;
42         }
43
44         if (!secrets_store_machine_password(new_password, lp_workgroup(), sec_channel_type)) {
45                 DEBUG(1,("Failed to save machine password\n"));
46                 ret = ADS_ERROR_SYSTEM(EACCES);
47                 goto failed;
48         }
49
50 failed:
51         SAFE_FREE(password);
52         return ret;
53 }
54
55 ADS_STATUS ads_guess_service_principal(ADS_STRUCT *ads,
56                                        char **returned_principal)
57 {
58         char *princ = NULL;
59
60         if (ads->server.realm && ads->server.ldap_server) {
61                 char *server, *server_realm;
62
63                 server = SMB_STRDUP(ads->server.ldap_server);
64                 server_realm = SMB_STRDUP(ads->server.realm);
65
66                 if (!server || !server_realm) {
67                         SAFE_FREE(server);
68                         SAFE_FREE(server_realm);
69                         return ADS_ERROR(LDAP_NO_MEMORY);
70                 }
71
72                 strlower_m(server);
73                 strupper_m(server_realm);
74                 if (asprintf(&princ, "ldap/%s@%s", server, server_realm) == -1) {
75                         SAFE_FREE(server);
76                         SAFE_FREE(server_realm);
77                         return ADS_ERROR(LDAP_NO_MEMORY);
78                 }
79
80                 SAFE_FREE(server);
81                 SAFE_FREE(server_realm);
82
83                 if (!princ) {
84                         return ADS_ERROR(LDAP_NO_MEMORY);
85                 }
86         } else if (ads->config.realm && ads->config.ldap_server_name) {
87                 char *server, *server_realm;
88
89                 server = SMB_STRDUP(ads->config.ldap_server_name);
90                 server_realm = SMB_STRDUP(ads->config.realm);
91
92                 if (!server || !server_realm) {
93                         SAFE_FREE(server);
94                         SAFE_FREE(server_realm);
95                         return ADS_ERROR(LDAP_NO_MEMORY);
96                 }
97
98                 strlower_m(server);
99                 strupper_m(server_realm);
100                 if (asprintf(&princ, "ldap/%s@%s", server, server_realm) == -1) {
101                         SAFE_FREE(server);
102                         SAFE_FREE(server_realm);
103                         return ADS_ERROR(LDAP_NO_MEMORY);
104                 }
105
106                 SAFE_FREE(server);
107                 SAFE_FREE(server_realm);
108
109                 if (!princ) {
110                         return ADS_ERROR(LDAP_NO_MEMORY);
111                 }
112         }
113
114         if (!princ) {
115                 return ADS_ERROR(LDAP_PARAM_ERROR);
116         }
117
118         *returned_principal = princ;
119
120         return ADS_SUCCESS;
121 }
122
123 #endif