]>
Commit | Line | Data |
---|---|---|
8efc0c15 | 1 | /* |
bcbf86ec | 2 | * Copyright (c) 1999 Dug Song. All rights reserved. |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted provided that the following conditions | |
6 | * are met: | |
7 | * 1. Redistributions of source code must retain the above copyright | |
8 | * notice, this list of conditions and the following disclaimer. | |
9 | * 2. Redistributions in binary form must reproduce the above copyright | |
10 | * notice, this list of conditions and the following disclaimer in the | |
11 | * documentation and/or other materials provided with the distribution. | |
12 | * | |
13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
15 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
16 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
19 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
20 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
5260325f | 23 | */ |
8efc0c15 | 24 | |
25 | #include "includes.h" | |
0608f8a7 | 26 | RCSID("$OpenBSD: auth-krb4.c,v 1.30 2003/04/08 20:21:28 itojun Exp $"); |
42f11eb2 | 27 | |
42f11eb2 | 28 | #include "ssh.h" |
29 | #include "ssh1.h" | |
8efc0c15 | 30 | #include "packet.h" |
31 | #include "xmalloc.h" | |
42f11eb2 | 32 | #include "log.h" |
57112b5a | 33 | #include "servconf.h" |
ced49be2 | 34 | #include "uidswap.h" |
42f11eb2 | 35 | #include "auth.h" |
53a24016 | 36 | |
37 | #ifdef AFS | |
42f11eb2 | 38 | #include "radix.h" |
53a24016 | 39 | #endif |
8efc0c15 | 40 | |
53a24016 | 41 | #ifdef KRB4 |
57112b5a | 42 | extern ServerOptions options; |
43 | ||
ced49be2 | 44 | static int |
45 | krb4_init(void *context) | |
46 | { | |
47 | static int cleanup_registered = 0; | |
48 | Authctxt *authctxt = (Authctxt *)context; | |
49 | const char *tkt_root = TKT_ROOT; | |
50 | struct stat st; | |
51 | int fd; | |
184eed6a | 52 | |
ced49be2 | 53 | if (!authctxt->krb4_ticket_file) { |
54 | /* Set unique ticket string manually since we're still root. */ | |
55 | authctxt->krb4_ticket_file = xmalloc(MAXPATHLEN); | |
56 | #ifdef AFS | |
57 | if (lstat("/ticket", &st) != -1) | |
58 | tkt_root = "/ticket/"; | |
59 | #endif /* AFS */ | |
c457707e | 60 | snprintf(authctxt->krb4_ticket_file, MAXPATHLEN, "%s%u_%ld", |
61 | tkt_root, authctxt->pw->pw_uid, (long)getpid()); | |
ced49be2 | 62 | krb_set_tkt_string(authctxt->krb4_ticket_file); |
63 | } | |
64 | /* Register ticket cleanup in case of fatal error. */ | |
65 | if (!cleanup_registered) { | |
66 | fatal_add_cleanup(krb4_cleanup_proc, authctxt); | |
67 | cleanup_registered = 1; | |
68 | } | |
69 | /* Try to create our ticket file. */ | |
70 | if ((fd = mkstemp(authctxt->krb4_ticket_file)) != -1) { | |
71 | close(fd); | |
72 | return (1); | |
73 | } | |
74 | /* Ticket file exists - make sure user owns it (just passed ticket). */ | |
75 | if (lstat(authctxt->krb4_ticket_file, &st) != -1) { | |
76 | if (st.st_mode == (S_IFREG | S_IRUSR | S_IWUSR) && | |
77 | st.st_uid == authctxt->pw->pw_uid) | |
78 | return (1); | |
79 | } | |
80 | /* Failure - cancel cleanup function, leaving ticket for inspection. */ | |
bbe88b6d | 81 | logit("WARNING: bad ticket file %s", authctxt->krb4_ticket_file); |
184eed6a | 82 | |
ced49be2 | 83 | fatal_remove_cleanup(krb4_cleanup_proc, authctxt); |
84 | cleanup_registered = 0; | |
184eed6a | 85 | |
ced49be2 | 86 | xfree(authctxt->krb4_ticket_file); |
87 | authctxt->krb4_ticket_file = NULL; | |
184eed6a | 88 | |
ced49be2 | 89 | return (0); |
90 | } | |
91 | ||
57112b5a | 92 | /* |
93 | * try krb4 authentication, | |
94 | * return 1 on success, 0 on failure, -1 if krb4 is not available | |
95 | */ | |
6ae2364d | 96 | int |
ced49be2 | 97 | auth_krb4_password(Authctxt *authctxt, const char *password) |
57112b5a | 98 | { |
99 | AUTH_DAT adata; | |
100 | KTEXT_ST tkt; | |
101 | struct hostent *hp; | |
ced49be2 | 102 | struct passwd *pw; |
103 | char localhost[MAXHOSTNAMELEN], phost[INST_SZ], realm[REALM_SZ]; | |
104 | u_int32_t faddr; | |
57112b5a | 105 | int r; |
184eed6a | 106 | |
ced49be2 | 107 | if ((pw = authctxt->pw) == NULL) |
108 | return (0); | |
184eed6a | 109 | |
57112b5a | 110 | /* |
111 | * Try Kerberos password authentication only for non-root | |
112 | * users and only if Kerberos is installed. | |
113 | */ | |
114 | if (pw->pw_uid != 0 && krb_get_lrealm(realm, 1) == KSUCCESS) { | |
57112b5a | 115 | /* Set up our ticket file. */ |
ced49be2 | 116 | if (!krb4_init(authctxt)) { |
bbe88b6d | 117 | logit("Couldn't initialize Kerberos ticket file for %s!", |
57112b5a | 118 | pw->pw_name); |
ced49be2 | 119 | goto failure; |
57112b5a | 120 | } |
121 | /* Try to get TGT using our password. */ | |
ced49be2 | 122 | r = krb_get_pw_in_tkt((char *) pw->pw_name, "", realm, |
123 | "krbtgt", realm, DEFAULT_TKT_LIFE, (char *)password); | |
57112b5a | 124 | if (r != INTK_OK) { |
ced49be2 | 125 | debug("Kerberos v4 password authentication for %s " |
126 | "failed: %s", pw->pw_name, krb_err_txt[r]); | |
127 | goto failure; | |
57112b5a | 128 | } |
129 | /* Successful authentication. */ | |
130 | chown(tkt_string(), pw->pw_uid, pw->pw_gid); | |
184eed6a | 131 | |
57112b5a | 132 | /* |
133 | * Now that we have a TGT, try to get a local | |
134 | * "rcmd" ticket to ensure that we are not talking | |
135 | * to a bogus Kerberos server. | |
136 | */ | |
ced49be2 | 137 | gethostname(localhost, sizeof(localhost)); |
138 | strlcpy(phost, (char *)krb_get_phost(localhost), | |
139 | sizeof(phost)); | |
57112b5a | 140 | r = krb_mk_req(&tkt, KRB4_SERVICE_NAME, phost, realm, 33); |
184eed6a | 141 | |
57112b5a | 142 | if (r == KSUCCESS) { |
ced49be2 | 143 | if ((hp = gethostbyname(localhost)) == NULL) { |
bbe88b6d | 144 | logit("Couldn't get local host address!"); |
ced49be2 | 145 | goto failure; |
57112b5a | 146 | } |
ced49be2 | 147 | memmove((void *)&faddr, (void *)hp->h_addr, |
57112b5a | 148 | sizeof(faddr)); |
184eed6a | 149 | |
57112b5a | 150 | /* Verify our "rcmd" ticket. */ |
151 | r = krb_rd_req(&tkt, KRB4_SERVICE_NAME, phost, | |
152 | faddr, &adata, ""); | |
153 | if (r == RD_AP_UNDEC) { | |
154 | /* | |
155 | * Probably didn't have a srvtab on | |
c345cf9d | 156 | * localhost. Disallow login. |
57112b5a | 157 | */ |
bbe88b6d | 158 | logit("Kerberos v4 TGT for %s unverifiable, " |
57112b5a | 159 | "no srvtab installed? krb_rd_req: %s", |
160 | pw->pw_name, krb_err_txt[r]); | |
ced49be2 | 161 | goto failure; |
57112b5a | 162 | } else if (r != KSUCCESS) { |
bbe88b6d | 163 | logit("Kerberos v4 %s ticket unverifiable: %s", |
57112b5a | 164 | KRB4_SERVICE_NAME, krb_err_txt[r]); |
ced49be2 | 165 | goto failure; |
57112b5a | 166 | } |
167 | } else if (r == KDC_PR_UNKNOWN) { | |
168 | /* | |
c345cf9d | 169 | * Disallow login if no rcmd service exists, and |
57112b5a | 170 | * log the error. |
171 | */ | |
bbe88b6d | 172 | logit("Kerberos v4 TGT for %s unverifiable: %s; %s.%s " |
57112b5a | 173 | "not registered, or srvtab is wrong?", pw->pw_name, |
ced49be2 | 174 | krb_err_txt[r], KRB4_SERVICE_NAME, phost); |
175 | goto failure; | |
57112b5a | 176 | } else { |
177 | /* | |
178 | * TGT is bad, forget it. Possibly spoofed! | |
179 | */ | |
ced49be2 | 180 | debug("WARNING: Kerberos v4 TGT possibly spoofed " |
181 | "for %s: %s", pw->pw_name, krb_err_txt[r]); | |
182 | goto failure; | |
57112b5a | 183 | } |
57112b5a | 184 | /* Authentication succeeded. */ |
ced49be2 | 185 | return (1); |
186 | } else | |
57112b5a | 187 | /* Logging in as root or no local Kerberos realm. */ |
ced49be2 | 188 | debug("Unable to authenticate to Kerberos."); |
184eed6a | 189 | |
ced49be2 | 190 | failure: |
191 | krb4_cleanup_proc(authctxt); | |
184eed6a | 192 | |
ced49be2 | 193 | if (!options.kerberos_or_local_passwd) |
194 | return (0); | |
184eed6a | 195 | |
57112b5a | 196 | /* Fall back to ordinary passwd authentication. */ |
ced49be2 | 197 | return (-1); |
57112b5a | 198 | } |
199 | ||
6a17f9c2 | 200 | void |
ced49be2 | 201 | krb4_cleanup_proc(void *context) |
6a17f9c2 | 202 | { |
ced49be2 | 203 | Authctxt *authctxt = (Authctxt *)context; |
5260325f | 204 | debug("krb4_cleanup_proc called"); |
ced49be2 | 205 | if (authctxt->krb4_ticket_file) { |
5260325f | 206 | (void) dest_tkt(); |
ced49be2 | 207 | xfree(authctxt->krb4_ticket_file); |
208 | authctxt->krb4_ticket_file = NULL; | |
5260325f | 209 | } |
8efc0c15 | 210 | } |
211 | ||
6ae2364d | 212 | int |
2db9d8aa | 213 | auth_krb4(Authctxt *authctxt, KTEXT auth, char **client, KTEXT reply) |
8efc0c15 | 214 | { |
5260325f | 215 | AUTH_DAT adat = {0}; |
ced49be2 | 216 | Key_schedule schedule; |
217 | struct sockaddr_in local, foreign; | |
5260325f | 218 | char instance[INST_SZ]; |
610cd5c6 | 219 | socklen_t slen; |
5260325f | 220 | u_int cksum; |
ced49be2 | 221 | int r, s; |
184eed6a | 222 | |
5260325f | 223 | s = packet_get_connection_in(); |
184eed6a | 224 | |
610cd5c6 | 225 | slen = sizeof(local); |
5260325f | 226 | memset(&local, 0, sizeof(local)); |
610cd5c6 | 227 | if (getsockname(s, (struct sockaddr *) & local, &slen) < 0) |
5260325f | 228 | debug("getsockname failed: %.100s", strerror(errno)); |
610cd5c6 | 229 | slen = sizeof(foreign); |
5260325f | 230 | memset(&foreign, 0, sizeof(foreign)); |
610cd5c6 | 231 | if (getpeername(s, (struct sockaddr *) & foreign, &slen) < 0) { |
5260325f | 232 | debug("getpeername failed: %.100s", strerror(errno)); |
233 | fatal_cleanup(); | |
234 | } | |
235 | instance[0] = '*'; | |
236 | instance[1] = 0; | |
184eed6a | 237 | |
5260325f | 238 | /* Get the encrypted request, challenge, and session key. */ |
ced49be2 | 239 | if ((r = krb_rd_req(auth, KRB4_SERVICE_NAME, instance, |
240 | 0, &adat, ""))) { | |
241 | debug("Kerberos v4 krb_rd_req: %.100s", krb_err_txt[r]); | |
242 | return (0); | |
5260325f | 243 | } |
244 | des_key_sched((des_cblock *) adat.session, schedule); | |
184eed6a | 245 | |
5260325f | 246 | *client = xmalloc(MAX_K_NAME_SZ); |
247 | (void) snprintf(*client, MAX_K_NAME_SZ, "%s%s%s@%s", adat.pname, | |
248 | *adat.pinst ? "." : "", adat.pinst, adat.prealm); | |
184eed6a | 249 | |
5260325f | 250 | /* Check ~/.klogin authorization now. */ |
ced49be2 | 251 | if (kuserok(&adat, authctxt->user) != KSUCCESS) { |
bbe88b6d | 252 | logit("Kerberos v4 .klogin authorization failed for %s to " |
ced49be2 | 253 | "account %s", *client, authctxt->user); |
5260325f | 254 | xfree(*client); |
9d0844e3 | 255 | *client = NULL; |
ced49be2 | 256 | return (0); |
5260325f | 257 | } |
258 | /* Increment the checksum, and return it encrypted with the | |
259 | session key. */ | |
260 | cksum = adat.checksum + 1; | |
261 | cksum = htonl(cksum); | |
184eed6a | 262 | |
5260325f | 263 | /* If we can't successfully encrypt the checksum, we send back an |
264 | empty message, admitting our failure. */ | |
2db9d8aa | 265 | if ((r = krb_mk_priv((u_char *) & cksum, reply->dat, sizeof(cksum) + 1, |
5260325f | 266 | schedule, &adat.session, &local, &foreign)) < 0) { |
ced49be2 | 267 | debug("Kerberos v4 mk_priv: (%d) %s", r, krb_err_txt[r]); |
2db9d8aa | 268 | reply->dat[0] = 0; |
269 | reply->length = 0; | |
5260325f | 270 | } else |
2db9d8aa | 271 | reply->length = r; |
184eed6a | 272 | |
5260325f | 273 | /* Clear session key. */ |
98a0a51a | 274 | memset(&adat.session, 0, sizeof(adat.session)); |
ced49be2 | 275 | return (1); |
8efc0c15 | 276 | } |
277 | #endif /* KRB4 */ | |
278 | ||
279 | #ifdef AFS | |
6ae2364d | 280 | int |
ced49be2 | 281 | auth_krb4_tgt(Authctxt *authctxt, const char *string) |
8efc0c15 | 282 | { |
5260325f | 283 | CREDENTIALS creds; |
ced49be2 | 284 | struct passwd *pw; |
184eed6a | 285 | |
ced49be2 | 286 | if ((pw = authctxt->pw) == NULL) |
287 | goto failure; | |
184eed6a | 288 | |
ced49be2 | 289 | temporarily_use_uid(pw); |
184eed6a | 290 | |
5260325f | 291 | if (!radix_to_creds(string, &creds)) { |
bbe88b6d | 292 | logit("Protocol error decoding Kerberos v4 TGT"); |
ced49be2 | 293 | goto failure; |
5260325f | 294 | } |
295 | if (strncmp(creds.service, "", 1) == 0) /* backward compatibility */ | |
296 | strlcpy(creds.service, "krbtgt", sizeof creds.service); | |
184eed6a | 297 | |
5260325f | 298 | if (strcmp(creds.service, "krbtgt")) { |
bbe88b6d | 299 | logit("Kerberos v4 TGT (%s%s%s@%s) rejected for %s", |
5260325f | 300 | creds.pname, creds.pinst[0] ? "." : "", creds.pinst, |
301 | creds.realm, pw->pw_name); | |
ced49be2 | 302 | goto failure; |
5260325f | 303 | } |
ced49be2 | 304 | if (!krb4_init(authctxt)) |
305 | goto failure; | |
184eed6a | 306 | |
5260325f | 307 | if (in_tkt(creds.pname, creds.pinst) != KSUCCESS) |
ced49be2 | 308 | goto failure; |
184eed6a | 309 | |
5260325f | 310 | if (save_credentials(creds.service, creds.instance, creds.realm, |
ced49be2 | 311 | creds.session, creds.lifetime, creds.kvno, &creds.ticket_st, |
312 | creds.issue_date) != KSUCCESS) { | |
313 | debug("Kerberos v4 TGT refused: couldn't save credentials"); | |
314 | goto failure; | |
5260325f | 315 | } |
316 | /* Successful authentication, passed all checks. */ | |
317 | chown(tkt_string(), pw->pw_uid, pw->pw_gid); | |
184eed6a | 318 | |
ced49be2 | 319 | debug("Kerberos v4 TGT accepted (%s%s%s@%s)", |
320 | creds.pname, creds.pinst[0] ? "." : "", creds.pinst, creds.realm); | |
5260325f | 321 | memset(&creds, 0, sizeof(creds)); |
184eed6a | 322 | |
ced49be2 | 323 | restore_uid(); |
184eed6a | 324 | |
ced49be2 | 325 | return (1); |
184eed6a | 326 | |
ced49be2 | 327 | failure: |
328 | krb4_cleanup_proc(authctxt); | |
5260325f | 329 | memset(&creds, 0, sizeof(creds)); |
ced49be2 | 330 | restore_uid(); |
184eed6a | 331 | |
ced49be2 | 332 | return (0); |
8efc0c15 | 333 | } |
334 | ||
6ae2364d | 335 | int |
ced49be2 | 336 | auth_afs_token(Authctxt *authctxt, const char *token_string) |
8efc0c15 | 337 | { |
5260325f | 338 | CREDENTIALS creds; |
ced49be2 | 339 | struct passwd *pw; |
94ec8c6b | 340 | uid_t uid; |
184eed6a | 341 | |
ced49be2 | 342 | if ((pw = authctxt->pw) == NULL) |
343 | return (0); | |
184eed6a | 344 | |
5260325f | 345 | if (!radix_to_creds(token_string, &creds)) { |
bbe88b6d | 346 | logit("Protocol error decoding AFS token"); |
ced49be2 | 347 | return (0); |
5260325f | 348 | } |
349 | if (strncmp(creds.service, "", 1) == 0) /* backward compatibility */ | |
350 | strlcpy(creds.service, "afs", sizeof creds.service); | |
184eed6a | 351 | |
5260325f | 352 | if (strncmp(creds.pname, "AFS ID ", 7) == 0) |
353 | uid = atoi(creds.pname + 7); | |
94ec8c6b | 354 | else |
355 | uid = pw->pw_uid; | |
184eed6a | 356 | |
5260325f | 357 | if (kafs_settoken(creds.realm, uid, &creds)) { |
bbe88b6d | 358 | logit("AFS token (%s@%s) rejected for %s", |
ced49be2 | 359 | creds.pname, creds.realm, pw->pw_name); |
5260325f | 360 | memset(&creds, 0, sizeof(creds)); |
ced49be2 | 361 | return (0); |
5260325f | 362 | } |
ced49be2 | 363 | debug("AFS token accepted (%s@%s)", creds.pname, creds.realm); |
5260325f | 364 | memset(&creds, 0, sizeof(creds)); |
184eed6a | 365 | |
ced49be2 | 366 | return (1); |
8efc0c15 | 367 | } |
368 | #endif /* AFS */ |