death-to-permit-xprt-h-20010327
[openafs.git] / src / auth / authcon.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #if defined(UKERNEL)
11 #include "../afs/param.h"
12 #include "../afs/sysincludes.h"
13 #include "../afs/afsincludes.h"
14 #include "../afs/stds.h"
15 #include "../afs/pthread_glock.h"
16 #include "../des/des.h"
17 #include "../rx/rxkad.h"
18 #include "../rx/rx.h"
19 #include "../afs/cellconfig.h"
20 #include "../afs/keys.h"
21 #include "../afs/auth.h"
22 #include "../afs/pthread_glock.h"
23 #else /* defined(UKERNEL) */
24 #include <afs/param.h>
25 #include <afs/stds.h>
26 #include <afs/pthread_glock.h>
27 #include <sys/types.h>
28 #ifdef AFS_NT40_ENV
29 #include <winsock2.h>
30 #else
31 #include <sys/file.h>
32 #include <sys/time.h>
33 #include <netinet/in.h>
34 #include <netdb.h>
35 #endif
36 #include <des.h>
37 #include <rx/rxkad.h>
38 #include <rx/rx.h>
39 #include "cellconfig.h"
40 #include "keys.h"
41 #include "auth.h"
42 #endif /* defined(UKERNEL) */
43
44
45 extern afs_int32 afsconf_Authenticate();
46 extern int afsconf_GetKey();
47 extern struct rx_securityClass *rxkad_NewServerSecurityObject();
48 extern struct rx_securityClass *rxkad_NewClientSecurityObject();
49
50 /* return a null security object if nothing else can be done */
51 static afs_int32 QuickAuth(astr, aindex)
52 struct rx_securityClass **astr;
53 afs_int32 *aindex; {
54     register struct rx_securityClass *tc;
55     tc = (struct rx_securityClass *) rxnull_NewClientSecurityObject();
56     *astr = tc;
57     *aindex = 0;
58     return 0;
59 }
60
61 #if !defined(UKERNEL)
62 /* Return an appropriate security class and index */
63 afs_int32 afsconf_ServerAuth(adir, astr, aindex)
64 register struct afsconf_dir *adir;
65 struct rx_securityClass **astr;
66 afs_int32 *aindex; {
67     register struct rx_securityClass *tclass;
68     
69     LOCK_GLOBAL_MUTEX
70     tclass = (struct rx_securityClass *)
71         rxkad_NewServerSecurityObject(0, adir, afsconf_GetKey, (char *) 0);
72     if (tclass) {
73         *astr = tclass;
74         *aindex = 2;    /* kerberos security index */
75         UNLOCK_GLOBAL_MUTEX
76         return 0;
77     }
78     else {
79         UNLOCK_GLOBAL_MUTEX
80         return 2;
81     }
82 }
83 #endif /* !defined(UKERNEL) */
84
85 static afs_int32 GenericAuth(adir, astr, aindex, enclevel)
86 struct afsconf_dir *adir;
87 struct rx_securityClass **astr;
88 afs_int32 *aindex; 
89 rxkad_level enclevel; {
90     char tbuffer[256];
91     struct ktc_encryptionKey key, session;
92     struct rx_securityClass *tclass;
93     afs_int32 kvno;
94     afs_int32 ticketLen;
95     struct timeval tv;
96     Key_schedule schedule;
97     register afs_int32 i, code;
98     
99     /* first, find the right key and kvno to use */
100     code = afsconf_GetLatestKey(adir, &kvno, &key);
101     if (code) {
102         return QuickAuth(astr, aindex);
103     }
104     
105     /* next create random session key, using key for seed to good random */
106     des_init_random_number_generator (&key);
107     code = des_random_key (&session);
108     if (code) {
109         return QuickAuth(astr, aindex);
110     }
111     
112     /* now create the actual ticket */
113     ticketLen = sizeof(tbuffer);
114     code = tkt_MakeTicket(tbuffer, &ticketLen, &key, AUTH_SUPERUSER, "", "", 0,
115                            0xffffffff, &session, 0, "afs", "");
116     /* parms were buffer, ticketlen, key to seal ticket with, principal
117         name, instance and cell, start time, end time, session key to seal
118         in ticket, inet host, server name and server instance */
119     if (code) {
120         return QuickAuth(astr, aindex);
121     }
122
123     /* Next, we have ticket, kvno and session key, authenticate the connection.
124      * We use a magic # instead of a constant because of basic compilation
125      * order when compiling the system from scratch (rx/rxkad.h isn't installed
126      * yet). */
127     tclass = (struct rx_securityClass *)
128         rxkad_NewClientSecurityObject(enclevel, &session, kvno,
129                                       ticketLen, tbuffer);
130     *astr = tclass;
131     *aindex = 2;    /* kerberos security index */
132     return 0;
133 }
134
135 /* build a fake ticket for 'afs' using keys from adir, returning an
136  * appropriate security class and index
137  */
138 afs_int32 afsconf_ClientAuth(adir, astr, aindex)
139 struct afsconf_dir *adir;
140 struct rx_securityClass **astr;
141 afs_int32 *aindex; {
142     afs_int32 rc;
143
144     LOCK_GLOBAL_MUTEX
145     rc = GenericAuth(adir, astr, aindex, rxkad_clear);
146     UNLOCK_GLOBAL_MUTEX
147     return rc;
148 }
149
150 /* build a fake ticket for 'afs' using keys from adir, returning an
151  * appropriate security class and index.  This one, unlike the above,
152  * tells rxkad to encrypt the data, too.
153  */
154 afs_int32 afsconf_ClientAuthSecure(adir, astr, aindex)
155 struct afsconf_dir *adir;
156 struct rx_securityClass **astr;
157 afs_int32 *aindex; {
158     afs_int32 rc;
159
160     LOCK_GLOBAL_MUTEX
161     rc = GenericAuth(adir, astr, aindex, rxkad_crypt);
162     UNLOCK_GLOBAL_MUTEX
163     return rc;
164 }
165