summaryrefslogtreecommitdiffstats
path: root/source3/libsmb/libsmb_compat.c
blob: 5b2ef2d6f8e1cef0016977663b6d3aeeadd13a3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/* 
   Unix SMB/CIFS implementation.
   SMB client library implementation (Old interface compatibility)
   Copyright (C) Andrew Tridgell 1998
   Copyright (C) Richard Sharpe 2000
   Copyright (C) John Terpstra 2000
   Copyright (C) Tom Jansen (Ninja ISD) 2002 
   Copyright (C) Derrell Lipman 2003, 2008

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#include "includes.h"
#include "libsmb_internal.h"

struct smbc_compat_fdlist {
	SMBCFILE * file;
	int fd;
	struct smbc_compat_fdlist *next, *prev;
};

static SMBCCTX * statcont = NULL;
static int smbc_compat_initialized = 0;
static int smbc_compat_nextfd = 0;
static struct smbc_compat_fdlist * smbc_compat_fd_in_use = NULL;
static struct smbc_compat_fdlist * smbc_compat_fd_avail = NULL;

/* Find an fd and return the SMBCFILE * or NULL on failure */
static SMBCFILE *
find_fd(int fd)
{
	struct smbc_compat_fdlist * f = smbc_compat_fd_in_use;
	while (f) {
		if (f->fd == fd) 
			return f->file;
		f = f->next;
	}
	return NULL;
}

/* Add an fd, returns 0 on success, -1 on error with errno set */
static int
add_fd(SMBCFILE * file)
{
        struct smbc_compat_fdlist * f = smbc_compat_fd_avail;

	if (f) {
                /* We found one that's available */
                DLIST_REMOVE(smbc_compat_fd_avail, f);
	} else {
                /*
                 * None were available, so allocate one.  Keep the number of
                 * file descriptors determinate.  This allows the application
                 * to allocate bitmaps or mapping of file descriptors based on
                 * a known maximum number of file descriptors that will ever
                 * be returned.
                 */
                if (smbc_compat_nextfd >= FD_SETSIZE) {
                        errno = EMFILE;
                        return -1;
                }

                f = SMB_MALLOC_P(struct smbc_compat_fdlist);
                if (!f) {
                        errno = ENOMEM;
                        return -1;
                }

                f->fd = SMBC_BASE_FD + smbc_compat_nextfd++;
        }

	f->file = file;
	DLIST_ADD(smbc_compat_fd_in_use, f);

	return f->fd;
}



/* Delete an fd, returns 0 on success */
static int
del_fd(int fd)
{
	struct smbc_compat_fdlist * f = smbc_compat_fd_in_use;

	while (f) {
		if (f->fd == fd) 
			break;
		f = f->next;
	}

	if (f) {
		/* found */
		DLIST_REMOVE(smbc_compat_fd_in_use, f);
                f->file = NULL;
                DLIST_ADD(smbc_compat_fd_avail, f);
		return 0;
	}
	return 1;
}



int
smbc_init(smbc_get_auth_data_fn fn,
          int debug)
{
	if (!smbc_compat_initialized) {
		statcont = smbc_new_context();
		if (!statcont) 
			return -1;

                smbc_setDebug(statcont, debug);
                smbc_setFunctionAuthData(statcont, fn);

		if (!smbc_init_context(statcont)) {
			smbc_free_context(statcont, False);
			return -1;
		}

		smbc_compat_initialized = 1;

		return 0;
	}
	return 0;
}


SMBCCTX *
smbc_set_context(SMBCCTX * context)
{
        SMBCCTX *old_context = statcont;

        if (context) {
                /* Save provided context.  It must have been initialized! */
                statcont = context;

                /* You'd better know what you're doing.  We won't help you. */
		smbc_compat_initialized = 1;
        }

        return old_context;
}


int
smbc_open(const char *furl,
          int flags,
          mode_t mode)
{
	SMBCFILE * file;
	int fd;

        file = smbc_getFunctionOpen(statcont)(statcont, furl, flags, mode);
	if (!file)
		return -1;

	fd = add_fd(file);
	if (fd == -1) 
                smbc_getFunctionClose(statcont)(statcont, file);
	return fd;
}


