]>
Commit | Line | Data |
---|---|---|
8efc0c15 | 1 | /* |
5260325f | 2 | * Dug Song <dugsong@UMICH.EDU> |
3 | * Kerberos v4 authentication and ticket-passing routines. | |
4 | */ | |
8efc0c15 | 5 | |
6 | #include "includes.h" | |
7 | #include "packet.h" | |
8 | #include "xmalloc.h" | |
9 | #include "ssh.h" | |
57112b5a | 10 | #include "servconf.h" |
8efc0c15 | 11 | |
14a9a859 | 12 | RCSID("$OpenBSD: auth-krb4.c,v 1.17 2000/08/28 03:50:54 deraadt Exp $"); |
e5a0294f | 13 | |
8efc0c15 | 14 | #ifdef KRB4 |
6a17f9c2 | 15 | char *ticket = NULL; |
16 | ||
57112b5a | 17 | extern ServerOptions options; |
18 | ||
19 | /* | |
20 | * try krb4 authentication, | |
21 | * return 1 on success, 0 on failure, -1 if krb4 is not available | |
22 | */ | |
23 | ||
6ae2364d | 24 | int |
57112b5a | 25 | auth_krb4_password(struct passwd * pw, const char *password) |
26 | { | |
27 | AUTH_DAT adata; | |
28 | KTEXT_ST tkt; | |
29 | struct hostent *hp; | |
30 | unsigned long faddr; | |
31 | char localhost[MAXHOSTNAMELEN]; | |
32 | char phost[INST_SZ]; | |
33 | char realm[REALM_SZ]; | |
34 | int r; | |
35 | ||
36 | /* | |
37 | * Try Kerberos password authentication only for non-root | |
38 | * users and only if Kerberos is installed. | |
39 | */ | |
40 | if (pw->pw_uid != 0 && krb_get_lrealm(realm, 1) == KSUCCESS) { | |
41 | ||
42 | /* Set up our ticket file. */ | |
43 | if (!krb4_init(pw->pw_uid)) { | |
44 | log("Couldn't initialize Kerberos ticket file for %s!", | |
45 | pw->pw_name); | |
46 | goto kerberos_auth_failure; | |
47 | } | |
48 | /* Try to get TGT using our password. */ | |
49 | r = krb_get_pw_in_tkt((char *) pw->pw_name, "", | |
50 | realm, "krbtgt", realm, | |
51 | DEFAULT_TKT_LIFE, (char *) password); | |
52 | if (r != INTK_OK) { | |
53 | packet_send_debug("Kerberos V4 password " | |
54 | "authentication for %s failed: %s", | |
55 | pw->pw_name, krb_err_txt[r]); | |
56 | goto kerberos_auth_failure; | |
57 | } | |
58 | /* Successful authentication. */ | |
59 | chown(tkt_string(), pw->pw_uid, pw->pw_gid); | |
60 | ||
61 | /* | |
62 | * Now that we have a TGT, try to get a local | |
63 | * "rcmd" ticket to ensure that we are not talking | |
64 | * to a bogus Kerberos server. | |
65 | */ | |
66 | (void) gethostname(localhost, sizeof(localhost)); | |
67 | (void) strlcpy(phost, (char *) krb_get_phost(localhost), | |
68 | INST_SZ); | |
69 | r = krb_mk_req(&tkt, KRB4_SERVICE_NAME, phost, realm, 33); | |
70 | ||
71 | if (r == KSUCCESS) { | |
72 | if (!(hp = gethostbyname(localhost))) { | |
73 | log("Couldn't get local host address!"); | |
74 | goto kerberos_auth_failure; | |
75 | } | |
76 | memmove((void *) &faddr, (void *) hp->h_addr, | |
77 | sizeof(faddr)); | |
78 | ||
79 | /* Verify our "rcmd" ticket. */ | |
80 | r = krb_rd_req(&tkt, KRB4_SERVICE_NAME, phost, | |
81 | faddr, &adata, ""); | |
82 | if (r == RD_AP_UNDEC) { | |
83 | /* | |
84 | * Probably didn't have a srvtab on | |
c345cf9d | 85 | * localhost. Disallow login. |
57112b5a | 86 | */ |
87 | log("Kerberos V4 TGT for %s unverifiable, " | |
88 | "no srvtab installed? krb_rd_req: %s", | |
89 | pw->pw_name, krb_err_txt[r]); | |
c345cf9d | 90 | goto kerberos_auth_failure; |
57112b5a | 91 | } else if (r != KSUCCESS) { |
92 | log("Kerberos V4 %s ticket unverifiable: %s", | |
93 | KRB4_SERVICE_NAME, krb_err_txt[r]); | |
94 | goto kerberos_auth_failure; | |
95 | } | |
96 | } else if (r == KDC_PR_UNKNOWN) { | |
97 | /* | |
c345cf9d | 98 | * Disallow login if no rcmd service exists, and |
57112b5a | 99 | * log the error. |
100 | */ | |
101 | log("Kerberos V4 TGT for %s unverifiable: %s; %s.%s " | |
102 | "not registered, or srvtab is wrong?", pw->pw_name, | |
103 | krb_err_txt[r], KRB4_SERVICE_NAME, phost); | |
c345cf9d | 104 | goto kerberos_auth_failure; |
57112b5a | 105 | } else { |
106 | /* | |
107 | * TGT is bad, forget it. Possibly spoofed! | |
108 | */ | |
109 | packet_send_debug("WARNING: Kerberos V4 TGT " | |
110 | "possibly spoofed for %s: %s", | |
111 | pw->pw_name, krb_err_txt[r]); | |
112 | goto kerberos_auth_failure; | |
113 | } | |
114 | ||
115 | /* Authentication succeeded. */ | |
116 | return 1; | |
117 | ||
118 | kerberos_auth_failure: | |
119 | krb4_cleanup_proc(NULL); | |
120 | ||
121 | if (!options.kerberos_or_local_passwd) | |
122 | return 0; | |
123 | } else { | |
124 | /* Logging in as root or no local Kerberos realm. */ | |
125 | packet_send_debug("Unable to authenticate to Kerberos."); | |
126 | } | |
127 | /* Fall back to ordinary passwd authentication. */ | |
128 | return -1; | |
129 | } | |
130 | ||
6a17f9c2 | 131 | void |
132 | krb4_cleanup_proc(void *ignore) | |
133 | { | |
5260325f | 134 | debug("krb4_cleanup_proc called"); |
135 | if (ticket) { | |
136 | (void) dest_tkt(); | |
137 | xfree(ticket); | |
138 | ticket = NULL; | |
139 | } | |
6a17f9c2 | 140 | } |
141 | ||
6ae2364d | 142 | int |
5260325f | 143 | krb4_init(uid_t uid) |
8efc0c15 | 144 | { |
5260325f | 145 | static int cleanup_registered = 0; |
4fe2af09 | 146 | const char *tkt_root = TKT_ROOT; |
5260325f | 147 | struct stat st; |
148 | int fd; | |
149 | ||
150 | if (!ticket) { | |
151 | /* Set unique ticket string manually since we're still root. */ | |
152 | ticket = xmalloc(MAXPATHLEN); | |
8efc0c15 | 153 | #ifdef AFS |
5260325f | 154 | if (lstat("/ticket", &st) != -1) |
155 | tkt_root = "/ticket/"; | |
8efc0c15 | 156 | #endif /* AFS */ |
14a9a859 | 157 | snprintf(ticket, MAXPATHLEN, "%s%u_%d", tkt_root, uid, getpid()); |
5260325f | 158 | (void) krb_set_tkt_string(ticket); |
159 | } | |
160 | /* Register ticket cleanup in case of fatal error. */ | |
161 | if (!cleanup_registered) { | |
162 | fatal_add_cleanup(krb4_cleanup_proc, NULL); | |
163 | cleanup_registered = 1; | |
164 | } | |
165 | /* Try to create our ticket file. */ | |
166 | if ((fd = mkstemp(ticket)) != -1) { | |
167 | close(fd); | |
168 | return 1; | |
169 | } | |
170 | /* Ticket file exists - make sure user owns it (just passed ticket). */ | |
171 | if (lstat(ticket, &st) != -1) { | |
172 | if (st.st_mode == (S_IFREG | S_IRUSR | S_IWUSR) && | |
173 | st.st_uid == uid) | |
174 | return 1; | |
175 | } | |
176 | /* Failure - cancel cleanup function, leaving bad ticket for inspection. */ | |
177 | log("WARNING: bad ticket file %s", ticket); | |
178 | fatal_remove_cleanup(krb4_cleanup_proc, NULL); | |
179 | cleanup_registered = 0; | |
180 | xfree(ticket); | |
181 | ticket = NULL; | |
182 | ||
183 | return 0; | |
8efc0c15 | 184 | } |
185 | ||
6ae2364d | 186 | int |
5260325f | 187 | auth_krb4(const char *server_user, KTEXT auth, char **client) |
8efc0c15 | 188 | { |
5260325f | 189 | AUTH_DAT adat = {0}; |
190 | KTEXT_ST reply; | |
191 | char instance[INST_SZ]; | |
192 | int r, s; | |
610cd5c6 | 193 | socklen_t slen; |
5260325f | 194 | u_int cksum; |
195 | Key_schedule schedule; | |
196 | struct sockaddr_in local, foreign; | |
197 | ||
198 | s = packet_get_connection_in(); | |
199 | ||
610cd5c6 | 200 | slen = sizeof(local); |
5260325f | 201 | memset(&local, 0, sizeof(local)); |
610cd5c6 | 202 | if (getsockname(s, (struct sockaddr *) & local, &slen) < 0) |
5260325f | 203 | debug("getsockname failed: %.100s", strerror(errno)); |
610cd5c6 | 204 | slen = sizeof(foreign); |
5260325f | 205 | memset(&foreign, 0, sizeof(foreign)); |
610cd5c6 | 206 | if (getpeername(s, (struct sockaddr *) & foreign, &slen) < 0) { |
5260325f | 207 | debug("getpeername failed: %.100s", strerror(errno)); |
208 | fatal_cleanup(); | |
209 | } | |
210 | instance[0] = '*'; | |
211 | instance[1] = 0; | |
212 | ||
213 | /* Get the encrypted request, challenge, and session key. */ | |
214 | if ((r = krb_rd_req(auth, KRB4_SERVICE_NAME, instance, 0, &adat, ""))) { | |
215 | packet_send_debug("Kerberos V4 krb_rd_req: %.100s", krb_err_txt[r]); | |
216 | return 0; | |
217 | } | |
218 | des_key_sched((des_cblock *) adat.session, schedule); | |
219 | ||
220 | *client = xmalloc(MAX_K_NAME_SZ); | |
221 | (void) snprintf(*client, MAX_K_NAME_SZ, "%s%s%s@%s", adat.pname, | |
222 | *adat.pinst ? "." : "", adat.pinst, adat.prealm); | |
223 | ||
224 | /* Check ~/.klogin authorization now. */ | |
225 | if (kuserok(&adat, (char *) server_user) != KSUCCESS) { | |
226 | packet_send_debug("Kerberos V4 .klogin authorization failed!"); | |
227 | log("Kerberos V4 .klogin authorization failed for %s to account %s", | |
228 | *client, server_user); | |
229 | xfree(*client); | |
230 | return 0; | |
231 | } | |
232 | /* Increment the checksum, and return it encrypted with the | |
233 | session key. */ | |
234 | cksum = adat.checksum + 1; | |
235 | cksum = htonl(cksum); | |
236 | ||
237 | /* If we can't successfully encrypt the checksum, we send back an | |
238 | empty message, admitting our failure. */ | |
239 | if ((r = krb_mk_priv((u_char *) & cksum, reply.dat, sizeof(cksum) + 1, | |
240 | schedule, &adat.session, &local, &foreign)) < 0) { | |
241 | packet_send_debug("Kerberos V4 mk_priv: (%d) %s", r, krb_err_txt[r]); | |
242 | reply.dat[0] = 0; | |
243 | reply.length = 0; | |
244 | } else | |
245 | reply.length = r; | |
246 | ||
247 | /* Clear session key. */ | |
248 | memset(&adat.session, 0, sizeof(&adat.session)); | |
249 | ||
250 | packet_start(SSH_SMSG_AUTH_KERBEROS_RESPONSE); | |
251 | packet_put_string((char *) reply.dat, reply.length); | |
252 | packet_send(); | |
253 | packet_write_wait(); | |
254 | return 1; | |
8efc0c15 | 255 | } |
256 | #endif /* KRB4 */ | |
257 | ||
258 | #ifdef AFS | |
6ae2364d | 259 | int |
5260325f | 260 | auth_kerberos_tgt(struct passwd *pw, const char *string) |
8efc0c15 | 261 | { |
5260325f | 262 | CREDENTIALS creds; |
263 | ||
264 | if (!radix_to_creds(string, &creds)) { | |
265 | log("Protocol error decoding Kerberos V4 tgt"); | |
266 | packet_send_debug("Protocol error decoding Kerberos V4 tgt"); | |
267 | goto auth_kerberos_tgt_failure; | |
268 | } | |
269 | if (strncmp(creds.service, "", 1) == 0) /* backward compatibility */ | |
270 | strlcpy(creds.service, "krbtgt", sizeof creds.service); | |
271 | ||
272 | if (strcmp(creds.service, "krbtgt")) { | |
273 | log("Kerberos V4 tgt (%s%s%s@%s) rejected for %s", creds.pname, | |
274 | creds.pinst[0] ? "." : "", creds.pinst, creds.realm, | |
275 | pw->pw_name); | |
276 | packet_send_debug("Kerberos V4 tgt (%s%s%s@%s) rejected for %s", | |
277 | creds.pname, creds.pinst[0] ? "." : "", creds.pinst, | |
278 | creds.realm, pw->pw_name); | |
279 | goto auth_kerberos_tgt_failure; | |
280 | } | |
281 | if (!krb4_init(pw->pw_uid)) | |
282 | goto auth_kerberos_tgt_failure; | |
283 | ||
284 | if (in_tkt(creds.pname, creds.pinst) != KSUCCESS) | |
285 | goto auth_kerberos_tgt_failure; | |
286 | ||
287 | if (save_credentials(creds.service, creds.instance, creds.realm, | |
288 | creds.session, creds.lifetime, creds.kvno, | |
289 | &creds.ticket_st, creds.issue_date) != KSUCCESS) { | |
290 | packet_send_debug("Kerberos V4 tgt refused: couldn't save credentials"); | |
291 | goto auth_kerberos_tgt_failure; | |
292 | } | |
293 | /* Successful authentication, passed all checks. */ | |
294 | chown(tkt_string(), pw->pw_uid, pw->pw_gid); | |
295 | ||
296 | packet_send_debug("Kerberos V4 tgt accepted (%s.%s@%s, %s%s%s@%s)", | |
297 | creds.service, creds.instance, creds.realm, creds.pname, | |
298 | creds.pinst[0] ? "." : "", creds.pinst, creds.realm); | |
299 | memset(&creds, 0, sizeof(creds)); | |
300 | packet_start(SSH_SMSG_SUCCESS); | |
301 | packet_send(); | |
302 | packet_write_wait(); | |
303 | return 1; | |
304 | ||
305 | auth_kerberos_tgt_failure: | |
306 | krb4_cleanup_proc(NULL); | |
307 | memset(&creds, 0, sizeof(creds)); | |
308 | packet_start(SSH_SMSG_FAILURE); | |
309 | packet_send(); | |
310 | packet_write_wait(); | |
311 | return 0; | |
8efc0c15 | 312 | } |
313 | ||
6ae2364d | 314 | int |
5260325f | 315 | auth_afs_token(struct passwd *pw, const char *token_string) |
8efc0c15 | 316 | { |
5260325f | 317 | CREDENTIALS creds; |
318 | uid_t uid = pw->pw_uid; | |
319 | ||
320 | if (!radix_to_creds(token_string, &creds)) { | |
321 | log("Protocol error decoding AFS token"); | |
322 | packet_send_debug("Protocol error decoding AFS token"); | |
323 | packet_start(SSH_SMSG_FAILURE); | |
324 | packet_send(); | |
325 | packet_write_wait(); | |
326 | return 0; | |
327 | } | |
328 | if (strncmp(creds.service, "", 1) == 0) /* backward compatibility */ | |
329 | strlcpy(creds.service, "afs", sizeof creds.service); | |
330 | ||
331 | if (strncmp(creds.pname, "AFS ID ", 7) == 0) | |
332 | uid = atoi(creds.pname + 7); | |
333 | ||
334 | if (kafs_settoken(creds.realm, uid, &creds)) { | |
335 | log("AFS token (%s@%s) rejected for %s", creds.pname, creds.realm, | |
336 | pw->pw_name); | |
337 | packet_send_debug("AFS token (%s@%s) rejected for %s", creds.pname, | |
338 | creds.realm, pw->pw_name); | |
339 | memset(&creds, 0, sizeof(creds)); | |
340 | packet_start(SSH_SMSG_FAILURE); | |
341 | packet_send(); | |
342 | packet_write_wait(); | |
343 | return 0; | |
344 | } | |
345 | packet_send_debug("AFS token accepted (%s@%s, %s@%s)", creds.service, | |
346 | creds.realm, creds.pname, creds.realm); | |
347 | memset(&creds, 0, sizeof(creds)); | |
348 | packet_start(SSH_SMSG_SUCCESS); | |
349 | packet_send(); | |
350 | packet_write_wait(); | |
351 | return 1; | |
8efc0c15 | 352 | } |
353 | #endif /* AFS */ |