Initial revamp of the libsmbclient interface.
[samba.git] / source / include / libsmb_internal.h
1 /* 
2    Unix SMB/Netbios implementation.
3    SMB client library implementation
4    Copyright (C) Andrew Tridgell 1998
5    Copyright (C) Richard Sharpe 2000, 2002
6    Copyright (C) John Terpstra 2000
7    Copyright (C) Tom Jansen (Ninja ISD) 2002 
8    Copyright (C) Derrell Lipman 2003-2008
9    Copyright (C) Jeremy Allison 2007, 2008
10    
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15    
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    
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include "includes.h"
26
27
28 #ifndef _LIBSMB_INTERNAL_H_
29 #define _LIBSMB_INTERNAL_H_
30
31 #include "../include/libsmbclient.h"
32
33 #define SMBC_MAX_NAME  1023
34 #define SMBC_FILE_MODE (S_IFREG | 0444)
35 #define SMBC_DIR_MODE  (S_IFDIR | 0555)
36
37 /*
38  * DOS Attribute values (used internally)
39  */
40 typedef struct DOS_ATTR_DESC {
41         int mode;
42         SMB_OFF_T size;
43         time_t create_time;
44         time_t access_time;
45         time_t write_time;
46         time_t change_time;
47         SMB_INO_T inode;
48 } DOS_ATTR_DESC;
49
50
51 /*
52  * Internal flags for extended attributes
53  */
54
55 /* internal mode values */
56 #define SMBC_XATTR_MODE_ADD          1
57 #define SMBC_XATTR_MODE_REMOVE       2
58 #define SMBC_XATTR_MODE_REMOVE_ALL   3
59 #define SMBC_XATTR_MODE_SET          4
60 #define SMBC_XATTR_MODE_CHOWN        5
61 #define SMBC_XATTR_MODE_CHGRP        6
62
63 #define CREATE_ACCESS_READ      READ_CONTROL_ACCESS
64
65 /*We should test for this in configure ... */
66 #ifndef ENOTSUP
67 #define ENOTSUP EOPNOTSUPP
68 #endif
69
70
71 struct _SMBCSRV {
72         struct cli_state *cli;
73         dev_t dev;
74         bool no_pathinfo;
75         bool no_pathinfo2;
76         bool no_nt_session;
77         POLICY_HND pol;
78
79         SMBCSRV *next, *prev;
80         
81 };
82
83 /* 
84  * Keep directory entries in a list 
85  */
86 struct smbc_dir_list {
87         struct smbc_dir_list *next;
88         struct smbc_dirent *dirent;
89 };
90
91
92 /*
93  * Structure for open file management
94  */ 
95 struct _SMBCFILE {
96         int cli_fd; 
97         char *fname;
98         SMB_OFF_T offset;
99         struct _SMBCSRV *srv;
100         bool file;
101         struct smbc_dir_list *dir_list, *dir_end, *dir_next;
102         int dir_type, dir_error;
103
104         SMBCFILE *next, *prev;
105 };
106
107
108 /*
109  * Context structure
110  */
111 struct _SMBCCTX {
112
113         /* True when this handle is initialized */
114         bool            initialized;
115
116         /* Netbios name used for making connections */
117         char *          netbios_name;
118
119         /* Workgroup used for making connections */
120         char *          workgroup;
121
122         /* Username used for making connections */
123         char *          user;
124
125         /* Debug level */
126         int             debug;
127
128         /* Connection timeout value */
129         int             timeout;
130
131         /* dirent pointer location
132          *
133          * Leave room for any urlencoded filename and the comment field.
134          *
135          * We really should use sizeof(struct smbc_dirent) plus (NAME_MAX * 3)
136          * plus whatever the max length of a comment is, plus a couple of null
137          * terminators (one after the filename, one after the comment).
138          *
139          * According to <linux/limits.h>, NAME_MAX is 255.  Is it longer
140          * anyplace else?
141          */
142         char            dirent[1024];
143
144         /*
145          * server connection list
146          */
147         SMBCSRV *       servers;
148         
149         /*
150          * open file/dir list
151          */
152         SMBCFILE *      files;
153
154         /*
155          * Log to standard error instead of the more typical standard output
156          */
157         bool            debug_stderr;
158
159         /*
160          * Support "Create Time" in get/set with the *xattr() functions, if
161          * true.  This replaces the dos attribute strings C_TIME, A_TIME and
162          * M_TIME with CHANGE_TIME, ACCESS_TIME and WRITE_TIME, and adds
163          * CREATE_TIME.  Default is FALSE, i.e.  to use the old-style shorter
164          * names and to not support CREATE time, for backward compatibility.
165          */
166         bool            full_time_names;
167
168         /*
169          * The share mode of a file being opened.  To match POSIX semantics
170          * (and maintain backward compatibility), DENY_NONE is the default.
171          */
172         smbc_share_mode share_mode;
173
174         /*
175          * An opaque (to this library) user data handle which can be set
176          * and retrieved with smbc_option_set() and smbc_option_get().
177          */
178         void *          user_data;
179
180         /*
181          * Should we attempt UNIX smb encryption ? 
182          * Set to 0 if we should never attempt, set to 1 if
183          * encryption requested, set to 2 if encryption required.
184          */
185         int             smb_encryption_level;
186
187         /*
188          * From how many local master browsers should the list of
189          * workgroups be retrieved?  It can take up to 12 minutes or
190          * longer after a server becomes a local master browser, for
191          * it to have the entire browse list (the list of
192          * workgroups/domains) from an entire network.  Since a client
193          * never knows which local master browser will be found first,
194          * the one which is found first and used to retrieve a browse
195          * list may have an incomplete or empty browse list.  By
196          * requesting the browse list from multiple local master
197          * browsers, a more complete list can be generated.  For small
198          * networks (few workgroups), it is recommended that this
199          * value be set to 0, causing the browse lists from all found
200          * local master browsers to be retrieved and merged.  For
201          * networks with many workgroups, a suitable value for this
202          * variable is probably somewhere around 3. (Default: 3).
203          */
204         int             browse_max_lmb_count;
205
206         /*
207          * There is a difference in the desired return strings from
208          * smbc_readdir() depending upon whether the filenames are to
209          * be displayed to the user, or whether they are to be
210          * appended to the path name passed to smbc_opendir() to call
211          * a further smbc_ function (e.g. open the file with
212          * smbc_open()).  In the former case, the filename should be
213          * in "human readable" form.  In the latter case, the smbc_
214          * functions expect a URL which must be url-encoded.  Those
215          * functions decode the URL.  If, for example, smbc_readdir()
216          * returned a file name of "abc%20def.txt", passing a path
217          * with this file name attached to smbc_open() would cause
218          * smbc_open to attempt to open the file "abc def.txt" since
219          * the %20 is decoded into a space.
220          *
221          * Set this option to True if the names returned by
222          * smbc_readdir() should be url-encoded such that they can be
223          * passed back to another smbc_ call.  Set it to False if the
224          * names returned by smbc_readdir() are to be presented to the
225          * user.
226          *
227          * For backwards compatibility, this option defaults to False.
228          */
229         bool            urlencode_readdir_entries;
230
231         /*
232          * Some Windows versions appear to have a limit to the number
233          * of concurrent SESSIONs and/or TREE CONNECTions.  In
234          * one-shot programs (i.e. the program runs and then quickly
235          * ends, thereby shutting down all connections), it is
236          * probably reasonable to establish a new connection for each
237          * share.  In long-running applications, the limitation can be
238          * avoided by using only a single connection to each server,
239          * and issuing a new TREE CONNECT when the share is accessed.
240          */
241         bool            one_share_per_server;
242
243         /* Kerberos-related flags */
244         bool            use_kerberos;
245         bool            fallback_after_kerberos;
246
247         /* Don't try to do automatic anonymous login */
248         bool            no_auto_anonymous_login;
249
250         /* Server-related functions */
251         struct
252         {
253                 smbc_get_auth_data_fn           get_auth_data_fn;
254                 smbc_check_server_fn            check_server_fn;
255                 smbc_remove_unused_server_fn    remove_unused_server_fn;
256         }               server;
257
258         /* Cache-related functions */
259         struct
260         {
261                 struct smbc_server_cache *      server_cache_data;
262                 smbc_add_cached_srv_fn          add_cached_server_fn;
263                 smbc_get_cached_srv_fn          get_cached_server_fn;
264                 smbc_remove_cached_srv_fn       remove_cached_server_fn;
265                 smbc_purge_cached_srv_fn        purge_cached_server_fn;
266         }               cache;
267
268         /* POSIX emulation functions */
269         struct
270         {
271                 smbc_open_fn                    open_fn;
272                 smbc_creat_fn                   creat_fn;
273                 smbc_read_fn                    read_fn;
274                 smbc_write_fn                   write_fn;
275                 smbc_unlink_fn                  unlink_fn;
276                 smbc_rename_fn                  rename_fn;
277                 smbc_lseek_fn                   lseek_fn;
278                 smbc_ftruncate_fn               ftruncate_fn;
279                 smbc_stat_fn                    stat_fn;
280                 smbc_fstat_fn                   fstat_fn;
281                 smbc_close_fn                   close_fn;
282                 smbc_opendir_fn                 opendir_fn;
283                 smbc_closedir_fn                closedir_fn;
284                 smbc_readdir_fn                 readdir_fn;
285                 smbc_getdents_fn                getdents_fn;
286                 smbc_mkdir_fn                   mkdir_fn;
287                 smbc_rmdir_fn                   rmdir_fn;
288                 smbc_telldir_fn                 telldir_fn;
289                 smbc_lseekdir_fn                lseekdir_fn;
290                 smbc_fstatdir_fn                fstatdir_fn;
291                 smbc_chmod_fn                   chmod_fn;
292                 smbc_utimes_fn                  utimes_fn;
293                 smbc_setxattr_fn                setxattr_fn;
294                 smbc_getxattr_fn                getxattr_fn;
295                 smbc_removexattr_fn             removexattr_fn;
296                 smbc_listxattr_fn               listxattr_fn;
297         }               posix_emu;
298
299         /* Printing-related functions */
300         struct
301         {
302                 smbc_print_file_fn              print_file_fn;
303                 smbc_open_print_job_fn          open_print_job_fn;
304                 smbc_list_print_jobs_fn         list_print_jobs_fn;
305                 smbc_unlink_print_job_fn        unlink_print_job_fn;
306         }               printing;
307
308 #if 0 /* not yet */
309         /* SMB high-level functions */
310         struct
311         {
312         }               smb;
313
314 #endif
315 };      
316
317 /* Functions in libsmb_cache.c */
318 int
319 SMBC_add_cached_server(SMBCCTX * context,
320                        SMBCSRV * newsrv,
321                        const char * server,
322                        const char * share, 
323                        const char * workgroup,
324                        const char * username);
325
326 SMBCSRV *
327 SMBC_get_cached_server(SMBCCTX * context,
328                        const char * server, 
329                        const char * share,
330                        const char * workgroup,
331                        const char * user);
332
333 int
334 SMBC_remove_cached_server(SMBCCTX * context,
335                           SMBCSRV * server);
336
337 int
338 SMBC_purge_cached_servers(SMBCCTX * context);
339
340
341 /* Functions in libsmb_dir.c */
342 int
343 SMBC_check_options(char *server,
344                    char *share,
345                    char *path,
346                    char *options);
347
348 SMBCFILE *
349 SMBC_opendir_ctx(SMBCCTX *context,
350                  const char *fname);
351
352 int
353 SMBC_closedir_ctx(SMBCCTX *context,
354                   SMBCFILE *dir);
355
356 struct smbc_dirent *
357 SMBC_readdir_ctx(SMBCCTX *context,
358                  SMBCFILE *dir);
359
360 int
361 SMBC_getdents_ctx(SMBCCTX *context,
362                   SMBCFILE *dir,
363                   struct smbc_dirent *dirp,
364                   int count);
365
366 int
367 SMBC_mkdir_ctx(SMBCCTX *context,
368                const char *fname,
369                mode_t mode);
370
371 int
372 SMBC_rmdir_ctx(SMBCCTX *context,
373                const char *fname);
374
375 off_t
376 SMBC_telldir_ctx(SMBCCTX *context,
377                  SMBCFILE *dir);
378
379 int
380 SMBC_lseekdir_ctx(SMBCCTX *context,
381                   SMBCFILE *dir,
382                   off_t offset);
383
384 int
385 SMBC_fstatdir_ctx(SMBCCTX *context,
386                   SMBCFILE *dir,
387                   struct stat *st);
388
389 int
390 SMBC_chmod_ctx(SMBCCTX *context,
391                const char *fname,
392                mode_t newmode);
393
394 int
395 SMBC_utimes_ctx(SMBCCTX *context,
396                 const char *fname,
397                 struct timeval *tbuf);
398
399 int
400 SMBC_unlink_ctx(SMBCCTX *context,
401                 const char *fname);
402
403 int
404 SMBC_rename_ctx(SMBCCTX *ocontext,
405                 const char *oname, 
406                 SMBCCTX *ncontext,
407                 const char *nname);
408
409
410 /* Functions in libsmb_file.c */
411 SMBCFILE *
412 SMBC_open_ctx(SMBCCTX *context,
413               const char *fname,
414               int flags,
415               mode_t mode);
416
417 SMBCFILE *
418 SMBC_creat_ctx(SMBCCTX *context,
419                const char *path,
420                mode_t mode);
421
422 ssize_t
423 SMBC_read_ctx(SMBCCTX *context,
424               SMBCFILE *file,
425               void *buf,
426               size_t count);
427
428 ssize_t
429 SMBC_write_ctx(SMBCCTX *context,
430                SMBCFILE *file,
431                void *buf,
432                size_t count);
433
434 int
435 SMBC_close_ctx(SMBCCTX *context,
436                SMBCFILE *file);
437
438 bool
439 SMBC_getatr(SMBCCTX * context,
440             SMBCSRV *srv,
441             char *path,
442             uint16 *mode,
443             SMB_OFF_T *size,
444             struct timespec *create_time_ts,
445             struct timespec *access_time_ts,
446             struct timespec *write_time_ts,
447             struct timespec *change_time_ts,
448             SMB_INO_T *ino);
449
450 bool
451 SMBC_setatr(SMBCCTX * context, SMBCSRV *srv, char *path, 
452             time_t create_time,
453             time_t access_time,
454             time_t write_time,
455             time_t change_time,
456             uint16 mode);
457
458 off_t
459 SMBC_lseek_ctx(SMBCCTX *context,
460                SMBCFILE *file,
461                off_t offset,
462                int whence);
463
464 int
465 SMBC_ftruncate_ctx(SMBCCTX *context,
466                    SMBCFILE *file,
467                    off_t length);
468
469
470 /* Functions in libsmb_misc.c */
471 int
472 SMBC_dlist_contains(SMBCFILE * list, SMBCFILE *p);
473
474 int
475 SMBC_errno(SMBCCTX *context,
476            struct cli_state *c);
477
478
479 /* Functions in libsmb_path.c */
480 int
481 SMBC_urldecode(char *dest,
482                char *src,
483                size_t max_dest_len);
484
485 int
486 SMBC_urlencode(char *dest,
487                char *src,
488                int max_dest_len);
489
490 int
491 SMBC_parse_path(TALLOC_CTX *ctx,
492                 SMBCCTX *context,
493                 const char *fname,
494                 char **pp_workgroup,
495                 char **pp_server,
496                 char **pp_share,
497                 char **pp_path,
498                 char **pp_user,
499                 char **pp_password,
500                 char **pp_options);
501
502
503 /* Functions in libsmb_printjob.c */
504 SMBCFILE *
505 SMBC_open_print_job_ctx(SMBCCTX *context,
506                         const char *fname);
507
508 int
509 SMBC_print_file_ctx(SMBCCTX *c_file,
510                     const char *fname,
511                     SMBCCTX *c_print,
512                     const char *printq);
513
514 int
515 SMBC_list_print_jobs_ctx(SMBCCTX *context,
516                          const char *fname,
517                          smbc_list_print_job_fn fn);
518
519 int
520 SMBC_unlink_print_job_ctx(SMBCCTX *context,
521                           const char *fname,
522                           int id);
523
524
525 /* Functions in libsmb_server.c */
526 int
527 SMBC_check_server(SMBCCTX * context,
528                   SMBCSRV * server);
529
530 int
531 SMBC_remove_unused_server(SMBCCTX * context,
532                           SMBCSRV * srv);
533
534 void
535 SMBC_call_auth_fn(TALLOC_CTX *ctx,
536                   SMBCCTX *context,
537                   const char *server,
538                   const char *share,
539                   char **pp_workgroup,
540                   char **pp_username,
541                   char **pp_password);
542
543 void
544 SMBC_get_auth_data(const char *server, const char *share,
545                    char *workgroup_buf, int workgroup_buf_len,
546                    char *username_buf, int username_buf_len,
547                    char *password_buf, int password_buf_len);
548
549 SMBCSRV *
550 SMBC_find_server(TALLOC_CTX *ctx,
551                  SMBCCTX *context,
552                  const char *server,
553                  const char *share,
554                  char **pp_workgroup,
555                  char **pp_username,
556                  char **pp_password);
557
558 SMBCSRV *
559 SMBC_server(TALLOC_CTX *ctx,
560             SMBCCTX *context,
561             bool connect_if_not_found,
562             const char *server,
563             const char *share,
564             char **pp_workgroup,
565             char **pp_username,
566             char **pp_password);
567
568 SMBCSRV *
569 SMBC_attr_server(TALLOC_CTX *ctx,
570                  SMBCCTX *context,
571                  const char *server,
572                  const char *share,
573                  char **pp_workgroup,
574                  char **pp_username,
575                  char **pp_password);
576
577
578 /* Functions in libsmb_stat.c */
579 int
580 SMBC_stat_ctx(SMBCCTX *context,
581               const char *fname,
582               struct stat *st);
583
584 int
585 SMBC_fstat_ctx(SMBCCTX *context,
586                SMBCFILE *file,
587                struct stat *st);
588
589
590 /* Functions in libsmb_xattr.c */
591 int
592 SMBC_setxattr_ctx(SMBCCTX *context,
593                   const char *fname,
594                   const char *name,
595                   const void *value,
596                   size_t size,
597                   int flags);
598
599 int
600 SMBC_getxattr_ctx(SMBCCTX *context,
601                   const char *fname,
602                   const char *name,
603                   const void *value,
604                   size_t size);
605
606 int
607 SMBC_removexattr_ctx(SMBCCTX *context,
608                      const char *fname,
609                      const char *name);
610
611 int
612 SMBC_listxattr_ctx(SMBCCTX *context,
613                    const char *fname,
614                    char *list,
615                    size_t size);
616
617
618 #endif