lib/roken: is_special_auxv_p test for AT_HWCAP2
[heimdal.git] / kdc / hpropd.c
blobfcd200dcf35a95f79617c85079bd5445e53d3898
1 /*
2 * Copyright (c) 1997-2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #include "hprop.h"
36 static int inetd_flag = -1;
37 static int help_flag;
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", NULL },
48 { "print", 0, arg_flag, &print_dump, "print dump to stdout", NULL },
49 #ifdef SUPPORT_INETD
50 { "inetd", 'i', arg_negative_flag, &inetd_flag,
51 "Not started from inetd", NULL },
52 #endif
53 { "keytab", 'k', arg_string, &ktname, "keytab to use for authentication", "keytab" },
54 { "realm", 'r', arg_string, &local_realm, "realm to use", NULL },
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]);
60 static char unparseable_name[] = "unparseable name";
62 static void
63 usage(int ret)
65 arg_printusage (args, num_args, NULL, "");
66 exit (ret);
69 int
70 main(int argc, char **argv)
72 krb5_error_code ret;
73 krb5_context context;
74 krb5_auth_context ac = NULL;
75 krb5_principal c1, c2;
76 krb5_authenticator authent;
77 krb5_keytab keytab;
78 krb5_socket_t sock = rk_INVALID_SOCKET;
79 HDB *db = NULL;
80 int optidx = 0;
81 char *tmp_db;
82 krb5_log_facility *fac;
83 int nprincs;
85 setprogname(argv[0]);
87 ret = krb5_init_context(&context);
88 if (ret)
89 exit(1);
91 ret = krb5_openlog(context, "hpropd", &fac);
92 if (ret)
93 errx(1, "krb5_openlog");
94 krb5_set_warn_dest(context, fac);
96 if (getarg(args, num_args, argc, argv, &optidx))
97 usage(1);
99 if (local_realm != NULL)
100 krb5_set_default_realm(context, local_realm);
102 if (help_flag)
103 usage(0);
104 if (version_flag) {
105 print_version(NULL);
106 exit(0);
109 argc -= optidx;
110 argv += optidx;
112 if (argc != 0)
113 usage(1);
115 if (database == NULL)
116 database = hdb_default_db(context);
118 if (from_stdin) {
119 sock = STDIN_FILENO;
120 } else {
121 struct sockaddr_storage ss;
122 struct sockaddr *sa = (struct sockaddr *)&ss;
123 socklen_t sin_len = sizeof(ss);
124 char addr_name[256];
125 krb5_ticket *ticket;
126 char *server;
128 sock = STDIN_FILENO;
129 #ifdef SUPPORT_INETD
130 if (inetd_flag == -1) {
131 if (getpeername (sock, sa, &sin_len) < 0) {
132 inetd_flag = 0;
133 } else {
134 inetd_flag = 1;
137 #else
138 inetd_flag = 0;
139 #endif
140 if (!inetd_flag) {
141 mini_inetd (krb5_getportbyname (context, "hprop", "tcp",
142 HPROP_PORT), &sock);
144 sin_len = sizeof(ss);
145 if (getpeername(sock, sa, &sin_len) < 0)
146 krb5_err(context, 1, errno, "getpeername");
148 if (inet_ntop(ss.ss_family,
149 socket_get_address (sa),
150 addr_name,
151 sizeof(addr_name)) == NULL)
152 strlcpy (addr_name, "unknown address",
153 sizeof(addr_name));
155 krb5_log(context, fac, 0, "Connection from %s", addr_name);
157 ret = krb5_kt_register(context, &hdb_get_kt_ops);
158 if (ret)
159 krb5_err(context, 1, ret, "krb5_kt_register");
161 if (ktname != NULL) {
162 ret = krb5_kt_resolve(context, ktname, &keytab);
163 if (ret)
164 krb5_err (context, 1, ret, "krb5_kt_resolve %s", ktname);
165 } else {
166 ret = krb5_kt_default (context, &keytab);
167 if (ret)
168 krb5_err (context, 1, ret, "krb5_kt_default");
171 ret = krb5_recvauth(context, &ac, &sock, HPROP_VERSION, NULL,
172 0, keytab, &ticket);
173 if (ret)
174 krb5_err(context, 1, ret, "krb5_recvauth");
176 ret = krb5_unparse_name(context, ticket->server, &server);
177 if (ret)
178 krb5_err(context, 1, ret, "krb5_unparse_name");
179 if (strncmp(server, "hprop/", 6) != 0)
180 krb5_errx(context, 1, "ticket not for hprop (%s)", server);
182 free(server);
183 krb5_free_ticket (context, ticket);
185 ret = krb5_auth_con_getauthenticator(context, ac, &authent);
186 if (ret)
187 krb5_err(context, 1, ret, "krb5_auth_con_getauthenticator");
189 ret = krb5_make_principal(context, &c1, NULL, "kadmin", "hprop", NULL);
190 if (ret)
191 krb5_err(context, 1, ret, "krb5_make_principal");
192 _krb5_principalname2krb5_principal(context, &c2,
193 authent->cname, authent->crealm);
194 if (!krb5_principal_compare(context, c1, c2)) {
195 char *s;
196 ret = krb5_unparse_name(context, c2, &s);
197 if (ret)
198 s = unparseable_name;
199 krb5_errx(context, 1, "Unauthorized connection from %s", s);
201 krb5_free_principal(context, c1);
202 krb5_free_principal(context, c2);
204 ret = krb5_kt_close(context, keytab);
205 if (ret)
206 krb5_err(context, 1, ret, "krb5_kt_close");
209 if (!print_dump) {
210 int aret;
212 aret = asprintf(&tmp_db, "%s~", database);
213 if (aret == -1)
214 krb5_errx(context, 1, "hdb_create: out of memory");
216 ret = hdb_create(context, &db, tmp_db);
217 if (ret)
218 krb5_err(context, 1, ret, "hdb_create(%s)", tmp_db);
219 ret = db->hdb_open(context, db, O_RDWR | O_CREAT | O_TRUNC, 0600);
220 if (ret)
221 krb5_err(context, 1, ret, "hdb_open(%s)", tmp_db);
224 nprincs = 0;
225 while (1){
226 krb5_data data;
227 hdb_entry_ex entry;
229 if (from_stdin) {
230 ret = krb5_read_message(context, &sock, &data);
231 if (ret != 0 && ret != HEIM_ERR_EOF)
232 krb5_err(context, 1, ret, "krb5_read_message");
233 } else {
234 ret = krb5_read_priv_message(context, ac, &sock, &data);
235 if (ret)
236 krb5_err(context, 1, ret, "krb5_read_priv_message");
239 if (ret == HEIM_ERR_EOF || data.length == 0) {
240 if (!from_stdin) {
241 data.data = NULL;
242 data.length = 0;
243 krb5_write_priv_message(context, ac, &sock, &data);
245 if (!print_dump) {
246 ret = db->hdb_close(context, db);
247 if (ret)
248 krb5_err(context, 1, ret, "db_close");
249 ret = db->hdb_rename(context, db, database);
250 if (ret)
251 krb5_err(context, 1, ret, "db_rename");
253 break;
255 memset(&entry, 0, sizeof(entry));
256 ret = hdb_value2entry(context, &data, &entry.entry);
257 krb5_data_free(&data);
258 if (ret)
259 krb5_err(context, 1, ret, "hdb_value2entry");
260 if (print_dump) {
261 struct hdb_print_entry_arg parg;
263 parg.out = stdout;
264 parg.fmt = HDB_DUMP_HEIMDAL;
265 hdb_print_entry(context, db, &entry, &parg);
266 } else {
267 ret = db->hdb_store(context, db, 0, &entry);
268 if (ret == HDB_ERR_EXISTS) {
269 char *s;
270 ret = krb5_unparse_name(context, entry.entry.principal, &s);
271 if (ret)
272 s = strdup(unparseable_name);
273 krb5_warnx(context, "Entry exists: %s", s);
274 free(s);
275 } else if (ret)
276 krb5_err(context, 1, ret, "db_store");
277 else
278 nprincs++;
280 hdb_free_entry(context, &entry);
282 if (!print_dump)
283 krb5_log(context, fac, 0, "Received %d principals", nprincs);
285 if (inetd_flag == 0)
286 rk_closesocket(sock);
288 exit(0);