3 * Handle server side of authentication
5 * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
6 * For copying and distribution information, please see the file
11 #include <mit-copyright.h>
12 #include "mr_server.h"
14 #include <sys/types.h>
16 #include <arpa/inet.h>
17 #include <netinet/in.h>
24 extern char *whoami, *host;
26 extern krb5_context context;
28 static int set_client(client *cl, char *kname,
29 char *name, char *inst, char *realm);
31 typedef struct _replay_cache {
34 struct _replay_cache *next;
37 replay_cache *rcache = NULL;
40 * Handle a MOIRA_AUTH RPC request.
42 * argv[0] is a kerberos authenticator. Decompose it, and if
43 * successful, store the name the user authenticated to in
47 void do_auth(client *cl)
52 replay_cache *rc, *rcnew;
55 auth.length = cl->req.mr_argl[0];
56 memcpy(auth.dat, cl->req.mr_argv[0], auth.length);
59 if ((status = krb_rd_req(&auth, MOIRA_SNAME, host,
60 cl->haddr.sin_addr.s_addr, &ad, "")))
62 status += ERROR_TABLE_BASE_krb;
63 client_reply(cl, status);
64 com_err(whoami, status, " (authentication failed)");
70 rcache = xmalloc(sizeof(replay_cache));
71 memset(rcache, 0, sizeof(replay_cache));
74 /* scan replay cache */
75 for (rc = rcache->next; rc; rc = rc->next)
77 if (auth.length == rc->auth.length &&
78 !memcmp(&(auth.dat), &(rc->auth.dat), auth.length))
81 "Authenticator replay from %s using authenticator for %s",
82 inet_ntoa(cl->haddr.sin_addr),
83 mr_kname_unparse(ad.pname, ad.pinst, ad.prealm));
84 com_err(whoami, KE_RD_AP_REPEAT, " (authentication failed)");
85 client_reply(cl, KE_RD_AP_REPEAT);
92 rcnew = xmalloc(sizeof(replay_cache));
93 memcpy(&(rcnew->auth), &auth, sizeof(KTEXT_ST));
94 rcnew->expires = now + 2 * CLOCK_SKEW;
95 rcnew->next = rcache->next;
99 for (rc = rcnew; rc->next; )
101 if (rc->next->expires < now)
104 rc->next = rc->next->next;
111 status = set_client(cl, mr_kname_unparse(ad.pname, ad.pinst, ad.prealm),
112 ad.pname, ad.pinst, ad.prealm);
114 strncpy(cl->entity, cl->req.mr_argv[1], sizeof(cl->entity) - 1);
115 cl->entity[sizeof(cl->entity) - 1] = 0;
117 memset(&ad, 0, sizeof(ad)); /* Clean up session key, etc. */
119 com_err(whoami, 0, "Auth to %s using %s, uid %d cid %d",
120 cl->clname, cl->entity, cl->users_id, cl->client_id);
122 if (status != MR_SUCCESS || cl->users_id != 0)
123 client_reply(cl, status);
125 client_reply(cl, MR_USER_AUTH);
128 void do_proxy(client *cl)
130 char name[ANAME_SZ] = "\0", inst[INST_SZ] = "\0", realm[REALM_SZ] = "\0";
131 char kname[MAX_K_NAME_SZ];
135 com_err(whoami, MR_PERM, "Cannot re-proxy");
136 client_reply(cl, MR_PERM);
140 if (kname_parse(name, inst, realm, cl->req.mr_argv[0]) != KSUCCESS)
142 com_err(whoami, KE_KNAME_FMT, "while parsing proxy name %s",
144 client_reply(cl, KE_KNAME_FMT);
150 strcpy(realm, krb_realm);
151 sprintf(kname, "%s@%s", cl->req.mr_argv[0], realm);
154 strcpy(kname, cl->req.mr_argv[0]);
156 if (find_member("LIST", proxy_acl, cl))
158 cl->proxy_id = cl->client_id;
159 set_client(cl, kname, name, inst, realm);
160 com_err(whoami, 0, "Proxy authentication as %s (uid %d cid %d) via %s",
161 kname, cl->users_id, cl->client_id, cl->req.mr_argv[1]);
162 client_reply(cl, MR_SUCCESS);
166 com_err(whoami, MR_PERM, "Proxy authentication denied");
167 client_reply(cl, MR_PERM);
171 static int set_client(client *cl, char *kname,
172 char *name, char *inst, char *realm)
176 strncpy(cl->clname, kname, sizeof(cl->clname));
177 cl->clname[sizeof(cl->clname) - 1] = '\0';
179 if ((!inst || inst[0] == 0) && !strcmp(realm, krb_realm))
183 /* this is in a separate function because it accesses the database */
184 return set_krb_mapping(cl->clname, name, ok, &cl->client_id, &cl->users_id);
187 void do_krb5_auth(client *cl)
190 krb5_auth_context auth_con = NULL;
191 krb5_principal server = NULL, client = NULL;
193 char name[ANAME_SZ], inst[INST_SZ], realm[REALM_SZ];
198 status = krb5_auth_con_init(context, &auth_con);
201 client_reply(cl, status);
202 com_err(whoami, status, "(krb5 auth context init failed)");
206 status = krb5_sname_to_principal(context, host, MOIRA_SNAME,
207 KRB5_NT_SRV_HST, &server);
210 client_reply(cl, status);
211 com_err(whoami, status, "(krb5_sname_to_principal failed)");
215 auth.length = cl->req.mr_argl[0];
216 auth.data = cl->req.mr_argv[0];
218 status = krb5_rd_req(context, &auth_con, &auth, server, NULL, NULL,
222 client_reply(cl, status);
223 com_err(whoami, status, " (krb5 authentication failed)");
227 status = krb5_copy_principal(context, ticket->enc_part2->client, &client);
230 client_reply(cl, status);
231 com_err(whoami, status, " (krb5_copy_principal failed)");
235 /* Always convert to krb4 style principal name for now. */
236 status = krb5_524_conv_principal(context, client, name, inst, realm);
239 client_reply(cl, status);
240 com_err(whoami, status, " (krb5_524_conv_principal failed)");
243 status = set_client(cl, mr_kname_unparse(name, inst, realm), name, inst,
246 strncpy(cl->entity, cl->req.mr_argv[1], sizeof(cl->entity) - 1);
247 cl->entity[sizeof(cl->entity) - 1] = 0;
249 com_err(whoami, 0, "krb5 auth to %s using %s, uid %d cid %d",
250 cl->clname, cl->entity, cl->users_id, cl->client_id);
252 if (status != MR_SUCCESS || cl->users_id != 0)
253 client_reply(cl, status);
255 client_reply(cl, MR_USER_AUTH);
259 krb5_free_principal(context, client);
261 krb5_free_principal(context, server);
263 krb5_free_ticket(context, ticket);
265 krb5_auth_con_free(context, auth_con);