]> andersk Git - moira.git/blob - update/auth_002.c
second code style cleanup: void/void * usage, proper #includes. try to
[moira.git] / update / auth_002.c
1 /* $Id$
2  *
3  * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology.
4  * For copying and distribution information, please see the file
5  * <mit-copyright.h>.
6  */
7
8 #include <mit-copyright.h>
9 #include <moira.h>
10 #include "update_server.h"
11
12 #include <sys/utsname.h>
13
14 #include <errno.h>
15 #include <stdio.h>
16
17 #include <gdb.h>
18 #include <krb.h>
19
20 RCSID("$Header$");
21
22 extern char buf[BUFSIZ];
23 extern int have_authorization;
24 extern CONNECTION conn;
25 extern int code;
26 static char service[] = "rcmd";
27 static char master[] = "sms";
28 static char qmark[] = "???";
29 extern C_Block session;
30
31 /*
32  * authentication request auth_002:
33  *
34  * >>> (STRING) "auth_002"
35  * <<< (int) 0
36  * >>> (STRING) ticket
37  * <<< (int) code
38  * <<< (STRING) nonce
39  * >>> (STRING) encrypted nonce
40  * <<< (int) code
41  *
42  */
43
44 int auth_002(char *str)
45 {
46   STRING data;
47   char aname[ANAME_SZ], ainst[INST_SZ], arealm[REALM_SZ];
48   AUTH_DAT ad;
49   char *p, *first;
50   KTEXT_ST ticket_st;
51   struct utsname name;
52   des_key_schedule sched;
53   C_Block nonce, nonce2;
54
55   if (send_ok())
56     lose("sending okay for authorization (auth_002)");
57   code = receive_object(conn, (char *)&data, STRING_T);
58   if (code)
59     {
60       code = connection_errno(conn);
61       lose("awaiting Kerberos authenticators");
62     }
63   uname(&name);
64   ticket_st.mbz = 0;
65   ticket_st.length = MAX_STRING_SIZE(data);
66   memcpy(ticket_st.dat, STRING_DATA(data), MAX_STRING_SIZE(data));
67   code = krb_rd_req(&ticket_st, service, krb_get_phost(name.nodename), 0,
68                     &ad, KEYFILE);
69   if (code)
70     {
71       code += ERROR_TABLE_BASE_krb;
72       strcpy(ad.pname, qmark);
73       strcpy(ad.pinst, qmark);
74       strcpy(ad.prealm, qmark);
75       goto auth_failed;
76     }
77
78   /* If there is an auth record in the config file matching the
79    * authenticator we received, then accept it.  If there's no
80    * auth record, assume [master]@[local realm].
81    */
82   if ((first = p = config_lookup("auth")))
83     {
84       do
85         {
86           kname_parse(aname, ainst, arealm, p);
87           if (strcmp(aname, ad.pname) ||
88               strcmp(ainst, ad.pinst) ||
89               strcmp(arealm, ad.prealm))
90             p = config_lookup("auth");
91           else
92             p = first;
93         }
94       while (p != first);
95     }
96   else
97     {
98       strcpy(aname, master);
99       strcpy(ainst, "");
100       if (krb_get_lrealm(arealm, 1))
101         strcpy(arealm, KRB_REALM);
102     }
103   code = EPERM;
104   if (strcmp(aname, ad.pname) ||
105       strcmp(ainst, ad.pinst) ||
106       strcmp(arealm, ad.prealm))
107     goto auth_failed;
108
109   if (send_ok())
110     lose("sending preliminary approval of authorization");
111
112   /* replay protection */
113   des_random_key(&nonce);
114   STRING_DATA(data) = (char *)nonce;
115   MAX_STRING_SIZE(data) = 8;
116   if (send_object(conn, (char *)&data, STRING_T))
117     lose("sending nonce");
118   code = receive_object(conn, (char *)&data, STRING_T);
119   if (code)
120     {
121       code = connection_errno(conn);
122       goto auth_failed;
123     }
124   des_key_sched(ad.session, sched);
125   des_ecb_encrypt(STRING_DATA(data), nonce2, sched, 0);
126   if (memcmp(nonce, nonce2, sizeof(nonce)))
127     goto auth_failed;
128
129   if (send_ok())
130     lose("sending approval of authorization");
131   have_authorization = 1;
132   /* Stash away session key */
133   memcpy(session, ad.session, sizeof(session));
134   return 0;
135 auth_failed:
136   sprintf(buf, "auth for %s.%s@%s failed: %s",
137           ad.pname, ad.pinst, ad.prealm, error_message(code));
138   {
139     int rc;
140     rc = send_object(conn, (char *)&code, INTEGER_T);
141     code = rc;
142   }
143   if (code)
144     lose("sending rejection of authenticator");
145   return EPERM;
146 }
This page took 0.063912 seconds and 5 git commands to generate.