2 * CIFS user-space helper.
3 * Copyright (C) Igor Mammedov (niallain@gmail.com) 2007
4 * Copyright (C) Jeff Layton (jlayton@samba.org) 2010
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
30 #endif /* HAVE_CONFIG_H */
34 #ifdef HAVE_KRB5_KRB5_H
35 #include <krb5/krb5.h>
36 #elif defined(HAVE_KRB5_H)
41 #include <sys/types.h>
47 #include <arpa/inet.h>
51 #include "data_blob.h"
53 #include "cifs_spnego.h"
55 #define CIFS_DEFAULT_KRB5_DIR "/tmp"
56 #define CIFS_DEFAULT_KRB5_PREFIX "krb5cc_"
57 #define CIFS_DEFAULT_KRB5_KEYTAB "/etc/krb5.keytab"
59 #define MAX_CCNAME_LEN PATH_MAX + 5
61 static const char *prog = "cifs.upcall";
62 typedef enum _sectype {
69 * smb_krb5_principal_get_realm
71 * @brief Get realm of a principal
73 * @param[in] context The krb5_context
74 * @param[in] principal The principal
75 * @return pointer to the realm
79 static char *cifs_krb5_principal_get_realm(krb5_context context __attribute__ ((unused)),
80 krb5_principal principal)
82 #ifdef HAVE_KRB5_PRINCIPAL_GET_REALM /* Heimdal */
83 return krb5_principal_get_realm(context, principal);
84 #elif defined(krb5_princ_realm) /* MIT */
86 realm = krb5_princ_realm(context, principal);
87 return (char *)realm->data;
93 #if !defined(HAVE_KRB5_FREE_UNPARSED_NAME)
94 static void krb5_free_unparsed_name(krb5_context context, char *val)
100 #if !defined(HAVE_KRB5_AUTH_CON_GETSENDSUBKEY) /* Heimdal */
101 static krb5_error_code
102 krb5_auth_con_getsendsubkey(krb5_context context,
103 krb5_auth_context auth_context,
104 krb5_keyblock **keyblock)
106 return krb5_auth_con_getlocalsubkey(context, auth_context, keyblock);
110 /* does the ccache have a valid TGT? */
111 static time_t get_tgt_time(const char *ccname)
113 krb5_context context;
117 krb5_principal principal;
121 if (krb5_init_context(&context)) {
122 syslog(LOG_DEBUG, "%s: unable to init krb5 context", __func__);
126 if (krb5_cc_resolve(context, ccname, &ccache)) {
127 syslog(LOG_DEBUG, "%s: unable to resolve krb5 cache", __func__);
131 if (krb5_cc_set_flags(context, ccache, 0)) {
132 syslog(LOG_DEBUG, "%s: unable to set flags", __func__);
136 if (krb5_cc_get_principal(context, ccache, &principal)) {
137 syslog(LOG_DEBUG, "%s: unable to get principal", __func__);
141 if (krb5_cc_start_seq_get(context, ccache, &cur)) {
142 syslog(LOG_DEBUG, "%s: unable to seq start", __func__);
146 if ((realm = cifs_krb5_principal_get_realm(context, principal)) == NULL) {
147 syslog(LOG_DEBUG, "%s: unable to get realm", __func__);
151 while (!credtime && !krb5_cc_next_cred(context, ccache, &cur, &creds)) {
153 if (krb5_unparse_name(context, creds.server, &name)) {
154 syslog(LOG_DEBUG, "%s: unable to unparse name",
158 if (krb5_realm_compare(context, creds.server, principal) &&
159 !strncasecmp(name, KRB5_TGS_NAME, KRB5_TGS_NAME_SIZE) &&
160 !strncasecmp(name + KRB5_TGS_NAME_SIZE + 1, realm,
162 && creds.times.endtime > time(NULL))
163 credtime = creds.times.endtime;
164 krb5_free_cred_contents(context, &creds);
165 krb5_free_unparsed_name(context, name);
168 krb5_cc_end_seq_get(context, ccache, &cur);
170 krb5_free_principal(context, principal);
172 #if defined(KRB5_TC_OPENCLOSE)
173 krb5_cc_set_flags(context, ccache, KRB5_TC_OPENCLOSE);
175 krb5_cc_close(context, ccache);
177 krb5_free_context(context);
181 static int krb5cc_filter(const struct dirent *dirent)
183 if (strstr(dirent->d_name, CIFS_DEFAULT_KRB5_PREFIX))
190 init_cc_from_keytab(const char *keytab_name, const char *user)
192 krb5_context context = NULL;
195 krb5_keytab keytab = NULL;
196 krb5_principal me = NULL;
197 krb5_ccache cc = NULL;
200 memset((char *) &my_creds, 0, sizeof(my_creds));
202 ret = krb5_init_context(&context);
204 syslog(LOG_DEBUG, "krb5_init_context: %d", (int)ret);
208 ret = krb5_kt_resolve(context, keytab_name, &keytab);
210 syslog(LOG_DEBUG, "krb5_kt_resolve: %d", (int)ret);
214 ret = krb5_parse_name(context, user, &me);
216 syslog(LOG_DEBUG, "krb5_parse_name: %d", (int)ret);
220 ret = krb5_get_init_creds_keytab(context, &my_creds, me,
221 keytab, 0, NULL, NULL);
223 syslog(LOG_DEBUG, "krb5_get_init_creds_keytab: %d", (int)ret);
227 ret = krb5_cc_default(context, &cc);
229 syslog(LOG_DEBUG, "krb5_cc_default: %d", (int)ret);
233 ret = krb5_cc_initialize(context, cc, me);
235 syslog(LOG_DEBUG, "krb5_cc_initialize: %d", (int)ret);
239 ret = krb5_cc_store_cred(context, cc, &my_creds);
241 syslog(LOG_DEBUG, "krb5_cc_store_cred: %d", (int)ret);
243 ccname = strdup(krb5_cc_default_name(context));
245 syslog(LOG_ERR, "Unable to allocate memory");
248 krb5_free_cred_contents(context, &my_creds);
251 krb5_free_principal(context, me);
253 krb5_cc_close(context, cc);
255 krb5_kt_close(context, keytab);
257 krb5_free_context(context);
261 /* search for a credcache that looks like a likely candidate */
262 static char *find_krb5_cc(const char *dirname, uid_t uid)
264 struct dirent **namelist;
266 char ccname[MAX_CCNAME_LEN], *credpath, *best_cache = NULL;
268 time_t cred_time, best_time = 0;
270 n = scandir(dirname, &namelist, krb5cc_filter, NULL);
272 syslog(LOG_DEBUG, "%s: scandir error on directory '%s': %s",
273 __func__, dirname, strerror(errno));
277 for (i = 0; i < n; i++) {
278 snprintf(ccname, sizeof(ccname), "FILE:%s/%s", dirname,
279 namelist[i]->d_name);
280 credpath = ccname + 5;
281 syslog(LOG_DEBUG, "%s: considering %s", __func__, credpath);
283 if (lstat(credpath, &sbuf)) {
284 syslog(LOG_DEBUG, "%s: stat error on '%s': %s",
285 __func__, credpath, strerror(errno));
289 if (sbuf.st_uid != uid) {
290 syslog(LOG_DEBUG, "%s: %s is owned by %u, not %u",
291 __func__, credpath, sbuf.st_uid, uid);
295 if (!S_ISREG(sbuf.st_mode)) {
296 syslog(LOG_DEBUG, "%s: %s is not a regular file",
301 if (!(cred_time = get_tgt_time(ccname))) {
302 syslog(LOG_DEBUG, "%s: %s is not a valid credcache.",
308 if (cred_time <= best_time) {
309 syslog(LOG_DEBUG, "%s: %s expires sooner than current "
310 "best.", __func__, ccname);
315 syslog(LOG_DEBUG, "%s: %s is valid ccache", __func__, ccname);
317 best_cache = strndup(ccname, MAX_CCNAME_LEN);
318 best_time = cred_time;
327 cifs_krb5_get_req(const char *host, const char *ccname,
328 DATA_BLOB * mechtoken, DATA_BLOB * sess_key)
332 krb5_context context;
334 krb5_creds in_creds, *out_creds;
335 krb5_data apreq_pkt, in_data;
336 krb5_auth_context auth_context = NULL;
337 #if defined(HAVE_KRB5_AUTH_CON_SETADDRS) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE)
338 static const uint8_t gss_cksum[24] = { 0x10, 0x00, /* ... */};
341 ret = krb5_init_context(&context);
343 syslog(LOG_DEBUG, "%s: unable to init krb5 context", __func__);
347 ret = krb5_cc_resolve(context, ccname, &ccache);
349 syslog(LOG_DEBUG, "%s: unable to resolve %s to ccache\n",
351 goto out_free_context;
354 memset(&in_creds, 0, sizeof(in_creds));
356 ret = krb5_cc_get_principal(context, ccache, &in_creds.client);
358 syslog(LOG_DEBUG, "%s: unable to get client principal name",
360 goto out_free_ccache;
363 ret = krb5_sname_to_principal(context, host, "cifs", KRB5_NT_UNKNOWN,
366 syslog(LOG_DEBUG, "%s: unable to convert sname to princ (%s).",
368 goto out_free_principal;
371 ret = krb5_get_credentials(context, 0, ccache, &in_creds, &out_creds);
372 krb5_free_principal(context, in_creds.server);
374 syslog(LOG_DEBUG, "%s: unable to get credentials for %s",
376 goto out_free_principal;
382 ret = krb5_auth_con_init(context, &auth_context);
384 syslog(LOG_DEBUG, "%s: unable to create auth_context: %d",
389 #if defined(HAVE_KRB5_AUTH_CON_SETADDRS) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE)
390 /* Ensure we will get an addressless ticket. */
391 ret = krb5_auth_con_setaddrs(context, auth_context, NULL, NULL);
393 syslog(LOG_DEBUG, "%s: unable to set NULL addrs: %d",
399 * Create a GSSAPI checksum (0x8003), see RFC 4121.
401 * The current layout is
403 * 0x10, 0x00, 0x00, 0x00 - length = 16
404 * 0x00, 0x00, 0x00, 0x00 - channel binding info - 16 zero bytes
405 * 0x00, 0x00, 0x00, 0x00
406 * 0x00, 0x00, 0x00, 0x00
407 * 0x00, 0x00, 0x00, 0x00
408 * 0x00, 0x00, 0x00, 0x00 - flags
410 * GSS_C_NO_CHANNEL_BINDINGS means 16 zero bytes,
411 * this is needed to work against some closed source
414 * See https://bugzilla.samba.org/show_bug.cgi?id=7890
416 in_data.data = discard_const_p(char, gss_cksum);
418 ret = krb5_auth_con_set_req_cksumtype(context, auth_context, 0x8003);
420 syslog(LOG_DEBUG, "%s: unable to set 0x8003 checksum",
426 apreq_pkt.length = 0;
427 apreq_pkt.data = NULL;
428 ret = krb5_mk_req_extended(context, &auth_context, AP_OPTS_USE_SUBKEY,
429 &in_data, out_creds, &apreq_pkt);
431 syslog(LOG_DEBUG, "%s: unable to make AP-REQ for %s",
436 ret = krb5_auth_con_getsendsubkey(context, auth_context, &tokb);
438 syslog(LOG_DEBUG, "%s: unable to get session key for %s",
443 *mechtoken = data_blob(apreq_pkt.data, apreq_pkt.length);
444 *sess_key = data_blob(KRB5_KEY_DATA(tokb), KRB5_KEY_LENGTH(tokb));
446 krb5_free_keyblock(context, tokb);
448 krb5_auth_con_free(context, auth_context);
450 krb5_free_creds(context, out_creds);
452 krb5_free_principal(context, in_creds.client);
454 #if defined(KRB5_TC_OPENCLOSE)
455 krb5_cc_set_flags(context, ccache, KRB5_TC_OPENCLOSE);
457 krb5_cc_close(context, ccache);
459 krb5_free_context(context);
464 * Prepares AP-REQ data for mechToken and gets session key
465 * Uses credentials from cache. It will not ask for password
466 * you should receive credentials for yuor name manually using
467 * kinit or whatever you wish.
470 * oid - string with OID/ Could be OID_KERBEROS5
471 * or OID_KERBEROS5_OLD
472 * principal - Service name.
473 * Could be "cifs/FQDN" for KRB5 OID
474 * or for MS_KRB5 OID style server principal
475 * like "pdc$@YOUR.REALM.NAME"
478 * secblob - pointer for spnego wrapped AP-REQ data to be stored
479 * sess_key- pointer for SessionKey data to be stored
481 * ret: 0 - success, others - failure
484 handle_krb5_mech(const char *oid, const char *host, DATA_BLOB * secblob,
485 DATA_BLOB * sess_key, const char *ccname)
488 DATA_BLOB tkt, tkt_wrapped;
490 syslog(LOG_DEBUG, "%s: getting service ticket for %s", __func__, host);
492 /* get a kerberos ticket for the service and extract the session key */
493 retval = cifs_krb5_get_req(host, ccname, &tkt, sess_key);
495 syslog(LOG_DEBUG, "%s: failed to obtain service ticket (%d)",
500 syslog(LOG_DEBUG, "%s: obtained service ticket", __func__);
502 /* wrap that up in a nice GSS-API wrapping */
503 tkt_wrapped = spnego_gen_krb5_wrap(tkt, TOK_ID_KRB_AP_REQ);
505 /* and wrap that in a shiny SPNEGO wrapper */
506 *secblob = gen_negTokenInit(oid, tkt_wrapped);
508 data_blob_free(&tkt_wrapped);
509 data_blob_free(&tkt);
513 #define DKD_HAVE_HOSTNAME 0x1
514 #define DKD_HAVE_VERSION 0x2
515 #define DKD_HAVE_SEC 0x4
516 #define DKD_HAVE_IP 0x8
517 #define DKD_HAVE_UID 0x10
518 #define DKD_HAVE_PID 0x20
519 #define DKD_HAVE_CREDUID 0x40
520 #define DKD_HAVE_USERNAME 0x80
521 #define DKD_MUSTHAVE_SET (DKD_HAVE_HOSTNAME|DKD_HAVE_VERSION|DKD_HAVE_SEC)
523 struct decoded_args {
535 decode_key_description(const char *desc, struct decoded_args *arg)
540 const char *tkn = desc;
543 pos = index(tkn, ';');
544 if (strncmp(tkn, "host=", 5) == 0) {
552 SAFE_FREE(arg->hostname);
553 arg->hostname = strndup(tkn + 5, len);
554 if (arg->hostname == NULL) {
555 syslog(LOG_ERR, "Unable to allocate memory");
558 retval |= DKD_HAVE_HOSTNAME;
559 syslog(LOG_DEBUG, "host=%s", arg->hostname);
560 } else if (!strncmp(tkn, "ip4=", 4) || !strncmp(tkn, "ip6=", 4)) {
568 arg->ip = strndup(tkn + 4, len);
569 if (arg->ip == NULL) {
570 syslog(LOG_ERR, "Unable to allocate memory");
573 retval |= DKD_HAVE_IP;
574 syslog(LOG_DEBUG, "ip=%s", arg->ip);
575 } else if (strncmp(tkn, "user=", 5) == 0) {
582 SAFE_FREE(arg->username);
583 arg->username = strndup(tkn + 5, len);
584 if (arg->username == NULL) {
585 syslog(LOG_ERR, "Unable to allocate memory");
588 retval |= DKD_HAVE_USERNAME;
589 syslog(LOG_DEBUG, "user=%s", arg->username);
590 } else if (strncmp(tkn, "pid=", 4) == 0) {
592 arg->pid = strtol(tkn + 4, NULL, 0);
594 syslog(LOG_ERR, "Invalid pid format: %s",
598 syslog(LOG_DEBUG, "pid=%u", arg->pid);
599 retval |= DKD_HAVE_PID;
600 } else if (strncmp(tkn, "sec=", 4) == 0) {
601 if (strncmp(tkn + 4, "krb5", 4) == 0) {
602 retval |= DKD_HAVE_SEC;
604 } else if (strncmp(tkn + 4, "mskrb5", 6) == 0) {
605 retval |= DKD_HAVE_SEC;
608 syslog(LOG_DEBUG, "sec=%d", arg->sec);
609 } else if (strncmp(tkn, "uid=", 4) == 0) {
611 arg->uid = strtol(tkn + 4, NULL, 16);
613 syslog(LOG_ERR, "Invalid uid format: %s",
617 retval |= DKD_HAVE_UID;
618 syslog(LOG_DEBUG, "uid=%u", arg->uid);
619 } else if (strncmp(tkn, "creduid=", 8) == 0) {
621 arg->creduid = strtol(tkn + 8, NULL, 16);
623 syslog(LOG_ERR, "Invalid creduid format: %s",
627 retval |= DKD_HAVE_CREDUID;
628 syslog(LOG_DEBUG, "creduid=%u", arg->creduid);
629 } else if (strncmp(tkn, "ver=", 4) == 0) { /* if version */
631 arg->ver = strtol(tkn + 4, NULL, 16);
633 syslog(LOG_ERR, "Invalid version format: %s",
637 retval |= DKD_HAVE_VERSION;
638 syslog(LOG_DEBUG, "ver=%d", arg->ver);
647 static int cifs_resolver(const key_serial_t key, const char *key_descr)
650 struct addrinfo *addr;
651 char ip[INET6_ADDRSTRLEN];
653 const char *keyend = key_descr;
654 /* skip next 4 ';' delimiters to get to description */
655 for (c = 1; c <= 4; c++) {
656 keyend = index(keyend + 1, ';');
658 syslog(LOG_ERR, "invalid key description: %s",
665 /* resolve name to ip */
666 c = getaddrinfo(keyend, NULL, NULL, &addr);
668 syslog(LOG_ERR, "unable to resolve hostname: %s [%s]",
669 keyend, gai_strerror(c));
673 /* conver ip to string form */
674 if (addr->ai_family == AF_INET)
675 p = &(((struct sockaddr_in *)addr->ai_addr)->sin_addr);
677 p = &(((struct sockaddr_in6 *)addr->ai_addr)->sin6_addr);
679 if (!inet_ntop(addr->ai_family, p, ip, sizeof(ip))) {
680 syslog(LOG_ERR, "%s: inet_ntop: %s", __func__, strerror(errno));
686 c = keyctl_instantiate(key, ip, strlen(ip) + 1, 0);
688 syslog(LOG_ERR, "%s: keyctl_instantiate: %s", __func__,
699 * Older kernels sent IPv6 addresses without colons. Well, at least
700 * they're fixed-length strings. Convert these addresses to have colon
701 * delimiters to make getaddrinfo happy.
703 static void convert_inet6_addr(const char *from, char *to)
709 if (!(i++ % 4) && *from)
715 static int ip_to_fqdn(const char *addrstr, char *host, size_t hostlen)
718 struct addrinfo hints = {.ai_flags = AI_NUMERICHOST };
719 struct addrinfo *res;
720 const char *ipaddr = addrstr;
721 char converted[INET6_ADDRSTRLEN + 1];
723 if ((strlen(ipaddr) > INET_ADDRSTRLEN) && !strchr(ipaddr, ':')) {
724 convert_inet6_addr(ipaddr, converted);
728 rc = getaddrinfo(ipaddr, NULL, &hints, &res);
730 syslog(LOG_DEBUG, "%s: failed to resolve %s to "
731 "ipaddr: %s", __func__, ipaddr,
732 rc == EAI_SYSTEM ? strerror(errno) : gai_strerror(rc));
736 rc = getnameinfo(res->ai_addr, res->ai_addrlen, host, hostlen,
737 NULL, 0, NI_NAMEREQD);
740 syslog(LOG_DEBUG, "%s: failed to resolve %s to fqdn: %s",
742 rc == EAI_SYSTEM ? strerror(errno) : gai_strerror(rc));
746 syslog(LOG_DEBUG, "%s: resolved %s to %s", __func__, ipaddr, host);
750 /* walk a string and lowercase it in-place */
752 lowercase_string(char *c)
760 static void usage(void)
762 fprintf(stderr, "Usage: %s [-k /path/to/krb5.conf] [-t] [-v] [-l] key_serial\n", prog);
765 const struct option long_options[] = {
766 {"krb5conf", 1, NULL, 'k'},
767 {"legacy-uid", 0, NULL, 'l'},
768 {"trust-dns", 0, NULL, 't'},
769 {"version", 0, NULL, 'v'},
773 int main(const int argc, char *const argv[])
775 struct cifs_spnego_msg *keydata = NULL;
776 DATA_BLOB secblob = data_blob_null;
777 DATA_BLOB sess_key = data_blob_null;
778 key_serial_t key = 0;
782 int c, try_dns = 0, legacy_uid = 0;
783 char *buf, *ccname = NULL;
784 char hostbuf[NI_MAXHOST], *host;
785 struct decoded_args arg;
788 char *keytab_name = CIFS_DEFAULT_KRB5_KEYTAB;
791 memset(&arg, 0, sizeof(arg));
793 openlog(prog, 0, LOG_DAEMON);
795 while ((c = getopt_long(argc, argv, "ck:ltv", long_options, NULL)) != -1) {
798 /* legacy option -- skip it */
804 if (setenv("KRB5_CONFIG", optarg, 1) != 0) {
805 syslog(LOG_ERR, "unable to set $KRB5_CONFIG: %d", errno);
813 printf("version: %s\n", VERSION);
816 syslog(LOG_ERR, "unknown option: %c", c);
821 /* is there a key? */
822 if (argc <= optind) {
827 /* get key and keyring values */
829 key = strtol(argv[optind], NULL, 10);
832 syslog(LOG_ERR, "Invalid key format: %s", strerror(errno));
836 rc = keyctl_describe_alloc(key, &buf);
838 syslog(LOG_ERR, "keyctl_describe_alloc failed: %s",
844 syslog(LOG_DEBUG, "key description: %s", buf);
846 if ((strncmp(buf, "cifs.resolver", sizeof("cifs.resolver") - 1) == 0) ||
847 (strncmp(buf, "dns_resolver", sizeof("dns_resolver") - 1) == 0)) {
848 rc = cifs_resolver(key, buf);
852 have = decode_key_description(buf, &arg);
854 if ((have & DKD_MUSTHAVE_SET) != DKD_MUSTHAVE_SET) {
855 syslog(LOG_ERR, "unable to get necessary params from key "
856 "description (0x%x)", have);
861 if (arg.ver > CIFS_SPNEGO_UPCALL_VERSION) {
862 syslog(LOG_ERR, "incompatible kernel upcall version: 0x%x",
868 if (strlen(arg.hostname) >= NI_MAXHOST) {
869 syslog(LOG_ERR, "hostname provided by kernel is too long");
875 if (!legacy_uid && (have & DKD_HAVE_CREDUID))
877 else if (have & DKD_HAVE_UID)
880 /* no uid= or creduid= parm -- something is wrong */
881 syslog(LOG_ERR, "No uid= or creduid= parm specified");
888 syslog(LOG_ERR, "setuid: %s", strerror(errno));
891 ccname = find_krb5_cc(CIFS_DEFAULT_KRB5_DIR, uid);
893 /* Couldn't find credcache? Try to use keytab */
894 if (ccname == NULL && arg.username != NULL)
895 ccname = init_cc_from_keytab(keytab_name, arg.username);
899 // do mech specific authorization
904 * Andrew Bartlett's suggested scheme for picking a principal
905 * name, based on a supplied hostname.
910 * cifs/fooo.<guessed domain ?>@REALM
912 * INPUT: bar.example.com
914 * cifs/bar.example.com@REALM
916 if (arg.sec == MS_KRB5)
917 oid = OID_KERBEROS5_OLD;
922 lowercase_string(host);
923 rc = handle_krb5_mech(oid, host, &secblob, &sess_key, ccname);
928 * If hostname has a '.', assume it's a FQDN, otherwise we
929 * want to guess the domainname.
931 if (!strchr(host, '.')) {
932 struct addrinfo hints;
935 char fqdn[NI_MAXHOST];
938 * use getaddrinfo() to resolve the hostname of the
939 * server and set ai_canonname.
941 memset(&hints, 0, sizeof(hints));
942 hints.ai_family = AF_UNSPEC;
943 hints.ai_flags = AI_CANONNAME;
944 rc = getaddrinfo(host, NULL, &hints, &ai);
946 syslog(LOG_ERR, "Unable to resolve host address: %s [%s]",
947 host, gai_strerror(rc));
951 /* scan forward to first '.' in ai_canonnname */
952 domainname = strchr(ai->ai_canonname, '.');
958 lowercase_string(domainname);
959 rc = snprintf(fqdn, sizeof(fqdn), "%s%s",
962 if (rc < 0 || (size_t)rc >= sizeof(fqdn)) {
963 syslog(LOG_ERR, "Problem setting hostname in string: %ld", rc);
968 rc = handle_krb5_mech(oid, fqdn, &secblob, &sess_key, ccname);
973 if (!try_dns || !(have & DKD_HAVE_IP))
976 rc = ip_to_fqdn(arg.ip, hostbuf, sizeof(hostbuf));
982 goto retry_new_hostname;
984 syslog(LOG_ERR, "sectype: %d is not implemented", arg.sec);
992 /* pack SecurityBLob and SessionKey into downcall packet */
994 sizeof(struct cifs_spnego_msg) + secblob.length + sess_key.length;
995 keydata = (struct cifs_spnego_msg *)calloc(sizeof(char), datalen);
1000 keydata->version = arg.ver;
1002 keydata->sesskey_len = sess_key.length;
1003 keydata->secblob_len = secblob.length;
1004 memcpy(&(keydata->data), sess_key.data, sess_key.length);
1005 memcpy(&(keydata->data) + keydata->sesskey_len,
1006 secblob.data, secblob.length);
1009 rc = keyctl_instantiate(key, keydata, datalen, 0);
1011 syslog(LOG_ERR, "keyctl_instantiate: %s", strerror(errno));
1015 /* BB: maybe we need use timeout for key: for example no more then
1016 * ticket lifietime? */
1017 /* keyctl_set_timeout( key, 60); */
1020 * on error, negatively instantiate the key ourselves so that we can
1021 * make sure the kernel doesn't hang it off of a searchable keyring
1022 * and interfere with the next attempt to instantiate the key.
1024 if (rc != 0 && key == 0)
1025 keyctl_negate(key, 1, KEY_REQKEY_DEFL_DEFAULT);
1026 data_blob_free(&secblob);
1027 data_blob_free(&sess_key);
1029 SAFE_FREE(arg.hostname);
1031 SAFE_FREE(arg.username);