s3: VFS: Modify mkdir to take a const struct smb_filename * instead of const char *
[sfrench/samba-autobuild/.git] / source3 / modules / vfs_cap.c
1 /*
2  * CAP VFS module for Samba 3.x Version 0.3
3  *
4  * Copyright (C) Tim Potter, 1999-2000
5  * Copyright (C) Alexander Bokovoy, 2002-2003
6  * Copyright (C) Stefan (metze) Metzmacher, 2003
7  * Copyright (C) TAKAHASHI Motonobu (monyo), 2003
8  * Copyright (C) Jeremy Allison, 2007
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, see <http://www.gnu.org/licenses/>.
22  */
23
24
25 #include "includes.h"
26 #include "smbd/smbd.h"
27
28 /* cap functions */
29 static char *capencode(TALLOC_CTX *ctx, const char *from);
30 static char *capdecode(TALLOC_CTX *ctx, const char *from);
31
32 static uint64_t cap_disk_free(vfs_handle_struct *handle, const char *path,
33                               uint64_t *bsize, uint64_t *dfree, uint64_t *dsize)
34 {
35         char *cappath = capencode(talloc_tos(), path);
36
37         if (!cappath) {
38                 errno = ENOMEM;
39                 return (uint64_t)-1;
40         }
41         return SMB_VFS_NEXT_DISK_FREE(handle, cappath, bsize, dfree, dsize);
42 }
43
44 static int cap_get_quota(vfs_handle_struct *handle, const char *path,
45                          enum SMB_QUOTA_TYPE qtype, unid_t id,
46                          SMB_DISK_QUOTA *dq)
47 {
48         char *cappath = capencode(talloc_tos(), path);
49
50         if (!cappath) {
51                 errno = ENOMEM;
52                 return -1;
53         }
54         return SMB_VFS_NEXT_GET_QUOTA(handle, cappath, qtype, id, dq);
55 }
56
57 static DIR *cap_opendir(vfs_handle_struct *handle, const char *fname, const char *mask, uint32_t attr)
58 {
59         char *capname = capencode(talloc_tos(), fname);
60
61         if (!capname) {
62                 errno = ENOMEM;
63                 return NULL;
64         }
65         return SMB_VFS_NEXT_OPENDIR(handle, capname, mask, attr);
66 }
67
68 static struct dirent *cap_readdir(vfs_handle_struct *handle,
69                                       DIR *dirp,
70                                       SMB_STRUCT_STAT *sbuf)
71 {
72         struct dirent *result;
73         struct dirent *newdirent;
74         char *newname;
75         size_t newnamelen;
76         DEBUG(3,("cap: cap_readdir\n"));
77
78         result = SMB_VFS_NEXT_READDIR(handle, dirp, NULL);
79         if (!result) {
80                 return NULL;
81         }
82
83         newname = capdecode(talloc_tos(), result->d_name);
84         if (!newname) {
85                 return NULL;
86         }
87         DEBUG(3,("cap: cap_readdir: %s\n", newname));
88         newnamelen = strlen(newname)+1;
89         newdirent = talloc_size(
90                 talloc_tos(), sizeof(struct dirent) + newnamelen);
91         if (!newdirent) {
92                 return NULL;
93         }
94         talloc_set_name_const(newdirent, "struct dirent");
95         memcpy(newdirent, result, sizeof(struct dirent));
96         memcpy(&newdirent->d_name, newname, newnamelen);
97         return newdirent;
98 }
99
100 static int cap_mkdir(vfs_handle_struct *handle,
101                 const struct smb_filename *smb_fname,
102                 mode_t mode)
103 {
104         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
105         struct smb_filename *cap_smb_fname = NULL;
106
107         if (!cappath) {
108                 errno = ENOMEM;
109                 return -1;
110         }
111
112         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
113                                         cappath,
114                                         NULL,
115                                         NULL);
116         if (cap_smb_fname == NULL) {
117                 TALLOC_FREE(cappath);
118                 errno = ENOMEM;
119                 return -1;
120         }
121
122         return SMB_VFS_NEXT_MKDIR(handle, cap_smb_fname, mode);
123 }
124
125 static int cap_rmdir(vfs_handle_struct *handle, const char *path)
126 {
127         char *cappath = capencode(talloc_tos(), path);
128
129         if (!cappath) {
130                 errno = ENOMEM;
131                 return -1;
132         }
133         return SMB_VFS_NEXT_RMDIR(handle, cappath);
134 }
135
136 static int cap_open(vfs_handle_struct *handle, struct smb_filename *smb_fname,
137                     files_struct *fsp, int flags, mode_t mode)
138 {
139         char *cappath;
140         char *tmp_base_name = NULL;
141         int ret;
142
143         cappath = capencode(talloc_tos(), smb_fname->base_name);
144
145         if (!cappath) {
146                 errno = ENOMEM;
147                 return -1;
148         }
149
150         tmp_base_name = smb_fname->base_name;
151         smb_fname->base_name = cappath;
152
153         DEBUG(3,("cap: cap_open for %s\n", smb_fname_str_dbg(smb_fname)));
154         ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
155
156         smb_fname->base_name = tmp_base_name;
157         TALLOC_FREE(cappath);
158
159         return ret;
160 }
161
162 static int cap_rename(vfs_handle_struct *handle,
163                       const struct smb_filename *smb_fname_src,
164                       const struct smb_filename *smb_fname_dst)
165 {
166         char *capold = NULL;
167         char *capnew = NULL;
168         struct smb_filename *smb_fname_src_tmp = NULL;
169         struct smb_filename *smb_fname_dst_tmp = NULL;
170         int ret = -1;
171
172         capold = capencode(talloc_tos(), smb_fname_src->base_name);
173         capnew = capencode(talloc_tos(), smb_fname_dst->base_name);
174         if (!capold || !capnew) {
175                 errno = ENOMEM;
176                 goto out;
177         }
178
179         /* Setup temporary smb_filename structs. */
180         smb_fname_src_tmp = cp_smb_filename(talloc_tos(), smb_fname_src);
181         if (smb_fname_src_tmp == NULL) {
182                 errno = ENOMEM;
183                 goto out;
184         }
185         smb_fname_dst_tmp = cp_smb_filename(talloc_tos(), smb_fname_dst);
186         if (smb_fname_dst_tmp == NULL) {
187                 errno = ENOMEM;
188                 goto out;
189         }
190
191         smb_fname_src_tmp->base_name = capold;
192         smb_fname_dst_tmp->base_name = capnew;
193
194         ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_tmp,
195                                   smb_fname_dst_tmp);
196  out:
197         TALLOC_FREE(capold);
198         TALLOC_FREE(capnew);
199         TALLOC_FREE(smb_fname_src_tmp);
200         TALLOC_FREE(smb_fname_dst_tmp);
201
202         return ret;
203 }
204
205 static int cap_stat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
206 {
207         char *cappath;
208         char *tmp_base_name = NULL;
209         int ret;
210
211         cappath = capencode(talloc_tos(), smb_fname->base_name);
212
213         if (!cappath) {
214                 errno = ENOMEM;
215                 return -1;
216         }
217
218         tmp_base_name = smb_fname->base_name;
219         smb_fname->base_name = cappath;
220
221         ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
222
223         smb_fname->base_name = tmp_base_name;
224         TALLOC_FREE(cappath);
225
226         return ret;
227 }
228
229 static int cap_lstat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
230 {
231         char *cappath;
232         char *tmp_base_name = NULL;
233         int ret;
234
235         cappath = capencode(talloc_tos(), smb_fname->base_name);
236
237         if (!cappath) {
238                 errno = ENOMEM;
239                 return -1;
240         }
241
242         tmp_base_name = smb_fname->base_name;
243         smb_fname->base_name = cappath;
244
245         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
246
247         smb_fname->base_name = tmp_base_name;
248         TALLOC_FREE(cappath);
249
250         return ret;
251 }
252
253 static int cap_unlink(vfs_handle_struct *handle,
254                       const struct smb_filename *smb_fname)
255 {
256         struct smb_filename *smb_fname_tmp = NULL;
257         char *cappath = NULL;
258         int ret;
259
260         cappath = capencode(talloc_tos(), smb_fname->base_name);
261         if (!cappath) {
262                 errno = ENOMEM;
263                 return -1;
264         }
265
266         /* Setup temporary smb_filename structs. */
267         smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
268         if (smb_fname_tmp == NULL) {
269                 errno = ENOMEM;
270                 return -1;
271         }
272
273         smb_fname_tmp->base_name = cappath;
274
275         ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
276
277         TALLOC_FREE(smb_fname_tmp);
278         return ret;
279 }
280
281 static int cap_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
282 {
283         char *cappath = capencode(talloc_tos(), path);
284
285         if (!cappath) {
286                 errno = ENOMEM;
287                 return -1;
288         }
289         return SMB_VFS_NEXT_CHMOD(handle, cappath, mode);
290 }
291
292 static int cap_chown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
293 {
294         char *cappath = capencode(talloc_tos(), path);
295
296         if (!cappath) {
297                 errno = ENOMEM;
298                 return -1;
299         }
300         return SMB_VFS_NEXT_CHOWN(handle, cappath, uid, gid);
301 }
302
303 static int cap_lchown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
304 {
305         char *cappath = capencode(talloc_tos(), path);
306
307         if (!cappath) {
308                 errno = ENOMEM;
309                 return -1;
310         }
311         return SMB_VFS_NEXT_LCHOWN(handle, cappath, uid, gid);
312 }
313
314 static int cap_chdir(vfs_handle_struct *handle, const char *path)
315 {
316         char *cappath = capencode(talloc_tos(), path);
317
318         if (!cappath) {
319                 errno = ENOMEM;
320                 return -1;
321         }
322         DEBUG(3,("cap: cap_chdir for %s\n", path));
323         return SMB_VFS_NEXT_CHDIR(handle, cappath);
324 }
325
326 static int cap_ntimes(vfs_handle_struct *handle,
327                       const struct smb_filename *smb_fname,
328                       struct smb_file_time *ft)
329 {
330         struct smb_filename *smb_fname_tmp = NULL;
331         char *cappath = NULL;
332         int ret;
333
334         cappath = capencode(talloc_tos(), smb_fname->base_name);
335
336         if (!cappath) {
337                 errno = ENOMEM;
338                 return -1;
339         }
340
341         /* Setup temporary smb_filename structs. */
342         smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
343         if (smb_fname_tmp == NULL) {
344                 errno = ENOMEM;
345                 return -1;
346         }
347
348         smb_fname_tmp->base_name = cappath;
349
350         ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname_tmp, ft);
351
352         TALLOC_FREE(smb_fname_tmp);
353         return ret;
354 }
355
356
357 static int cap_symlink(vfs_handle_struct *handle, const char *oldpath,
358                        const char *newpath)
359 {
360         char *capold = capencode(talloc_tos(), oldpath);
361         char *capnew = capencode(talloc_tos(), newpath);
362
363         if (!capold || !capnew) {
364                 errno = ENOMEM;
365                 return -1;
366         }
367         return SMB_VFS_NEXT_SYMLINK(handle, capold, capnew);
368 }
369
370 static int cap_readlink(vfs_handle_struct *handle, const char *path,
371                         char *buf, size_t bufsiz)
372 {
373         char *cappath = capencode(talloc_tos(), path);
374
375         if (!cappath) {
376                 errno = ENOMEM;
377                 return -1;
378         }
379         return SMB_VFS_NEXT_READLINK(handle, cappath, buf, bufsiz);
380 }
381
382 static int cap_link(vfs_handle_struct *handle, const char *oldpath, const char *newpath)
383 {
384         char *capold = capencode(talloc_tos(), oldpath);
385         char *capnew = capencode(talloc_tos(), newpath);
386
387         if (!capold || !capnew) {
388                 errno = ENOMEM;
389                 return -1;
390         }
391         return SMB_VFS_NEXT_LINK(handle, capold, capnew);
392 }
393
394 static int cap_mknod(vfs_handle_struct *handle, const char *path, mode_t mode, SMB_DEV_T dev)
395 {
396         char *cappath = capencode(talloc_tos(), path);
397
398         if (!cappath) {
399                 errno = ENOMEM;
400                 return -1;
401         }
402         return SMB_VFS_NEXT_MKNOD(handle, cappath, mode, dev);
403 }
404
405 static char *cap_realpath(vfs_handle_struct *handle, const char *path)
406 {
407         /* monyo need capencode'ed and capdecode'ed? */
408         char *cappath = capencode(talloc_tos(), path);
409
410         if (!cappath) {
411                 errno = ENOMEM;
412                 return NULL;
413         }
414         return SMB_VFS_NEXT_REALPATH(handle, cappath);
415 }
416
417 static int cap_chmod_acl(vfs_handle_struct *handle, const char *path, mode_t mode)
418 {
419         char *cappath = capencode(talloc_tos(), path);
420
421         /* If the underlying VFS doesn't have ACL support... */
422         if (!cappath) {
423                 errno = ENOMEM;
424                 return -1;
425         }
426         return SMB_VFS_NEXT_CHMOD_ACL(handle, cappath, mode);
427 }
428
429 static SMB_ACL_T cap_sys_acl_get_file(vfs_handle_struct *handle,
430                                       const char *path, SMB_ACL_TYPE_T type,
431                                       TALLOC_CTX *mem_ctx)
432 {
433         char *cappath = capencode(talloc_tos(), path);
434
435         if (!cappath) {
436                 errno = ENOMEM;
437                 return (SMB_ACL_T)NULL;
438         }
439         return SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, cappath, type, mem_ctx);
440 }
441
442 static int cap_sys_acl_set_file(vfs_handle_struct *handle, const char *path, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
443 {
444         char *cappath = capencode(talloc_tos(), path);
445
446         if (!cappath) {
447                 errno = ENOMEM;
448                 return -1;
449         }
450         return SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, cappath, acltype, theacl);
451 }
452
453 static int cap_sys_acl_delete_def_file(vfs_handle_struct *handle, const char *path)
454 {
455         char *cappath = capencode(talloc_tos(), path);
456
457         if (!cappath) {
458                 errno = ENOMEM;
459                 return -1;
460         }
461         return SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, cappath);
462 }
463
464 static ssize_t cap_getxattr(vfs_handle_struct *handle, const char *path, const char *name, void *value, size_t size)
465 {
466         char *cappath = capencode(talloc_tos(), path);
467         char *capname = capencode(talloc_tos(), name);
468
469         if (!cappath || !capname) {
470                 errno = ENOMEM;
471                 return -1;
472         }
473         return SMB_VFS_NEXT_GETXATTR(handle, cappath, capname, value, size);
474 }
475
476 static ssize_t cap_fgetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, void *value, size_t size)
477 {
478         char *cappath = capencode(talloc_tos(), path);
479
480         if (!cappath) {
481                 errno = ENOMEM;
482                 return -1;
483         }
484         return SMB_VFS_NEXT_FGETXATTR(handle, fsp, cappath, value, size);
485 }
486
487 static ssize_t cap_listxattr(vfs_handle_struct *handle, const char *path, char *list, size_t size)
488 {
489         char *cappath = capencode(talloc_tos(), path);
490
491         if (!cappath) {
492                 errno = ENOMEM;
493                 return -1;
494         }
495         return SMB_VFS_NEXT_LISTXATTR(handle, cappath, list, size);
496 }
497
498 static int cap_removexattr(vfs_handle_struct *handle, const char *path, const char *name)
499 {
500         char *cappath = capencode(talloc_tos(), path);
501         char *capname = capencode(talloc_tos(), name);
502
503         if (!cappath || !capname) {
504                 errno = ENOMEM;
505                 return -1;
506         }
507         return SMB_VFS_NEXT_REMOVEXATTR(handle, cappath, capname);
508 }
509
510 static int cap_fremovexattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path)
511 {
512         char *cappath = capencode(talloc_tos(), path);
513
514         if (!cappath) {
515                 errno = ENOMEM;
516                 return -1;
517         }
518         return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, cappath);
519 }
520
521 static int cap_setxattr(vfs_handle_struct *handle, const char *path, const char *name, const void *value, size_t size, int flags)
522 {
523         char *cappath = capencode(talloc_tos(), path);
524         char *capname = capencode(talloc_tos(), name);
525
526         if (!cappath || !capname) {
527                 errno = ENOMEM;
528                 return -1;
529         }
530         return SMB_VFS_NEXT_SETXATTR(handle, cappath, capname, value, size, flags);
531 }
532
533 static int cap_fsetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, const void *value, size_t size, int flags)
534 {
535         char *cappath = capencode(talloc_tos(), path);
536
537         if (!cappath) {
538                 errno = ENOMEM;
539                 return -1;
540         }
541         return SMB_VFS_NEXT_FSETXATTR(handle, fsp, cappath, value, size, flags);
542 }
543
544 static struct vfs_fn_pointers vfs_cap_fns = {
545         .disk_free_fn = cap_disk_free,
546         .get_quota_fn = cap_get_quota,
547         .opendir_fn = cap_opendir,
548         .readdir_fn = cap_readdir,
549         .mkdir_fn = cap_mkdir,
550         .rmdir_fn = cap_rmdir,
551         .open_fn = cap_open,
552         .rename_fn = cap_rename,
553         .stat_fn = cap_stat,
554         .lstat_fn = cap_lstat,
555         .unlink_fn = cap_unlink,
556         .chmod_fn = cap_chmod,
557         .chown_fn = cap_chown,
558         .lchown_fn = cap_lchown,
559         .chdir_fn = cap_chdir,
560         .ntimes_fn = cap_ntimes,
561         .symlink_fn = cap_symlink,
562         .readlink_fn = cap_readlink,
563         .link_fn = cap_link,
564         .mknod_fn = cap_mknod,
565         .realpath_fn = cap_realpath,
566         .chmod_acl_fn = cap_chmod_acl,
567         .sys_acl_get_file_fn = cap_sys_acl_get_file,
568         .sys_acl_set_file_fn = cap_sys_acl_set_file,
569         .sys_acl_delete_def_file_fn = cap_sys_acl_delete_def_file,
570         .getxattr_fn = cap_getxattr,
571         .fgetxattr_fn = cap_fgetxattr,
572         .listxattr_fn = cap_listxattr,
573         .removexattr_fn = cap_removexattr,
574         .fremovexattr_fn = cap_fremovexattr,
575         .setxattr_fn = cap_setxattr,
576         .fsetxattr_fn = cap_fsetxattr
577 };
578
579 NTSTATUS vfs_cap_init(void);
580 NTSTATUS vfs_cap_init(void)
581 {
582         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "cap",
583                                 &vfs_cap_fns);
584 }
585
586 /* For CAP functions */
587 #define hex_tag ':'
588 #define hex2bin(c)              hex2bin_table[(unsigned char)(c)]
589 #define bin2hex(c)              bin2hex_table[(unsigned char)(c)]
590 #define is_hex(s)               ((s)[0] == hex_tag)
591
592 static unsigned char hex2bin_table[256] = {
593 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
594 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 */
595 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */
596 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0x30 */
597 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x40 */
598 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
599 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 */
600 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x60 */
601 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
602 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 */
603 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 */
604 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 */
605 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 */
606 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 */
607 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 */
608 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 */
609 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 */
610 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  /* 0xf0 */
611 };
612 static unsigned char bin2hex_table[256] = "0123456789abcdef";
613
614 /*******************************************************************
615   original code -> ":xx"  - CAP format
616 ********************************************************************/
617
618 static char *capencode(TALLOC_CTX *ctx, const char *from)
619 {
620         char *out = NULL;
621         const char *p1;
622         char *to = NULL;
623         size_t len = 0;
624
625         for (p1 = from; *p1; p1++) {
626                 if ((unsigned char)*p1 >= 0x80) {
627                         len += 3;
628                 } else {
629                         len++;
630                 }
631         }
632         len++;
633
634         to = talloc_array(ctx, char, len);
635         if (!to) {
636                 return NULL;
637         }
638
639         for (out = to; *from;) {
640                 /* buffer husoku error */
641                 if ((unsigned char)*from >= 0x80) {
642                         *out++ = hex_tag;
643                         *out++ = bin2hex (((*from)>>4)&0x0f);
644                         *out++ = bin2hex ((*from)&0x0f);
645                         from++;
646                 } else {
647                         *out++ = *from++;
648                 }
649         }
650         *out = '\0';
651         return to;
652 }
653
654 /*******************************************************************
655   CAP -> original code
656 ********************************************************************/
657 /* ":xx" -> a byte */
658
659 static char *capdecode(TALLOC_CTX *ctx, const char *from)
660 {
661         const char *p1;
662         char *out = NULL;
663         char *to = NULL;
664         size_t len = 0;
665
666         for (p1 = from; *p1; len++) {
667                 if (is_hex(p1)) {
668                         p1 += 3;
669                 } else {
670                         p1++;
671                 }
672         }
673         len++;
674
675         to = talloc_array(ctx, char, len);
676         if (!to) {
677                 return NULL;
678         }
679
680         for (out = to; *from;) {
681                 if (is_hex(from)) {
682                         *out++ = (hex2bin(from[1])<<4) | (hex2bin(from[2]));
683                         from += 3;
684                 } else {
685                         *out++ = *from++;
686                 }
687         }
688         *out = '\0';
689         return to;
690 }