Debian Codegraph @

libinetutils/kerberos5.c

raw
1 /* kerberos5.c -- functions to use Heimdal's and MIT's Kerberos V
2 Copyright (C) 2014-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils 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 (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 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 #include <config.h>
20
21 #ifdef KRB5
22 # include <stdio.h>
23 # include <stdlib.h>
24 # include <string.h>
25 # include <unistd.h>
26
27 # include <krb5.h>
28 # include "kerberos5_def.h"
29
30 /* authentication, client side */
31 int
32 kerberos_auth (krb5_context *ctx, int verbose, char **cname,
33 const char *sname, int sock, char *cmd,
34 unsigned short port, krb5_keyblock **key, const char *realm)
35 {
36 int rc;
37 char *out, *p;
38 size_t outlen;
39 int krb5len, msglen;
40 char *tmpserver;
41 char auth;
42 /* KERBEROS 5 SENDAUTH MESSAGE */
43 char krb5sendauth[] = "KRB5_SENDAUTH_V1.0";
44 /* PROTOCOL VERSION */
45 char krb5sendclient[] = "KCMDV0.2";
46
47 /* to store error msg sent by server */
48 char errormsg[101];
49 char cksumdata[101];
50
51 krb5_data cksum_data;
52 krb5_principal server;
53 krb5_auth_context auth_ctx = NULL;
54 krb5_flags authopts = AP_OPTS_USE_SUBKEY;
55
56 if (krb5_sname_to_principal (*ctx, sname, "host", KRB5_NT_SRV_HST, &server))
57 return (-1);
58
59 /* If realm is null, look up from table */
60 if (realm == NULL || realm[0] == '\0')
61 # ifdef KRB5_GENERAL__ /* MIT */
62 realm = (char *) krb5_princ_realm (*ctx, server);
63 # else/* Heimdal */
64 realm = krb5_principal_get_realm (*ctx, server);
65 # endif
66
67 /* size of KRB5 auth message */
68 krb5len = strlen (krb5sendauth) + 1;
69 msglen = htonl (krb5len);
70 write (sock, &msglen, sizeof (int));
71 /* KRB5 authentication message */
72 write (sock, krb5sendauth, krb5len);
73 /* size of client message */
74 krb5len = strlen (krb5sendclient) + 1;
75 msglen = htonl (krb5len);
76 write (sock, &msglen, sizeof (int));
77 /* KRB5 client message */
78 write (sock, krb5sendclient, krb5len);
79
80 /* get answer from server 0 = ok, 1 = error with message */
81 read (sock, &auth, 1);
82 if (auth)
83 {
84 ssize_t n;
85
86 errormsg[0] = '\0';
87 n = read (sock, errormsg, sizeof (errormsg) - 1);
88
89 if (n >= 0 && n < (ssize_t) sizeof (errormsg))
90 errormsg[n] = '\0';
91 else
92 errormsg[sizeof (errormsg) - 1] = '\0';
93
94 fprintf (stderr, "Error during server authentication : %s\n", errormsg);
95 return -1;
96 }
97
98 if (verbose)
99 {
100 printf ("Client: %s\n", *cname);
101 printf ("Server: %s\n", sname);
102 }
103
104 /* Get a ticket for the server. */
105
106 tmpserver = malloc (strlen (SERVICE) + strlen (sname) + 2);
107 if (!tmpserver)
108 {
109 perror ("kerberos_auth()");
110 return -1;
111 }
112
113 p = strchr (sname, '/');
114 if (p && (p != sname))
115 strcpy (tmpserver, sname); /* Non-empty prefix. */
116 else
117 sprintf (tmpserver, "%s/%s", SERVICE, sname + (p ? 1 : 0));
118
119 /* Retrieve realm assigned to this server as per configuration,
120 * unless an explicit domain was passed in the call.
121 */
122 if (!realm)
123 {
124 if (!p)
125 p = (char *) sname;
126 else if (*p == '/')
127 ++p;
128 }
129
130 /* checksum = port: terminal name */
131
132 cksum_data.length = snprintf (cksumdata, sizeof (cksumdata) - 1,
133 "%u:%s%s", ntohs (port), cmd, *cname);
134
135 if (strncmp (cmd, "-x ", 3) == 0)
136 authopts |= AP_OPTS_MUTUAL_REQUIRED;
137
138 cksum_data.data = cksumdata;
139
140 rc = krb5_sendauth (*ctx, &auth_ctx, &sock, "KCMDV0.2",
141 NULL, server, authopts, &cksum_data,
142 NULL, NULL, NULL, NULL, NULL);
143
144 if (rc == KRB5_SENDAUTH_REJECTED)
145 {
146 fprintf (stderr, "server rejected authentication");
147 return rc;
148 }
149
150 krb5_free_principal (*ctx, server);
151 # if 0
152 krb5_data_free (&cksum_data);
153 # endif
154
155 rc = krb5_auth_con_getsendsubkey (*ctx, auth_ctx, key);
156
157 /* send size of AP-REQ to the server */
158
159 msglen = outlen;
160 msglen = htonl (msglen);
161 write (sock, (char *) &msglen, sizeof (int));
162
163 /* send AP-REQ to the server */
164
165 write (sock, out, outlen);
166
167 /* read response from server - what ? */
168
169 read (sock, &rc, sizeof (rc));
170 if (rc)
171 return -1 /* SHISHI_APREP_VERIFY_FAILED */ ;
172
173 /* For mutual authentication, wait for server reply. */
174
175 /* We are now authenticated. */
176 if (verbose)
177 printf ("User authenticated.\n");
178
179 return 0;
180
181 }
182
183 /* authentication, server side */
184 int
185 get_auth (int infd, krb5_context *ctx, krb5_auth_context *actx,
186 krb5_keyblock **key, const char **err_msg,
187 int *protoversion, int *cksumtype,
188 char **cksum, size_t *cksumlen, char *srvname)
189 {
190 char *out;
191 size_t outlen;
192 char *buf;
193 int buflen;
194 int len;
195 int rc;
196 int error;
197 /* KERBEROS 5 SENDAUTH MESSAGE */
198 char krb5sendauth[] = "KRB5_SENDAUTH_V1.0";
199 /* PROTOCOL VERSION */
200 char krb5kcmd1[] = "KCMDV0.1";
201 char krb5kcmd2[] = "KCMDV0.2";
202 char *servername, *server = NULL, *realm = NULL;
203
204 *err_msg = NULL;
205 /* Get key for the server. */
206
207 # if 0
208 /*
209 * XXX: Taken straight from the version for libshishi.
210 * XXX: No adaptions yet.
211 */
212 rc = shishi_init_server (handle);
213 if (rc != SHISHI_OK)
214 return rc;
215
216 if (srvname && *srvname)
217 {
218 rc = shishi_parse_name (*handle, srvname, &server, &realm);
219 if (rc != SHISHI_OK)
220 {
221 *err_msg = shishi_strerror (rc);
222 return rc;
223 }
224 }
225
226 if (server && *server)
227 {
228 char *p;
229
230 servername = malloc (sizeof (SERVICE) + strlen (server) + 2);
231 if (!servername)
232 {
233 *err_msg = "Not enough memory";
234 return SHISHI_TOO_SMALL_BUFFER;
235 }
236
237 p = strchr (server, '/');
238 if (p && (p != server))
239 sprintf (servername, "%s", server); /* Non-empty prefix. */
240 else
241 sprintf (servername, "%s/%s", SERVICE, server + (p ? 1 : 0)); /* Remove initial slash. */
242 }
243 else
244 servername = shishi_server_for_local_service (*handle, SERVICE);
245
246 if (realm && *realm)
247 shishi_realm_default_set (*handle, realm);
248
249 free (server);
250 free (realm);
251
252 /* Enable use of `~/.k5login'. */
253 if (shishi_check_version ("1.0.2")) /* Faulty in version 1.0.1. */
254 {
255 rc = shishi_cfg_authorizationtype_set (*handle, "k5login basic");
256 if (rc != SHISHI_OK)
257 {
258 *err_msg = shishi_error (*handle);
259 return rc;
260 }
261 }
262
263 key = shishi_hostkeys_for_serverrealm (*handle, servername,
264 shishi_realm_default (*handle));
265 free (servername);
266 if (!key)
267 {
268 *err_msg = shishi_error (*handle);
269 return SHISHI_INVALID_KEY;
270 }
271
272 /* Read Kerberos 5 sendauth message */
273 rc = read (infd, &len, sizeof (int));
274 if (rc != sizeof (int))
275 {
276 *err_msg = "Error reading message size";
277 return SHISHI_IO_ERROR;
278 }
279
280 buflen = ntohl (len);
281 buf = malloc (buflen);
282 if (!buf)
283 {
284 *err_msg = "Not enough memory";
285 return SHISHI_TOO_SMALL_BUFFER;
286 }
287
288 rc = read (infd, buf, buflen);
289 if (rc != buflen)
290 {
291 *err_msg = "Error reading authentication message";
292 return SHISHI_IO_ERROR;
293 }
294
295 len = strlen (krb5sendauth);
296 rc = strncmp (buf, krb5sendauth, buflen >= len ? len : buflen);
297 if (rc)
298 {
299 *err_msg = "Invalid authentication type";
300 /* Authentication type is wrong. */
301 write (infd, "\001", 1);
302 return SHISHI_VERIFY_FAILED;
303 }
304
305 free (buf);
306
307 /* Read protocol version */
308 rc = read (infd, &len, sizeof (int));
309 if (rc != sizeof (int))
310 {
311 *err_msg = "Error reading protocol message size";
312 return SHISHI_IO_ERROR;
313 }
314 buflen = ntohl (len);
315 buf = malloc (buflen);
316 if (!buf)
317 {
318 *err_msg = "Not enough memory";
319 return SHISHI_TOO_SMALL_BUFFER;
320 }
321
322 rc = read (infd, buf, buflen);
323 if (rc != buflen)
324 {
325 *err_msg = "Error reading protocol message";
326 return SHISHI_IO_ERROR;
327 }
328
329 len = strlen (krb5kcmd1);
330 rc = strncmp (buf, krb5kcmd1, buflen >= len ? len : buflen);
331 if (rc)
332 {
333 len = strlen (krb5kcmd2);
334 rc = strncmp (buf, krb5kcmd2, buflen >= len ? len : buflen);
335 if (rc)
336 {
337 *err_msg = "Protocol version not supported";
338 /* Protocol version is wrong. */
339 write (infd, "\002", 1);
340 return SHISHI_VERIFY_FAILED;
341 }
342 *protoversion = 2;
343 }
344 else
345 *protoversion = 1;
346
347 free (buf);
348
349 /* Authentication type is ok */
350
351 write (infd, "\0", 1);
352
353 /* Read Authentication request from client */
354
355 rc = read (infd, &len, sizeof (int));
356 if (rc != sizeof (int))
357 {
358 *err_msg = "Error reading authentication request size";
359 return SHISHI_IO_ERROR;
360 }
361
362 buflen = ntohl (len);
363 buf = malloc (buflen);
364 if (!buf)
365 {
366 *err_msg = "Not enough memory";
367 return SHISHI_TOO_SMALL_BUFFER;
368 }
369
370 rc = read (infd, buf, buflen);
371 if (rc != buflen)
372 {
373 *err_msg = "Error reading authentication request";
374 return SHISHI_IO_ERROR;
375 }
376
377 /* Create Authentication context */
378
379 rc = shishi_ap_nosubkey (*handle, ap);
380 if (rc != SHISHI_OK)
381 return rc;
382
383 /* Store request in context */
384
385 rc = shishi_ap_req_der_set (*ap, buf, buflen);
386 if (rc != SHISHI_OK)
387 return rc;
388
389 free (buf);
390
391 /* Process authentication request */
392
393 rc = shishi_ap_req_process (*ap, key);
394 if (rc != SHISHI_OK)
395 return rc;
396
397 # ifdef ENCRYPTION
398
399 /* extract subkey if present from ap exchange for secure connection */
400 if (*protoversion == 2)
401 {
402 *enckey = NULL;
403 shishi_authenticator_get_subkey (*handle,
404 shishi_ap_authenticator (*ap), enckey);
405 }
406
407 # endif
408
409 /* Get authenticator checksum */
410 rc = shishi_authenticator_cksum (*handle,
411 shishi_ap_authenticator (*ap),
412 cksumtype, cksum, cksumlen);
413 if (rc != SHISHI_OK)
414 return rc;
415
416 /* User is authenticated. */
417 error = 0;
418 write (infd, &error, sizeof (int));
419
420 /* Authenticate ourself to client, if requested. */
421
422 if (shishi_apreq_mutual_required_p (*handle, shishi_ap_req (*ap)))
423 {
424 int len;
425
426 rc = shishi_ap_rep_der (*ap, &out, &outlen);
427 if (rc != SHISHI_OK)
428 return rc;
429
430 len = outlen;
431 len = htonl (len);
432 rc = write (infd, &len, sizeof (len));
433 if (rc != sizeof (int))
434 {
435 *err_msg = "Error sending AP-REP";
436 free (out);
437 return SHISHI_IO_ERROR;
438 }
439
440 rc = write (infd, out, ntohl (len));
441 if (rc != (int) ntohl (len))
442 {
443 *err_msg = "Error sending AP-REP";
444 free (out);
445 return SHISHI_IO_ERROR;
446 }
447
448 free (out);
449
450 /* We are authenticated to client */
451 }
452
453 # ifdef ENCRYPTION
454 if (*protoversion == 1)
455 {
456 Shishi_tkt *tkt;
457
458 tkt = shishi_ap_tkt (*ap);
459 if (tkt == NULL)
460 {
461 *err_msg = "Could not get tkt from AP-REQ";
462 return SHISHI_INVALID_TICKET;
463 }
464
465 rc = shishi_encticketpart_get_key (*handle,
466 shishi_tkt_encticketpart (tkt),
467 enckey);
468 if (rc != SHISHI_OK)
469 return rc;
470 }
471 # endif
472 /* ENCRYPTION */
473
474 return 0;
475 # else
476 return -1;
477 # endif
478 }
479 #endif /* KRB5 */