6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
7 * For copying and distribution information, please see the file
13 static char *rcsid_sms_sauth_c = "$Header$";
16 #include <mit-copyright.h>
18 #include "mr_server.h"
24 extern char *whoami, *host;
27 char *kname_unparse(char *, char *, char *);
29 typedef struct _replay_cache {
32 struct _replay_cache *next;
35 replay_cache *rcache = NULL;
38 * Handle a MOIRA_AUTH RPC request.
40 * argv[0] is a kerberos authenticator. Decompose it, and if
41 * successful, store the name the user authenticated to in
53 replay_cache *rc, *rcnew;
56 auth.length = cl->args->mr_argl[0];
57 memcpy((char *)auth.dat, cl->args->mr_argv[0], auth.length);
60 if ((status = krb_rd_req (&auth, MOIRA_SNAME, host,
61 cl->haddr.sin_addr.s_addr, &ad, "")) != 0) {
62 status += ERROR_TABLE_BASE_krb;
63 cl->reply.mr_status = status;
64 if (log_flags & LOG_RES)
65 com_err(whoami, status, " (authentication failed)");
70 rcache = (replay_cache*)malloc(sizeof(replay_cache));
71 memset(rcache, 0, sizeof(replay_cache));
74 /* scan replay cache */
75 for (rc = rcache->next; rc; rc = rc->next) {
76 if(auth.length == rc->auth.length &&
77 !memcmp(&(auth.dat), &(rc->auth.dat), auth.length)) {
78 com_err(whoami, 0, "Authenticator replay from %s using authenticator for %s",
79 inet_ntoa(cl->haddr.sin_addr),
80 kname_unparse(ad.pname, ad.pinst, ad.prealm));
81 com_err(whoami, KE_RD_AP_REPEAT, " (authentication failed)");
82 cl->reply.mr_status = KE_RD_AP_REPEAT;
89 rcnew = (replay_cache*)malloc(sizeof(replay_cache));
90 memcpy(&(rcnew->auth), &auth, sizeof(KTEXT_ST));
91 rcnew->expires = now + 2*CLOCK_SKEW;
92 rcnew->next = rcache->next;
96 for (rc = rcnew; rc->next; ) {
97 if(rc->next->expires < now) {
99 rc->next = rc->next->next;
101 } else rc = rc->next;
104 memcpy(cl->kname.name, ad.pname, ANAME_SZ);
105 memcpy(cl->kname.inst, ad.pinst, INST_SZ);
106 memcpy(cl->kname.realm, ad.prealm, REALM_SZ);
107 strcpy(cl->clname, kname_unparse(ad.pname, ad.pinst, ad.prealm));
109 if (ad.pinst[0] == 0 && !strcmp(ad.prealm, krb_realm))
113 /* this is in a separate function because it accesses the database */
114 status = set_krb_mapping(cl->clname, ad.pname, ok,
115 &cl->client_id, &cl->users_id);
117 if (cl->args->mr_version_no == MR_VERSION_2) {
118 strncpy(cl->entity, cl->args->mr_argv[1], 8);
121 strcpy(cl->entity, "???");
123 memset(&ad, 0, sizeof(ad)); /* Clean up session key, etc. */
125 if (log_flags & LOG_RES)
126 com_err(whoami, 0, "Auth to %s using %s, uid %d cid %d",
127 cl->clname, cl->entity, cl->users_id, cl->client_id);
128 if (status != MR_SUCCESS)
129 cl->reply.mr_status = status;
130 else if (cl->users_id == 0)
131 cl->reply.mr_status = MR_USER_AUTH;