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