s3: Change SMB_VFS_OPEN to take an smb_filename struct
[metze/samba/wip.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
27 /* cap functions */
28 static char *capencode(TALLOC_CTX *ctx, const char *from);
29 static char *capdecode(TALLOC_CTX *ctx, const char *from);
30
31 static uint64_t cap_disk_free(vfs_handle_struct *handle, const char *path,
32         bool small_query, uint64_t *bsize,
33         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, small_query, bsize,
42                                         dfree, dsize);
43 }
44
45 static SMB_STRUCT_DIR *cap_opendir(vfs_handle_struct *handle, const char *fname, const char *mask, uint32 attr)
46 {
47         char *capname = capencode(talloc_tos(), fname);
48
49         if (!capname) {
50                 errno = ENOMEM;
51                 return NULL;
52         }
53         return SMB_VFS_NEXT_OPENDIR(handle, capname, mask, attr);
54 }
55
56 static SMB_STRUCT_DIRENT *cap_readdir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp)
57 {
58         SMB_STRUCT_DIRENT *result;
59         SMB_STRUCT_DIRENT *newdirent;
60         char *newname;
61         size_t newnamelen;
62         DEBUG(3,("cap: cap_readdir\n"));
63
64         result = SMB_VFS_NEXT_READDIR(handle, dirp, NULL);
65         if (!result) {
66                 return NULL;
67         }
68
69         newname = capdecode(talloc_tos(), result->d_name);
70         if (!newname) {
71                 return NULL;
72         }
73         DEBUG(3,("cap: cap_readdir: %s\n", newname));
74         newnamelen = strlen(newname)+1;
75         newdirent = (SMB_STRUCT_DIRENT *)TALLOC_ARRAY(talloc_tos(),
76                         char,
77                         sizeof(SMB_STRUCT_DIRENT)+
78                                 newnamelen);
79         if (!newdirent) {
80                 return NULL;
81         }
82         memcpy(newdirent, result, sizeof(SMB_STRUCT_DIRENT));
83         memcpy(&newdirent->d_name, newname, newnamelen);
84         return newdirent;
85 }
86
87 static int cap_mkdir(vfs_handle_struct *handle, const char *path, mode_t mode)
88 {
89         char *cappath = capencode(talloc_tos(), path);
90
91         if (!cappath) {
92                 errno = ENOMEM;
93                 return -1;
94         }
95         return SMB_VFS_NEXT_MKDIR(handle, cappath, mode);
96 }
97
98 static int cap_rmdir(vfs_handle_struct *handle, const char *path)
99 {
100         char *cappath = capencode(talloc_tos(), path);
101
102         if (!cappath) {
103                 errno = ENOMEM;
104                 return -1;
105         }
106         return SMB_VFS_NEXT_RMDIR(handle, cappath);
107 }
108
109 static int cap_open(vfs_handle_struct *handle, struct smb_filename *smb_fname,
110                     files_struct *fsp, int flags, mode_t mode)
111 {
112         char *cappath;
113         char *tmp_base_name = NULL;
114         int ret;
115
116         cappath = capencode(talloc_tos(), smb_fname->base_name);
117
118         if (!cappath) {
119                 errno = ENOMEM;
120                 return -1;
121         }
122
123         tmp_base_name = smb_fname->base_name;
124         smb_fname->base_name = cappath;
125
126         DEBUG(3,("cap: cap_open for %s\n", smb_fname_str_dbg(smb_fname)));
127         ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
128
129         smb_fname->base_name = tmp_base_name;
130         TALLOC_FREE(cappath);
131
132         return ret;
133 }
134
135 static int cap_rename(vfs_handle_struct *handle, const char *oldname, const char *newname)
136 {
137         char *capold = capencode(talloc_tos(), oldname);
138         char *capnew = capencode(talloc_tos(), newname);
139
140         if (!capold || !capnew) {
141                 errno = ENOMEM;
142                 return -1;
143         }
144         return SMB_VFS_NEXT_RENAME(handle, capold, capnew);
145 }
146
147 static int cap_stat(vfs_handle_struct *handle, const char *path, SMB_STRUCT_STAT *sbuf)
148 {
149         char *cappath = capencode(talloc_tos(), path);
150
151         if (!cappath) {
152                 errno = ENOMEM;
153                 return -1;
154         }
155         return SMB_VFS_NEXT_STAT(handle, cappath, sbuf);
156 }
157
158 static int cap_lstat(vfs_handle_struct *handle, const char *path, SMB_STRUCT_STAT *sbuf)
159 {
160         char *cappath = capencode(talloc_tos(), path);
161
162         if (!cappath) {
163                 errno = ENOMEM;
164                 return -1;
165         }
166         return SMB_VFS_NEXT_LSTAT(handle, cappath, sbuf);
167 }
168
169 static int cap_unlink(vfs_handle_struct *handle, const char *path)
170 {
171         char *cappath = capencode(talloc_tos(), path);
172
173         if (!cappath) {
174                 errno = ENOMEM;
175                 return -1;
176         }
177         return SMB_VFS_NEXT_UNLINK(handle, cappath);
178 }
179
180 static int cap_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
181 {
182         char *cappath = capencode(talloc_tos(), path);
183
184         if (!cappath) {
185                 errno = ENOMEM;
186                 return -1;
187         }
188         return SMB_VFS_NEXT_CHMOD(handle, cappath, mode);
189 }
190
191 static int cap_chown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
192 {
193         char *cappath = capencode(talloc_tos(), path);
194
195         if (!cappath) {
196                 errno = ENOMEM;
197                 return -1;
198         }
199         return SMB_VFS_NEXT_CHOWN(handle, cappath, uid, gid);
200 }
201
202 static int cap_lchown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
203 {
204         char *cappath = capencode(talloc_tos(), path);
205
206         if (!cappath) {
207                 errno = ENOMEM;
208                 return -1;
209         }
210         return SMB_VFS_NEXT_LCHOWN(handle, cappath, uid, gid);
211 }
212
213 static int cap_chdir(vfs_handle_struct *handle, const char *path)
214 {
215         char *cappath = capencode(talloc_tos(), path);
216
217         if (!cappath) {
218                 errno = ENOMEM;
219                 return -1;
220         }
221         DEBUG(3,("cap: cap_chdir for %s\n", path));
222         return SMB_VFS_NEXT_CHDIR(handle, cappath);
223 }
224
225 static int cap_ntimes(vfs_handle_struct *handle, const char *path,
226                       struct smb_file_time *ft)
227 {
228         char *cappath = capencode(talloc_tos(), path);
229
230         if (!cappath) {
231                 errno = ENOMEM;
232                 return -1;
233         }
234         return SMB_VFS_NEXT_NTIMES(handle, cappath, ft);
235 }
236
237
238 static bool cap_symlink(vfs_handle_struct *handle, const char *oldpath, const char *newpath)
239 {
240         char *capold = capencode(talloc_tos(), oldpath);
241         char *capnew = capencode(talloc_tos(), newpath);
242
243         if (!capold || !capnew) {
244                 errno = ENOMEM;
245                 return -1;
246         }
247         return SMB_VFS_NEXT_SYMLINK(handle, capold, capnew);
248 }
249
250 static bool cap_readlink(vfs_handle_struct *handle, const char *path, char *buf, size_t bufsiz)
251 {
252         char *cappath = capencode(talloc_tos(), path);
253
254         if (!cappath) {
255                 errno = ENOMEM;
256                 return -1;
257         }
258         return SMB_VFS_NEXT_READLINK(handle, cappath, buf, bufsiz);
259 }
260
261 static int cap_link(vfs_handle_struct *handle, const char *oldpath, const char *newpath)
262 {
263         char *capold = capencode(talloc_tos(), oldpath);
264         char *capnew = capencode(talloc_tos(), newpath);
265
266         if (!capold || !capnew) {
267                 errno = ENOMEM;
268                 return -1;
269         }
270         return SMB_VFS_NEXT_LINK(handle, capold, capnew);
271 }
272
273 static int cap_mknod(vfs_handle_struct *handle, const char *path, mode_t mode, SMB_DEV_T dev)
274 {
275         char *cappath = capencode(talloc_tos(), path);
276
277         if (!cappath) {
278                 errno = ENOMEM;
279                 return -1;
280         }
281         return SMB_VFS_NEXT_MKNOD(handle, cappath, mode, dev);
282 }
283
284 static char *cap_realpath(vfs_handle_struct *handle, const char *path, char *resolved_path)
285 {
286         /* monyo need capencode'ed and capdecode'ed? */
287         char *cappath = capencode(talloc_tos(), path);
288
289         if (!cappath) {
290                 errno = ENOMEM;
291                 return NULL;
292         }
293         return SMB_VFS_NEXT_REALPATH(handle, path, resolved_path);
294 }
295
296 static int cap_chmod_acl(vfs_handle_struct *handle, const char *path, mode_t mode)
297 {
298         char *cappath = capencode(talloc_tos(), path);
299
300         /* If the underlying VFS doesn't have ACL support... */
301         if (!handle->vfs_next.ops.chmod_acl) {
302                 errno = ENOSYS;
303                 return -1;
304         }
305         if (!cappath) {
306                 errno = ENOMEM;
307                 return -1;
308         }
309         return SMB_VFS_NEXT_CHMOD_ACL(handle, cappath, mode);
310 }
311
312 static SMB_ACL_T cap_sys_acl_get_file(vfs_handle_struct *handle, const char *path, SMB_ACL_TYPE_T type)
313 {
314         char *cappath = capencode(talloc_tos(), path);
315
316         if (!cappath) {
317                 errno = ENOMEM;
318                 return (SMB_ACL_T)NULL;
319         }
320         return SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, cappath, type);
321 }
322
323 static int cap_sys_acl_set_file(vfs_handle_struct *handle, const char *path, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
324 {
325         char *cappath = capencode(talloc_tos(), path);
326
327         if (!cappath) {
328                 errno = ENOMEM;
329                 return -1;
330         }
331         return SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, cappath, acltype, theacl);
332 }
333
334 static int cap_sys_acl_delete_def_file(vfs_handle_struct *handle, const char *path)
335 {
336         char *cappath = capencode(talloc_tos(), path);
337
338         if (!cappath) {
339                 errno = ENOMEM;
340                 return -1;
341         }
342         return SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, cappath);
343 }
344
345 static ssize_t cap_getxattr(vfs_handle_struct *handle, const char *path, const char *name, void *value, size_t size)
346 {
347         char *cappath = capencode(talloc_tos(), path);
348         char *capname = capencode(talloc_tos(), name);
349
350         if (!cappath || !capname) {
351                 errno = ENOMEM;
352                 return -1;
353         }
354         return SMB_VFS_NEXT_GETXATTR(handle, cappath, capname, value, size);
355 }
356
357 static ssize_t cap_lgetxattr(vfs_handle_struct *handle, const char *path, const char *name, void *value, size_t
358 size)
359 {
360         char *cappath = capencode(talloc_tos(), path);
361         char *capname = capencode(talloc_tos(), name);
362
363         if (!cappath || !capname) {
364                 errno = ENOMEM;
365                 return -1;
366         }
367         return SMB_VFS_NEXT_LGETXATTR(handle, cappath, capname, value, size);
368 }
369
370 static ssize_t cap_fgetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, void *value, size_t size)
371 {
372         char *cappath = capencode(talloc_tos(), path);
373
374         if (!cappath) {
375                 errno = ENOMEM;
376                 return -1;
377         }
378         return SMB_VFS_NEXT_FGETXATTR(handle, fsp, cappath, value, size);
379 }
380
381 static ssize_t cap_listxattr(vfs_handle_struct *handle, const char *path, char *list, size_t size)
382 {
383         char *cappath = capencode(talloc_tos(), path);
384
385         if (!cappath) {
386                 errno = ENOMEM;
387                 return -1;
388         }
389         return SMB_VFS_NEXT_LISTXATTR(handle, cappath, list, size);
390 }
391
392 static ssize_t cap_llistxattr(vfs_handle_struct *handle, const char *path, char *list, size_t size)
393 {
394         char *cappath = capencode(talloc_tos(), path);
395
396         if (!cappath) {
397                 errno = ENOMEM;
398                 return -1;
399         }
400         return SMB_VFS_NEXT_LLISTXATTR(handle, cappath, list, size);
401 }
402
403 static int cap_removexattr(vfs_handle_struct *handle, const char *path, const char *name)
404 {
405         char *cappath = capencode(talloc_tos(), path);
406         char *capname = capencode(talloc_tos(), name);
407
408         if (!cappath || !capname) {
409                 errno = ENOMEM;
410                 return -1;
411         }
412         return SMB_VFS_NEXT_REMOVEXATTR(handle, cappath, capname);
413 }
414
415 static int cap_lremovexattr(vfs_handle_struct *handle, const char *path, const char *name)
416 {
417         char *cappath = capencode(talloc_tos(), path);
418         char *capname = capencode(talloc_tos(), name);
419
420         if (!cappath || !capname) {
421                 errno = ENOMEM;
422                 return -1;
423         }
424         return SMB_VFS_NEXT_LREMOVEXATTR(handle, cappath, capname);
425 }
426
427 static int cap_fremovexattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path)
428 {
429         char *cappath = capencode(talloc_tos(), path);
430
431         if (!cappath) {
432                 errno = ENOMEM;
433                 return -1;
434         }
435         return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, cappath);
436 }
437
438 static int cap_setxattr(vfs_handle_struct *handle, const char *path, const char *name, const void *value, size_t size, int flags)
439 {
440         char *cappath = capencode(talloc_tos(), path);
441         char *capname = capencode(talloc_tos(), name);
442
443         if (!cappath || !capname) {
444                 errno = ENOMEM;
445                 return -1;
446         }
447         return SMB_VFS_NEXT_SETXATTR(handle, cappath, capname, value, size, flags);
448 }
449
450 static int cap_lsetxattr(vfs_handle_struct *handle, const char *path, const char *name, const void *value, size_t size, int flags)
451 {
452         char *cappath = capencode(talloc_tos(), path);
453         char *capname = capencode(talloc_tos(), name);
454
455         if (!cappath || !capname) {
456                 errno = ENOMEM;
457                 return -1;
458         }
459         return SMB_VFS_NEXT_LSETXATTR(handle, cappath, capname, value, size, flags);
460 }
461
462 static int cap_fsetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, const void *value, size_t size, int flags)
463 {
464         char *cappath = capencode(talloc_tos(), path);
465
466         if (!cappath) {
467                 errno = ENOMEM;
468                 return -1;
469         }
470         return SMB_VFS_NEXT_FSETXATTR(handle, fsp, cappath, value, size, flags);
471 }
472
473 /* VFS operations structure */
474
475 static vfs_op_tuple cap_op_tuples[] = {
476
477         /* Disk operations */
478
479         {SMB_VFS_OP(cap_disk_free),                     SMB_VFS_OP_DISK_FREE,           SMB_VFS_LAYER_TRANSPARENT},
480
481         /* Directory operations */
482
483         {SMB_VFS_OP(cap_opendir),                       SMB_VFS_OP_OPENDIR,             SMB_VFS_LAYER_TRANSPARENT},
484         {SMB_VFS_OP(cap_readdir),                       SMB_VFS_OP_READDIR,             SMB_VFS_LAYER_TRANSPARENT},
485         {SMB_VFS_OP(cap_mkdir),                 SMB_VFS_OP_MKDIR,               SMB_VFS_LAYER_TRANSPARENT},
486         {SMB_VFS_OP(cap_rmdir),                 SMB_VFS_OP_RMDIR,               SMB_VFS_LAYER_TRANSPARENT},
487
488         /* File operations */
489
490         {SMB_VFS_OP(cap_open),                          SMB_VFS_OP_OPEN,                SMB_VFS_LAYER_TRANSPARENT},
491         {SMB_VFS_OP(cap_rename),                        SMB_VFS_OP_RENAME,              SMB_VFS_LAYER_TRANSPARENT},
492         {SMB_VFS_OP(cap_stat),                          SMB_VFS_OP_STAT,                SMB_VFS_LAYER_TRANSPARENT},
493         {SMB_VFS_OP(cap_lstat),                 SMB_VFS_OP_LSTAT,               SMB_VFS_LAYER_TRANSPARENT},
494         {SMB_VFS_OP(cap_unlink),                        SMB_VFS_OP_UNLINK,              SMB_VFS_LAYER_TRANSPARENT},
495         {SMB_VFS_OP(cap_chmod),                 SMB_VFS_OP_CHMOD,               SMB_VFS_LAYER_TRANSPARENT},
496         {SMB_VFS_OP(cap_chown),                 SMB_VFS_OP_CHOWN,               SMB_VFS_LAYER_TRANSPARENT},
497         {SMB_VFS_OP(cap_lchown),                SMB_VFS_OP_LCHOWN,              SMB_VFS_LAYER_TRANSPARENT},
498         {SMB_VFS_OP(cap_chdir),                 SMB_VFS_OP_CHDIR,               SMB_VFS_LAYER_TRANSPARENT},
499         {SMB_VFS_OP(cap_ntimes),                        SMB_VFS_OP_NTIMES,              SMB_VFS_LAYER_TRANSPARENT},
500         {SMB_VFS_OP(cap_symlink),                       SMB_VFS_OP_SYMLINK,             SMB_VFS_LAYER_TRANSPARENT},
501         {SMB_VFS_OP(cap_readlink),                      SMB_VFS_OP_READLINK,            SMB_VFS_LAYER_TRANSPARENT},
502         {SMB_VFS_OP(cap_link),                          SMB_VFS_OP_LINK,                SMB_VFS_LAYER_TRANSPARENT},
503         {SMB_VFS_OP(cap_mknod),                 SMB_VFS_OP_MKNOD,               SMB_VFS_LAYER_TRANSPARENT},
504         {SMB_VFS_OP(cap_realpath),                      SMB_VFS_OP_REALPATH,            SMB_VFS_LAYER_TRANSPARENT},
505
506         /* POSIX ACL operations */
507
508         {SMB_VFS_OP(cap_chmod_acl),                     SMB_VFS_OP_CHMOD_ACL,           SMB_VFS_LAYER_TRANSPARENT},
509
510         {SMB_VFS_OP(cap_sys_acl_get_file),              SMB_VFS_OP_SYS_ACL_GET_FILE,            SMB_VFS_LAYER_TRANSPARENT},
511         {SMB_VFS_OP(cap_sys_acl_set_file),              SMB_VFS_OP_SYS_ACL_SET_FILE,            SMB_VFS_LAYER_TRANSPARENT},
512         {SMB_VFS_OP(cap_sys_acl_delete_def_file),       SMB_VFS_OP_SYS_ACL_DELETE_DEF_FILE,     SMB_VFS_LAYER_TRANSPARENT},
513
514         /* EA operations. */
515         {SMB_VFS_OP(cap_getxattr),                      SMB_VFS_OP_GETXATTR,                    SMB_VFS_LAYER_TRANSPARENT},
516         {SMB_VFS_OP(cap_lgetxattr),                     SMB_VFS_OP_LGETXATTR,                   SMB_VFS_LAYER_TRANSPARENT},
517         {SMB_VFS_OP(cap_fgetxattr),                     SMB_VFS_OP_FGETXATTR,                   SMB_VFS_LAYER_TRANSPARENT},
518         {SMB_VFS_OP(cap_listxattr),                     SMB_VFS_OP_LISTXATTR,                   SMB_VFS_LAYER_TRANSPARENT},
519         {SMB_VFS_OP(cap_llistxattr),                    SMB_VFS_OP_LLISTXATTR,                  SMB_VFS_LAYER_TRANSPARENT},
520         {SMB_VFS_OP(cap_removexattr),                   SMB_VFS_OP_REMOVEXATTR,                 SMB_VFS_LAYER_TRANSPARENT},
521         {SMB_VFS_OP(cap_lremovexattr),                  SMB_VFS_OP_LREMOVEXATTR,                SMB_VFS_LAYER_TRANSPARENT},
522         {SMB_VFS_OP(cap_fremovexattr),                  SMB_VFS_OP_FREMOVEXATTR,                SMB_VFS_LAYER_TRANSPARENT},
523         {SMB_VFS_OP(cap_setxattr),                      SMB_VFS_OP_SETXATTR,                    SMB_VFS_LAYER_TRANSPARENT},
524         {SMB_VFS_OP(cap_lsetxattr),                     SMB_VFS_OP_LSETXATTR,                   SMB_VFS_LAYER_TRANSPARENT},
525         {SMB_VFS_OP(cap_fsetxattr),                     SMB_VFS_OP_FSETXATTR,                   SMB_VFS_LAYER_TRANSPARENT},
526
527         {NULL,                                          SMB_VFS_OP_NOOP,                        SMB_VFS_LAYER_NOOP}
528 };
529
530 NTSTATUS vfs_cap_init(void);
531 NTSTATUS vfs_cap_init(void)
532 {
533         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "cap", cap_op_tuples);
534 }
535
536 /* For CAP functions */
537 #define hex_tag ':'
538 #define hex2bin(c)              hex2bin_table[(unsigned char)(c)]
539 #define bin2hex(c)              bin2hex_table[(unsigned char)(c)]
540 #define is_hex(s)               ((s)[0] == hex_tag)
541
542 static unsigned char hex2bin_table[256] = {
543 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
544 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 */
545 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */
546 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0x30 */
547 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x40 */
548 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
549 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 */
550 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x60 */
551 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
552 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 */
553 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 */
554 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 */
555 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 */
556 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 */
557 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 */
558 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 */
559 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 */
560 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  /* 0xf0 */
561 };
562 static unsigned char bin2hex_table[256] = "0123456789abcdef";
563
564 /*******************************************************************
565   original code -> ":xx"  - CAP format
566 ********************************************************************/
567
568 static char *capencode(TALLOC_CTX *ctx, const char *from)
569 {
570         char *out = NULL;
571         const char *p1;
572         char *to = NULL;
573         size_t len = 0;
574
575         for (p1 = from; *p1; p1++) {
576                 if ((unsigned char)*p1 >= 0x80) {
577                         len += 3;
578                 } else {
579                         len++;
580                 }
581         }
582         len++;
583
584         to = TALLOC_ARRAY(ctx, char, len);
585         if (!to) {
586                 return NULL;
587         }
588
589         for (out = to; *from;) {
590                 /* buffer husoku error */
591                 if ((unsigned char)*from >= 0x80) {
592                         *out++ = hex_tag;
593                         *out++ = bin2hex (((*from)>>4)&0x0f);
594                         *out++ = bin2hex ((*from)&0x0f);
595                         from++;
596                 } else {
597                         *out++ = *from++;
598                 }
599         }
600         *out = '\0';
601         return to;
602 }
603
604 /*******************************************************************
605   CAP -> original code
606 ********************************************************************/
607 /* ":xx" -> a byte */
608
609 static char *capdecode(TALLOC_CTX *ctx, const char *from)
610 {
611         const char *p1;
612         char *out = NULL;
613         char *to = NULL;
614         size_t len = 0;
615
616         for (p1 = from; *p1; len++) {
617                 if (is_hex(from)) {
618                         p1 += 3;
619                 } else {
620                         p1++;
621                 }
622         }
623
624         to = TALLOC_ARRAY(ctx, char, len);
625         if (!to) {
626                 return NULL;
627         }
628
629         for (out = to; *from;) {
630                 if (is_hex(from)) {
631                         *out++ = (hex2bin(from[1])<<4) | (hex2bin(from[2]));
632                         from += 3;
633                 } else {
634                         *out++ = *from++;
635                 }
636         }
637         *out = '\0';
638         return to;
639 }