]> andersk Git - moira.git/blob - server/mr_sauth.c
second code style cleanup: void/void * usage, proper #includes. try to
[moira.git] / server / mr_sauth.c
1 /* $Id$
2  *
3  * Handle server side of authentication
4  *
5  * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
6  * For copying and distribution information, please see the file
7  * <mit-copyright.h>.
8  *
9  */
10
11 #include <mit-copyright.h>
12 #include "mr_server.h"
13
14 #include <sys/types.h>
15
16 #include <arpa/inet.h>
17 #include <netinet/in.h>
18
19 #include <stdlib.h>
20 #include <string.h>
21
22 RCSID("$Header$");
23
24 extern char *whoami, *host;
25
26 typedef struct _replay_cache {
27   KTEXT_ST auth;
28   time_t expires;
29   struct _replay_cache *next;
30 } replay_cache;
31
32 replay_cache *rcache = NULL;
33
34 /*
35  * Handle a MOIRA_AUTH RPC request.
36  *
37  * argv[0] is a kerberos authenticator.  Decompose it, and if
38  * successful, store the name the user authenticated to in
39  * cl->cl_name.
40  */
41
42 void do_auth(client *cl)
43 {
44   KTEXT_ST auth;
45   AUTH_DAT ad;
46   int status, ok;
47   replay_cache *rc, *rcnew;
48   time_t now;
49
50   auth.length = cl->args->mr_argl[0];
51   memcpy(auth.dat, cl->args->mr_argv[0], auth.length);
52   auth.mbz = 0;
53
54   if ((status = krb_rd_req (&auth, MOIRA_SNAME, host,
55                             cl->haddr.sin_addr.s_addr, &ad, "")))
56     {
57       status += ERROR_TABLE_BASE_krb;
58       cl->reply.mr_status = status;
59       if (log_flags & LOG_RES)
60         com_err(whoami, status, " (authentication failed)");
61       return;
62     }
63
64   if (!rcache)
65     {
66       rcache = malloc(sizeof(replay_cache));
67       memset(rcache, 0, sizeof(replay_cache));
68     }
69
70   /* scan replay cache */
71   for (rc = rcache->next; rc; rc = rc->next)
72     {
73       if (auth.length == rc->auth.length &&
74           !memcmp(&(auth.dat), &(rc->auth.dat), auth.length))
75         {
76           com_err(whoami, 0,
77                   "Authenticator replay from %s using authenticator for %s",
78                   inet_ntoa(cl->haddr.sin_addr),
79                   kname_unparse(ad.pname, ad.pinst, ad.prealm));
80           com_err(whoami, KE_RD_AP_REPEAT, " (authentication failed)");
81           cl->reply.mr_status = KE_RD_AP_REPEAT;
82           return;
83         }
84     }
85
86   /* add new entry */
87   time(&now);
88   rcnew = malloc(sizeof(replay_cache));
89   memcpy(&(rcnew->auth), &auth, sizeof(KTEXT_ST));
90   rcnew->expires = now + 2 * CLOCK_SKEW;
91   rcnew->next = rcache->next;
92   rcache->next = rcnew;
93
94   /* clean cache */
95   for (rc = rcnew; rc->next; )
96     {
97       if (rc->next->expires < now)
98         {
99           rcnew = rc->next;
100           rc->next = rc->next->next;
101           free(rcnew);
102         }
103       else
104         rc = rc->next;
105     }
106
107   memcpy(cl->kname.name, ad.pname, ANAME_SZ);
108   memcpy(cl->kname.inst, ad.pinst, INST_SZ);
109   memcpy(cl->kname.realm, ad.prealm, REALM_SZ);
110   strcpy(cl->clname, kname_unparse(ad.pname, ad.pinst, ad.prealm));
111
112   if (ad.pinst[0] == 0 && !strcmp(ad.prealm, krb_realm))
113     ok = 1;
114   else
115     ok = 0;
116   /* this is in a separate function because it accesses the database */
117   status = set_krb_mapping(cl->clname, ad.pname, ok,
118                            &cl->client_id, &cl->users_id);
119
120   if (cl->args->mr_version_no == MR_VERSION_2)
121     {
122       strncpy(cl->entity, cl->args->mr_argv[1], 8);
123       cl->entity[8] = 0;
124     }
125   else
126     strcpy(cl->entity, "???");
127   memset(&ad, 0, sizeof(ad));   /* Clean up session key, etc. */
128
129   if (log_flags & LOG_RES)
130     {
131       com_err(whoami, 0, "Auth to %s using %s, uid %d cid %d",
132               cl->clname, cl->entity, cl->users_id, cl->client_id);
133     }
134   if (status != MR_SUCCESS)
135     cl->reply.mr_status = status;
136   else if (cl->users_id == 0)
137     cl->reply.mr_status = MR_USER_AUTH;
138 }
This page took 0.537368 seconds and 5 git commands to generate.