1/*
2 * Copyright (c) 2008-2010 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Portions Copyright (c) 2008-2010 Apple Inc. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * 3. Neither the name of the Institute nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include "heim.h"
37#include <string.h>
38
39mit_krb5_error_code KRB5_CALLCONV
40krb5_set_password_using_ccache(mit_krb5_context context,
41			       mit_krb5_ccache ccache,
42			       char *newpw,
43			       mit_krb5_principal change_password_for,
44			       int *result_code,
45			       mit_krb5_data *result_code_string,
46			       mit_krb5_data *result_string)
47{
48    krb5_error_code ret;
49    krb5_principal target = NULL;
50    krb5_data code_string, string;
51
52    LOG_ENTRY();
53
54    if (change_password_for) {
55	struct comb_principal *p;
56	p = (struct comb_principal *)change_password_for;
57	target = p->heim;
58    }
59
60    memset(&code_string, 0, sizeof(code_string));
61    memset(&string, 0, sizeof(string));
62
63    ret = heim_krb5_set_password_using_ccache(HC(context),
64					      (krb5_ccache)ccache,
65					      newpw,
66					      target,
67					      result_code,
68					      &code_string,
69					      &string);
70    if (ret) {
71	LOG_FAILURE(ret, "krb5_set_password_using_ccache");
72	return ret;
73    }
74
75    if (result_code_string)
76	mshim_hdata2mdata(&code_string, result_code_string);
77    else
78	heim_krb5_data_free(&code_string);
79
80    if (result_string)
81	mshim_hdata2mdata(&string, result_string);
82    else
83	heim_krb5_data_free(&string);
84
85    return 0;
86}
87
88mit_krb5_error_code KRB5_CALLCONV
89krb5_set_password(mit_krb5_context context,
90		  mit_krb5_creds *creds,
91		  char *newpw,
92		  mit_krb5_principal change_password_for,
93		  int *result_code,
94		  mit_krb5_data *result_code_string,
95		  mit_krb5_data *result_string)
96{
97    krb5_error_code ret;
98    krb5_principal target = NULL;
99    krb5_data code_string, string;
100    krb5_creds hcred;
101
102    LOG_ENTRY();
103
104    if (change_password_for) {
105	struct comb_principal *p;
106	p = (struct comb_principal *)change_password_for;
107	target = p->heim;
108    }
109
110    memset(&code_string, 0, sizeof(code_string));
111    memset(&string, 0, sizeof(string));
112
113    mshim_mcred2hcred(HC(context), creds, &hcred);
114
115    ret = heim_krb5_set_password(HC(context),
116				 &hcred,
117				 newpw,
118				 target,
119				 result_code,
120				 &code_string,
121				 &string);
122    heim_krb5_free_cred_contents(HC(context), &hcred);
123    if (ret) {
124	LOG_FAILURE(ret, "krb5_set_password");
125	return ret;
126    }
127
128    if (result_code_string)
129	mshim_hdata2mdata(&code_string, result_code_string);
130    else
131	heim_krb5_data_free(&code_string);
132
133    if (result_string)
134	mshim_hdata2mdata(&string, result_string);
135    else
136	heim_krb5_data_free(&string);
137
138    return 0;
139}
140
141