summaryrefslogtreecommitdiff
path: root/source3
diff options
context:
space:
mode:
Diffstat (limited to 'source3')
-rw-r--r--source3/client/clitar.c38
-rw-r--r--source3/include/charset.h12
-rw-r--r--source3/include/includes.h44
-rw-r--r--source3/include/kanji.h4
-rw-r--r--source3/include/proto.h2
-rw-r--r--source3/include/smb.h25
-rw-r--r--source3/include/version.h2
-rw-r--r--source3/lib/charcnv.c35
-rw-r--r--source3/lib/charset.c116
-rw-r--r--source3/lib/interface.c2
-rw-r--r--source3/lib/system.c4
-rw-r--r--source3/lib/util.c10
-rw-r--r--source3/locking/locking.c64
-rw-r--r--source3/smbd/pipes.c2
-rw-r--r--source3/smbd/reply.c40
-rw-r--r--source3/smbd/server.c412
-rw-r--r--source3/smbd/trans2.c24
-rw-r--r--source3/utils/status.c17
18 files changed, 629 insertions, 224 deletions
diff --git a/source3/client/clitar.c b/source3/client/clitar.c
index 191e0e4dab..cd819ad21f 100644
--- a/source3/client/clitar.c
+++ b/source3/client/clitar.c
@@ -702,6 +702,22 @@ static BOOL ensurepath(char *fname, char *inbuf, char *outbuf)
return True;
}
+int padit(char *buf, int bufsize, int padsize)
+{
+ int berr= 0;
+ int bytestowrite;
+
+ DEBUG(0, ("Padding with %d zeros\n", padsize));
+ memset(buf, 0, bufsize);
+ while( !berr && padsize > 0 ) {
+ bytestowrite= MIN(bufsize, padsize);
+ berr = dotarbuf(tarhandle, buf, bytestowrite) != bytestowrite;
+ padsize -= bytestowrite;
+ }
+
+ return berr;
+}
+
/*
* smbclient functions
*/
@@ -1033,7 +1049,15 @@ static void do_atar(char *rname,char *lname,file_info *finfo1)
dataptr=NULL;
datalen=0;
}
-
+
+ /* pad tar file with zero's if we couldn't get entire file */
+ if (nread < finfo.size)
+ {
+ DEBUG(0, ("Didn't get entire file. size=%d, nread=%d\n", finfo.size, nread));
+ if (padit(inbuf, BUFFER_SIZE, finfo.size - nread))
+ DEBUG(0,("Error writing local file\n"));
+ }
+
/* round tar file to nearest block */
if (finfo.size % TBLOCK)
dozerobuf(tarhandle, TBLOCK - (finfo.size % TBLOCK));
@@ -1681,17 +1705,15 @@ int tar_parseargs(int argc, char *argv[], char *Optarg, int Optind)
return 0;
}
+ tar_excl=tar_clipfl!='X';
+ if (Optind+1<argc) {
+ cliplist=argv+Optind+1;
+ clipn=argc-Optind-1;
+ }
if (Optind>=argc || !strcmp(argv[Optind], "-")) {
/* Sets tar handle to either 0 or 1, as appropriate */
tarhandle=(tar_type=='c');
} else {
- tar_excl=tar_clipfl!='X';
-
- if (Optind+1<argc) {
- cliplist=argv+Optind+1;
- clipn=argc-Optind-1;
- }
-
if ((tar_type=='x' && (tarhandle = open(argv[Optind], O_RDONLY)) == -1)
|| (tar_type=='c' && (tarhandle=creat(argv[Optind], 0644)) < 0))
{
diff --git a/source3/include/charset.h b/source3/include/charset.h
index 14b6ec2020..72a8810e42 100644
--- a/source3/include/charset.h
+++ b/source3/include/charset.h
@@ -51,15 +51,15 @@ extern void charset_initialise(void);
#undef isspace
#endif
-#define toupper(c) upper_char_map[(char)(c)]
-#define tolower(c) lower_char_map[(char)(c)]
-#define isupper(c) (((char)(c)) != tolower(c))
-#define islower(c) (((char)(c)) != toupper(c))
-#define isdoschar(c) (dos_char_map[(char)(c)] != 0)
+#define toupper(c) (upper_char_map[(c&0xff)] & 0xff)
+#define tolower(c) (lower_char_map[(c&0xff)] & 0xff)
+#define isupper(c) ((c&0xff) != tolower(c&0xff))
+#define islower(c) ((c&0xff) != toupper(c&0xff))
+#define isdoschar(c) (dos_char_map[(c&0xff)] != 0)
#define isspace(c) ((c)==' ' || (c) == '\t')
/* this is used to determine if a character is safe to use in
something that may be put on a command line */
-#define issafe(c) (isalnum(c) || strchr("-._",c))
+#define issafe(c) (isalnum((c&0xff)) || strchr("-._",c))
#endif
diff --git a/source3/include/includes.h b/source3/include/includes.h
index 0b989df20d..0e93df35fd 100644
--- a/source3/include/includes.h
+++ b/source3/include/includes.h
@@ -259,11 +259,21 @@ typedef unsigned short mode_t;
#include <utime.h>
#define NO_STRERROR
#endif
+#ifndef REPLACE_GETPASS
#define REPLACE_GETPASS
+#endif
+#ifndef BSD_TERMIO
#define BSD_TERMIO
+#endif
+#ifndef USE_SIGPROCMASK
#define USE_SIGPROCMASK
+#endif
+#ifndef USE_WAITPID
#define USE_WAITPID
#endif
+/* SunOS doesn't have POSIX atexit */
+#define atexit on_exit
+#endif
#ifdef SUNOS5
@@ -496,6 +506,7 @@ char *mktemp(char *); /* No standard include */
#include <sys/id.h>
#include <sys/priv.h>
#include <netinet/tcp.h>
+#include <locale.h>
#define SYSV
#define USE_WAITPID
#define USE_SIGBLOCK
@@ -642,6 +653,7 @@ char *mktemp(char *); /* No standard include */
#include <sys/statfs.h>
#include <sys/stropts.h>
#include <limits.h>
+#include <locale.h>
#ifdef EVEREST
#include <unistd.h>
#endif
@@ -916,6 +928,38 @@ typedef int mode_t;
#include <sys/bsdioctl.h>
#endif
+#ifdef AMIGA
+#include <arpa/inet.h>
+#include <dirent.h>
+#include <string.h>
+#include <netinet/tcp.h>
+#include <sys/acct.h>
+#include <sys/fcntl.h>
+#include <sys/filio.h>
+#include <sys/sockio.h>
+#include <netinet/in_systm.h>
+#include <netinet/ip.h>
+#include <sys/termios.h>
+#include <limits.h>
+#include <sys/timeb.h>
+
+#define SIGNAL_CAST (void (*)(int))
+#define USE_GETCWD
+#define HAVE_BZERO
+#define HAVE_MEMMOVE
+#define USE_SIGPROCMASK
+#define USE_WAITPID
+#define USE_DIRECT
+#define USE_F_FSIZE
+#define HAVE_FCNTL_LOCK 0
+#define HAVE_GETTIMEOFDAY
+#define HAVE_PATHCONF
+
+#define HAVE_NO_PROC
+#define NO_FORK_DEBUG
+#define HAVE_FORK 0
+#define HAVE_VFORK 1
+#endif
/*******************************************************************
diff --git a/source3/include/kanji.h b/source3/include/kanji.h
index 4f18305c63..6bc88b704d 100644
--- a/source3/include/kanji.h
+++ b/source3/include/kanji.h
@@ -122,8 +122,8 @@ int interpret_coding_system (char *str, int def);
#else
-#define unix_to_dos(x,y) (x)
-#define dos_to_unix(x,y) (x)
+#define unix_to_dos(x,y) unix2dos_format(x,y)
+#define dos_to_unix(x,y) dos2unix_format(x,y)
#endif /* not KANJI */
diff --git a/source3/include/proto.h b/source3/include/proto.h
index 3ba06116ed..bbe74e3270 100644
--- a/source3/include/proto.h
+++ b/source3/include/proto.h
@@ -666,7 +666,7 @@ BOOL unix_convert(char *name,int cnum);
int disk_free(char *path,int *bsize,int *dfree,int *dsize);
int sys_disk_free(char *path,int *bsize,int *dfree,int *dsize);
BOOL check_name(char *name,int cnum);
-void open_file(int fnum,int cnum,char *fname1,int flags,int mode);
+void open_file(int fnum,int cnum,char *fname1,int flags,int mode, struct stat *st);
void sync_file(int fnum);
void close_file(int fnum);
BOOL check_file_sharing(int cnum,char *fname);
diff --git a/source3/include/smb.h b/source3/include/smb.h
index f499459f98..bbb477f5f7 100644
--- a/source3/include/smb.h
+++ b/source3/include/smb.h
@@ -286,10 +286,27 @@ typedef struct
BOOL wr_discard; /* discard all further data */
} write_bmpx_struct;
+/*
+ * Structure used to indirect fd's from the files_struct.
+ * Needed as POSIX locking is based on file and process, not
+ * file descriptor and process.
+ */
+
typedef struct
{
- int cnum;
+ uint16 ref_count;
+ int32 dev;
+ int32 inode;
int fd;
+ int fd_readonly;
+ int fd_writeonly;
+ int real_open_flags;
+} file_fd_struct;
+
+typedef struct
+{
+ int cnum;
+ file_fd_struct *fd_ptr;
int pos;
int size;
int mode;
@@ -297,7 +314,7 @@ typedef struct
char *mmap_ptr;
int mmap_size;
write_bmpx_struct *wbmpx_ptr;
- time_t open_time;
+ struct timeval open_time;
BOOL open;
BOOL can_lock;
BOOL can_read;
@@ -405,7 +422,7 @@ typedef struct
shm_offset_t next_offset; /* offset of next record in list in shared mem */
int locking_version;
int share_mode;
- time_t time;
+ struct timeval time;
int pid;
dev_t st_dev;
ino_t st_ino;
@@ -428,7 +445,7 @@ struct connect_record
};
-#define LOCKING_VERSION 2
+#define LOCKING_VERSION 3
/* these are useful macros for checking validity of handles */
#define VALID_FNUM(fnum) (((fnum) >= 0) && ((fnum) < MAX_OPEN_FILES))
diff --git a/source3/include/version.h b/source3/include/version.h
index ff567e4e59..9bad7178c4 100644
--- a/source3/include/version.h
+++ b/source3/include/version.h
@@ -1 +1 @@
-#define VERSION "1.9.16p9"
+#define VERSION "1.9.16p10"
diff --git a/source3/lib/charcnv.c b/source3/lib/charcnv.c
index d9ee551d6a..4654101a5c 100644
--- a/source3/lib/charcnv.c
+++ b/source3/lib/charcnv.c
@@ -20,6 +20,7 @@
*/
#include "includes.h"
+#define CTRLZ 26
extern int DEBUGLEVEL;
static char cvtbuf[1024];
@@ -52,20 +53,30 @@ static void update_map(char * str) {
static void initiso() {
+ int i;
if (!mapsinited) initmaps();
- update_map("\241\255\242\233\243\234\244\236\245\235\246\272\247\025\250\251");
- update_map("\251\273\252\246\253\256\254\252\255\274\256\310\257\257\260\370");
- update_map("\261\361\262\375\263\264\264\265\265\266\266\024\267\371\270\267");
- update_map("\271\270\272\247\273\275\274\254\275\253\276\276\277\250\200\277");
- update_map("\301\300\302\301\303\302\304\216\305\217\306\222\307\200\310\303");
- update_map("\311\220\312\305\313\306\314\307\315\315\316\317\317\320\320\311");
- update_map("\321\245\322\321\323\322\324\323\325\324\326\231\327\312\330\325");
- update_map("\331\326\332\327\333\330\334\232\335\313\336\314\337\341\340\205");
- update_map("\341\240\342\203\343\331\344\204\345\206\346\221\347\207\350\212");
- update_map("\351\202\352\210\353\211\354\215\355\241\356\214\357\213\360\316");
- update_map("\361\244\362\225\363\242\364\223\365\332\366\224\367\366\370\362");
- update_map("\371\227\372\243\373\226\374\201\375\304\376\263\377\230");
+ /* Do not map undefined characters to some accidental code */
+ for (i = 128; i < 256; i++)
+ {
+ unix2dos[i] = CTRLZ;
+ dos2unix[i] = CTRLZ;
+ }
+
+/* MSDOS Code Page 850 -> ISO-8859 */
+update_map("\240\377\241\255\242\275\243\234\244\317\245\276\246\335\247\365");
+update_map("\250\371\251\270\252\246\253\256\254\252\255\360\256\251\257\356");
+update_map("\260\370\261\361\262\375\263\374\264\357\265\346\266\364\267\372");
+update_map("\270\367\271\373\272\247\273\257\274\254\275\253\276\363\277\250");
+update_map("\300\267\301\265\302\266\303\307\304\216\305\217\306\222\307\200");
+update_map("\310\324\311\220\312\322\313\323\314\336\315\326\316\327\317\330");
+update_map("\320\321\321\245\322\343\323\340\324\342\325\345\326\231\327\236");
+update_map("\330\235\331\353\332\351\333\352\334\232\335\355\336\350\337\341");
+update_map("\340\205\341\240\342\203\343\306\344\204\345\206\346\221\347\207");
+update_map("\350\212\351\202\352\210\353\211\354\215\355\241\356\214\357\213");
+update_map("\360\320\361\244\362\225\363\242\364\223\365\344\366\224\367\366");
+update_map("\370\233\371\227\372\243\373\226\374\201\375\354\376\347\377\230");
+
}
/*
diff --git a/source3/lib/charset.c b/source3/lib/charset.c
index cd8c7577c9..65e0cc9a34 100644
--- a/source3/lib/charset.c
+++ b/source3/lib/charset.c
@@ -28,20 +28,20 @@ char xx_dos_char_map[256];
char xx_upper_char_map[256];
char xx_lower_char_map[256];
-char *dos_char_map = NULL;
-char *upper_char_map = NULL;
-char *lower_char_map = NULL;
+char *dos_char_map = xx_dos_char_map;
+char *upper_char_map = xx_upper_char_map;
+char *lower_char_map = xx_lower_char_map;
static void add_dos_char(int lower, int upper)
{
+ lower &= 0xff;
+ upper &= 0xff;
DEBUG(6,("Adding chars 0%o 0%o\n",lower,upper));
- if (lower) dos_char_map[(char)lower] = 1;
- if (upper) dos_char_map[(char)upper] = 1;
+ if (lower) dos_char_map[lower] = 1;
+ if (upper) dos_char_map[upper] = 1;
if (lower && upper) {
- lower_char_map[(char)upper] = (char)lower;
- upper_char_map[(char)lower] = (char)upper;
- lower_char_map[(char)lower] = (char)lower;
- upper_char_map[(char)upper] = (char)upper;
+ lower_char_map[upper] = (char)lower;
+ upper_char_map[lower] = (char)upper;
}
}
@@ -52,12 +52,14 @@ void charset_initialise(void)
{
int i;
- dos_char_map = &xx_dos_char_map[128];
- upper_char_map = &xx_upper_char_map[128];
- lower_char_map = &xx_lower_char_map[128];
+#ifdef LC_ALL
+ /* include <locale.h> in includes.h if available for OS */
+ /* we take only standard 7-bit ASCII definitions from ctype */
+ setlocale(LC_ALL,"C");
+#endif
- for (i= -128;i<=127;i++) {
- dos_char_map[(char)i] = 0;
+ for (i= 0;i<=255;i++) {
+ dos_char_map[i] = 0;
}
for (i=0;i<=127;i++) {
@@ -65,29 +67,83 @@ void charset_initialise(void)
add_dos_char(i,0);
}
- for (i= -128;i<=127;i++) {
+ for (i=0; i<=255; i++) {
char c = (char)i;
upper_char_map[i] = lower_char_map[i] = c;
- if (isupper(c)) lower_char_map[c] = tolower(c);
- if (islower(c)) upper_char_map[c] = toupper(c);
+ if (isupper(c)) lower_char_map[i] = tolower(c);
+ if (islower(c)) upper_char_map[i] = toupper(c);
}
- /* valid for all DOS PC */
- add_dos_char(142,0); /* A trema */
- add_dos_char(143,0); /* A o */
- add_dos_char(144,0); /* E ' */
- add_dos_char(146,0); /* AE */
- add_dos_char(153,0); /* O trema */
- add_dos_char(154,0); /* U trema */
- add_dos_char(165,0); /* N tilda */
- add_dos_char(128,0); /* C cedille */
+#define CP850
+#ifdef CP850
+/* lower->upper mapping for IBM Code Page 850 */
+
+/* dec col/row oct hex description */
+/* 133 08/05 205 85 a grave */
+/* 183 11/07 267 B7 A grave */ add_dos_char(0205,0267);
+/* 160 10/00 240 A0 a acute */
+/* 181 11/05 265 B5 A acute */ add_dos_char(0240,0265);
+/* 131 08/03 203 83 a circumflex */
+/* 182 11/06 266 B6 A circumflex */ add_dos_char(0203,0266);
+/* 198 12/06 306 C6 a tilde */
+/* 199 12/07 307 C7 A tilde */ add_dos_char(0306,0307);
+/* 132 08/04 204 84 a diaeresis */
+/* 142 08/14 216 8E A diaeresis */ add_dos_char(0204,0216);
+/* 134 08/06 206 86 a ring */
+/* 143 08/15 217 8F A ring */ add_dos_char(0206,0217);
+/* 145 09/01 221 91 ae diphthong */
+/* 146 09/02 222 92 AE diphthong */ add_dos_char(0221,0222);
+/* 128 08/00 200 80 C cedilla */
+/* 135 08/07 207 87 c cedilla */ add_dos_char(0207,0200);
+/* 138 08/10 212 8A e grave */
+/* 212 13/04 324 D4 E grave */ add_dos_char(0212,0324);
+/* 130 08/02 202 82 e acute */
+/* 144 09/00 220 90 E acute */ add_dos_char(0202,0220);
+/* 136 08/08 210 88 e circumflex */
+/* 210 13/02 322 D2 E circumflex */ add_dos_char(0210,0322);
+/* 137 08/09 211 89 e diaeresis */
+/* 211 13/03 323 D3 E diaeresis */ add_dos_char(0211,0323);
+/* 141 08/13 215 8D i grave */
+/* 222 13/14 336 DE I grave */ add_dos_char(0215,0336);
+/* 161 10/01 241 A1 i acute */
+/* 214 13/06 326 D6 I acute */ add_dos_char(0241,0326);
+/* 140 08/12 214 8C i circumflex */
+/* 215 13/07 327 D7 I circumflex */ add_dos_char(0214,0327);
+/* 139 08/11 213 8B i diaeresis */
+/* 216 13/08 330 D8 I diaeresis */ add_dos_char(0213,0330);
+/* 208 13/00 320 D0 Icelandic eth */
+/* 209 13/01 321 D1 Icelandic Eth */ add_dos_char(0320,0321);
+/* 164 10/04 244 A4 n tilde */
+/* 165 10/05 245 A5 N tilde */ add_dos_char(0244,0245);
+/* 149 09/05 225 95 o grave */
+/* 227 14/03 343 E3 O grave */ add_dos_char(0225,0343);
+/* 162 10/02 242 A2 o acute */
+/* 224 14/00 340 E0 O acute */ add_dos_char(0242,0340);
+/* 147 09/03 223 93 o circumflex */
+/* 226 14/02 342 E2 O circumflex */ add_dos_char(0223,0342);
+/* 228 14/04 344 E4 o tilde */
+/* 229 14/05 345 E5 O tilde */ add_dos_char(0344,0345);
+/* 148 09/04 224 94 o diaeresis */
+/* 153 09/09 231 99 O diaeresis */ add_dos_char(0224,0231);
+/* 155 09/11 233 9B o slash */
+/* 157 09/13 235 9D O slash */ add_dos_char(0233,0235);
+/* 151 09/07 227 97 u grave */
+/* 235 14/11 353 EB U grave */ add_dos_char(0227,0353);
+/* 163 10/03 243 A3 u acute */
+/* 233 14/09 351 E9 U acute */ add_dos_char(0243,0351);
+/* 150 09/06 226 96 u circumflex */
+/* 234 14/10 352 EA U circumflex */ add_dos_char(0226,0352);
+/* 129 08/01 201 81 u diaeresis */
+/* 154 09/10 232 9A U diaeresis */ add_dos_char(0201,0232);
+/* 236 14/12 354 EC y acute */
+/* 237 14/13 355 ED Y acute */ add_dos_char(0354,0355);
+/* 231 14/07 347 E7 Icelandic thorn */
+/* 232 14/08 350 E8 Icelandic Thorn */ add_dos_char(0347,0350);
+
add_dos_char(156,0); /* Pound */
- add_dos_char(183,0); /* A ` (WIN)*/
- add_dos_char(157,0); /* Phi (WIN)*/
- add_dos_char(212,0); /* E` (WIN)*/
+#endif
}
-
/*******************************************************************
add characters depending on a string passed by the user
********************************************************************/
diff --git a/source3/lib/interface.c b/source3/lib/interface.c
index 59a542ca0e..1c41293cf7 100644
--- a/source3/lib/interface.c
+++ b/source3/lib/interface.c
@@ -154,7 +154,7 @@ static void get_broadcast(struct in_addr *if_ipaddr,
}
}
}
-#elif defined(__FreeBSD__) || defined(NETBSD)
+#elif defined(__FreeBSD__) || defined(NETBSD) || defined(AMIGA)
ifc.ifc_len = sizeof(buff);
ifc.ifc_buf = buff;
if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
diff --git a/source3/lib/system.c b/source3/lib/system.c
index 995c6beed5..ac97449da2 100644
--- a/source3/lib/system.c
+++ b/source3/lib/system.c
@@ -198,14 +198,10 @@ for rename()
********************************************************************/
int sys_rename(char *from, char *to)
{
-#ifdef KANJI
pstring zfrom, zto;
strcpy (zfrom, dos_to_unix (from, False));
strcpy (zto, dos_to_unix (to, False));
return rename (zfrom, zto);
-#else
- return rename (from, to);
-#endif /* KANJI */
}
/*******************************************************************
diff --git a/source3/lib/util.c b/source3/lib/util.c
index 8730ae3143..318ac3fc61 100644
--- a/source3/lib/util.c
+++ b/source3/lib/util.c
@@ -957,9 +957,6 @@ void unix_format(char *fname)
{
pstring namecopy;
string_replace(fname,'\\','/');
-#ifndef KANJI
- dos2unix_format(fname, True);
-#endif /* KANJI */
if (*fname == '/')
{
@@ -974,9 +971,6 @@ void unix_format(char *fname)
****************************************************************************/
void dos_format(char *fname)
{
-#ifndef KANJI
- unix2dos_format(fname, True);
-#endif /* KANJI */
string_replace(fname,'/','\\');
}
@@ -3083,6 +3077,7 @@ char *client_name(void)
if (getpeername(Client, &sa, &length) < 0) {
DEBUG(0,("getpeername failed\n"));
+ done = False;
return name_buf;
}
@@ -3092,6 +3087,7 @@ char *client_name(void)
AF_INET)) == 0) {
DEBUG(1,("Gethostbyaddr failed for %s\n",client_addr()));
StrnCpy(name_buf,client_addr(),sizeof(name_buf) - 1);
+ done = False;
} else {
StrnCpy(name_buf,(char *)hp->h_name,sizeof(name_buf) - 1);
if (!matchname(name_buf, sockin->sin_addr)) {
@@ -3362,14 +3358,12 @@ char *readdirname(void *p)
dname = ptr->d_name;
-#ifdef KANJI
{
static pstring buf;
strcpy(buf, dname);
unix_to_dos(buf, True);
dname = buf;
}
-#endif
#ifdef NEXT2
if (telldir(p) < 0) return(NULL);
diff --git a/source3/locking/locking.c b/source3/locking/locking.c
index dde9477e23..d94cc9c35f 100644
--- a/source3/locking/locking.c
+++ b/source3/locking/locking.c
@@ -45,7 +45,7 @@ BOOL is_locked(int fnum,int cnum,uint32 count,uint32 offset)
if (!lp_locking(snum) || !lp_strict_locking(snum))
return(False);
- return(fcntl_lock(Files[fnum].fd,F_GETLK,offset,count,
+ return(fcntl_lock(Files[fnum].fd_ptr->fd,F_GETLK,offset,count,
(Files[fnum].can_write?F_WRLCK:F_RDLCK)));
}
@@ -67,7 +67,7 @@ BOOL do_lock(int fnum,int cnum,uint32 count,uint32 offset,int *eclass,uint32 *ec
}
if (Files[fnum].can_lock && OPEN_FNUM(fnum) && (Files[fnum].cnum == cnum))
- ok = fcntl_lock(Files[fnum].fd,F_SETLK,offset,count,
+ ok = fcntl_lock(Files[fnum].fd_ptr->fd,F_SETLK,offset,count,
(Files[fnum].can_write?F_WRLCK:F_RDLCK));
if (!ok) {
@@ -90,7 +90,7 @@ BOOL do_unlock(int fnum,int cnum,uint32 count,uint32 offset,int *eclass,uint32 *
return(True);
if (Files[fnum].can_lock && OPEN_FNUM(fnum) && (Files[fnum].cnum == cnum))
- ok = fcntl_lock(Files[fnum].fd,F_SETLK,offset,count,F_UNLCK);
+ ok = fcntl_lock(Files[fnum].fd_ptr->fd,F_SETLK,offset,count,F_UNLCK);
if (!ok) {
*eclass = ERRDOS;
@@ -151,7 +151,7 @@ static BOOL share_name(int cnum,struct stat *st,char *name)
static BOOL share_name_fnum(int fnum,char *name)
{
struct stat st;
- if (fstat(Files[fnum].fd,&st) != 0) return(False);
+ if (fstat(Files[fnum].fd_ptr->fd,&st) != 0) return(False);
return(share_name(Files[fnum].cnum,&st,name));
}
@@ -163,7 +163,7 @@ static BOOL share_name_fnum(int fnum,char *name)
int get_share_mode_by_fnum(int cnum,int fnum,int *pid)
{
struct stat sbuf;
- if (fstat(Files[fnum].fd,&sbuf) == -1) return(0);
+ if (fstat(Files[fnum].fd_ptr->fd,&sbuf) == -1) return(0);
return(get_share_mode(cnum,&sbuf,pid));
}
@@ -257,9 +257,9 @@ int get_share_mode(int cnum,struct stat *sbuf,int *pid)
#else
pstring fname;
int fd2;
- char buf[16];
+ char buf[20];
int ret;
- time_t t;
+ struct timeval t;
*pid = 0;
@@ -268,18 +268,20 @@ int get_share_mode(int cnum,struct stat *sbuf,int *pid)
fd2 = open(fname,O_RDONLY,0);
if (fd2 < 0) return(0);
- if (read(fd2,buf,16) != 16) {
+ if (read(fd2,buf,20) != 20) {
+ DEBUG(2,("Failed to read share file %s\n",fname));
close(fd2);
unlink(fname);
return(0);
}
close(fd2);
- t = IVAL(buf,0);
- ret = IVAL(buf,4);
- *pid = IVAL(buf,8);
+ t.tv_sec = IVAL(buf,4);
+ t.tv_usec = IVAL(buf,8);
+ ret = IVAL(buf,12);
+ *pid = IVAL(buf,16);
- if (IVAL(buf,12) != LOCKING_VERSION) {
+ if (IVAL(buf,0) != LOCKING_VERSION) {
if (!unlink(fname)) DEBUG(2,("Deleted old locking file %s",fname));
*pid = 0;
return(0);
@@ -368,29 +370,31 @@ void del_share_mode(int fnum)
#else
pstring fname;
int fd2;
- char buf[16];
- time_t t=0;
+ char buf[20];
+ struct timeval t;
int pid=0;
BOOL del = False;
+ t.tv_sec = t.tv_usec = 0;
if (!share_name_fnum(fnum,fname)) return;
fd2 = open(fname,O_RDONLY,0);
if (fd2 < 0) return;
- if (read(fd2,buf,16) != 16)
+ if (read(fd2,buf,20) != 20)
del = True;
close(fd2);
if (!del) {
- t = IVAL(buf,0);
- pid = IVAL(buf,8);
+ t.tv_sec = IVAL(buf,4);
+ t.tv_usec = IVAL(buf,8);
+ pid = IVAL(buf,16);
}
if (!del)
- if (IVAL(buf,12) != LOCKING_VERSION || !pid || !process_exists(pid))
+ if (IVAL(buf,0) != LOCKING_VERSION || !pid || !process_exists(pid))
del = True;
- if (!del && t == Files[fnum].open_time && pid==(int)getpid())
+ if (!del && (memcmp(&t,&Files[fnum].open_time,sizeof(t)) == 0) && (pid==(int)getpid()))
del = True;
if (del) {
@@ -443,7 +447,7 @@ BOOL set_share_mode(int fnum,int mode)
#else
pstring fname;
int fd2;
- char buf[16];
+ char buf[20];
int pid = (int)getpid();
if (!share_name_fnum(fnum,fname)) return(False);
@@ -458,12 +462,14 @@ BOOL set_share_mode(int fnum,int mode)
return(False);
}
- SIVAL(buf,0,Files[fnum].open_time);
- SIVAL(buf,4,mode);
- SIVAL(buf,8,pid);
- SIVAL(buf,12,LOCKING_VERSION);
+ SIVAL(buf,0,LOCKING_VERSION);
+ SIVAL(buf,4,Files[fnum].open_time.tv_sec);
+ SIVAL(buf,8,Files[fnum].open_time.tv_usec);
+ SIVAL(buf,12,mode);
+ SIVAL(buf,16,pid);
- if (write(fd2,buf,16) != 16) {
+ if (write(fd2,buf,20) != 20) {
+ DEBUG(2,("Failed to write share file %s\n",fname));
close(fd2);
unlink(fname);
return(False);
@@ -538,7 +544,7 @@ void clean_share_modes(void)
if (!dir) return;
while ((s=readdirname(dir))) {
- char buf[16];
+ char buf[20];
int pid;
int fd;
pstring lname;
@@ -554,7 +560,7 @@ void clean_share_modes(void)
fd = open(lname,O_RDONLY,0);
if (fd < 0) continue;
- if (read(fd,buf,16) != 16) {
+ if (read(fd,buf,20) != 20) {
close(fd);
if (!unlink(lname))
printf("Deleted corrupt share file %s\n",s);
@@ -562,9 +568,9 @@ void clean_share_modes(void)
}
close(fd);
- pid = IVAL(buf,8);
+ pid = IVAL(buf,16);
- if (IVAL(buf,12) != LOCKING_VERSION || !process_exists(pid)) {
+ if (IVAL(buf,0) != LOCKING_VERSION || !process_exists(pid)) {
if (!unlink(lname))
printf("Deleted stale share file %s\n",s);
}
diff --git a/source3/smbd/pipes.c b/source3/smbd/pipes.c
index ffa46083c3..cb498a6195 100644
--- a/source3/smbd/pipes.c
+++ b/source3/smbd/pipes.c
@@ -131,7 +131,7 @@ int reply_open_pipe_and_X(char *inbuf,char *outbuf,int length,int bufsize)
if (!Files[fnum].open)
return(UNIXERROR(ERRDOS,ERRnoaccess));
- if (fstat(Files[fnum].fd,&sbuf) != 0) {
+ if (fstat(Files[fnum].fd_ptr->fd,&sbuf) != 0) {
close_file(fnum);
return(ERROR(ERRDOS,ERRnoaccess));
}
diff --git a/source3/smbd/reply.c b/source3/smbd/reply.c
index 4ba438f190..812618b2c7 100644
--- a/source3/smbd/reply.c
+++ b/source3/smbd/reply.c
@@ -982,7 +982,7 @@ int reply_open(char *inbuf,char *outbuf)
if (!Files[fnum].open)
return(UNIXERROR(ERRDOS,ERRnoaccess));
- if (fstat(Files[fnum].fd,&sbuf) != 0) {
+ if (fstat(Files[fnum].fd_ptr->fd,&sbuf) != 0) {
close_file(fnum);
return(ERROR(ERRDOS,ERRnoaccess));
}
@@ -1065,7 +1065,7 @@ int reply_open_and_X(char *inbuf,char *outbuf,int length,int bufsize)
if (!Files[fnum].open)
return(UNIXERROR(ERRDOS,ERRnoaccess));
- if (fstat(Files[fnum].fd,&sbuf) != 0) {
+ if (fstat(Files[fnum].fd_ptr->fd,&sbuf) != 0) {
close_file(fnum);
return(ERROR(ERRDOS,ERRnoaccess));
}
@@ -1164,7 +1164,7 @@ int reply_mknew(char *inbuf,char *outbuf)
if (!check_name(fname,cnum))
return(UNIXERROR(ERRDOS,ERRnoaccess));
- open_file(fnum,cnum,fname,O_RDWR | O_CREAT | O_TRUNC,unixmode);
+ open_file(fnum,cnum,fname,O_RDWR | O_CREAT | O_TRUNC,unixmode, 0);
if (!Files[fnum].open)
return(UNIXERROR(ERRDOS,ERRnoaccess));
@@ -1177,7 +1177,7 @@ int reply_mknew(char *inbuf,char *outbuf)
}
DEBUG(2,("new file %s\n",fname));
- DEBUG(3,("%s mknew %s fd=%d fnum=%d cnum=%d dmode=%d umode=%o\n",timestring(),fname,Files[fnum].fd,fnum,cnum,createmode,unixmode));
+ DEBUG(3,("%s mknew %s fd=%d fnum=%d cnum=%d dmode=%d umode=%o\n",timestring(),fname,Files[fnum].fd_ptr->fd,fnum,cnum,createmode,unixmode));
return(outsize);
}
@@ -1212,7 +1212,7 @@ int reply_ctemp(char *inbuf,char *outbuf)
strcpy(fname2,(char *)mktemp(fname));
- open_file(fnum,cnum,fname2,O_RDWR | O_CREAT | O_TRUNC,unixmode);
+ open_file(fnum,cnum,fname2,O_RDWR | O_CREAT | O_TRUNC,unixmode, 0);
if (!Files[fnum].open)
return(UNIXERROR(ERRDOS,ERRnoaccess));
@@ -1227,7 +1227,7 @@ int reply_ctemp(char *inbuf,char *outbuf)
}
DEBUG(2,("created temp file %s\n",fname2));
- DEBUG(3,("%s ctemp %s fd=%d fnum=%d cnum=%d dmode=%d umode=%o\n",timestring(),fname2,Files[fnum].fd,fnum,cnum,createmode,unixmode));
+ DEBUG(3,("%s ctemp %s fd=%d fnum=%d cnum=%d dmode=%d umode=%o\n",timestring(),fname2,Files[fnum].fd_ptr->fd,fnum,cnum,createmode,unixmode));
return(outsize);
}
@@ -1385,7 +1385,7 @@ int reply_readbraw(char *inbuf, char *outbuf)
}
else
{
- fd = Files[fnum].fd;
+ fd = Files[fnum].fd_ptr->fd;
fname = Files[fnum].name;
}
@@ -1397,7 +1397,7 @@ int reply_readbraw(char *inbuf, char *outbuf)
if (size < sizeneeded) {
struct stat st;
- if (fstat(Files[fnum].fd,&st) == 0)
+ if (fstat(Files[fnum].fd_ptr->fd,&st) == 0)
size = st.st_size;
if (!Files[fnum].can_write)
Files[fnum].size = size;
@@ -1661,7 +1661,7 @@ int reply_writebraw(char *inbuf,char *outbuf)
tcount,nwritten,numtowrite));
}
- nwritten = transfer_file(Client,Files[fnum].fd,numtowrite,NULL,0,
+ nwritten = transfer_file(Client,Files[fnum].fd_ptr->fd,numtowrite,NULL,0,
startpos+nwritten);
total_written += nwritten;
@@ -1781,7 +1781,7 @@ int reply_write(char *inbuf,char *outbuf,int dum1,int dum2)
zero then the file size should be extended or
truncated to the size given in smb_vwv[2-3] */
if(numtowrite == 0)
- nwritten = set_filelen(Files[fnum].fd, startpos);
+ nwritten = set_filelen(Files[fnum].fd_ptr->fd, startpos);
else
nwritten = write_file(fnum,data,numtowrite);
@@ -1894,7 +1894,7 @@ int reply_lseek(char *inbuf,char *outbuf)
umode = SEEK_SET; break;
}
- res = lseek(Files[fnum].fd,startpos,umode);
+ res = lseek(Files[fnum].fd_ptr->fd,startpos,umode);
Files[fnum].pos = res;
outsize = set_message(outbuf,2,0,True);
@@ -1983,7 +1983,7 @@ int reply_close(char *inbuf,char *outbuf)
return(ERROR(eclass,err));
DEBUG(3,("%s close fd=%d fnum=%d cnum=%d (numopen=%d)\n",
- timestring(),Files[fnum].fd,fnum,cnum,
+ timestring(),Files[fnum].fd_ptr->fd,fnum,cnum,
Connections[cnum].num_files_open));
return(outsize);
@@ -2059,7 +2059,7 @@ int reply_lock(char *inbuf,char *outbuf)
count = IVAL(inbuf,smb_vwv1);
offset = IVAL(inbuf,smb_vwv3);
- DEBUG(3,("%s lock fd=%d fnum=%d cnum=%d ofs=%d cnt=%d\n",timestring(),Files[fnum].fd,fnum,cnum,offset,count));
+ DEBUG(3,("%s lock fd=%d fnum=%d cnum=%d ofs=%d cnt=%d\n",timestring(),Files[fnum].fd_ptr->fd,fnum,cnum,offset,count));
if(!do_lock( fnum, cnum, count, offset, &eclass, &ecode))
return (ERROR(eclass,ecode));
@@ -2091,7 +2091,7 @@ int reply_unlock(char *inbuf,char *outbuf)
if(!do_unlock(fnum, cnum, count, offset, &eclass, &ecode))
return (ERROR(eclass,ecode));
- DEBUG(3,("%s unlock fd=%d fnum=%d cnum=%d ofs=%d cnt=%d\n",timestring(),Files[fnum].fd,fnum,cnum,offset,count));
+ DEBUG(3,("%s unlock fd=%d fnum=%d cnum=%d ofs=%d cnt=%d\n",timestring(),Files[fnum].fd_ptr->fd,fnum,cnum,offset,count));
return(outsize);
}
@@ -2214,7 +2214,7 @@ int reply_printopen(char *inbuf,char *outbuf)
return(ERROR(ERRDOS,ERRnoaccess));
open_file(fnum,cnum,fname2,O_WRONLY | O_CREAT | O_TRUNC,
- unix_mode(cnum,0));
+ unix_mode(cnum,0), 0);
if (!Files[fnum].open)
return(UNIXERROR(ERRDOS,ERRnoaccess));
@@ -2225,7 +2225,7 @@ int reply_printopen(char *inbuf,char *outbuf)
outsize = set_message(outbuf,1,0,True);
SSVAL(outbuf,smb_vwv0,fnum);
- DEBUG(3,("%s openprint %s fd=%d fnum=%d cnum=%d\n",timestring(),fname2,Files[fnum].fd,fnum,cnum));
+ DEBUG(3,("%s openprint %s fd=%d fnum=%d cnum=%d\n",timestring(),fname2,Files[fnum].fd_ptr->fd,fnum,cnum));
return(outsize);
}
@@ -2250,7 +2250,7 @@ int reply_printclose(char *inbuf,char *outbuf)
close_file(fnum);
- DEBUG(3,("%s printclose fd=%d fnum=%d cnum=%d\n",timestring(),Files[fnum].fd,fnum,cnum));
+ DEBUG(3,("%s printclose fd=%d fnum=%d cnum=%d\n",timestring(),Files[fnum].fd_ptr->fd,fnum,cnum));
return(outsize);
}
@@ -2678,11 +2678,11 @@ static BOOL copy_file(char *src,char *dest1,int cnum,int ofun,
}
if ((ofun&3) == 1) {
- lseek(Files[fnum2].fd,0,SEEK_END);
+ lseek(Files[fnum2].fd_ptr->fd,0,SEEK_END);
}
if (st.st_size)
- ret = transfer_file(Files[fnum1].fd,Files[fnum2].fd,st.st_size,NULL,0,0);
+ ret = transfer_file(Files[fnum1].fd_ptr->fd,Files[fnum2].fd_ptr->fd,st.st_size,NULL,0,0);
close_file(fnum1);
close_file(fnum2);
@@ -3220,7 +3220,7 @@ int reply_getattrE(char *inbuf,char *outbuf)
CHECK_ERROR(fnum);
/* Do an fstat on this file */
- if(fstat(Files[fnum].fd, &sbuf))
+ if(fstat(Files[fnum].fd_ptr->fd, &sbuf))
return(UNIXERROR(ERRDOS,ERRnoaccess));
mode = dos_mode(cnum,Files[fnum].name,&sbuf);
diff --git a/source3/smbd/server.c b/source3/smbd/server.c
index 29360c40b3..75e7279e6d 100644
--- a/source3/smbd/server.c
+++ b/source3/smbd/server.c
@@ -58,6 +58,13 @@ extern pstring user_socket_options;
connection_struct Connections[MAX_CONNECTIONS];
files_struct Files[MAX_OPEN_FILES];
+/*
+ * Indirection for file fd's. Needed as POSIX locking
+ * is based on file/process, not fd/process.
+ */
+file_fd_struct FileFd[MAX_OPEN_FILES];
+int max_file_fd_used = 0;
+
extern int Protocol;
int maxxmit = BUFFER_SIZE;
@@ -731,17 +738,182 @@ static void check_for_pipe(char *fname)
}
}
+/****************************************************************************
+fd support routines - attempt to do a sys_open
+****************************************************************************/
+
+int fd_attempt_open(char *fname, int flags, int mode)
+{
+ int fd = sys_open(fname,flags,mode);
+
+ /* Fix for files ending in '.' */
+ if((fd == -1) && (errno == ENOENT) &&
+ (strchr(fname,'.')==NULL))
+ {
+ strcat(fname,".");
+ fd = sys_open(fname,flags,mode);
+ }
+
+#if (defined(ENAMETOOLONG) && defined(HAVE_PATHCONF))
+ if ((fd == -1) && (errno == ENAMETOOLONG))
+ {
+ int max_len;
+ char *p = strrchr(fname, '/');
+
+ if (p == fname) /* name is "/xxx" */
+ {
+ max_len = pathconf("/", _PC_NAME_MAX);
+ p++;
+ }
+ else if ((p == NULL) || (p == fname))
+ {
+ p = fname;
+ max_len = pathconf(".", _PC_NAME_MAX);
+ }
+ else
+ {
+ *p = '\0';
+ max_len = pathconf(fname, _PC_NAME_MAX);
+ *p = '/';
+ p++;
+ }
+ if (strlen(p) > max_len)
+ {
+ char tmp = p[max_len];
+
+ p[max_len] = '\0';
+ if ((fd = sys_open(fname,flags,mode)) == -1)
+ p[max_len] = tmp;
+ }
+ }
+#endif
+ return fd;
+}
+
+/****************************************************************************
+fd support routines - attempt to find an already open file by dev
+and inode - increments the ref_count of the returned file_fd_struct *.
+****************************************************************************/
+file_fd_struct *fd_get_already_open(struct stat *sbuf)
+{
+ int i;
+ file_fd_struct *fd_ptr;
+
+ if(sbuf == 0)
+ return 0;
+
+ for(i = 0; i <= max_file_fd_used; i++) {
+ fd_ptr = &FileFd[i];
+ if((fd_ptr->ref_count > 0) &&
+ (((int32)sbuf->st_dev) == fd_ptr->dev) &&
+ (((int32)sbuf->st_ino) == fd_ptr->inode)) {
+ fd_ptr->ref_count++;
+ DEBUG(3,
+ ("Re-used file_fd_struct %d, dev = %x, inode = %x, ref_count = %d\n",
+ i, fd_ptr->dev, fd_ptr->inode, fd_ptr->ref_count));
+ return fd_ptr;
+ }
+ }
+ return 0;
+}
+
+/****************************************************************************
+fd support routines - attempt to find a empty slot in the FileFd array.
+Increments the ref_count of the returned entry.
+****************************************************************************/
+file_fd_struct *fd_get_new()
+{
+ int i;
+ file_fd_struct *fd_ptr;
+
+ for(i = 0; i < MAX_OPEN_FILES; i++) {
+ fd_ptr = &FileFd[i];
+ if(fd_ptr->ref_count == 0) {
+ fd_ptr->dev = (int32)-1;
+ fd_ptr->inode = (int32)-1;
+ fd_ptr->fd = -1;
+ fd_ptr->fd_readonly = -1;
+ fd_ptr->fd_writeonly = -1;
+ fd_ptr->real_open_flags = -1;
+ fd_ptr->ref_count++;
+ /* Increment max used counter if neccessary, cuts down
+ on search time when re-using */
+ if(i > max_file_fd_used)
+ max_file_fd_used = i;
+ DEBUG(3,("Allocated new file_fd_struct %d, dev = %x, inode = %x\n",
+ i, fd_ptr->dev, fd_ptr->inode));
+ return fd_ptr;
+ }
+ }
+ DEBUG(1,("ERROR! Out of file_fd structures - perhaps increase MAX_OPEN_FILES?\
+n"));
+ return 0;
+}
+
+/****************************************************************************
+fd support routines - attempt to re-open an already open fd as O_RDWR.
+Save the already open fd (we cannot close due to POSIX file locking braindamage.
+****************************************************************************/
+
+void fd_attempt_reopen(char *fname, int mode, file_fd_struct *fd_ptr)
+{
+ int fd = sys_open( fname, O_RDWR, mode);
+
+ if(fd == -1)
+ return;
+
+ if(fd_ptr->real_open_flags == O_RDONLY)
+ fd_ptr->fd_readonly = fd_ptr->fd;
+ if(fd_ptr->real_open_flags == O_WRONLY)
+ fd_ptr->fd_writeonly = fd_ptr->fd;
+
+ fd_ptr->fd = fd;
+ fd_ptr->real_open_flags = O_RDWR;
+}
+
+/****************************************************************************
+fd support routines - attempt to close the file referenced by this fd.
+Decrements the ref_count and returns it.
+****************************************************************************/
+int fd_attempt_close(file_fd_struct *fd_ptr)
+{
+ DEBUG(3,("fd_attempt_close on file_fd_struct %d, fd = %d, dev = %x, inode = %x, open_flags = %d, ref_count = %d.\n",
+ fd_ptr - &FileFd[0],
+ fd_ptr->fd, fd_ptr->dev, fd_ptr->inode,
+ fd_ptr->real_open_flags,
+ fd_ptr->ref_count));
+ if(fd_ptr->ref_count > 0) {
+ fd_ptr->ref_count--;
+ if(fd_ptr->ref_count == 0) {
+ if(fd_ptr->fd != -1)
+ close(fd_ptr->fd);
+ if(fd_ptr->fd_readonly != -1)
+ close(fd_ptr->fd_readonly);
+ if(fd_ptr->fd_writeonly != -1)
+ close(fd_ptr->fd_writeonly);
+ fd_ptr->fd = -1;
+ fd_ptr->fd_readonly = -1;
+ fd_ptr->fd_writeonly = -1;
+ fd_ptr->real_open_flags = -1;
+ fd_ptr->dev = -1;
+ fd_ptr->inode = -1;
+ }
+ }
+ return fd_ptr->ref_count;
+}
/****************************************************************************
open a file
****************************************************************************/
-void open_file(int fnum,int cnum,char *fname1,int flags,int mode)
+void open_file(int fnum,int cnum,char *fname1,int flags,int mode, struct stat *sbuf)
{
extern struct current_user current_user;
pstring fname;
+ struct stat statbuf;
+ file_fd_struct *fd_ptr;
Files[fnum].open = False;
- Files[fnum].fd = -1;
+ Files[fnum].fd_ptr = 0;
errno = EPERM;
strcpy(fname,fname1);
@@ -771,10 +943,104 @@ void open_file(int fnum,int cnum,char *fname1,int flags,int mode)
sys_unlink(fname);
#endif
+ /*
+ * Ensure we have a valid struct stat so we can search the
+ * open fd table.
+ */
+ if(sbuf == 0) {
+ if(stat(fname, &statbuf) < 0) {
+ if(errno != ENOENT) {
+ DEBUG(3,("Error doing stat on file %s (%s)\n",
+ fname,strerror(errno)));
+
+ check_for_pipe(fname);
+ return;
+ }
+ sbuf = 0;
+ } else {
+ sbuf = &statbuf;
+ }
+ }
- Files[fnum].fd = sys_open(fname,flags,mode);
+ /*
+ * Check to see if we have this file already
+ * open. If we do, just use the already open fd and increment the
+ * reference count (fd_get_already_open increments the ref_count).
+ */
+ if((fd_ptr = fd_get_already_open(sbuf))!= 0) {
+
+ int accmode = (flags & (O_RDONLY | O_WRONLY | O_RDWR));
+
+ /* File was already open. */
+ if((flags & O_CREAT) && (flags & O_EXCL)) {
+ fd_ptr->ref_count--;
+ errno = EEXIST;
+ return;
+ }
+
+ /*
+ * If not opened O_RDWR try
+ * and do that here - a chmod may have been done
+ * between the last open and now.
+ */
+ if(fd_ptr->real_open_flags != O_RDWR)
+ fd_attempt_reopen(fname, mode, fd_ptr);
+
+ /*
+ * Ensure that if we wanted write access
+ * it has been opened for write, and if we wanted read it
+ * was open for read.
+ */
+ if(((accmode == O_WRONLY) && (fd_ptr->real_open_flags == O_RDONLY)) ||
+ ((accmode == O_RDONLY) && (fd_ptr->real_open_flags == O_WRONLY)) ||
+ ((accmode == O_RDWR) && (fd_ptr->real_open_flags != O_RDWR))) {
+ DEBUG(3,("Error opening (already open for flags=%d) file %s (%s) (flags=%d)\n",
+ fd_ptr->real_open_flags, fname,strerror(EACCES),flags));
+ check_for_pipe(fname);
+ fd_ptr->ref_count--;
+ return;
+ }
+
+ /*
+ * If O_TRUNC was set, ensure we truncate the file.
+ * open_file_shared explicitly clears this flag before
+ * calling open_file, so we can safely do this here.
+ */
+ if(flags & O_TRUNC)
+ ftruncate(fd_ptr->fd, 0);
+
+ } else {
+ int open_flags;
+ /* We need to allocate a new file_fd_struct (this increments the
+ ref_count). */
+ if((fd_ptr = fd_get_new()) == 0)
+ return;
+ /*
+ * Whatever the requested flags, attempt read/write access,
+ * as we don't know what flags future file opens may require.
+ * If this fails, try again with the required flags.
+ * Even if we open read/write when only read access was
+ * requested the setting of the can_write flag in
+ * the file_struct will protect us from errant
+ * write requests. We never need to worry about O_APPEND
+ * as this is not set anywhere in Samba.
+ */
+ fd_ptr->real_open_flags = O_RDWR;
+ /* Set the flags as needed without the read/write modes. */
+ open_flags = flags & ~(O_RDWR|O_WRONLY|O_RDONLY);
+ fd_ptr->fd = fd_attempt_open(fname, open_flags|O_RDWR, mode);
+ if((fd_ptr->fd == -1) && (errno == EACCES)) {
+ if(flags & O_WRONLY) {
+ fd_ptr->fd = fd_attempt_open(fname, open_flags|O_WRONLY, mode);
+ fd_ptr->real_open_flags = O_WRONLY;
+ } else {
+ fd_ptr->fd = fd_attempt_open(fname, open_flags|O_RDONLY, mode);
+ fd_ptr->real_open_flags = O_RDONLY;
+ }
+ }
+ }
- if ((Files[fnum].fd>=0) &&
+ if ((fd_ptr->fd >=0) &&
Connections[cnum].printer && lp_minprintspace(SNUM(cnum))) {
pstring dname;
int dum1,dum2,dum3;
@@ -784,72 +1050,47 @@ void open_file(int fnum,int cnum,char *fname1,int flags,int mode)
if (p) *p = 0;
if (sys_disk_free(dname,&dum1,&dum2,&dum3) <
lp_minprintspace(SNUM(cnum))) {
- close(Files[fnum].fd);
- Files[fnum].fd = -1;
- sys_unlink(fname);
+ fd_attempt_close(fd_ptr);
+ Files[fnum].fd_ptr = 0;
+ if(fd_ptr->ref_count == 0)
+ sys_unlink(fname);
errno = ENOSPC;
return;
}
}
-
- /* Fix for files ending in '.' */
- if((Files[fnum].fd == -1) && (errno == ENOENT) &&
- (strchr(fname,'.')==NULL))
- {
- strcat(fname,".");
- Files[fnum].fd = sys_open(fname,flags,mode);
- }
-
-#if (defined(ENAMETOOLONG) && defined(HAVE_PATHCONF))
- if ((Files[fnum].fd == -1) && (errno == ENAMETOOLONG))
- {
- int max_len;
- char *p = strrchr(fname, '/');
-
- if (p == fname) /* name is "/xxx" */
- {
- max_len = pathconf("/", _PC_NAME_MAX);
- p++;
- }
- else if ((p == NULL) || (p == fname))
- {
- p = fname;
- max_len = pathconf(".", _PC_NAME_MAX);
- }
- else
- {
- *p = '\0';
- max_len = pathconf(fname, _PC_NAME_MAX);
- *p = '/';
- p++;
- }
- if (strlen(p) > max_len)
- {
- char tmp = p[max_len];
-
- p[max_len] = '\0';
- if ((Files[fnum].fd = sys_open(fname,flags,mode)) == -1)
- p[max_len] = tmp;
- }
- }
-#endif
-
- if (Files[fnum].fd < 0)
+ if (fd_ptr->fd < 0)
{
DEBUG(3,("Error opening file %s (%s) (flags=%d)\n",
fname,strerror(errno),flags));
+ /* Ensure the ref_count is decremented. */
+ fd_attempt_close(fd_ptr);
check_for_pipe(fname);
return;
}
- if (Files[fnum].fd >= 0)
+ if (fd_ptr->fd >= 0)
{
- struct stat st;
+ if(sbuf == 0) {
+ /* Do the fstat */
+ if(fstat(fd_ptr->fd, &statbuf) == -1) {
+ /* Error - backout !! */
+ DEBUG(3,("Error doing fstat on fd %d, file %s (%s)\n",
+ fd_ptr->fd, fname,strerror(errno)));
+ /* Ensure the ref_count is decremented. */
+ fd_attempt_close(fd_ptr);
+ return;
+ }
+ sbuf = &statbuf;
+ }
+ /* Set the correct entries in fd_ptr. */
+ fd_ptr->dev = (int32)sbuf->st_dev;
+ fd_ptr->inode = (int32)sbuf->st_ino;
+
+ Files[fnum].fd_ptr = fd_ptr;
Connections[cnum].num_files_open++;
- fstat(Files[fnum].fd,&st);
- Files[fnum].mode = st.st_mode;
- Files[fnum].open_time = time(NULL);
+ Files[fnum].mode = sbuf->st_mode;
+ GetTimeOfDay(&Files[fnum].open_time);
Files[fnum].uid = current_user.id;
Files[fnum].size = 0;
Files[fnum].pos = -1;
@@ -911,7 +1152,7 @@ sync a file
void sync_file(int fnum)
{
#ifndef NO_FSYNC
- fsync(Files[fnum].fd);
+ fsync(Files[fnum].fd_ptr->fd);
#endif
}
@@ -961,7 +1202,7 @@ close a file - possibly invalidating the read prediction
void close_file(int fnum)
{
int cnum = Files[fnum].cnum;
- invalidate_read_prediction(Files[fnum].fd);
+ invalidate_read_prediction(Files[fnum].fd_ptr->fd);
Files[fnum].open = False;
Connections[cnum].num_files_open--;
if(Files[fnum].wbmpx_ptr)
@@ -981,7 +1222,7 @@ void close_file(int fnum)
if (lp_share_modes(SNUM(cnum)))
del_share_mode(fnum);
- close(Files[fnum].fd);
+ fd_attempt_close(Files[fnum].fd_ptr);
/* NT uses smbclose to start a print - weird */
if (Files[fnum].print_file)
@@ -1081,7 +1322,7 @@ static void truncate_unless_locked(int fnum, int cnum)
unix_ERR_code = ERRlock;
}
else
- ftruncate(Files[fnum].fd,0);
+ ftruncate(Files[fnum].fd_ptr->fd,0);
}
}
@@ -1101,7 +1342,7 @@ void open_file_shared(int fnum,int cnum,char *fname,int share_mode,int ofun,
int share_pid=0;
Files[fnum].open = False;
- Files[fnum].fd = -1;
+ Files[fnum].fd_ptr = 0;
/* this is for OS/2 EAs - try and say we don't support them */
if (strstr(fname,".+,;=[].")) {
@@ -1208,10 +1449,10 @@ void open_file_shared(int fnum,int cnum,char *fname,int share_mode,int ofun,
DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o\n",
flags,flags2,mode));
- open_file(fnum,cnum,fname,flags|(flags2&~(O_TRUNC)),mode);
+ open_file(fnum,cnum,fname,flags|(flags2&~(O_TRUNC)),mode,&sbuf);
if (!Files[fnum].open && flags==O_RDWR && errno!=ENOENT && fcbopen) {
flags = O_RDONLY;
- open_file(fnum,cnum,fname,flags,mode);
+ open_file(fnum,cnum,fname,flags,mode,&sbuf);
}
if (Files[fnum].open) {
@@ -1282,7 +1523,7 @@ int seek_file(int fnum,int pos)
if (Files[fnum].print_file && POSTSCRIPT(Files[fnum].cnum))
offset = 3;
- Files[fnum].pos = lseek(Files[fnum].fd,pos+offset,SEEK_SET) - offset;
+ Files[fnum].pos = lseek(Files[fnum].fd_ptr->fd,pos+offset,SEEK_SET) - offset;
return(Files[fnum].pos);
}
@@ -1295,7 +1536,7 @@ int read_file(int fnum,char *data,int pos,int n)
if (!Files[fnum].can_write)
{
- ret = read_predict(Files[fnum].fd,pos,data,NULL,n);
+ ret = read_predict(Files[fnum].fd_ptr->fd,pos,data,NULL,n);
data += ret;
n -= ret;
@@ -1327,7 +1568,7 @@ int read_file(int fnum,char *data,int pos,int n)
}
if (n > 0) {
- readret = read(Files[fnum].fd,data,n);
+ readret = read(Files[fnum].fd_ptr->fd,data,n);
if (readret > 0) ret += readret;
}
@@ -1348,7 +1589,7 @@ int write_file(int fnum,char *data,int n)
if (!Files[fnum].modified) {
struct stat st;
Files[fnum].modified = True;
- if (fstat(Files[fnum].fd,&st) == 0) {
+ if (fstat(Files[fnum].fd_ptr->fd,&st) == 0) {
int dosmode = dos_mode(Files[fnum].cnum,Files[fnum].name,&st);
if (MAP_ARCHIVE(Files[fnum].cnum) && !IS_DOS_ARCHIVE(dosmode)) {
dos_chmod(Files[fnum].cnum,Files[fnum].name,dosmode | aARCH,&st);
@@ -1356,7 +1597,7 @@ int write_file(int fnum,char *data,int n)
}
}
- return(write_data(Files[fnum].fd,data,n));
+ return(write_data(Files[fnum].fd_ptr->fd,data,n));
}
@@ -2057,14 +2298,21 @@ int make_connection(char *service,char *user,char *password, int pwlen, char *de
#if HAVE_GETGRNAM
if (*lp_force_group(snum))
{
- struct group *gptr = (struct group *)getgrnam(lp_force_group(snum));
+ struct group *gptr;
+ pstring gname;
+
+ StrnCpy(gname,lp_force_group(snum),sizeof(pstring)-1);
+ /* default service may be a group name */
+ string_sub(gname,"%S",service);
+ gptr = (struct group *)getgrnam(gname);
+
if (gptr)
{
pcon->gid = gptr->gr_gid;
- DEBUG(3,("Forced group %s\n",lp_force_group(snum)));
+ DEBUG(3,("Forced group %s\n",gname));
}
else
- DEBUG(1,("Couldn't find group %s\n",lp_force_group(snum)));
+ DEBUG(1,("Couldn't find group %s\n",gname));
}
#endif
@@ -2936,6 +3184,11 @@ void exit_server(char *reason)
if (dump_core()) return;
#endif
}
+
+#if FAST_SHARE_MODES
+ stop_share_mode_mgmt();
+#endif
+
DEBUG(3,("%s Server exit (%s)\n",timestring(),reason?reason:""));
exit(0);
}
@@ -3574,6 +3827,19 @@ static void init_structs(void )
{
Files[i].open = False;
string_init(&Files[i].name,"");
+
+ }
+
+ for (i=0;i<MAX_OPEN_FILES;i++)
+ {
+ file_fd_struct *fd_ptr = &FileFd[i];
+ fd_ptr->ref_count = 0;
+ fd_ptr->dev = (int32)-1;
+ fd_ptr->inode = (int32)-1;
+ fd_ptr->fd = -1;
+ fd_ptr->fd_readonly = -1;
+ fd_ptr->fd_writeonly = -1;
+ fd_ptr->real_open_flags = -1;
}
init_dptrs();
@@ -3808,10 +4074,6 @@ static void usage(char *pname)
process();
close_sockets();
-#if FAST_SHARE_MODES
- stop_share_mode_mgmt();
-#endif
-
exit_server("normal exit");
return(0);
}
diff --git a/source3/smbd/trans2.c b/source3/smbd/trans2.c
index 3b4cdeb1c6..b2bee17eea 100644
--- a/source3/smbd/trans2.c
+++ b/source3/smbd/trans2.c
@@ -210,7 +210,7 @@ static int call_trans2open(char *inbuf, char *outbuf, int bufsize, int cnum,
if (!Files[fnum].open)
return(UNIXERROR(ERRDOS,ERRnoaccess));
- if (fstat(Files[fnum].fd,&sbuf) != 0) {
+ if (fstat(Files[fnum].fd_ptr->fd,&sbuf) != 0) {
close_file(fnum);
return(ERROR(ERRDOS,ERRnoaccess));
}
@@ -351,9 +351,6 @@ static int get_lanman2_dir_entry(int cnum,char *path_mask,int dirtype,int info_l
}
-#ifndef KANJI
- unix2dos_format(fname, True);
-#endif
p = pdata;
nameptr = p;
@@ -447,11 +444,7 @@ static int get_lanman2_dir_entry(int cnum,char *path_mask,int dirtype,int info_l
SIVAL(p,0,strlen(fname)); p += 4;
SIVAL(p,0,0); p += 4;
if (!was_8_3) {
-#ifndef KANJI
- strcpy(p+2,unix2dos_format(fname,False));
-#else
strcpy(p+2,fname);
-#endif
if (!name_map_mangle(p+2,True,SNUM(cnum)))
(p+2)[12] = 0;
} else
@@ -937,9 +930,12 @@ static int call_trans2qfsinfo(char *inbuf, char *outbuf, int length, int bufsize
strcpy(pdata+4,vname);
break;
case SMB_QUERY_FS_VOLUME_INFO:
- data_len = 17 + strlen(vname);
- SIVAL(pdata,12,strlen(vname));
- strcpy(pdata+17,vname);
+ data_len = 18 + 2*strlen(vname);
+ SIVAL(pdata,12,2*strlen(vname));
+ PutUniCode(pdata+18,vname);
+ DEBUG(5,("call_trans2qfsinfo : SMB_QUERY_FS_VOLUME_INFO namelen = %d, vol = %s\n", strlen(vname),
+ vname));
+ break;
break;
case SMB_QUERY_FS_SIZE_INFO:
{
@@ -1018,11 +1014,11 @@ static int call_trans2qfilepathinfo(char *inbuf, char *outbuf, int length,
CHECK_ERROR(fnum);
fname = Files[fnum].name;
- if (fstat(Files[fnum].fd,&sbuf) != 0) {
+ if (fstat(Files[fnum].fd_ptr->fd,&sbuf) != 0) {
DEBUG(3,("fstat of fnum %d failed (%s)\n",fnum, strerror(errno)));
return(UNIXERROR(ERRDOS,ERRbadfid));
}
- pos = lseek(Files[fnum].fd,0,SEEK_CUR);
+ pos = lseek(Files[fnum].fd_ptr->fd,0,SEEK_CUR);
} else {
/* qpathinfo */
info_level = SVAL(params,0);
@@ -1212,7 +1208,7 @@ static int call_trans2setfilepathinfo(char *inbuf, char *outbuf, int length,
CHECK_ERROR(fnum);
fname = Files[fnum].name;
- fd = Files[fnum].fd;
+ fd = Files[fnum].fd_ptr->fd;
if(fstat(fd,&st)!=0) {
DEBUG(3,("fstat of %s failed (%s)\n", fname, strerror(errno)));
diff --git a/source3/utils/status.c b/source3/utils/status.c
index ab61a4db89..69fbda5ea8 100644
--- a/source3/utils/status.c
+++ b/source3/utils/status.c
@@ -224,9 +224,9 @@ unsigned int Ucrit_IsActive = 0; /* added by OH */
dir = opendir(lp_lockdir());
if (!dir) return(0);
while ((s=readdirname(dir))) {
- char buf[16];
+ char buf[20];
int pid,mode;
- time_t t;
+ struct timeval t;
int fd;
pstring lname;
int dev,inode;
@@ -240,19 +240,20 @@ unsigned int Ucrit_IsActive = 0; /* added by OH */
fd = open(lname,O_RDONLY,0);
if (fd < 0) continue;
- if (read(fd,buf,16) != 16) continue;
+ if (read(fd,buf,20) != 20) continue;
n = read(fd,fname,sizeof(fname));
fname[MAX(n,0)]=0;
close(fd);
- t = IVAL(buf,0);
- mode = IVAL(buf,4);
- pid = IVAL(buf,8);
+ t.tv_sec = IVAL(buf,4);
+ t.tv_usec = IVAL(buf,8);
+ mode = IVAL(buf,12);
+ pid = IVAL(buf,16);
if ( !Ucrit_checkPid(pid) ) /* added by OH */
continue;
- if (IVAL(buf,12) != LOCKING_VERSION || !process_exists(pid)) {
+ if (IVAL(buf,0) != LOCKING_VERSION || !process_exists(pid)) {
if (unlink(lname)==0)
printf("Deleted stale share file %s\n",s);
continue;
@@ -284,7 +285,7 @@ unsigned int Ucrit_IsActive = 0; /* added by OH */
case 1: printf("WRONLY "); break;
case 2: printf("RDWR "); break;
}
- printf(" %s %s",fname,asctime(LocalTime(&t)));
+ printf(" %s %s",fname,asctime(LocalTime((time_t *)&t.tv_sec)));
#if FAST_SHARE_MODES
prev_p = scanner_p ;