2 * Copyright (c) 1997-2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static int inetd_flag
= -1;
38 static int version_flag
;
39 static int print_dump
;
40 static const char *database
;
41 static int from_stdin
;
42 static char *local_realm
;
43 static char *ktname
= NULL
;
45 struct getargs args
[] = {
46 { "database", 'd', arg_string
, rk_UNCONST(&database
), "database", "file" },
47 { "stdin", 'n', arg_flag
, &from_stdin
, "read from stdin" },
48 { "print", 0, arg_flag
, &print_dump
, "print dump to stdout" },
50 { "inetd", 'i', arg_negative_flag
, &inetd_flag
,
51 "Not started from inetd" },
53 { "keytab", 'k', arg_string
, &ktname
, "keytab to use for authentication", "keytab" },
54 { "realm", 'r', arg_string
, &local_realm
, "realm to use" },
55 { "version", 0, arg_flag
, &version_flag
, NULL
, NULL
},
56 { "help", 'h', arg_flag
, &help_flag
, NULL
, NULL
}
59 static int num_args
= sizeof(args
) / sizeof(args
[0]);
64 arg_printusage (args
, num_args
, NULL
, "");
69 main(int argc
, char **argv
)
73 krb5_auth_context ac
= NULL
;
74 krb5_principal c1
, c2
;
75 krb5_authenticator authent
;
77 krb5_socket_t sock
= rk_INVALID_SOCKET
;
81 krb5_log_facility
*fac
;
86 ret
= krb5_init_context(&context
);
90 ret
= krb5_openlog(context
, "hpropd", &fac
);
92 errx(1, "krb5_openlog");
93 krb5_set_warn_dest(context
, fac
);
95 if(getarg(args
, num_args
, argc
, argv
, &optidx
))
98 if(local_realm
!= NULL
)
99 krb5_set_default_realm(context
, local_realm
);
114 if (database
== NULL
)
115 database
= hdb_default_db(context
);
120 struct sockaddr_storage ss
;
121 struct sockaddr
*sa
= (struct sockaddr
*)&ss
;
122 socklen_t sin_len
= sizeof(ss
);
129 if (inetd_flag
== -1) {
130 if (getpeername (sock
, sa
, &sin_len
) < 0) {
140 mini_inetd (krb5_getportbyname (context
, "hprop", "tcp",
143 sin_len
= sizeof(ss
);
144 if(getpeername(sock
, sa
, &sin_len
) < 0)
145 krb5_err(context
, 1, errno
, "getpeername");
147 if (inet_ntop(sa
->sa_family
,
148 socket_get_address (sa
),
150 sizeof(addr_name
)) == NULL
)
151 strlcpy (addr_name
, "unknown address",
154 krb5_log(context
, fac
, 0, "Connection from %s", addr_name
);
156 ret
= krb5_kt_register(context
, &hdb_kt_ops
);
158 krb5_err(context
, 1, ret
, "krb5_kt_register");
160 if (ktname
!= NULL
) {
161 ret
= krb5_kt_resolve(context
, ktname
, &keytab
);
163 krb5_err (context
, 1, ret
, "krb5_kt_resolve %s", ktname
);
165 ret
= krb5_kt_default (context
, &keytab
);
167 krb5_err (context
, 1, ret
, "krb5_kt_default");
170 ret
= krb5_recvauth(context
, &ac
, &sock
, HPROP_VERSION
, NULL
,
173 krb5_err(context
, 1, ret
, "krb5_recvauth");
175 ret
= krb5_unparse_name(context
, ticket
->server
, &server
);
177 krb5_err(context
, 1, ret
, "krb5_unparse_name");
178 if (strncmp(server
, "hprop/", 5) != 0)
179 krb5_errx(context
, 1, "ticket not for hprop (%s)", server
);
182 krb5_free_ticket (context
, ticket
);
184 ret
= krb5_auth_con_getauthenticator(context
, ac
, &authent
);
186 krb5_err(context
, 1, ret
, "krb5_auth_con_getauthenticator");
188 ret
= krb5_make_principal(context
, &c1
, NULL
, "kadmin", "hprop", NULL
);
190 krb5_err(context
, 1, ret
, "krb5_make_principal");
191 _krb5_principalname2krb5_principal(context
, &c2
,
192 authent
->cname
, authent
->crealm
);
193 if(!krb5_principal_compare(context
, c1
, c2
)) {
195 ret
= krb5_unparse_name(context
, c2
, &s
);
197 s
= "unparseable name";
198 krb5_errx(context
, 1, "Unauthorized connection from %s", s
);
200 krb5_free_principal(context
, c1
);
201 krb5_free_principal(context
, c2
);
203 ret
= krb5_kt_close(context
, keytab
);
205 krb5_err(context
, 1, ret
, "krb5_kt_close");
209 asprintf(&tmp_db
, "%s~", database
);
211 ret
= hdb_create(context
, &db
, tmp_db
);
213 krb5_err(context
, 1, ret
, "hdb_create(%s)", tmp_db
);
214 ret
= db
->hdb_open(context
, db
, O_RDWR
| O_CREAT
| O_TRUNC
, 0600);
216 krb5_err(context
, 1, ret
, "hdb_open(%s)", tmp_db
);
225 ret
= krb5_read_message(context
, &sock
, &data
);
226 if(ret
!= 0 && ret
!= HEIM_ERR_EOF
)
227 krb5_err(context
, 1, ret
, "krb5_read_message");
229 ret
= krb5_read_priv_message(context
, ac
, &sock
, &data
);
231 krb5_err(context
, 1, ret
, "krb5_read_priv_message");
234 if(ret
== HEIM_ERR_EOF
|| data
.length
== 0) {
238 krb5_write_priv_message(context
, ac
, &sock
, &data
);
241 ret
= db
->hdb_close(context
, db
);
243 krb5_err(context
, 1, ret
, "db_close");
244 ret
= db
->hdb_rename(context
, db
, database
);
246 krb5_err(context
, 1, ret
, "db_rename");
250 memset(&entry
, 0, sizeof(entry
));
251 ret
= hdb_value2entry(context
, &data
, &entry
.entry
);
252 krb5_data_free(&data
);
254 krb5_err(context
, 1, ret
, "hdb_value2entry");
256 hdb_print_entry(context
, db
, &entry
, stdout
);
258 ret
= db
->hdb_store(context
, db
, 0, &entry
);
259 if(ret
== HDB_ERR_EXISTS
) {
261 ret
= krb5_unparse_name(context
, entry
.entry
.principal
, &s
);
263 s
= strdup("unparseable name");
264 krb5_warnx(context
, "Entry exists: %s", s
);
267 krb5_err(context
, 1, ret
, "db_store");
271 hdb_free_entry(context
, &entry
);
274 krb5_log(context
, fac
, 0, "Received %d principals", nprincs
);
277 rk_closesocket(sock
);