include-afsconfig-before-param-h-20010712
[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     struct timeval tv;
103     Key_schedule schedule;
104     register afs_int32 i, code;
105     
106     /* first, find the right key and kvno to use */
107     code = afsconf_GetLatestKey(adir, &kvno, &key);
108     if (code) {
109         return QuickAuth(astr, aindex);
110     }
111     
112     /* next create random session key, using key for seed to good random */
113     des_init_random_number_generator (&key);
114     code = des_random_key (&session);
115     if (code) {
116         return QuickAuth(astr, aindex);
117     }
118     
119     /* now create the actual ticket */
120     ticketLen = sizeof(tbuffer);
121     code = tkt_MakeTicket(tbuffer, &ticketLen, &key, AUTH_SUPERUSER, "", "", 0,
122                            0xffffffff, &session, 0, "afs", "");
123     /* parms were buffer, ticketlen, key to seal ticket with, principal
124         name, instance and cell, start time, end time, session key to seal
125         in ticket, inet host, server name and server instance */
126     if (code) {
127         return QuickAuth(astr, aindex);
128     }
129
130     /* Next, we have ticket, kvno and session key, authenticate the connection.
131      * We use a magic # instead of a constant because of basic compilation
132      * order when compiling the system from scratch (rx/rxkad.h isn't installed
133      * yet). */
134     tclass = (struct rx_securityClass *)
135         rxkad_NewClientSecurityObject(enclevel, &session, kvno,
136                                       ticketLen, tbuffer);
137     *astr = tclass;
138     *aindex = 2;    /* kerberos security index */
139     return 0;
140 }
141
142 /* build a fake ticket for 'afs' using keys from adir, returning an
143  * appropriate security class and index
144  */
145 afs_int32 afsconf_ClientAuth(adir, astr, aindex)
146 struct afsconf_dir *adir;
147 struct rx_securityClass **astr;
148 afs_int32 *aindex; {
149     afs_int32 rc;
150
151     LOCK_GLOBAL_MUTEX
152     rc = GenericAuth(adir, astr, aindex, rxkad_clear);
153     UNLOCK_GLOBAL_MUTEX
154     return rc;
155 }
156
157 /* build a fake ticket for 'afs' using keys from adir, returning an
158  * appropriate security class and index.  This one, unlike the above,
159  * tells rxkad to encrypt the data, too.
160  */
161 afs_int32 afsconf_ClientAuthSecure(adir, astr, aindex)
162 struct afsconf_dir *adir;
163 struct rx_securityClass **astr;
164 afs_int32 *aindex; {
165     afs_int32 rc;
166
167     LOCK_GLOBAL_MUTEX
168     rc = GenericAuth(adir, astr, aindex, rxkad_crypt);
169     UNLOCK_GLOBAL_MUTEX
170     return rc;
171 }
172