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