umount.cifs: do not attempt to update /etc/mtab if it is symbolic link
[sfrench/samba-autobuild/.git] / client / cifs.upcall.c
1 /*
2 * CIFS user-space helper.
3 * Copyright (C) Igor Mammedov (niallain@gmail.com) 2007
4 * Copyright (C) Jeff Layton (jlayton@redhat.com) 2009
5 *
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:
11
12 create cifs.spnego * * /usr/local/sbin/cifs.upcall %k
13 create dns_resolver * * /usr/local/sbin/cifs.upcall %k
14
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
26 */
27
28 #include "includes.h"
29 #include <keyutils.h>
30
31 #include "cifs_spnego.h"
32
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;
40
41 /*
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.
44  */
45 static char *
46 get_krb5_ccname(pid_t pid)
47 {
48         int fd;
49         ssize_t len, left;
50
51         /*
52          * FIXME: sysconf for ARG_MAX instead? Kernel seems to be limited to a
53          * page however, so it may not matter.
54          */
55         char buf[4096];
56         char *p, *value = NULL;
57         
58         buf[4095] = '\0';
59         snprintf(buf, 4095, "/proc/%d/environ", pid);
60         fd = open(buf, O_RDONLY);
61         if (fd < 0)
62                 return NULL;
63
64         /* FIXME: don't assume that we get it all in the first read? */
65         len = read(fd, buf, 4096);
66         close(fd);
67         if (len < 0)
68                 return NULL;
69
70         left = len;
71         p = buf;
72
73         /* can't have valid KRB5CCNAME if there are < 13 bytes left */
74         while (left > 12) {
75                 if (strncmp("KRB5CCNAME=", p, 11)) {
76                         p += strnlen(p, left);
77                         ++p;
78                         left = buf + len - p;
79                         continue;
80                 }
81                 p += 11;
82                 left -= 11;
83                 value = SMB_STRNDUP(p, left);
84                 break;
85         }
86         return value;
87 }
88
89 /*
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.
94  *
95  * in:
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"
102  *
103  * out:
104  *      secblob -       pointer for spnego wrapped AP-REQ data to be stored
105  *      sess_key-       pointer for SessionKey data to be stored
106  *
107  * ret: 0 - success, others - failure
108 */
109 static int
110 handle_krb5_mech(const char *oid, const char *principal, DATA_BLOB *secblob,
111                  DATA_BLOB *sess_key, const char *ccname)
112 {
113         int retval;
114         DATA_BLOB tkt, tkt_wrapped;
115
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,
118                                      NULL);
119
120         if (retval)
121                 return retval;
122
123         /* wrap that up in a nice GSS-API wrapping */
124         tkt_wrapped = spnego_gen_krb5_wrap(tkt, TOK_ID_KRB_AP_REQ);
125
126         /* and wrap that in a shiny SPNEGO wrapper */
127         *secblob = gen_negTokenInit(oid, tkt_wrapped);
128
129         data_blob_free(&tkt_wrapped);
130         data_blob_free(&tkt);
131         return retval;
132 }
133
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)
142
143 static int
144 decode_key_description(const char *desc, int *ver, secType_t *sec,
145                            char **hostname, uid_t *uid, pid_t *pid)
146 {
147         int retval = 0;
148         char *pos;
149         const char *tkn = desc;
150
151         do {
152                 pos = index(tkn, ';');
153                 if (strncmp(tkn, "host=", 5) == 0) {
154                         int len;
155
156                         if (pos == NULL) {
157                                 len = strlen(tkn);
158                         } else {
159                                 len = pos - tkn;
160                         }
161                         len -= 4;
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) {
171                         errno = 0;
172                         *pid = strtol(tkn + 4, NULL, 0);
173                         if (errno != 0) {
174                                 syslog(LOG_WARNING, "Invalid pid format: %s",
175                                        strerror(errno));
176                                 return 1;
177                         } else {
178                                 retval |= DKD_HAVE_PID;
179                         }
180                 } else if (strncmp(tkn, "sec=", 4) == 0) {
181                         if (strncmp(tkn + 4, "krb5", 4) == 0) {
182                                 retval |= DKD_HAVE_SEC;
183                                 *sec = KRB5;
184                         } else if (strncmp(tkn + 4, "mskrb5", 6) == 0) {
185                                 retval |= DKD_HAVE_SEC;
186                                 *sec = MS_KRB5;
187                         }
188                 } else if (strncmp(tkn, "uid=", 4) == 0) {
189                         errno = 0;
190                         *uid = strtol(tkn + 4, NULL, 16);
191                         if (errno != 0) {
192                                 syslog(LOG_WARNING, "Invalid uid format: %s",
193                                        strerror(errno));
194                                 return 1;
195                         } else {
196                                 retval |= DKD_HAVE_UID;
197                         }
198                 } else if (strncmp(tkn, "ver=", 4) == 0) {      /* if version */
199                         errno = 0;
200                         *ver = strtol(tkn + 4, NULL, 16);
201                         if (errno != 0) {
202                                 syslog(LOG_WARNING,
203                                        "Invalid version format: %s",
204                                        strerror(errno));
205                                 return 1;
206                         } else {
207                                 retval |= DKD_HAVE_VERSION;
208                         }
209                 }
210                 if (pos == NULL)
211                         break;
212                 tkn = pos + 1;
213         } while (tkn);
214         return retval;
215 }
216
217 static int
218 cifs_resolver(const key_serial_t key, const char *key_descr)
219 {
220         int c;
221         struct addrinfo *addr;
222         char ip[INET6_ADDRSTRLEN];
223         void *p;
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, ';');
228                 if (!keyend) {
229                         syslog(LOG_WARNING, "invalid key description: %s",
230                                         key_descr);
231                         return 1;
232                 }
233         }
234         keyend++;
235
236         /* resolve name to ip */
237         c = getaddrinfo(keyend, NULL, NULL, &addr);
238         if (c) {
239                 syslog(LOG_WARNING, "unable to resolve hostname: %s [%s]",
240                                 keyend, gai_strerror(c));
241                 return 1;
242         }
243
244         /* conver ip to string form */
245         if (addr->ai_family == AF_INET) {
246                 p = &(((struct sockaddr_in *)addr->ai_addr)->sin_addr);
247         } else {
248                 p = &(((struct sockaddr_in6 *)addr->ai_addr)->sin6_addr);
249         }
250         if (!inet_ntop(addr->ai_family, p, ip, sizeof(ip))) {
251                 syslog(LOG_WARNING, "%s: inet_ntop: %s",
252                                 __FUNCTION__, strerror(errno));
253                 freeaddrinfo(addr);
254                 return 1;
255         }
256
257         /* setup key */
258         c = keyctl_instantiate(key, ip, strlen(ip)+1, 0);
259         if (c == -1) {
260                 syslog(LOG_WARNING, "%s: keyctl_instantiate: %s",
261                                 __FUNCTION__, strerror(errno));
262                 freeaddrinfo(addr);
263                 return 1;
264         }
265
266         freeaddrinfo(addr);
267         return 0;
268 }
269
270 static void
271 usage(void)
272 {
273         syslog(LOG_WARNING, "Usage: %s [-c] [-v] key_serial", prog);
274         fprintf(stderr, "Usage: %s [-c] [-v] key_serial\n", prog);
275 }
276
277 int main(const int argc, char *const argv[])
278 {
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;
284         size_t datalen;
285         long rc = 1;
286         uid_t uid = 0;
287         pid_t pid = 0;
288         int kernel_upcall_version = 0;
289         int c, use_cifs_service_prefix = 0;
290         char *buf, *ccname = NULL, *hostname = NULL;
291         const char *oid;
292
293         openlog(prog, 0, LOG_DAEMON);
294
295         while ((c = getopt(argc, argv, "cv")) != -1) {
296                 switch (c) {
297                 case 'c':{
298                         use_cifs_service_prefix = 1;
299                         break;
300                         }
301                 case 'v':{
302                         printf("version: %s\n", CIFSSPNEGO_VERSION);
303                         goto out;
304                         }
305                 default:{
306                         syslog(LOG_WARNING, "unknown option: %c", c);
307                         goto out;
308                         }
309                 }
310         }
311
312         /* is there a key? */
313         if (argc <= optind) {
314                 usage();
315                 goto out;
316         }
317
318         /* get key and keyring values */
319         errno = 0;
320         key = strtol(argv[optind], NULL, 10);
321         if (errno != 0) {
322                 key = 0;
323                 syslog(LOG_WARNING, "Invalid key format: %s", strerror(errno));
324                 goto out;
325         }
326
327         rc = keyctl_describe_alloc(key, &buf);
328         if (rc == -1) {
329                 syslog(LOG_WARNING, "keyctl_describe_alloc failed: %s",
330                        strerror(errno));
331                 rc = 1;
332                 goto out;
333         }
334
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);
338                 goto out;
339         }
340
341         rc = decode_key_description(buf, &kernel_upcall_version, &sectype,
342                                     &hostname, &uid, &pid);
343         if ((rc & DKD_MUSTHAVE_SET) != DKD_MUSTHAVE_SET) {
344                 syslog(LOG_WARNING,
345                        "unable to get from description necessary params");
346                 rc = 1;
347                 SAFE_FREE(buf);
348                 goto out;
349         }
350         SAFE_FREE(buf);
351
352         if (kernel_upcall_version > CIFS_SPNEGO_UPCALL_VERSION) {
353                 syslog(LOG_WARNING,
354                        "incompatible kernel upcall version: 0x%x",
355                        kernel_upcall_version);
356                 rc = 1;
357                 goto out;
358         }
359
360         if (rc & DKD_HAVE_PID)
361                 ccname = get_krb5_ccname(pid);
362
363         if (rc & DKD_HAVE_UID) {
364                 rc = setuid(uid);
365                 if (rc == -1) {
366                         syslog(LOG_WARNING, "setuid: %s", strerror(errno));
367                         goto out;
368                 }
369         }
370
371         // do mech specific authorization
372         switch (sectype) {
373         case MS_KRB5:
374         case KRB5:{
375                         char *princ;
376                         size_t len;
377
378                         /* for "cifs/" service name + terminating 0 */
379                         len = strlen(hostname) + 5 + 1;
380                         princ = SMB_XMALLOC_ARRAY(char, len);
381                         if (!princ) {
382                                 rc = 1;
383                                 break;
384                         }
385                         if (use_cifs_service_prefix) {
386                                 strlcpy(princ, "cifs/", len);
387                         } else {
388                                 strlcpy(princ, "host/", len);
389                         }
390                         strlcpy(princ + 5, hostname, len - 5);
391
392                         if (sectype == MS_KRB5)
393                                 oid = OID_KERBEROS5_OLD;
394                         else
395                                 oid = OID_KERBEROS5;
396
397                         rc = handle_krb5_mech(oid, princ, &secblob, &sess_key,
398                                               ccname);
399                         SAFE_FREE(princ);
400                         break;
401                 }
402         default:{
403                         syslog(LOG_WARNING, "sectype: %d is not implemented",
404                                sectype);
405                         rc = 1;
406                         break;
407                 }
408         }
409
410         if (rc) {
411                 goto out;
412         }
413
414         /* pack SecurityBLob and SessionKey into downcall packet */
415         datalen =
416             sizeof(struct cifs_spnego_msg) + secblob.length + sess_key.length;
417         keydata = (struct cifs_spnego_msg*)SMB_XMALLOC_ARRAY(char, datalen);
418         if (!keydata) {
419                 rc = 1;
420                 goto out;
421         }
422         keydata->version = kernel_upcall_version;
423         keydata->flags = 0;
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);
429
430         /* setup key */
431         rc = keyctl_instantiate(key, keydata, datalen, 0);
432         if (rc == -1) {
433                 syslog(LOG_WARNING, "keyctl_instantiate: %s", strerror(errno));
434                 goto out;
435         }
436
437         /* BB: maybe we need use timeout for key: for example no more then
438          * ticket lifietime? */
439         /* keyctl_set_timeout( key, 60); */
440 out:
441         /*
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.
445          */
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);
450         SAFE_FREE(ccname);
451         SAFE_FREE(hostname);
452         SAFE_FREE(keydata);
453         return rc;
454 }