int
smbc_creat(const char *furl,
           mode_t mode)
{
	SMBCFILE * file;
	int fd;

        file = smbc_getFunctionCreat(statcont)(statcont, furl, mode);
	if (!file)
		return -1;

	fd = add_fd(file);
	if (fd == -1) {
		/* Hmm... should we delete the file too ? I guess we could try */
                smbc_getFunctionClose(statcont)(statcont, file);
                smbc_getFunctionUnlink(statcont)(statcont, furl);
	}
	return fd;
}


ssize_t
smbc_read(int fd,
          void *buf,
          size_t bufsize)
{
	SMBCFILE * file = find_fd(fd);
        return smbc_getFunctionRead(statcont)(statcont, file, buf, bufsize);
}

ssize_t
smbc_write(int fd,
           const void *buf,
           size_t bufsize)
{
	SMBCFILE * file = find_fd(fd);
        return smbc_getFunctionWrite(statcont)(statcont, file, buf, bufsize);
}

off_t
smbc_lseek(int fd,
           off_t offset,
           int whence)
{
	SMBCFILE * file = find_fd(fd);
        return smbc_getFunctionLseek(statcont)(statcont, file, offset, whence);
}

int
smbc_close(int fd)
{
	SMBCFILE * file = find_fd(fd);
	del_fd(fd);
        return smbc_getFunctionClose(statcont)(statcont, file);
}

int
smbc_unlink(const char *fname)
{
        return smbc_getFunctionUnlink(statcont)(statcont, fname);
}

int
smbc_rename(const char *ourl,
            const char *nurl)
{
        return smbc_getFunctionRename(statcont)(statcont, ourl,
                                                statcont, nurl);
}

int
smbc_opendir(const char *durl)
{
	SMBCFILE * file;
	int fd;

        file = smbc_getFunctionOpendir(statcont)(statcont, durl);
	if (!file)
		return -1;

	fd = add_fd(file);
	if (fd == -1) 
                smbc_getFunctionClosedir(statcont)(statcont, file);

	return fd;
}

int
smbc_closedir(int dh) 
{
	SMBCFILE * file = find_fd(dh);
	del_fd(dh);
        return smbc_getFunctionClosedir(statcont)(statcont, file);
}

int
smbc_getdents(unsigned int dh,
              struct smbc_dirent *dirp,
              int count)
{
	SMBCFILE * file = find_fd(dh);
        return smbc_getFunctionGetdents(statcont)(statcont, file, dirp, count);
}

struct smbc_dirent *
smbc_readdir(unsigned int dh)
{
	SMBCFILE * file = find_fd(dh);
        return smbc_getFunctionReaddir(statcont)(statcont, file);
}

off_t
smbc_telldir(int dh)
{
	SMBCFILE * file = find_fd(dh);
        return smbc_getFunctionTelldir(statcont)(statcont, file);
}

int
smbc_lseekdir(int fd,
              off_t offset)
{
	SMBCFILE * file = find_fd(fd);
        return smbc_getFunctionLseekdir(statcont)(statcont, file, offset);
}

int
smbc_mkdir(const char *durl,
           mode_t mode)
{
        return smbc_getFunctionMkdir(statcont)(statcont, durl, mode);
}

int
smbc_rmdir(const char *durl)
{
        return smbc_getFunctionRmdir(statcont)(statcont, durl);
}

int
smbc_stat(const char *url,
          struct stat *st)
{
        return smbc_getFunctionStat(statcont)(statcont, url, st);
}

int
smbc_fstat(int fd,
           struct stat *st)
{
	SMBCFILE * file = find_fd(fd);
        return smbc_getFunctionFstat(statcont)(statcont, file, st);
}

int
smbc_statvfs(char *path,
             struct statvfs *st)
{
        return smbc_getFunctionStatVFS(statcont)(statcont, path, st);
}

int
smbc_fstatvfs(int fd,
              struct statvfs *st)
{
	SMBCFILE * file = find_fd(fd);
        return smbc_getFunctionFstatVFS(statcont)(statcont, file, st);
}

int
smbc_ftruncate(int fd,
               off_t size)
{
	SMBCFILE * file = find_fd(fd);
        return smbc_getFunctionFtruncate(statcont)(statcont, file, size);
}

int
smbc_chmod(const char *url,
           mode_t mode)
{
        return smbc_getFunctionChmod(statcont)(statcont, url, mode);
}

int
smbc_utimes(const char *fname,
            struct timeval *tbuf)
{
        return smbc_getFunctionUtimes(statcont)(statcont, fname, tbuf);
}

