More null deref fixes.
[gd/samba/.git] / source / client / cifs.spnego.c
1 /*
2 * CIFS SPNEGO user-space helper.
3 * Copyright (C) Igor Mammedov (niallain@gmail.com) 2007
4 *
5 * Used by /sbin/request-key for handling
6 * cifs upcall for kerberos authorization of access to share.
7 * You should have keyutils installed and add following line to
8 * /etc/request-key.conf file
9
10 create cifs.spnego * * /usr/local/sbin/cifs.spnego [-v][-c] %k
11
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25 #include "includes.h"
26 #include <keyutils.h>
27
28 #include "cifs_spnego.h"
29
30 const char* CIFSSPNEGO_VERSION="1.0";
31 static const char *prog = "cifs.spnego";
32 typedef enum _secType {
33         KRB5,
34         MS_KRB5
35 } secType_t;
36
37 /*
38  * Prepares AP-REQ data for mechToken and gets session key
39  * Uses credentials from cache. It will not ask for password
40  * you should receive credentials for yuor name manually using
41  * kinit or whatever you wish.
42  *
43  * in:
44  *      oid -           string with OID/ Could be OID_KERBEROS5
45  *                      or OID_KERBEROS5_OLD
46  *      principal -     Service name.
47  *                      Could be "cifs/FQDN" for KRB5 OID
48  *                      or for MS_KRB5 OID style server principal
49  *                      like "pdc$@YOUR.REALM.NAME"
50  *
51  * out:
52  *      secblob -       pointer for spnego wrapped AP-REQ data to be stored
53  *      sess_key-       pointer for SessionKey data to be stored
54  *
55  * ret: 0 - success, others - failure
56 */
57 int handle_krb5_mech(const char *oid, const char *principal,
58                      DATA_BLOB * secblob, DATA_BLOB * sess_key)
59 {
60         int retval;
61         DATA_BLOB tkt, tkt_wrapped;
62
63         /* get a kerberos ticket for the service and extract the session key */
64         retval = cli_krb5_get_ticket(principal, 0,
65                                      &tkt, sess_key, 0, NULL, NULL);
66
67         if (retval)
68                 return retval;
69
70         /* wrap that up in a nice GSS-API wrapping */
71         tkt_wrapped = spnego_gen_krb5_wrap(tkt, TOK_ID_KRB_AP_REQ);
72
73         /* and wrap that in a shiny SPNEGO wrapper */
74         *secblob = gen_negTokenInit(OID_KERBEROS5, tkt_wrapped);
75
76         data_blob_free(&tkt_wrapped);
77         data_blob_free(&tkt);
78         return retval;
79 }
80
81 #define DKD_HAVE_HOSTNAME       1
82 #define DKD_HAVE_VERSION        2
83 #define DKD_HAVE_SEC            4
84 #define DKD_HAVE_IPV4           8
85 #define DKD_HAVE_IPV6           16
86 #define DKD_HAVE_UID            32
87 #define DKD_MUSTHAVE_SET (DKD_HAVE_HOSTNAME|DKD_HAVE_VERSION|DKD_HAVE_SEC)
88
89 int decode_key_description(const char *desc, int *ver, secType_t * sec,
90                            char **hostname, uid_t * uid)
91 {
92         int retval = 0;
93         char *pos;
94         const char *tkn = desc;
95
96         do {
97                 pos = index(tkn, ';');
98                 if (strncmp(tkn, "host=", 5) == 0) {
99                         int len;
100
101                         if (pos == NULL) {
102                                 len = strlen(tkn);
103                         } else {
104                                 len = pos - tkn;
105                         }
106                         len -= 4;
107                         SAFE_FREE(*hostname);
108                         *hostname = SMB_XMALLOC_ARRAY(char, len);
109                         strlcpy(*hostname, tkn + 5, len);
110                         retval |= DKD_HAVE_HOSTNAME;
111                 } else if (strncmp(tkn, "ipv4=", 5) == 0) {
112                         /* BB: do we need it if we have hostname already? */
113                 } else if (strncmp(tkn, "ipv6=", 5) == 0) {
114                         /* BB: do we need it if we have hostname already? */
115                 } else if (strncmp(tkn, "sec=", 4) == 0) {
116                         if (strncmp(tkn + 4, "krb5", 4) == 0) {
117                                 retval |= DKD_HAVE_SEC;
118                                 *sec = KRB5;
119                         }
120                 } else if (strncmp(tkn, "uid=", 4) == 0) {
121                         errno = 0;
122                         *uid = strtol(tkn + 4, NULL, 16);
123                         if (errno != 0) {
124                                 syslog(LOG_WARNING, "Invalid uid format: %s",
125                                        strerror(errno));
126                                 return 1;
127                         } else {
128                                 retval |= DKD_HAVE_UID;
129                         }
130                 } else if (strncmp(tkn, "ver=", 4) == 0) {      /* if version */
131                         errno = 0;
132                         *ver = strtol(tkn + 4, NULL, 16);
133                         if (errno != 0) {
134                                 syslog(LOG_WARNING,
135                                        "Invalid version format: %s",
136                                        strerror(errno));
137                                 return 1;
138                         } else {
139                                 retval |= DKD_HAVE_VERSION;
140                         }
141                 }
142                 if (pos == NULL)
143                         break;
144                 tkn = pos + 1;
145         } while (tkn);
146         return retval;
147 }
148
149 int main(const int argc, char *const argv[])
150 {
151         struct cifs_spnego_msg *keydata = NULL;
152         DATA_BLOB secblob = data_blob_null;
153         DATA_BLOB sess_key = data_blob_null;
154         secType_t sectype;
155         key_serial_t key;
156         size_t datalen;
157         long rc = 1;
158         uid_t uid;
159         int kernel_upcall_version;
160         int c, use_cifs_service_prefix = 0;
161         char *buf, *hostname = NULL;
162
163         openlog(prog, 0, LOG_DAEMON);
164         if (argc < 1) {
165                 syslog(LOG_WARNING, "Usage: %s [-c] key_serial", prog);
166                 goto out;
167         }
168
169         while ((c = getopt(argc, argv, "cv")) != -1) {
170                 switch (c) {
171                 case 'c':{
172                         use_cifs_service_prefix = 1;
173                         break;
174                         }
175                 case 'v':{
176                         syslog(LOG_WARNING, "version: %s", CIFSSPNEGO_VERSION);
177                         fprintf(stderr, "version: %s", CIFSSPNEGO_VERSION);
178                         break;
179                         }
180                 default:{
181                         syslog(LOG_WARNING, "unknow option: %c", c);
182                         goto out;
183                         }
184                 }
185         }
186         /* get key and keyring values */
187         errno = 0;
188         key = strtol(argv[optind], NULL, 10);
189         if (errno != 0) {
190                 syslog(LOG_WARNING, "Invalid key format: %s", strerror(errno));
191                 goto out;
192         }
193
194         rc = keyctl_describe_alloc(key, &buf);
195         if (rc == -1) {
196                 syslog(LOG_WARNING, "keyctl_describe_alloc failed: %s",
197                        strerror(errno));
198                 rc = 1;
199                 goto out;
200         }
201
202         rc = decode_key_description(buf, &kernel_upcall_version, &sectype,
203                                     &hostname, &uid);
204         if ((rc & DKD_MUSTHAVE_SET) != DKD_MUSTHAVE_SET) {
205                 syslog(LOG_WARNING,
206                        "unable to get from description necessary params");
207                 rc = 1;
208                 SAFE_FREE(buf);
209                 goto out;
210         }
211         SAFE_FREE(buf);
212
213         if (kernel_upcall_version != CIFS_SPNEGO_UPCALL_VERSION) {
214                 syslog(LOG_WARNING,
215                        "incompatible kernel upcall version: 0x%x",
216                        kernel_upcall_version);
217                 rc = 1;
218                 goto out;
219         }
220
221         if (rc & DKD_HAVE_UID) {
222                 rc = setuid(uid);
223                 if (rc == -1) {
224                         syslog(LOG_WARNING, "setuid: %s", strerror(errno));
225                         goto out;
226                 }
227         }
228
229         /* BB: someday upcall SPNEGO blob could be checked here to decide
230          * what mech to use */
231
232         // do mech specific authorization
233         switch (sectype) {
234         case KRB5:{
235                         char *princ;
236                         size_t len;
237
238                         /* for "cifs/" service name + terminating 0 */
239                         len = strlen(hostname) + 5 + 1;
240                         princ = SMB_XMALLOC_ARRAY(char, len);
241                         if (!princ) {
242                                 rc = 1;
243                                 break;
244                         }
245                         if (use_cifs_service_prefix) {
246                                 strlcpy(princ, "cifs/", len);
247                         } else {
248                                 strlcpy(princ, "host/", len);
249                         }
250                         strlcpy(princ + 5, hostname, len - 5);
251
252                         rc = handle_krb5_mech(OID_KERBEROS5, princ,
253                                               &secblob, &sess_key);
254                         SAFE_FREE(princ);
255                         break;
256                 }
257         default:{
258                         syslog(LOG_WARNING, "sectype: %d is not implemented",
259                                sectype);
260                         rc = 1;
261                         break;
262                 }
263         }
264
265         if (rc) {
266                 goto out;
267         }
268
269         /* pack SecurityBLob and SessionKey into downcall packet */
270         datalen =
271             sizeof(struct cifs_spnego_msg) + secblob.length + sess_key.length;
272         keydata = (struct cifs_spnego_msg*)SMB_XMALLOC_ARRAY(char, datalen);
273         if (!keydata) {
274                 rc = 1;
275                 goto out;
276         }
277         keydata->version = CIFS_SPNEGO_UPCALL_VERSION;
278         keydata->flags = 0;
279         keydata->sesskey_len = sess_key.length;
280         keydata->secblob_len = secblob.length;
281         memcpy(&(keydata->data), sess_key.data, sess_key.length);
282         memcpy(&(keydata->data) + keydata->sesskey_len,
283                secblob.data, secblob.length);
284
285         /* setup key */
286         rc = keyctl_instantiate(key, keydata, datalen, 0);
287         if (rc == -1) {
288                 syslog(LOG_WARNING, "keyctl_instantiate: %s", strerror(errno));
289                 goto out;
290         }
291
292         /* BB: maybe we need use timeout for key: for example no more then
293          * ticket lifietime? */
294         /* keyctl_set_timeout( key, 60); */
295       out:
296         data_blob_free(&secblob);
297         data_blob_free(&sess_key);
298         SAFE_FREE(hostname);
299         SAFE_FREE(keydata);
300         return rc;
301 }