2 * CIFS user-space helper.
3 * Copyright (C) Igor Mammedov (niallain@gmail.com) 2007
4 * Copyright (C) Jeff Layton (jlayton@redhat.com) 2009
6 * Used by /sbin/request-key for handling
7 * cifs upcall for kerberos authorization of access to share and
8 * cifs upcall for DFS srver name resolving (IPv4/IPv6 aware).
9 * You should have keyutils installed and add something like the
10 * following lines to /etc/request-key.conf file:
12 create cifs.spnego * * /usr/local/sbin/cifs.upcall %k
13 create dns_resolver * * /usr/local/sbin/cifs.upcall %k
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include "cifs_spnego.h"
33 const char *CIFSSPNEGO_VERSION = "1.2";
34 static const char *prog = "cifs.upcall";
35 typedef enum _secType {
42 * given a process ID, get the value of the KRB5CCNAME environment variable
43 * in the context of that process. On error, just return NULL.
46 get_krb5_ccname(pid_t pid)
52 * FIXME: sysconf for ARG_MAX instead? Kernel seems to be limited to a
53 * page however, so it may not matter.
56 char *p, *value = NULL;
59 snprintf(buf, 4095, "/proc/%d/environ", pid);
60 fd = open(buf, O_RDONLY);
64 /* FIXME: don't assume that we get it all in the first read? */
65 len = read(fd, buf, 4096);
73 /* can't have valid KRB5CCNAME if there are < 13 bytes left */
75 if (strncmp("KRB5CCNAME=", p, 11)) {
76 p += strnlen(p, left);
83 value = SMB_STRNDUP(p, left);
90 * Prepares AP-REQ data for mechToken and gets session key
91 * Uses credentials from cache. It will not ask for password
92 * you should receive credentials for yuor name manually using
93 * kinit or whatever you wish.
96 * oid - string with OID/ Could be OID_KERBEROS5
97 * or OID_KERBEROS5_OLD
98 * principal - Service name.
99 * Could be "cifs/FQDN" for KRB5 OID
100 * or for MS_KRB5 OID style server principal
101 * like "pdc$@YOUR.REALM.NAME"
104 * secblob - pointer for spnego wrapped AP-REQ data to be stored
105 * sess_key- pointer for SessionKey data to be stored
107 * ret: 0 - success, others - failure
110 handle_krb5_mech(const char *oid, const char *principal, DATA_BLOB *secblob,
111 DATA_BLOB *sess_key, const char *ccname)
114 DATA_BLOB tkt, tkt_wrapped;
116 /* get a kerberos ticket for the service and extract the session key */
117 retval = cli_krb5_get_ticket(principal, 0, &tkt, sess_key, 0, ccname,
123 /* wrap that up in a nice GSS-API wrapping */
124 tkt_wrapped = spnego_gen_krb5_wrap(tkt, TOK_ID_KRB_AP_REQ);
126 /* and wrap that in a shiny SPNEGO wrapper */
127 *secblob = gen_negTokenInit(oid, tkt_wrapped);
129 data_blob_free(&tkt_wrapped);
130 data_blob_free(&tkt);
134 #define DKD_HAVE_HOSTNAME 1
135 #define DKD_HAVE_VERSION 2
136 #define DKD_HAVE_SEC 4
137 #define DKD_HAVE_IPV4 8
138 #define DKD_HAVE_IPV6 16
139 #define DKD_HAVE_UID 32
140 #define DKD_HAVE_PID 64
141 #define DKD_MUSTHAVE_SET (DKD_HAVE_HOSTNAME|DKD_HAVE_VERSION|DKD_HAVE_SEC)
144 decode_key_description(const char *desc, int *ver, secType_t *sec,
145 char **hostname, uid_t *uid, pid_t *pid)
149 const char *tkn = desc;
152 pos = index(tkn, ';');
153 if (strncmp(tkn, "host=", 5) == 0) {
162 SAFE_FREE(*hostname);
163 *hostname = SMB_XMALLOC_ARRAY(char, len);
164 strlcpy(*hostname, tkn + 5, len);
165 retval |= DKD_HAVE_HOSTNAME;
166 } else if (strncmp(tkn, "ipv4=", 5) == 0) {
167 /* BB: do we need it if we have hostname already? */
168 } else if (strncmp(tkn, "ipv6=", 5) == 0) {
169 /* BB: do we need it if we have hostname already? */
170 } else if (strncmp(tkn, "pid=", 4) == 0) {
172 *pid = strtol(tkn + 4, NULL, 0);
174 syslog(LOG_WARNING, "Invalid pid format: %s",
178 retval |= DKD_HAVE_PID;
180 } else if (strncmp(tkn, "sec=", 4) == 0) {
181 if (strncmp(tkn + 4, "krb5", 4) == 0) {
182 retval |= DKD_HAVE_SEC;
184 } else if (strncmp(tkn + 4, "mskrb5", 6) == 0) {
185 retval |= DKD_HAVE_SEC;
188 } else if (strncmp(tkn, "uid=", 4) == 0) {
190 *uid = strtol(tkn + 4, NULL, 16);
192 syslog(LOG_WARNING, "Invalid uid format: %s",
196 retval |= DKD_HAVE_UID;
198 } else if (strncmp(tkn, "ver=", 4) == 0) { /* if version */
200 *ver = strtol(tkn + 4, NULL, 16);
203 "Invalid version format: %s",
207 retval |= DKD_HAVE_VERSION;
218 cifs_resolver(const key_serial_t key, const char *key_descr)
221 struct addrinfo *addr;
222 char ip[INET6_ADDRSTRLEN];
224 const char *keyend = key_descr;
225 /* skip next 4 ';' delimiters to get to description */
226 for (c = 1; c <= 4; c++) {
227 keyend = index(keyend+1, ';');
229 syslog(LOG_WARNING, "invalid key description: %s",
236 /* resolve name to ip */
237 c = getaddrinfo(keyend, NULL, NULL, &addr);
239 syslog(LOG_WARNING, "unable to resolve hostname: %s [%s]",
240 keyend, gai_strerror(c));
244 /* conver ip to string form */
245 if (addr->ai_family == AF_INET) {
246 p = &(((struct sockaddr_in *)addr->ai_addr)->sin_addr);
248 p = &(((struct sockaddr_in6 *)addr->ai_addr)->sin6_addr);
250 if (!inet_ntop(addr->ai_family, p, ip, sizeof(ip))) {
251 syslog(LOG_WARNING, "%s: inet_ntop: %s",
252 __FUNCTION__, strerror(errno));
258 c = keyctl_instantiate(key, ip, strlen(ip)+1, 0);
260 syslog(LOG_WARNING, "%s: keyctl_instantiate: %s",
261 __FUNCTION__, strerror(errno));
273 syslog(LOG_WARNING, "Usage: %s [-c] [-v] key_serial", prog);
274 fprintf(stderr, "Usage: %s [-c] [-v] key_serial\n", prog);
277 int main(const int argc, char *const argv[])
279 struct cifs_spnego_msg *keydata = NULL;
280 DATA_BLOB secblob = data_blob_null;
281 DATA_BLOB sess_key = data_blob_null;
282 secType_t sectype = NONE;
283 key_serial_t key = 0;
288 int kernel_upcall_version = 0;
289 int c, use_cifs_service_prefix = 0;
290 char *buf, *ccname = NULL, *hostname = NULL;
293 openlog(prog, 0, LOG_DAEMON);
295 while ((c = getopt(argc, argv, "cv")) != -1) {
298 use_cifs_service_prefix = 1;
302 printf("version: %s\n", CIFSSPNEGO_VERSION);
306 syslog(LOG_WARNING, "unknown option: %c", c);
312 /* is there a key? */
313 if (argc <= optind) {
318 /* get key and keyring values */
320 key = strtol(argv[optind], NULL, 10);
323 syslog(LOG_WARNING, "Invalid key format: %s", strerror(errno));
327 rc = keyctl_describe_alloc(key, &buf);
329 syslog(LOG_WARNING, "keyctl_describe_alloc failed: %s",
335 if ((strncmp(buf, "cifs.resolver", sizeof("cifs.resolver")-1) == 0) ||
336 (strncmp(buf, "dns_resolver", sizeof("dns_resolver")-1) == 0)) {
337 rc = cifs_resolver(key, buf);
341 rc = decode_key_description(buf, &kernel_upcall_version, §ype,
342 &hostname, &uid, &pid);
343 if ((rc & DKD_MUSTHAVE_SET) != DKD_MUSTHAVE_SET) {
345 "unable to get from description necessary params");
352 if (kernel_upcall_version > CIFS_SPNEGO_UPCALL_VERSION) {
354 "incompatible kernel upcall version: 0x%x",
355 kernel_upcall_version);
360 if (rc & DKD_HAVE_PID)
361 ccname = get_krb5_ccname(pid);
363 if (rc & DKD_HAVE_UID) {
366 syslog(LOG_WARNING, "setuid: %s", strerror(errno));
371 // do mech specific authorization
378 /* for "cifs/" service name + terminating 0 */
379 len = strlen(hostname) + 5 + 1;
380 princ = SMB_XMALLOC_ARRAY(char, len);
385 if (use_cifs_service_prefix) {
386 strlcpy(princ, "cifs/", len);
388 strlcpy(princ, "host/", len);
390 strlcpy(princ + 5, hostname, len - 5);
392 if (sectype == MS_KRB5)
393 oid = OID_KERBEROS5_OLD;
397 rc = handle_krb5_mech(oid, princ, &secblob, &sess_key,
403 syslog(LOG_WARNING, "sectype: %d is not implemented",
414 /* pack SecurityBLob and SessionKey into downcall packet */
416 sizeof(struct cifs_spnego_msg) + secblob.length + sess_key.length;
417 keydata = (struct cifs_spnego_msg*)SMB_XMALLOC_ARRAY(char, datalen);
422 keydata->version = kernel_upcall_version;
424 keydata->sesskey_len = sess_key.length;
425 keydata->secblob_len = secblob.length;
426 memcpy(&(keydata->data), sess_key.data, sess_key.length);
427 memcpy(&(keydata->data) + keydata->sesskey_len,
428 secblob.data, secblob.length);
431 rc = keyctl_instantiate(key, keydata, datalen, 0);
433 syslog(LOG_WARNING, "keyctl_instantiate: %s", strerror(errno));
437 /* BB: maybe we need use timeout for key: for example no more then
438 * ticket lifietime? */
439 /* keyctl_set_timeout( key, 60); */
442 * on error, negatively instantiate the key ourselves so that we can
443 * make sure the kernel doesn't hang it off of a searchable keyring
444 * and interfere with the next attempt to instantiate the key.
446 if (rc != 0 && key == 0)
447 keyctl_negate(key, 1, KEY_REQKEY_DEFL_DEFAULT);
448 data_blob_free(&secblob);
449 data_blob_free(&sess_key);