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