summaryrefslogtreecommitdiff
path: root/source3/smbd/vfs.c
blob: 7304229f8d56e9eaa2cb1abfeb2e9fd034fdbf18 (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
/* 
   Unix SMB/Netbios implementation.
   Version 1.9.
   vfs initialisation and support functions
   Copyright (C) Tim Potter 1992-1998
   
   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 2 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, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#ifdef HAVE_LIBDL
#include <dlfcn.h>
#endif

extern int DEBUGLEVEL;

/* Some structures to help us initialise the vfs operations table */

struct vfs_syminfo {
    char *name;
    void *fptr;
};

struct vfs_ops dl_ops;

struct vfs_syminfo vfs_syminfo[] = {

    /* Global operations */

    {"vfs_init",       &dl_ops.init},

    /* Disk operations */

    {"vfs_connect",    &dl_ops.connect},
    {"vfs_disconnect", &dl_ops.disconnect},
    {"vfs_disk_free",  &dl_ops.disk_free},

    /* Directory operations */

    {"vfs_opendir",    &dl_ops.opendir},
    {"vfs_readdir",    &dl_ops.readdir},
    {"vfs_mkdir",      &dl_ops.mkdir},
    {"vfs_rmdir",      &dl_ops.rmdir},
    {"vfs_closedir",   &dl_ops.closedir},

    /* File operations */

    {"vfs_open",       &dl_ops.open},
    {"vfs_close",      &dl_ops.close},
    {"vfs_read",       &dl_ops.read},
    {"vfs_write",      &dl_ops.write},
    {"vfs_lseek",      &dl_ops.lseek},
    {"vfs_rename",     &dl_ops.rename},
    {"vfs_sync",       &dl_ops.sync},
    {"vfs_stat",       &dl_ops.stat},
    {"vfs_fstat",      &dl_ops.fstat},
    {"vfs_lstat",      &dl_ops.lstat},
    {"vfs_lock",       &dl_ops.lock},
    {"vfs_unlink",     &dl_ops.unlink},
    {"vfs_chmod",      &dl_ops.chmod},
    {"vfs_utime",      &dl_ops.utime},
    
    {NULL, 0}
};

/* Default vfs hooks.  WARNING: The order of these initialisers is
   very important.  Change at your own peril. */

struct vfs_ops default_vfs_ops = {

    /* Global operations */

    NULL,                         /* init */

    /* Disk operations */        

    NULL,                         /* connect */
    NULL,                         /* disconnect */
    vfswrap_disk_free,

    /* Directory operations */

    vfswrap_opendir,
    vfswrap_readdir,
    vfswrap_mkdir,
    vfswrap_rmdir,
    vfswrap_closedir,

    /* File operations */

    vfswrap_open,
    vfswrap_close,
    vfswrap_read,
    vfswrap_write,
    vfswrap_lseek,
    vfswrap_rename,
    vfswrap_sync_file,
    vfswrap_stat,
    vfswrap_fstat,
    vfswrap_lstat,
    vfswrap_fcntl_lock,
    vfswrap_unlink,
    vfswrap_chmod,
    vfswrap_utime
};

/****************************************************************************
  call vfs_init function of loadable module
****************************************************************************/
#ifdef HAVE_LIBDL
BOOL do_vfs_init(char *vfs_object)
{
    void *handle, (*fptr)(void);

    DEBUG(3, ("Calling vfs_init for module %s\n", vfs_object));

    handle = dlopen(vfs_object, RTLD_NOW);
    if (!handle) {
	DEBUG(0, ("Error opening %s: %s\n", vfs_object, dlerror()));
	return False;
    }

    fptr = dlsym(handle, "vfs_init");

    /* Call initialisation function */

    if (fptr != NULL) {
	fptr();
    }

    dlclose(handle);

    return True;
}
#endif

/****************************************************************************
  initialise default vfs hooks
****************************************************************************/
int vfs_init_default(connection_struct *conn)
{
    DEBUG(3, ("Initialising default vfs hooks\n"));

    bcopy(&default_vfs_ops, &conn->vfs_ops, sizeof(conn->vfs_ops));
    return 0;
}

/****************************************************************************
  initialise custom vfs hooks
****************************************************************************/
#ifdef HAVE_LIBDL
int vfs_init_custom(connection_struct *conn)
{
    void *handle, *fptr;
    int index;

    DEBUG(3, ("Initialising custom vfs hooks from %s\n",
	      lp_vfsobj(SNUM(conn))));

    /* Open object file */

    handle = dlopen(lp_vfsobj(SNUM(conn)), RTLD_NOW);
    if (!handle) {
	DEBUG(0, ("Error opening %s: %s\n", lp_vfsobj(SNUM(conn)),
		  dlerror()));
	return -1;
    }

    /* Read list of symbols */

    for(index = 0; vfs_syminfo[index].name; index++) {
	fptr = dlsym(handle, vfs_syminfo[index].name);
	if (fptr == NULL) {
	    DEBUG(0, ("Symbol %s not found in %s\n", vfs_syminfo[index].name,
		      lp_vfsobj(SNUM(conn))));
	    return -1;
	}

	*((void **)vfs_syminfo[index].fptr) = fptr;
    }

    /* Copy loaded symbols into connection struct */

    bcopy(&dl_ops, &conn->vfs_ops, sizeof(dl_ops));
    dlclose(handle);

    do_vfs_init(lp_vfsobj(SNUM(conn)));

    return 0;
}
#endif

/*******************************************************************
  check if a vfs file exists
********************************************************************/
BOOL vfs_file_exist(connection_struct *conn,char *fname,SMB_STRUCT_STAT *sbuf)
{
  SMB_STRUCT_STAT st;
  if (!sbuf) sbuf = &st;
  
  if (conn->vfs_ops.stat(fname,sbuf) != 0) 
    return(False);

  return(S_ISREG(sbuf->st_mode));
}

/****************************************************************************
  write data to a fd on the vfs
****************************************************************************/
ssize_t vfs_write_data(files_struct *fsp,char *buffer,size_t N)
{
  size_t total=0;
  ssize_t ret;
  int fd = fsp->fd_ptr->fd;

  while (total < N)
  {
    ret = fsp->conn->vfs_ops.write(fd,buffer + total,N - total);

    if (ret == -1) return -1;
    if (ret == 0) return total;

    total += ret;
  }
  return (ssize_t)total;
}

/****************************************************************************
transfer some data between two file_struct's
****************************************************************************/
SMB_OFF_T vfs_transfer_file(int in_fd, files_struct *in_fsp, 
			    int out_fd, files_struct *out_fsp,
			    SMB_OFF_T n, char *header, int headlen, int align)
{
  static char *buf=NULL;  
  static int size=0;
  char *buf1,*abuf;
  SMB_OFF_T total = 0;

  DEBUG(4,("vfs_transfer_file n=%.0f  (head=%d) called\n",(double)n,headlen));

  /* Check we have at least somewhere to read from */

  SMB_ASSERT((in_fd != -1) || (in_fsp != NULL));

  if (size == 0) {
    size = lp_readsize();
    size = MAX(size,1024);
  }

  while (!buf && size>0) {
    buf = (char *)Realloc(buf,size+8);
    if (!buf) size /= 2;
  }

  if (!buf) {
    DEBUG(0,("Can't allocate transfer buffer!\n"));
    exit(1);
  }

  abuf = buf + (align%8);

  if (header)
    n += headlen;

  while (n > 0)
  {
    int s = (int)MIN(n,(SMB_OFF_T)size);
    int ret,ret2=0;

    ret = 0;

    if (header && (headlen >= MIN(s,1024))) {
      buf1 = header;
      s = headlen;
      ret = headlen;
      headlen = 0;
      header = NULL;
    } else {
      buf1 = abuf;
    }

    if (header && headlen > 0)
    {
      ret = MIN(headlen,size);
      memcpy(buf1,header,ret);
      headlen -= ret;
      header += ret;
      if (headlen <= 0) header = NULL;
    }

    if (s > ret) {
      ret += in_fsp ? 
	  in_fsp->conn->vfs_ops.read(in_fsp->fd_ptr->fd,buf1+ret,s-ret) : read(in_fd,buf1+ret,s-ret);
    }

    if (ret > 0)
    {
	if (out_fsp) {
	    ret2 = out_fsp->conn->vfs_ops.write(out_fsp->fd_ptr->fd,buf1,ret);
	} else {
	    ret2= (out_fd != -1) ? write_data(out_fd,buf1,ret) : ret;
	}
    }

      if (ret2 > 0) total += ret2;
      /* if we can't write then dump excess data */
      if (ret2 != ret)
        vfs_transfer_file(in_fd, in_fsp, -1,NULL,n-(ret+headlen),NULL,0,0);

    if (ret <= 0 || ret2 != ret)
      return(total);
    n -= ret;
  }
  return(total);
}

/*******************************************************************
a vfs_readdir wrapper which just returns the file name
********************************************************************/
char *vfs_readdirname(connection_struct *conn, void *p)
{
	struct dirent *ptr;
	char *dname;

	if (!p) return(NULL);
  
	ptr = (struct dirent *)conn->vfs_ops.readdir(p);
	if (!ptr) return(NULL);

	dname = ptr->d_name;

#ifdef NEXT2
	if (telldir(p) < 0) return(NULL);
#endif

#ifdef HAVE_BROKEN_READDIR
	/* using /usr/ucb/cc is BAD */
	dname = dname - 2;
#endif

	{
		static pstring buf;
		memcpy(buf, dname, NAMLEN(ptr)+1);
		unix_to_dos(buf, True);
		dname = buf;
	}

	unix_to_dos(dname, True);
	return(dname);
}