cifs.upcall: fix IPv6 addrs sent to upcall to have colon delimiters
[Samba.git] / source / client / cifs.upcall.c
blobf433e495c6879f910586657ffc83fa37d441b0b9
1 /*
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
28 #include "includes.h"
29 #include <keyutils.h>
31 #include "cifs_spnego.h"
33 const char *CIFSSPNEGO_VERSION = "1.3";
34 static const char *prog = "cifs.upcall";
35 typedef enum _sectype {
36 NONE = 0,
37 KRB5,
38 MS_KRB5
39 } sectype_t;
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.
45 static char *
46 get_krb5_ccname(pid_t pid)
48 int fd;
49 ssize_t len, left;
52 * FIXME: sysconf for ARG_MAX instead? Kernel seems to be limited to a
53 * page however, so it may not matter.
55 char buf[4096];
56 char *p, *value = NULL;
58 buf[4095] = '\0';
59 snprintf(buf, 4095, "/proc/%d/environ", pid);
60 fd = open(buf, O_RDONLY);
61 if (fd < 0) {
62 syslog(LOG_DEBUG, "%s: unable to open %s: %d", __func__, buf,
63 errno);
64 return NULL;
67 /* FIXME: don't assume that we get it all in the first read? */
68 len = read(fd, buf, 4096);
69 close(fd);
70 if (len < 0) {
71 syslog(LOG_DEBUG, "%s: unable to read from /proc/%d/environ: "
72 "%d", __func__, pid, errno);
73 return NULL;
76 left = len;
77 p = buf;
79 /* can't have valid KRB5CCNAME if there are < 13 bytes left */
80 while (left > 12) {
81 if (strncmp("KRB5CCNAME=", p, 11)) {
82 p += strnlen(p, left);
83 ++p;
84 left = buf + len - p;
85 continue;
87 p += 11;
88 left -= 11;
89 value = SMB_STRNDUP(p, left);
90 break;
92 syslog(LOG_DEBUG, "%s: KRB5CCNAME=%s", __func__,
93 value ? value : "(null)");
94 return value;
98 * Prepares AP-REQ data for mechToken and gets session key
99 * Uses credentials from cache. It will not ask for password
100 * you should receive credentials for yuor name manually using
101 * kinit or whatever you wish.
103 * in:
104 * oid - string with OID/ Could be OID_KERBEROS5
105 * or OID_KERBEROS5_OLD
106 * principal - Service name.
107 * Could be "cifs/FQDN" for KRB5 OID
108 * or for MS_KRB5 OID style server principal
109 * like "pdc$@YOUR.REALM.NAME"
111 * out:
112 * secblob - pointer for spnego wrapped AP-REQ data to be stored
113 * sess_key- pointer for SessionKey data to be stored
115 * ret: 0 - success, others - failure
117 static int
118 handle_krb5_mech(const char *oid, const char *principal, DATA_BLOB *secblob,
119 DATA_BLOB *sess_key, const char *ccname)
121 int retval;
122 DATA_BLOB tkt, tkt_wrapped;
124 syslog(LOG_DEBUG, "%s: getting service ticket for %s", __func__,
125 principal);
127 /* get a kerberos ticket for the service and extract the session key */
128 retval = cli_krb5_get_ticket(principal, 0, &tkt, sess_key, 0, ccname,
129 NULL);
131 if (retval) {
132 syslog(LOG_DEBUG, "%s: failed to obtain service ticket (%d)",
133 __func__, retval);
134 return retval;
137 syslog(LOG_DEBUG, "%s: obtained service ticket", __func__);
139 /* wrap that up in a nice GSS-API wrapping */
140 tkt_wrapped = spnego_gen_krb5_wrap(tkt, TOK_ID_KRB_AP_REQ);
142 /* and wrap that in a shiny SPNEGO wrapper */
143 *secblob = gen_negTokenInit(oid, tkt_wrapped);
145 data_blob_free(&tkt_wrapped);
146 data_blob_free(&tkt);
147 return retval;
150 #define DKD_HAVE_HOSTNAME 0x1
151 #define DKD_HAVE_VERSION 0x2
152 #define DKD_HAVE_SEC 0x4
153 #define DKD_HAVE_IP 0x8
154 #define DKD_HAVE_UID 0x10
155 #define DKD_HAVE_PID 0x20
156 #define DKD_MUSTHAVE_SET (DKD_HAVE_IP|DKD_HAVE_VERSION|DKD_HAVE_SEC)
158 static struct decoded_args {
159 int ver;
160 char *hostname;
161 char *ip;
162 uid_t uid;
163 pid_t pid;
164 sectype_t sec;
167 static unsigned int
168 decode_key_description(const char *desc, struct decoded_args *arg)
170 int len;
171 int retval = 0;
172 char *pos;
173 const char *tkn = desc;
175 do {
176 pos = index(tkn, ';');
177 if (strncmp(tkn, "host=", 5) == 0) {
179 if (pos == NULL)
180 len = strlen(tkn);
181 else
182 len = pos - tkn;
184 len -= 4;
185 SAFE_FREE(arg->hostname);
186 arg->hostname = SMB_XMALLOC_ARRAY(char, len);
187 strlcpy(arg->hostname, tkn + 5, len);
188 retval |= DKD_HAVE_HOSTNAME;
189 } else if (!strncmp(tkn, "ip4=", 4) ||
190 !strncmp(tkn, "ip6=", 4)) {
191 if (pos == NULL)
192 len = strlen(tkn);
193 else
194 len = pos - tkn;
196 len -= 3;
197 SAFE_FREE(arg->ip);
198 arg->ip = SMB_XMALLOC_ARRAY(char, len);
199 strlcpy(arg->ip, tkn + 4, len);
200 retval |= DKD_HAVE_IP;
201 } else if (strncmp(tkn, "pid=", 4) == 0) {
202 errno = 0;
203 arg->pid = strtol(tkn + 4, NULL, 0);
204 if (errno != 0) {
205 syslog(LOG_ERR, "Invalid pid format: %s",
206 strerror(errno));
207 return 1;
208 } else {
209 retval |= DKD_HAVE_PID;
211 } else if (strncmp(tkn, "sec=", 4) == 0) {
212 if (strncmp(tkn + 4, "krb5", 4) == 0) {
213 retval |= DKD_HAVE_SEC;
214 arg->sec = KRB5;
215 } else if (strncmp(tkn + 4, "mskrb5", 6) == 0) {
216 retval |= DKD_HAVE_SEC;
217 arg->sec = MS_KRB5;
219 } else if (strncmp(tkn, "uid=", 4) == 0) {
220 errno = 0;
221 arg->uid = strtol(tkn + 4, NULL, 16);
222 if (errno != 0) {
223 syslog(LOG_ERR, "Invalid uid format: %s",
224 strerror(errno));
225 return 1;
226 } else {
227 retval |= DKD_HAVE_UID;
229 } else if (strncmp(tkn, "ver=", 4) == 0) { /* if version */
230 errno = 0;
231 arg->ver = strtol(tkn + 4, NULL, 16);
232 if (errno != 0) {
233 syslog(LOG_ERR, "Invalid version format: %s",
234 strerror(errno));
235 return 1;
236 } else {
237 retval |= DKD_HAVE_VERSION;
240 if (pos == NULL)
241 break;
242 tkn = pos + 1;
243 } while (tkn);
244 return retval;
247 static int
248 cifs_resolver(const key_serial_t key, const char *key_descr)
250 int c;
251 struct addrinfo *addr;
252 char ip[INET6_ADDRSTRLEN];
253 void *p;
254 const char *keyend = key_descr;
255 /* skip next 4 ';' delimiters to get to description */
256 for (c = 1; c <= 4; c++) {
257 keyend = index(keyend+1, ';');
258 if (!keyend) {
259 syslog(LOG_ERR, "invalid key description: %s",
260 key_descr);
261 return 1;
264 keyend++;
266 /* resolve name to ip */
267 c = getaddrinfo(keyend, NULL, NULL, &addr);
268 if (c) {
269 syslog(LOG_ERR, "unable to resolve hostname: %s [%s]",
270 keyend, gai_strerror(c));
271 return 1;
274 /* conver ip to string form */
275 if (addr->ai_family == AF_INET)
276 p = &(((struct sockaddr_in *)addr->ai_addr)->sin_addr);
277 else
278 p = &(((struct sockaddr_in6 *)addr->ai_addr)->sin6_addr);
280 if (!inet_ntop(addr->ai_family, p, ip, sizeof(ip))) {
281 syslog(LOG_ERR, "%s: inet_ntop: %s", __func__, strerror(errno));
282 freeaddrinfo(addr);
283 return 1;
286 /* setup key */
287 c = keyctl_instantiate(key, ip, strlen(ip)+1, 0);
288 if (c == -1) {
289 syslog(LOG_ERR, "%s: keyctl_instantiate: %s", __func__,
290 strerror(errno));
291 freeaddrinfo(addr);
292 return 1;
295 freeaddrinfo(addr);
296 return 0;
300 * Older kernels sent IPv6 addresses without colons. Well, at least
301 * they're fixed-length strings. Convert these addresses to have colon
302 * delimiters to make getaddrinfo happy.
304 static void
305 convert_inet6_addr(const char *from, char *to)
307 int i = 1;
309 while (*from) {
310 *to++ = *from++;
311 if (!(i++ % 4) && *from)
312 *to++ = ':';
314 *to = 0;
317 static int
318 ip_to_fqdn(const char *addrstr, char *host, size_t hostlen)
320 int rc;
321 struct addrinfo hints = { .ai_flags = AI_NUMERICHOST };
322 struct addrinfo *res;
323 const char *ipaddr = addrstr;
324 char converted[INET6_ADDRSTRLEN + 1];
326 if ((strlen(ipaddr) > INET_ADDRSTRLEN) && !strchr(ipaddr, ':')) {
327 convert_inet6_addr(ipaddr, converted);
328 ipaddr = converted;
331 rc = getaddrinfo(ipaddr, NULL, &hints, &res);
332 if (rc) {
333 syslog(LOG_DEBUG, "%s: failed to resolve %s to "
334 "ipaddr: %s", __func__, ipaddr,
335 rc == EAI_SYSTEM ? strerror(errno) : gai_strerror(rc));
336 return rc;
339 rc = getnameinfo(res->ai_addr, res->ai_addrlen, host, hostlen,
340 NULL, 0, NI_NAMEREQD);
341 freeaddrinfo(res);
342 if (rc) {
343 syslog(LOG_DEBUG, "%s: failed to resolve %s to fqdn: %s",
344 __func__, ipaddr,
345 rc == EAI_SYSTEM ? strerror(errno) : gai_strerror(rc));
346 return rc;
349 syslog(LOG_DEBUG, "%s: resolved %s to %s", __func__, ipaddr, host);
350 return 0;
353 static void
354 usage(void)
356 syslog(LOG_INFO, "Usage: %s [-v] key_serial", prog);
357 fprintf(stderr, "Usage: %s [-v] key_serial\n", prog);
360 int main(const int argc, char *const argv[])
362 struct cifs_spnego_msg *keydata = NULL;
363 DATA_BLOB secblob = data_blob_null;
364 DATA_BLOB sess_key = data_blob_null;
365 key_serial_t key = 0;
366 size_t datalen;
367 unsigned int have;
368 long rc = 1;
369 int c;
370 char *buf, *princ, *ccname = NULL;
371 char hostbuf[NI_MAXHOST];
372 struct decoded_args arg = { };
373 const char *oid;
375 openlog(prog, 0, LOG_DAEMON);
377 while ((c = getopt(argc, argv, "cv")) != -1) {
378 switch (c) {
379 case 'c':
380 /* legacy option -- skip it */
381 break;
382 case 'v':
383 printf("version: %s\n", CIFSSPNEGO_VERSION);
384 goto out;
385 default:
386 syslog(LOG_ERR, "unknown option: %c", c);
387 goto out;
391 /* is there a key? */
392 if (argc <= optind) {
393 usage();
394 goto out;
397 /* get key and keyring values */
398 errno = 0;
399 key = strtol(argv[optind], NULL, 10);
400 if (errno != 0) {
401 key = 0;
402 syslog(LOG_ERR, "Invalid key format: %s", strerror(errno));
403 goto out;
406 rc = keyctl_describe_alloc(key, &buf);
407 if (rc == -1) {
408 syslog(LOG_ERR, "keyctl_describe_alloc failed: %s",
409 strerror(errno));
410 rc = 1;
411 goto out;
414 syslog(LOG_DEBUG, "key description: %s", buf);
416 if ((strncmp(buf, "cifs.resolver", sizeof("cifs.resolver")-1) == 0) ||
417 (strncmp(buf, "dns_resolver", sizeof("dns_resolver")-1) == 0)) {
418 rc = cifs_resolver(key, buf);
419 goto out;
422 have = decode_key_description(buf, &arg);
423 SAFE_FREE(buf);
424 if ((have & DKD_MUSTHAVE_SET) != DKD_MUSTHAVE_SET) {
425 syslog(LOG_ERR, "unable to get necessary params from key "
426 "description (0x%x)", have);
427 rc = 1;
428 goto out;
431 if (arg.ver > CIFS_SPNEGO_UPCALL_VERSION) {
432 syslog(LOG_ERR, "incompatible kernel upcall version: 0x%x",
433 arg.ver);
434 rc = 1;
435 goto out;
438 if (have & DKD_HAVE_UID) {
439 rc = setuid(arg.uid);
440 if (rc == -1) {
441 syslog(LOG_ERR, "setuid: %s", strerror(errno));
442 goto out;
446 if (have & DKD_HAVE_PID)
447 ccname = get_krb5_ccname(arg.pid);
449 if (have & DKD_HAVE_IP) {
450 rc = ip_to_fqdn(arg.ip, hostbuf, sizeof(hostbuf));
451 if (rc)
452 goto out;
455 // do mech specific authorization
456 switch (arg.sec) {
457 case MS_KRB5:
458 case KRB5:
459 /* for "cifs/" service name + terminating 0 */
460 datalen = strnlen(hostbuf, sizeof(hostbuf)) + 5 + 1;
461 princ = SMB_XMALLOC_ARRAY(char, datalen);
462 if (!princ) {
463 rc = 1;
464 break;
467 if (arg.sec == MS_KRB5)
468 oid = OID_KERBEROS5_OLD;
469 else
470 oid = OID_KERBEROS5;
473 * try getting a cifs/ principal first and then fall back to
474 * getting a host/ principal if that doesn't work.
476 strlcpy(princ, "cifs/", datalen);
477 strlcpy(princ + 5, hostbuf, datalen - 5);
478 rc = handle_krb5_mech(oid, princ, &secblob, &sess_key, ccname);
479 if (rc) {
480 memcpy(princ, "host/", 5);
481 rc = handle_krb5_mech(oid, princ, &secblob, &sess_key,
482 ccname);
484 SAFE_FREE(princ);
485 break;
486 default:
487 syslog(LOG_ERR, "sectype: %d is not implemented", arg.sec);
488 rc = 1;
489 break;
492 if (rc)
493 goto out;
495 /* pack SecurityBLob and SessionKey into downcall packet */
496 datalen =
497 sizeof(struct cifs_spnego_msg) + secblob.length + sess_key.length;
498 keydata = (struct cifs_spnego_msg*)SMB_XMALLOC_ARRAY(char, datalen);
499 if (!keydata) {
500 rc = 1;
501 goto out;
503 keydata->version = arg.ver;
504 keydata->flags = 0;
505 keydata->sesskey_len = sess_key.length;
506 keydata->secblob_len = secblob.length;
507 memcpy(&(keydata->data), sess_key.data, sess_key.length);
508 memcpy(&(keydata->data) + keydata->sesskey_len,
509 secblob.data, secblob.length);
511 /* setup key */
512 rc = keyctl_instantiate(key, keydata, datalen, 0);
513 if (rc == -1) {
514 syslog(LOG_ERR, "keyctl_instantiate: %s", strerror(errno));
515 goto out;
518 /* BB: maybe we need use timeout for key: for example no more then
519 * ticket lifietime? */
520 /* keyctl_set_timeout( key, 60); */
521 out:
523 * on error, negatively instantiate the key ourselves so that we can
524 * make sure the kernel doesn't hang it off of a searchable keyring
525 * and interfere with the next attempt to instantiate the key.
527 if (rc != 0 && key == 0)
528 keyctl_negate(key, 1, KEY_REQKEY_DEFL_DEFAULT);
529 data_blob_free(&secblob);
530 data_blob_free(&sess_key);
531 SAFE_FREE(ccname);
532 SAFE_FREE(arg.hostname);
533 SAFE_FREE(keydata);
534 return rc;