cifs.upcall: formatting cleanup
[Samba.git] / client / cifs.upcall.c
blob926ec2064d7794953e8fbc8dced8d41d7e67e45c
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.2";
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_IPV4 0x8
154 #define DKD_HAVE_IPV6 0x10
155 #define DKD_HAVE_UID 0x20
156 #define DKD_HAVE_PID 0x40
157 #define DKD_MUSTHAVE_SET (DKD_HAVE_HOSTNAME|DKD_HAVE_VERSION|DKD_HAVE_SEC)
159 static int
160 decode_key_description(const char *desc, int *ver, secType_t *sec,
161 char **hostname, uid_t *uid, pid_t *pid)
163 int retval = 0;
164 char *pos;
165 const char *tkn = desc;
167 do {
168 pos = index(tkn, ';');
169 if (strncmp(tkn, "host=", 5) == 0) {
170 int len;
172 if (pos == NULL)
173 len = strlen(tkn);
174 else
175 len = pos - tkn;
177 len -= 4;
178 SAFE_FREE(*hostname);
179 *hostname = SMB_XMALLOC_ARRAY(char, len);
180 strlcpy(*hostname, tkn + 5, len);
181 retval |= DKD_HAVE_HOSTNAME;
182 } else if (strncmp(tkn, "ipv4=", 5) == 0) {
183 /* BB: do we need it if we have hostname already? */
184 } else if (strncmp(tkn, "ipv6=", 5) == 0) {
185 /* BB: do we need it if we have hostname already? */
186 } else if (strncmp(tkn, "pid=", 4) == 0) {
187 errno = 0;
188 *pid = strtol(tkn + 4, NULL, 0);
189 if (errno != 0) {
190 syslog(LOG_ERR, "Invalid pid format: %s",
191 strerror(errno));
192 return 1;
193 } else {
194 retval |= DKD_HAVE_PID;
196 } else if (strncmp(tkn, "sec=", 4) == 0) {
197 if (strncmp(tkn + 4, "krb5", 4) == 0) {
198 retval |= DKD_HAVE_SEC;
199 *sec = KRB5;
200 } else if (strncmp(tkn + 4, "mskrb5", 6) == 0) {
201 retval |= DKD_HAVE_SEC;
202 *sec = MS_KRB5;
204 } else if (strncmp(tkn, "uid=", 4) == 0) {
205 errno = 0;
206 *uid = strtol(tkn + 4, NULL, 16);
207 if (errno != 0) {
208 syslog(LOG_ERR, "Invalid uid format: %s",
209 strerror(errno));
210 return 1;
211 } else {
212 retval |= DKD_HAVE_UID;
214 } else if (strncmp(tkn, "ver=", 4) == 0) { /* if version */
215 errno = 0;
216 *ver = strtol(tkn + 4, NULL, 16);
217 if (errno != 0) {
218 syslog(LOG_ERR, "Invalid version format: %s",
219 strerror(errno));
220 return 1;
221 } else {
222 retval |= DKD_HAVE_VERSION;
225 if (pos == NULL)
226 break;
227 tkn = pos + 1;
228 } while (tkn);
229 return retval;
232 static int
233 cifs_resolver(const key_serial_t key, const char *key_descr)
235 int c;
236 struct addrinfo *addr;
237 char ip[INET6_ADDRSTRLEN];
238 void *p;
239 const char *keyend = key_descr;
240 /* skip next 4 ';' delimiters to get to description */
241 for (c = 1; c <= 4; c++) {
242 keyend = index(keyend+1, ';');
243 if (!keyend) {
244 syslog(LOG_ERR, "invalid key description: %s",
245 key_descr);
246 return 1;
249 keyend++;
251 /* resolve name to ip */
252 c = getaddrinfo(keyend, NULL, NULL, &addr);
253 if (c) {
254 syslog(LOG_ERR, "unable to resolve hostname: %s [%s]",
255 keyend, gai_strerror(c));
256 return 1;
259 /* conver ip to string form */
260 if (addr->ai_family == AF_INET)
261 p = &(((struct sockaddr_in *)addr->ai_addr)->sin_addr);
262 else
263 p = &(((struct sockaddr_in6 *)addr->ai_addr)->sin6_addr);
265 if (!inet_ntop(addr->ai_family, p, ip, sizeof(ip))) {
266 syslog(LOG_ERR, "%s: inet_ntop: %s", __func__, strerror(errno));
267 freeaddrinfo(addr);
268 return 1;
271 /* setup key */
272 c = keyctl_instantiate(key, ip, strlen(ip)+1, 0);
273 if (c == -1) {
274 syslog(LOG_ERR, "%s: keyctl_instantiate: %s", __func__,
275 strerror(errno));
276 freeaddrinfo(addr);
277 return 1;
280 freeaddrinfo(addr);
281 return 0;
284 static void
285 usage(void)
287 syslog(LOG_INFO, "Usage: %s [-c] [-v] key_serial", prog);
288 fprintf(stderr, "Usage: %s [-c] [-v] key_serial\n", prog);
291 int main(const int argc, char *const argv[])
293 struct cifs_spnego_msg *keydata = NULL;
294 DATA_BLOB secblob = data_blob_null;
295 DATA_BLOB sess_key = data_blob_null;
296 secType_t sectype = NONE;
297 key_serial_t key = 0;
298 size_t datalen;
299 long rc = 1;
300 uid_t uid = 0;
301 pid_t pid = 0;
302 int kernel_upcall_version = 0;
303 int c, use_cifs_service_prefix = 0;
304 char *buf, *princ, *ccname = NULL, *hostname = NULL;
305 const char *oid;
307 openlog(prog, 0, LOG_DAEMON);
309 while ((c = getopt(argc, argv, "cv")) != -1) {
310 switch (c) {
311 case 'c':
312 use_cifs_service_prefix = 1;
313 break;
314 case 'v':
315 printf("version: %s\n", CIFSSPNEGO_VERSION);
316 goto out;
317 default:
318 syslog(LOG_ERR, "unknown option: %c", c);
319 goto out;
323 /* is there a key? */
324 if (argc <= optind) {
325 usage();
326 goto out;
329 /* get key and keyring values */
330 errno = 0;
331 key = strtol(argv[optind], NULL, 10);
332 if (errno != 0) {
333 key = 0;
334 syslog(LOG_ERR, "Invalid key format: %s", strerror(errno));
335 goto out;
338 rc = keyctl_describe_alloc(key, &buf);
339 if (rc == -1) {
340 syslog(LOG_ERR, "keyctl_describe_alloc failed: %s",
341 strerror(errno));
342 rc = 1;
343 goto out;
346 syslog(LOG_DEBUG, "key description: %s", buf);
348 if ((strncmp(buf, "cifs.resolver", sizeof("cifs.resolver")-1) == 0) ||
349 (strncmp(buf, "dns_resolver", sizeof("dns_resolver")-1) == 0)) {
350 rc = cifs_resolver(key, buf);
351 goto out;
354 rc = decode_key_description(buf, &kernel_upcall_version, &sectype,
355 &hostname, &uid, &pid);
356 if ((rc & DKD_MUSTHAVE_SET) != DKD_MUSTHAVE_SET) {
357 syslog(LOG_ERR, "unable to get necessary params from key "
358 "description (0x%x)", rc);
359 rc = 1;
360 SAFE_FREE(buf);
361 goto out;
363 SAFE_FREE(buf);
365 if (kernel_upcall_version > CIFS_SPNEGO_UPCALL_VERSION) {
366 syslog(LOG_ERR, "incompatible kernel upcall version: 0x%x",
367 kernel_upcall_version);
368 rc = 1;
369 goto out;
372 if (rc & DKD_HAVE_PID)
373 ccname = get_krb5_ccname(pid);
375 if (rc & DKD_HAVE_UID) {
376 rc = setuid(uid);
377 if (rc == -1) {
378 syslog(LOG_ERR, "setuid: %s", strerror(errno));
379 goto out;
383 // do mech specific authorization
384 switch (sectype) {
385 case MS_KRB5:
386 case KRB5:
387 /* for "cifs/" service name + terminating 0 */
388 datalen = strlen(hostname) + 5 + 1;
389 princ = SMB_XMALLOC_ARRAY(char, datalen);
390 if (!princ) {
391 rc = 1;
392 break;
395 if (use_cifs_service_prefix)
396 strlcpy(princ, "cifs/", datalen);
397 else
398 strlcpy(princ, "host/", datalen);
400 strlcpy(princ + 5, hostname, datalen - 5);
402 if (sectype == MS_KRB5)
403 oid = OID_KERBEROS5_OLD;
404 else
405 oid = OID_KERBEROS5;
407 rc = handle_krb5_mech(oid, princ, &secblob, &sess_key, ccname);
408 SAFE_FREE(princ);
409 break;
410 default:
411 syslog(LOG_ERR, "sectype: %d is not implemented", sectype);
412 rc = 1;
413 break;
416 if (rc)
417 goto out;
419 /* pack SecurityBLob and SessionKey into downcall packet */
420 datalen =
421 sizeof(struct cifs_spnego_msg) + secblob.length + sess_key.length;
422 keydata = (struct cifs_spnego_msg*)SMB_XMALLOC_ARRAY(char, datalen);
423 if (!keydata) {
424 rc = 1;
425 goto out;
427 keydata->version = kernel_upcall_version;
428 keydata->flags = 0;
429 keydata->sesskey_len = sess_key.length;
430 keydata->secblob_len = secblob.length;
431 memcpy(&(keydata->data), sess_key.data, sess_key.length);
432 memcpy(&(keydata->data) + keydata->sesskey_len,
433 secblob.data, secblob.length);
435 /* setup key */
436 rc = keyctl_instantiate(key, keydata, datalen, 0);
437 if (rc == -1) {
438 syslog(LOG_ERR, "keyctl_instantiate: %s", strerror(errno));
439 goto out;
442 /* BB: maybe we need use timeout for key: for example no more then
443 * ticket lifietime? */
444 /* keyctl_set_timeout( key, 60); */
445 out:
447 * on error, negatively instantiate the key ourselves so that we can
448 * make sure the kernel doesn't hang it off of a searchable keyring
449 * and interfere with the next attempt to instantiate the key.
451 if (rc != 0 && key == 0)
452 keyctl_negate(key, 1, KEY_REQKEY_DEFL_DEFAULT);
453 data_blob_free(&secblob);
454 data_blob_free(&sess_key);
455 SAFE_FREE(ccname);
456 SAFE_FREE(hostname);
457 SAFE_FREE(keydata);
458 return rc;