2 Copyright (C) 2003 - 2010 Chaskiel Grundman
3 Copyright (c) 2011 Your Filesystem Inc.
4 Copyright (c) 2012 Sine Nomine Associates
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright
15 notice, this list of conditions and the following disclaimer in the
16 documentation and/or other materials provided with the distribution.
18 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <afsconfig.h>
30 #include <afs/param.h>
35 #include <afs/cellconfig.h>
36 #ifndef AFSCONF_CLIENTNAME
37 #include <afs/dirpath.h>
38 #define AFSCONF_CLIENTNAME AFSDIR_CLIENT_ETC_DIRPATH
41 #include <rx/rx_null.h>
44 # define KERBEROS_APPLE_DEPRECATED(x)
48 #include "afscp_internal.h"
51 #include <hcrypto/des.h>
53 #ifdef HAVE_KRB5_CREDS_KEYBLOCK_ENCTYPE
54 #define Z_keydata(keyblock) ((keyblock)->contents)
55 #define Z_keylen(keyblock) ((keyblock)->length)
56 #define Z_credskey(creds) (&(creds)->keyblock)
57 #define Z_enctype(keyblock) ((keyblock)->enctype)
59 #define Z_keydata(keyblock) ((keyblock)->keyvalue.data)
60 #define Z_keylen(keyblock) ((keyblock)->keyvalue.length)
61 #define Z_credskey(creds) (&(creds)->session)
62 #define Z_enctype(keyblock) ((keyblock)->keytype)
65 static int insecure
= 0;
66 static int try_anonymous
= 0;
67 static char authas_name
[256];
68 static char authas_inst
[256];
78 afscp_AnonymousAuth(int state
)
80 try_anonymous
= state
;
85 * Connect to all servers using authenticated connections, using the local
86 * KeyFile to appear as an arbitrary user.
88 * @param[in] aname The pts username to impersonate
90 * @note aname is krb4-based name, not a krb5 principal. So for example, you
91 * probably want to give "user.admin" instead of "user/admin".
93 * @return operation status
97 afscp_LocalAuthAs(const char *aname
)
99 const char *ainst
= strchr(aname
, '.');
100 size_t namelen
, instlen
;
103 namelen
= ainst
- aname
;
105 instlen
= strlen(ainst
);
107 namelen
= strlen(aname
);
112 if (namelen
+1 > sizeof(authas_name
) || instlen
+1 > sizeof(authas_inst
)) {
115 strncpy(authas_name
, aname
, namelen
);
116 strncpy(authas_inst
, ainst
, instlen
);
120 static struct afsconf_dir
*confdir
;
123 afscp_SetConfDir(char *confDir
)
126 afsconf_Close(confdir
);
128 confdir
= afsconf_Open(confDir
);
132 _GetCellInfo(char *cell
, struct afsconf_cell
*celldata
)
136 confdir
= afsconf_Open(AFSCONF_CLIENTNAME
);
137 if (confdir
== NULL
) {
140 code
= afsconf_GetCellInfo(confdir
, cell
, AFSCONF_VLDBSERVICE
, celldata
);
145 _GetNullSecurityObject(struct afscp_cell
*cell
)
147 cell
->security
= (struct rx_securityClass
*)rxnull_NewClientSecurityObject();
148 cell
->scindex
= RX_SECIDX_NULL
;
153 _GetLocalSecurityObject(struct afscp_cell
*cell
,
154 char *aname
, char *ainst
)
158 struct ktc_encryptionKey key
, session
;
159 struct rx_securityClass
*tc
;
163 struct afsconf_dir
*tdir
;
165 tdir
= afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH
);
167 code
= AFSCONF_FAILURE
;
171 code
= afsconf_GetLatestKey(tdir
, &kvno
, &key
);
176 DES_init_random_number_generator((DES_cblock
*)&key
);
177 code
= DES_new_random_key((DES_cblock
*)&session
);
182 ticketLen
= sizeof(tbuffer
);
183 memset(tbuffer
, 0, sizeof(tbuffer
));
184 code
= tkt_MakeTicket(tbuffer
, &ticketLen
, &key
, aname
, ainst
, "", 0,
185 0xffffffff, &session
, 0, "afs", "");
196 tc
= (struct rx_securityClass
*)
197 rxkad_NewClientSecurityObject(lev
, &session
, kvno
, ticketLen
,
215 _GetSecurityObject(struct afscp_cell
*cell
)
219 krb5_context context
;
223 char **realms
, *realm
;
224 struct afsconf_cell celldata
;
225 char localcell
[MAXCELLCHARS
+ 1];
226 struct rx_securityClass
*sc
;
227 struct ktc_encryptionKey k
;
230 code
= _GetCellInfo(cell
->name
, &celldata
);
235 if (authas_name
[0]) {
236 code
= _GetLocalSecurityObject(cell
, authas_name
, authas_inst
);
242 code
= krb5_init_context(&context
); /* see aklog.c main() */
247 if (cell
->realm
== NULL
) {
249 code
= krb5_get_host_realm(context
, celldata
.hostName
[0], &realms
);
252 strlcpy(localcell
, realms
[0], sizeof(localcell
));
253 krb5_free_host_realm(context
, realms
);
259 strlcpy(localcell
, realm
, MAXCELLCHARS
+ 1);
263 for (i
= 0; (i
< MAXCELLCHARS
&& cell
->name
[i
]); i
++) {
264 if (isalpha(cell
->name
[i
]))
265 localcell
[i
] = toupper(cell
->name
[i
]);
267 localcell
[i
] = cell
->name
[i
];
273 code
= krb5_cc_default(context
, &cc
);
275 memset(&match
, 0, sizeof(match
));
276 Z_enctype(Z_credskey(&match
)) = ENCTYPE_DES_CBC_CRC
;
279 code
= krb5_cc_get_principal(context
, cc
, &match
.client
);
281 code
= krb5_build_principal(context
, &match
.server
,
282 strlen(realm
), realm
,
283 "afs", cell
->name
, NULL
);
286 krb5_free_cred_contents(context
, &match
);
288 krb5_cc_close(context
, cc
);
289 krb5_free_context(context
);
293 code
= krb5_get_credentials(context
, 0, cc
, &match
, &cred
);
295 krb5_free_principal(context
, match
.server
);
298 code
= krb5_build_principal(context
, &match
.server
,
299 strlen(realm
), realm
, "afs", NULL
);
301 code
= krb5_get_credentials(context
, 0, cc
, &match
, &cred
);
303 krb5_free_cred_contents(context
, &match
);
305 krb5_cc_close(context
, cc
);
306 krb5_free_context(context
);
315 memcpy(&k
.data
, Z_keydata(Z_credskey(cred
)), 8);
316 sc
= (struct rx_securityClass
*)rxkad_NewClientSecurityObject
317 (l
, &k
, RXKAD_TKT_TYPE_KERBEROS_V5
,
318 cred
->ticket
.length
, cred
->ticket
.data
);
319 krb5_free_creds(context
, cred
);
320 krb5_free_cred_contents(context
, &match
);
322 krb5_cc_close(context
, cc
);
323 krb5_free_context(context
);
329 #endif /* HAVE_KERBEROS */
331 return _GetNullSecurityObject(cell
);
337 _GetVLservers(struct afscp_cell
*cell
)
339 struct rx_connection
*conns
[MAXHOSTSPERCELL
+ 1];
342 struct afsconf_cell celldata
;
344 code
= _GetCellInfo(cell
->name
, &celldata
);
349 for (i
= 0; i
< celldata
.numServers
; i
++) {
350 conns
[i
] = rx_NewConnection(celldata
.hostAddr
[i
].sin_addr
.s_addr
,
351 htons(AFSCONF_VLDBPORT
),
352 USER_SERVICE_ID
, cell
->security
,
356 return ubik_ClientInit(conns
, &cell
->vlservers
);