2 * CIFS SPNEGO user-space helper.
3 * Copyright (C) Igor Mammedov (niallain@gmail.com) 2007
5 * Used by /sbin/request-key for handling
6 * cifs upcall for kerberos authorization of access to share.
7 * You should have keyutils installed and add following line to
8 * /etc/request-key.conf file
10 create cifs.spnego * * /usr/local/sbin/cifs.spnego [-v][-c] %k
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "cifs_spnego.h"
30 const char* CIFSSPNEGO_VERSION="1.0";
31 static const char *prog = "cifs.spnego";
32 typedef enum _secType {
38 * Prepares AP-REQ data for mechToken and gets session key
39 * Uses credentials from cache. It will not ask for password
40 * you should receive credentials for yuor name manually using
41 * kinit or whatever you wish.
44 * oid - string with OID/ Could be OID_KERBEROS5
45 * or OID_KERBEROS5_OLD
46 * principal - Service name.
47 * Could be "cifs/FQDN" for KRB5 OID
48 * or for MS_KRB5 OID style server principal
49 * like "pdc$@YOUR.REALM.NAME"
52 * secblob - pointer for spnego wrapped AP-REQ data to be stored
53 * sess_key- pointer for SessionKey data to be stored
55 * ret: 0 - success, others - failure
57 int handle_krb5_mech(const char *oid, const char *principal,
58 DATA_BLOB * secblob, DATA_BLOB * sess_key)
61 DATA_BLOB tkt, tkt_wrapped;
63 /* get a kerberos ticket for the service and extract the session key */
64 retval = cli_krb5_get_ticket(principal, 0,
65 &tkt, sess_key, 0, NULL, NULL);
70 /* wrap that up in a nice GSS-API wrapping */
71 tkt_wrapped = spnego_gen_krb5_wrap(tkt, TOK_ID_KRB_AP_REQ);
73 /* and wrap that in a shiny SPNEGO wrapper */
74 *secblob = gen_negTokenInit(OID_KERBEROS5, tkt_wrapped);
76 data_blob_free(&tkt_wrapped);
81 #define DKD_HAVE_HOSTNAME 1
82 #define DKD_HAVE_VERSION 2
83 #define DKD_HAVE_SEC 4
84 #define DKD_HAVE_IPV4 8
85 #define DKD_HAVE_IPV6 16
86 #define DKD_HAVE_UID 32
87 #define DKD_MUSTHAVE_SET (DKD_HAVE_HOSTNAME|DKD_HAVE_VERSION|DKD_HAVE_SEC)
89 int decode_key_description(const char *desc, int *ver, secType_t * sec,
90 char **hostname, uid_t * uid)
94 const char *tkn = desc;
97 pos = index(tkn, ';');
98 if (strncmp(tkn, "host=", 5) == 0) {
107 SAFE_FREE(*hostname);
108 *hostname = SMB_XMALLOC_ARRAY(char, len);
109 strlcpy(*hostname, tkn + 5, len);
110 retval |= DKD_HAVE_HOSTNAME;
111 } else if (strncmp(tkn, "ipv4=", 5) == 0) {
112 /* BB: do we need it if we have hostname already? */
113 } else if (strncmp(tkn, "ipv6=", 5) == 0) {
114 /* BB: do we need it if we have hostname already? */
115 } else if (strncmp(tkn, "sec=", 4) == 0) {
116 if (strncmp(tkn + 4, "krb5", 4) == 0) {
117 retval |= DKD_HAVE_SEC;
120 } else if (strncmp(tkn, "uid=", 4) == 0) {
122 *uid = strtol(tkn + 4, NULL, 16);
124 syslog(LOG_WARNING, "Invalid uid format: %s",
128 retval |= DKD_HAVE_UID;
130 } else if (strncmp(tkn, "ver=", 4) == 0) { /* if version */
132 *ver = strtol(tkn + 4, NULL, 16);
135 "Invalid version format: %s",
139 retval |= DKD_HAVE_VERSION;
149 int main(const int argc, char *const argv[])
151 struct cifs_spnego_msg *keydata = NULL;
152 DATA_BLOB secblob = data_blob_null;
153 DATA_BLOB sess_key = data_blob_null;
159 int kernel_upcall_version;
160 int c, use_cifs_service_prefix = 0;
161 char *buf, *hostname = NULL;
163 openlog(prog, 0, LOG_DAEMON);
165 syslog(LOG_WARNING, "Usage: %s [-c] key_serial", prog);
169 while ((c = getopt(argc, argv, "cv")) != -1) {
172 use_cifs_service_prefix = 1;
176 syslog(LOG_WARNING, "version: %s", CIFSSPNEGO_VERSION);
177 fprintf(stderr, "version: %s", CIFSSPNEGO_VERSION);
181 syslog(LOG_WARNING, "unknow option: %c", c);
186 /* get key and keyring values */
188 key = strtol(argv[optind], NULL, 10);
190 syslog(LOG_WARNING, "Invalid key format: %s", strerror(errno));
194 rc = keyctl_describe_alloc(key, &buf);
196 syslog(LOG_WARNING, "keyctl_describe_alloc failed: %s",
202 rc = decode_key_description(buf, &kernel_upcall_version, §ype,
204 if ((rc & DKD_MUSTHAVE_SET) != DKD_MUSTHAVE_SET) {
206 "unable to get from description necessary params");
213 if (kernel_upcall_version != CIFS_SPNEGO_UPCALL_VERSION) {
215 "incompatible kernel upcall version: 0x%x",
216 kernel_upcall_version);
221 if (rc & DKD_HAVE_UID) {
224 syslog(LOG_WARNING, "setuid: %s", strerror(errno));
229 /* BB: someday upcall SPNEGO blob could be checked here to decide
230 * what mech to use */
232 // do mech specific authorization
238 /* for "cifs/" service name + terminating 0 */
239 len = strlen(hostname) + 5 + 1;
240 princ = SMB_XMALLOC_ARRAY(char, len);
245 if (use_cifs_service_prefix) {
246 strlcpy(princ, "cifs/", len);
248 strlcpy(princ, "host/", len);
250 strlcpy(princ + 5, hostname, len - 5);
252 rc = handle_krb5_mech(OID_KERBEROS5, princ,
253 &secblob, &sess_key);
258 syslog(LOG_WARNING, "sectype: %d is not implemented",
269 /* pack SecurityBLob and SessionKey into downcall packet */
271 sizeof(struct cifs_spnego_msg) + secblob.length + sess_key.length;
272 keydata = (struct cifs_spnego_msg*)SMB_XMALLOC_ARRAY(char, datalen);
277 keydata->version = CIFS_SPNEGO_UPCALL_VERSION;
279 keydata->sesskey_len = sess_key.length;
280 keydata->secblob_len = secblob.length;
281 memcpy(&(keydata->data), sess_key.data, sess_key.length);
282 memcpy(&(keydata->data) + keydata->sesskey_len,
283 secblob.data, secblob.length);
286 rc = keyctl_instantiate(key, keydata, datalen, 0);
288 syslog(LOG_WARNING, "keyctl_instantiate: %s", strerror(errno));
292 /* BB: maybe we need use timeout for key: for example no more then
293 * ticket lifietime? */
294 /* keyctl_set_timeout( key, 60); */
296 data_blob_free(&secblob);
297 data_blob_free(&sess_key);