1/*
2 * Copyright (c) 1997-2008 Kungliga Tekniska H�gskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Portions Copyright (c) 2009 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 <stdio.h>
37#include <krb5.h>
38#include <kcm.h>
39
40
41static void
42add_cred(krb5_context context)
43{
44    krb5_error_code ret;
45    krb5_storage *request, *response;
46    krb5_data response_data;
47    krb5_data data;
48
49#if 0
50    char password[512];
51
52    if (UI_UTIL_read_pw_string(password, sizeof(password),
53			       "Password:", 0) != 1)
54       errx(1, "failed reading password");
55#endif
56
57    ret = krb5_kcm_storage_request(context, KCM_OP_ADD_NTLM_CRED, &request);
58    if (ret)
59	krb5_err(context, 1, ret, "krb5_kcm_storage_request");
60
61    krb5_store_stringz(request, "lha");
62    krb5_store_stringz(request, "BUILTIN");
63    data.data = "\xac\x8e\x65\x7f\x83\xdf\x82\xbe\xea\x5d\x43\xbd\xaf\x78\x0\xcc"; /* foo */
64    data.length = 16;
65    krb5_store_data(request, data);
66
67    ret = krb5_kcm_call(context, request, &response, &response_data);
68    if (ret)
69	krb5_err(context, 1, ret, "krb5_kcm_call");
70
71    krb5_storage_free(request);
72    krb5_storage_free(response);
73    krb5_data_free(&response_data);
74}
75
76
77static void
78list_cred(krb5_context context)
79{
80    krb5_error_code ret;
81    krb5_storage *request, *response;
82    krb5_data response_data;
83
84    ret = krb5_kcm_storage_request(context, KCM_OP_GET_NTLM_USER_LIST, &request);
85    if (ret)
86	krb5_err(context, 1, ret, "krb5_kcm_storage_request");
87
88    ret = krb5_kcm_call(context, request, &response, &response_data);
89    if (ret)
90	krb5_err(context, 1, ret, "krb5_kcm_call");
91
92    while (1) {
93	uint32_t morep;
94	char *user = NULL, *domain = NULL;
95
96	ret = krb5_ret_uint32(response, &morep);
97	if (ret)
98	    krb5_err(context, ret, 1, "ret: morep");
99
100	if (morep == 0)
101	    break;
102
103	ret = krb5_ret_stringz(response, &user);
104	if (ret)
105	    krb5_err(context, ret, 1, "ret: user");
106	ret = krb5_ret_stringz(response, &domain);
107	if (ret)
108	    krb5_err(context, ret, 1, "ret: domain");
109
110
111	printf("user: %s domain: %s\n", user, domain);
112    }
113
114    krb5_storage_free(request);
115    krb5_storage_free(response);
116    krb5_data_free(&response_data);
117}
118
119
120
121int
122main(int argc, char **argv)
123{
124    krb5_error_code ret;
125    krb5_context context;
126
127    ret = krb5_init_context(&context);
128    if (ret)
129	errx(1, "krb5_init_context");
130
131    list_cred(context);
132
133    add_cred(context);
134
135    krb5_free_context(context);
136
137    return 0;
138}
139