#ifdef HAVE_UTIME_H
int
smbc_utime(const char *fname,
           struct utimbuf *utbuf)
{
        struct timeval tv[2];

        if (utbuf == NULL)
                return smbc_getFunctionUtimes(statcont)(statcont, fname, NULL);

        tv[0].tv_sec = utbuf->actime;
        tv[1].tv_sec = utbuf->modtime;
        tv[0].tv_usec = tv[1].tv_usec = 0;

        return smbc_getFunctionUtimes(statcont)(statcont, fname, tv);
}
#endif

int
smbc_setxattr(const char *fname,
              const char *name,
              const void *value,
              size_t size,
              int flags)
{
        return smbc_getFunctionSetxattr(statcont)(statcont,
                                                  fname, name,
                                                  value, size, flags);
}

int
smbc_lsetxattr(const char *fname,
               const char *name,
               const void *value,
               size_t size,
               int flags)
{
        return smbc_getFunctionSetxattr(statcont)(statcont,
                                                  fname, name,
                                                  value, size, flags);
}

int
smbc_fsetxattr(int fd,
               const char *name,
               const void *value,
               size_t size,
               int flags)
{
	SMBCFILE * file = find_fd(fd);
	if (file == NULL) {
		errno = EBADF;
		return -1;
	}
        return smbc_getFunctionSetxattr(statcont)(statcont,
                                                  file->fname, name,
                                                  value, size, flags);
}

int
smbc_getxattr(const char *fname,
              const char *name,
              const void *value,
              size_t size)
{
        return smbc_getFunctionGetxattr(statcont)(statcont,
                                                  fname, name,
                                                  value, size);
}

int
smbc_lgetxattr(const char *fname,
               const char *name,
               const void *value,
               size_t size)
{
        return smbc_getFunctionGetxattr(statcont)(statcont,
                                                  fname, name,
                                                  value, size);
}

int
smbc_fgetxattr(int fd,
               const char *name,
               const void *value,
               size_t size)
{
	SMBCFILE * file = find_fd(fd);
	if (file == NULL) {
		errno = EBADF;
		return -1;
	}
        return smbc_getFunctionGetxattr(statcont)(statcont,
                                                  file->fname, name,
                                                  value, size);
}

int
smbc_removexattr(const char *fname,
                 const char *name)
{
        return smbc_getFunctionRemovexattr(statcont)(statcont, fname, name);
}

int
smbc_lremovexattr(const char *fname,
                  const char *name)
{
        return smbc_getFunctionRemovexattr(statcont)(statcont, fname, name);
}

int
smbc_fremovexattr(int fd,
                  const char *name)
{
	SMBCFILE * file = find_fd(fd);
	if (file == NULL) {
		errno = EBADF;
		return -1;
	}
        return smbc_getFunctionRemovexattr(statcont)(statcont,
                                                     file->fname, name);
}

int
smbc_listxattr(const char *fname,
               char *list,
               size_t size)
{
        return smbc_getFunctionListxattr(statcont)(statcont,
                                                   fname, list, size);
}

int
smbc_llistxattr(const char *fname,
                char *list,
                size_t size)
{
        return smbc_getFunctionListxattr(statcont)(statcont,
                                                   fname, list, size);
}

int
smbc_flistxattr(int fd,
                char *list,
                size_t size)
{
	SMBCFILE * file = find_fd(fd);
	if (file == NULL) {
		errno = EBADF;
		return -1;
	}
        return smbc_getFunctionListxattr(statcont)(statcont,
                                                   file->fname, list, size);
}

int
smbc_print_file(const char *fname,
                const char *printq)
{
        return smbc_getFunctionPrintFile(statcont)(statcont, fname,
                                                   statcont, printq);
}

int
smbc_open_print_job(const char *fname)
{
        SMBCFILE * file;

        file = smbc_getFunctionOpenPrintJob(statcont)(statcont, fname);
	if (!file) return -1;
	return file->cli_fd;
}

int
smbc_list_print_jobs(const char *purl,
                     smbc_list_print_job_fn fn)
{
        return smbc_getFunctionListPrintJobs(statcont)(statcont, purl, fn);
}

int
smbc_unlink_print_job(const char *purl,
                      int id)
{
        return smbc_getFunctionUnlinkPrintJob(statcont)(statcont, purl, id);
}