Merge branch 'master' of ssh://git.samba.org/data/git/samba
[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         /* dirent pointer location */
117         struct smbc_dirent                      dirent;
118         /*
119          * Leave room for any urlencoded filename and the comment field.
120          *
121          * We use (NAME_MAX * 3) plus whatever the max length of a comment is,
122          * plus a couple of null terminators (one after the filename,
123          * one after the comment).
124          *
125          * According to <linux/limits.h>, NAME_MAX is 255.  Is it longer
126          * anyplace else?
127          */
128         char                                    _dirent_name[1024];
129
130         /*
131          * server connection list
132          */
133         SMBCSRV *                               servers;
134         
135         /*
136          * open file/dir list
137          */
138         SMBCFILE *                              files;
139
140         /*
141          * Log to standard error instead of the more typical standard output
142          */
143         bool                                    debug_stderr;
144
145         /*
146          * Support "Create Time" in get/set with the *xattr() functions, if
147          * true.  This replaces the dos attribute strings C_TIME, A_TIME and
148          * M_TIME with CHANGE_TIME, ACCESS_TIME and WRITE_TIME, and adds
149          * CREATE_TIME.  Default is FALSE, i.e.  to use the old-style shorter
150          * names and to not support CREATE time, for backward compatibility.
151          */
152         bool                                    full_time_names;
153
154         /*
155          * The share mode of a file being opened.  To match POSIX semantics
156          * (and maintain backward compatibility), DENY_NONE is the default.
157          */
158         smbc_share_mode                         share_mode;
159
160         /*
161          * Authentication function which includes the context.  This will be
162          * used if set; otherwise context->callbacks.auth_fn() will be used.
163          */
164         smbc_get_auth_data_with_context_fn      auth_fn_with_context;
165
166         /*
167          * An opaque (to this library) user data handle which can be set
168          * and retrieved with smbc_option_set() and smbc_option_get().
169          */
170         void *                                  user_data;
171
172         /*
173          * Should we attempt UNIX smb encryption ? 
174          * Set to 0 if we should never attempt, set to 1 if
175          * encryption requested, set to 2 if encryption required.
176          */
177         smbc_smb_encrypt_level                  smb_encryption_level;
178
179         struct smbc_server_cache * server_cache;
180
181         /* POSIX emulation functions */
182         struct
183         {
184 #if 0 /* Left in libsmbclient.h for backward compatibility */
185                 smbc_open_fn                    open_fn;
186                 smbc_creat_fn                   creat_fn;
187                 smbc_read_fn                    read_fn;
188                 smbc_write_fn                   write_fn;
189                 smbc_unlink_fn                  unlink_fn;
190                 smbc_rename_fn                  rename_fn;
191                 smbc_lseek_fn                   lseek_fn;
192                 smbc_stat_fn                    stat_fn;
193                 smbc_fstat_fn                   fstat_fn;
194 #endif
195                 smbc_ftruncate_fn               ftruncate_fn;
196 #if 0 /* Left in libsmbclient.h for backward compatibility */
197                 smbc_close_fn                   close_fn;
198                 smbc_opendir_fn                 opendir_fn;
199                 smbc_closedir_fn                closedir_fn;
200                 smbc_readdir_fn                 readdir_fn;
201                 smbc_getdents_fn                getdents_fn;
202                 smbc_mkdir_fn                   mkdir_fn;
203                 smbc_rmdir_fn                   rmdir_fn;
204                 smbc_telldir_fn                 telldir_fn;
205                 smbc_lseekdir_fn                lseekdir_fn;
206                 smbc_fstatdir_fn                fstatdir_fn;
207                 smbc_chmod_fn                   chmod_fn;
208                 smbc_utimes_fn                  utimes_fn;
209                 smbc_setxattr_fn                setxattr_fn;
210                 smbc_getxattr_fn                getxattr_fn;
211                 smbc_removexattr_fn             removexattr_fn;
212                 smbc_listxattr_fn               listxattr_fn;
213 #endif
214         }               posix_emu;
215
216 #if 0 /* Left in libsmbclient.h for backward compatibility */
217         /* Printing-related functions */
218         struct
219         {
220                 smbc_print_file_fn              print_file_fn;
221                 smbc_open_print_job_fn          open_print_job_fn;
222                 smbc_list_print_jobs_fn         list_print_jobs_fn;
223                 smbc_unlink_print_job_fn        unlink_print_job_fn;
224         }               printing;
225 #endif
226
227 #if 0 /* None available yet */
228         /* SMB high-level functions */
229         struct
230         {
231         }               smb;
232
233 #endif
234 };      
235
236 /* Functions in libsmb_cache.c */
237 int
238 SMBC_add_cached_server(SMBCCTX * context,
239                        SMBCSRV * newsrv,
240                        const char * server,
241                        const char * share, 
242                        const char * workgroup,
243                        const char * username);
244
245 SMBCSRV *
246 SMBC_get_cached_server(SMBCCTX * context,
247                        const char * server, 
248                        const char * share,
249                        const char * workgroup,
250                        const char * user);
251
252 int
253 SMBC_remove_cached_server(SMBCCTX * context,
254                           SMBCSRV * server);
255
256 int
257 SMBC_purge_cached_servers(SMBCCTX * context);
258
259
260 /* Functions in libsmb_dir.c */
261 int
262 SMBC_check_options(char *server,
263                    char *share,
264                    char *path,
265                    char *options);
266
267 SMBCFILE *
268 SMBC_opendir_ctx(SMBCCTX *context,
269                  const char *fname);
270
271 int
272 SMBC_closedir_ctx(SMBCCTX *context,
273                   SMBCFILE *dir);
274
275 struct smbc_dirent *
276 SMBC_readdir_ctx(SMBCCTX *context,
277                  SMBCFILE *dir);
278
279 int
280 SMBC_getdents_ctx(SMBCCTX *context,
281                   SMBCFILE *dir,
282                   struct smbc_dirent *dirp,
283                   int count);
284
285 int
286 SMBC_mkdir_ctx(SMBCCTX *context,
287                const char *fname,
288                mode_t mode);
289
290 int
291 SMBC_rmdir_ctx(SMBCCTX *context,
292                const char *fname);
293
294 off_t
295 SMBC_telldir_ctx(SMBCCTX *context,
296                  SMBCFILE *dir);
297
298 int
299 SMBC_lseekdir_ctx(SMBCCTX *context,
300                   SMBCFILE *dir,
301                   off_t offset);
302
303 int
304 SMBC_fstatdir_ctx(SMBCCTX *context,
305                   SMBCFILE *dir,
306                   struct stat *st);
307
308 int
309 SMBC_chmod_ctx(SMBCCTX *context,
310                const char *fname,
311                mode_t newmode);
312
313 int
314 SMBC_utimes_ctx(SMBCCTX *context,
315                 const char *fname,
316                 struct timeval *tbuf);
317
318 int
319 SMBC_unlink_ctx(SMBCCTX *context,
320                 const char *fname);
321
322 int
323 SMBC_rename_ctx(SMBCCTX *ocontext,
324                 const char *oname, 
325                 SMBCCTX *ncontext,
326                 const char *nname);
327
328
329 /* Functions in libsmb_file.c */
330 SMBCFILE *
331 SMBC_open_ctx(SMBCCTX *context,
332               const char *fname,
333               int flags,
334               mode_t mode);
335
336 SMBCFILE *
337 SMBC_creat_ctx(SMBCCTX *context,
338                const char *path,
339                mode_t mode);
340
341 ssize_t
342 SMBC_read_ctx(SMBCCTX *context,
343               SMBCFILE *file,
344               void *buf,
345               size_t count);
346
347 ssize_t
348 SMBC_write_ctx(SMBCCTX *context,
349                SMBCFILE *file,
350                const void *buf,
351                size_t count);
352
353 int
354 SMBC_close_ctx(SMBCCTX *context,
355                SMBCFILE *file);
356
357 bool
358 SMBC_getatr(SMBCCTX * context,
359             SMBCSRV *srv,
360             char *path,
361             uint16 *mode,
362             SMB_OFF_T *size,
363             struct timespec *create_time_ts,
364             struct timespec *access_time_ts,
365             struct timespec *write_time_ts,
366             struct timespec *change_time_ts,
367             SMB_INO_T *ino);
368
369 bool
370 SMBC_setatr(SMBCCTX * context, SMBCSRV *srv, char *path, 
371             time_t create_time,
372             time_t access_time,
373             time_t write_time,
374             time_t change_time,
375             uint16 mode);
376
377 off_t
378 SMBC_lseek_ctx(SMBCCTX *context,
379                SMBCFILE *file,
380                off_t offset,
381                int whence);
382
383 int
384 SMBC_ftruncate_ctx(SMBCCTX *context,
385                    SMBCFILE *file,
386                    off_t length);
387
388
389 /* Functions in libsmb_misc.c */
390 int
391 SMBC_dlist_contains(SMBCFILE * list, SMBCFILE *p);
392
393 int
394 SMBC_errno(SMBCCTX *context,
395            struct cli_state *c);
396
397
398 /* Functions in libsmb_path.c */
399 int
400 SMBC_urldecode(char *dest,
401                char *src,
402                size_t max_dest_len);
403
404 int
405 SMBC_urlencode(char *dest,
406                char *src,
407                int max_dest_len);
408
409 int
410 SMBC_parse_path(TALLOC_CTX *ctx,
411                 SMBCCTX *context,
412                 const char *fname,
413                 char **pp_workgroup,
414                 char **pp_server,
415                 char **pp_share,
416                 char **pp_path,
417                 char **pp_user,
418                 char **pp_password,
419                 char **pp_options);
420
421
422 /* Functions in libsmb_printjob.c */
423 SMBCFILE *
424 SMBC_open_print_job_ctx(SMBCCTX *context,
425                         const char *fname);
426
427 int
428 SMBC_print_file_ctx(SMBCCTX *c_file,
429                     const char *fname,
430                     SMBCCTX *c_print,
431                     const char *printq);
432
433 int
434 SMBC_list_print_jobs_ctx(SMBCCTX *context,
435                          const char *fname,
436                          smbc_list_print_job_fn fn);
437
438 int
439 SMBC_unlink_print_job_ctx(SMBCCTX *context,
440                           const char *fname,
441                           int id);
442
443
444 /* Functions in libsmb_server.c */
445 int
446 SMBC_check_server(SMBCCTX * context,
447                   SMBCSRV * server);
448
449 int
450 SMBC_remove_unused_server(SMBCCTX * context,
451                           SMBCSRV * srv);
452
453 void
454 SMBC_call_auth_fn(TALLOC_CTX *ctx,
455                   SMBCCTX *context,
456                   const char *server,
457                   const char *share,
458                   char **pp_workgroup,
459                   char **pp_username,
460                   char **pp_password);
461
462 void
463 SMBC_get_auth_data(const char *server, const char *share,
464                    char *workgroup_buf, int workgroup_buf_len,
465                    char *username_buf, int username_buf_len,
466                    char *password_buf, int password_buf_len);
467
468 SMBCSRV *
469 SMBC_find_server(TALLOC_CTX *ctx,
470                  SMBCCTX *context,
471                  const char *server,
472                  const char *share,
473                  char **pp_workgroup,
474                  char **pp_username,
475                  char **pp_password);
476
477 SMBCSRV *
478 SMBC_server(TALLOC_CTX *ctx,
479             SMBCCTX *context,
480             bool connect_if_not_found,
481             const char *server,
482             const char *share,
483             char **pp_workgroup,
484             char **pp_username,
485             char **pp_password);
486
487 SMBCSRV *
488 SMBC_attr_server(TALLOC_CTX *ctx,
489                  SMBCCTX *context,
490                  const char *server,
491                  const char *share,
492                  char **pp_workgroup,
493                  char **pp_username,
494                  char **pp_password);
495
496
497 /* Functions in libsmb_stat.c */
498 int
499 SMBC_stat_ctx(SMBCCTX *context,
500               const char *fname,
501               struct stat *st);
502
503 int
504 SMBC_fstat_ctx(SMBCCTX *context,
505                SMBCFILE *file,
506                struct stat *st);
507
508
509 /* Functions in libsmb_xattr.c */
510 int
511 SMBC_setxattr_ctx(SMBCCTX *context,
512                   const char *fname,
513                   const char *name,
514                   const void *value,
515                   size_t size,
516                   int flags);
517
518 int
519 SMBC_getxattr_ctx(SMBCCTX *context,
520                   const char *fname,
521                   const char *name,
522                   const void *value,
523                   size_t size);
524
525 int
526 SMBC_removexattr_ctx(SMBCCTX *context,
527                      const char *fname,
528                      const char *name);
529
530 int
531 SMBC_listxattr_ctx(SMBCCTX *context,
532                    const char *fname,
533                    char *list,
534                    size_t size);
535
536
537 #endif