configure: Changes for extra headers.
[kai/samba.git] / source3 / smbd / server.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Main SMB server routines
5    Copyright (C) Andrew Tridgell 1992-1998
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "trans2.h"
24
25 pstring servicesf = CONFIGFILE;
26 extern pstring debugf;
27 extern pstring sesssetup_user;
28 extern fstring global_myworkgroup;
29 extern pstring global_myname;
30
31 char *InBuffer = NULL;
32 char *OutBuffer = NULL;
33 char *last_inbuf = NULL;
34
35 int am_parent = 1;
36
37 /* the last message the was processed */
38 int last_message = -1;
39
40 /* a useful macro to debug the last message processed */
41 #define LAST_MESSAGE() smb_fn_name(last_message)
42
43 extern pstring scope;
44 extern int DEBUGLEVEL;
45 extern int case_default;
46 extern BOOL case_sensitive;
47 extern BOOL case_preserve;
48 extern BOOL use_mangled_map;
49 extern BOOL short_case_preserve;
50 extern BOOL case_mangle;
51 time_t smb_last_time=(time_t)0;
52 extern BOOL global_machine_pasword_needs_changing;
53
54 extern int smb_read_error;
55
56 extern pstring user_socket_options;
57
58 #ifdef WITH_DFS
59 extern int dcelogin_atmost_once;
60 #endif /* WITH_DFS */
61
62 /*
63  * This is set on startup - it defines the SID for this
64  * machine.
65  */
66 extern DOM_SID global_machine_sid;
67
68 static connection_struct Connections[MAX_CONNECTIONS];
69 files_struct Files[MAX_FNUMS];
70
71 /*
72  * Indirection for file fd's. Needed as POSIX locking
73  * is based on file/process, not fd/process.
74  */
75 file_fd_struct FileFd[MAX_OPEN_FILES];
76 int max_file_fd_used = 0;
77
78 extern int Protocol;
79
80 /* 
81  * Size of data we can send to client. Set
82  *  by the client for all protocols above CORE.
83  *  Set by us for CORE protocol.
84  */
85 int max_send = BUFFER_SIZE;
86 /*
87  * Size of the data we can receive. Set by us.
88  * Can be modified by the max xmit parameter.
89  */
90 int max_recv = BUFFER_SIZE;
91
92 /* a fnum to use when chaining */
93 int chain_fnum = -1;
94
95 /* number of open connections */
96 static int num_connections_open = 0;
97
98 /* Oplock ipc UDP socket. */
99 int oplock_sock = -1;
100 uint16 oplock_port = 0;
101 /* Current number of oplocks we have outstanding. */
102 int32 global_oplocks_open = 0;
103
104 BOOL global_oplock_break = False;
105
106 extern fstring remote_machine;
107
108 extern pstring OriginalDir;
109
110 /* these can be set by some functions to override the error codes */
111 int unix_ERR_class=SMB_SUCCESS;
112 int unix_ERR_code=0;
113
114
115 extern int extra_time_offset;
116
117 extern pstring myhostname;
118
119 /* for readability... */
120 #define IS_DOS_READONLY(test_mode) (((test_mode) & aRONLY) != 0)
121 #define IS_DOS_DIR(test_mode) (((test_mode) & aDIR) != 0)
122 #define IS_DOS_ARCHIVE(test_mode) (((test_mode) & aARCH) != 0)
123 #define IS_DOS_SYSTEM(test_mode) (((test_mode) & aSYSTEM) != 0)
124 #define IS_DOS_HIDDEN(test_mode) (((test_mode) & aHIDDEN) != 0)
125
126 /****************************************************************************
127   when exiting, take the whole family
128 ****************************************************************************/
129 void  *dflt_sig(void)
130 {
131   exit_server("caught signal");
132   return 0; /* Keep -Wall happy :-) */
133 }
134 /****************************************************************************
135   Send a SIGTERM to our process group.
136 *****************************************************************************/
137 void  killkids(void)
138 {
139   if(am_parent) kill(0,SIGTERM);
140 }
141
142 /****************************************************************************
143   change a dos mode to a unix mode
144     base permission for files:
145          everybody gets read bit set
146          dos readonly is represented in unix by removing everyone's write bit
147          dos archive is represented in unix by the user's execute bit
148          dos system is represented in unix by the group's execute bit
149          dos hidden is represented in unix by the other's execute bit
150          Then apply create mask,
151          then add force bits.
152     base permission for directories:
153          dos directory is represented in unix by unix's dir bit and the exec bit
154          Then apply create mask,
155          then add force bits.
156 ****************************************************************************/
157 mode_t unix_mode(connection_struct *conn,int dosmode)
158 {
159   mode_t result = (S_IRUSR | S_IRGRP | S_IROTH);
160
161   if ( !IS_DOS_READONLY(dosmode) )
162     result |= (S_IWUSR | S_IWGRP | S_IWOTH);
163  
164   if (IS_DOS_DIR(dosmode)) {
165     /* We never make directories read only for the owner as under DOS a user
166        can always create a file in a read-only directory. */
167     result |= (S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH | S_IWUSR);
168     /* Apply directory mask */
169     result &= lp_dir_mode(SNUM(conn));
170     /* Add in force bits */
171     result |= lp_force_dir_mode(SNUM(conn));
172   } else { 
173     if (lp_map_archive(SNUM(conn)) && IS_DOS_ARCHIVE(dosmode))
174       result |= S_IXUSR;
175
176     if (lp_map_system(SNUM(conn)) && IS_DOS_SYSTEM(dosmode))
177       result |= S_IXGRP;
178  
179     if (lp_map_hidden(SNUM(conn)) && IS_DOS_HIDDEN(dosmode))
180       result |= S_IXOTH;  
181  
182     /* Apply mode mask */
183     result &= lp_create_mode(SNUM(conn));
184     /* Add in force bits */
185     result |= lp_force_create_mode(SNUM(conn));
186   }
187   return(result);
188 }
189
190
191 /****************************************************************************
192   change a unix mode to a dos mode
193 ****************************************************************************/
194 int dos_mode(connection_struct *conn,char *path,struct stat *sbuf)
195 {
196   int result = 0;
197   extern struct current_user current_user;
198
199   DEBUG(8,("dos_mode: %s\n", path));
200
201   if (CAN_WRITE(conn) && !lp_alternate_permissions(SNUM(conn))) {
202     if (!((sbuf->st_mode & S_IWOTH) ||
203           conn->admin_user ||
204           ((sbuf->st_mode & S_IWUSR) && current_user.uid==sbuf->st_uid) ||
205           ((sbuf->st_mode & S_IWGRP) && 
206            in_group(sbuf->st_gid,current_user.gid,
207                     current_user.ngroups,current_user.groups))))
208       result |= aRONLY;
209   } else {
210     if ((sbuf->st_mode & S_IWUSR) == 0)
211       result |= aRONLY;
212   }
213
214   if (MAP_ARCHIVE(conn) && ((sbuf->st_mode & S_IXUSR) != 0))
215     result |= aARCH;
216
217   if (MAP_SYSTEM(conn) && ((sbuf->st_mode & S_IXGRP) != 0))
218     result |= aSYSTEM;
219
220   if (MAP_HIDDEN(conn) && ((sbuf->st_mode & S_IXOTH) != 0))
221     result |= aHIDDEN;   
222   
223   if (S_ISDIR(sbuf->st_mode))
224     result = aDIR | (result & aRONLY);
225
226 #ifdef S_ISLNK
227 #if LINKS_READ_ONLY
228   if (S_ISLNK(sbuf->st_mode) && S_ISDIR(sbuf->st_mode))
229     result |= aRONLY;
230 #endif
231 #endif
232
233   /* hide files with a name starting with a . */
234   if (lp_hide_dot_files(SNUM(conn)))
235     {
236       char *p = strrchr(path,'/');
237       if (p)
238         p++;
239       else
240         p = path;
241       
242       if (p[0] == '.' && p[1] != '.' && p[1] != 0)
243         result |= aHIDDEN;
244     }
245
246   /* Optimization : Only call is_hidden_path if it's not already
247      hidden. */
248   if (!(result & aHIDDEN) && IS_HIDDEN_PATH(conn,path))
249   {
250     result |= aHIDDEN;
251   }
252
253   DEBUG(8,("dos_mode returning "));
254
255   if (result & aHIDDEN) DEBUG(8, ("h"));
256   if (result & aRONLY ) DEBUG(8, ("r"));
257   if (result & aSYSTEM) DEBUG(8, ("s"));
258   if (result & aDIR   ) DEBUG(8, ("d"));
259   if (result & aARCH  ) DEBUG(8, ("a"));
260
261   DEBUG(8,("\n"));
262
263   return(result);
264 }
265
266 /*******************************************************************
267 chmod a file - but preserve some bits
268 ********************************************************************/
269 int dos_chmod(connection_struct *conn,char *fname,int dosmode,struct stat *st)
270 {
271   struct stat st1;
272   int mask=0;
273   int tmp;
274   int unixmode;
275
276   if (!st) {
277     st = &st1;
278     if (sys_stat(fname,st)) return(-1);
279   }
280
281   if (S_ISDIR(st->st_mode)) dosmode |= aDIR;
282
283   if (dos_mode(conn,fname,st) == dosmode) return(0);
284
285   unixmode = unix_mode(conn,dosmode);
286
287   /* preserve the s bits */
288   mask |= (S_ISUID | S_ISGID);
289
290   /* preserve the t bit */
291 #ifdef S_ISVTX
292   mask |= S_ISVTX;
293 #endif
294
295   /* possibly preserve the x bits */
296   if (!MAP_ARCHIVE(conn)) mask |= S_IXUSR;
297   if (!MAP_SYSTEM(conn)) mask |= S_IXGRP;
298   if (!MAP_HIDDEN(conn)) mask |= S_IXOTH;
299
300   unixmode |= (st->st_mode & mask);
301
302   /* if we previously had any r bits set then leave them alone */
303   if ((tmp = st->st_mode & (S_IRUSR|S_IRGRP|S_IROTH))) {
304     unixmode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
305     unixmode |= tmp;
306   }
307
308   /* if we previously had any w bits set then leave them alone 
309    if the new mode is not rdonly */
310   if (!IS_DOS_READONLY(dosmode) &&
311       (tmp = st->st_mode & (S_IWUSR|S_IWGRP|S_IWOTH))) {
312     unixmode &= ~(S_IWUSR|S_IWGRP|S_IWOTH);
313     unixmode |= tmp;
314   }
315
316   return(sys_chmod(fname,unixmode));
317 }
318
319 /*******************************************************************
320 Wrapper around sys_utime that possibly allows DOS semantics rather
321 than POSIX.
322 *******************************************************************/
323
324 int file_utime(connection_struct *conn, char *fname, struct utimbuf *times)
325 {
326   extern struct current_user current_user;
327   struct stat sb;
328   int ret = -1;
329
330   errno = 0;
331
332   if(sys_utime(fname, times) == 0)
333     return 0;
334
335   if((errno != EPERM) && (errno != EACCES))
336     return -1;
337
338   if(!lp_dos_filetimes(SNUM(conn)))
339     return -1;
340
341   /* We have permission (given by the Samba admin) to
342      break POSIX semantics and allow a user to change
343      the time on a file they don't own but can write to
344      (as DOS does).
345    */
346
347   if(sys_stat(fname,&sb) != 0)
348     return -1;
349
350   /* Check if we have write access. */
351   if (CAN_WRITE(conn)) {
352           if (((sb.st_mode & S_IWOTH) ||
353                conn->admin_user ||
354                ((sb.st_mode & S_IWUSR) && current_user.uid==sb.st_uid) ||
355                ((sb.st_mode & S_IWGRP) &&
356                 in_group(sb.st_gid,current_user.gid,
357                          current_user.ngroups,current_user.groups)))) {
358                   /* We are allowed to become root and change the filetime. */
359                   become_root(False);
360                   ret = sys_utime(fname, times);
361                   unbecome_root(False);
362           }
363   }
364
365   return ret;
366 }
367   
368 /*******************************************************************
369 Change a filetime - possibly allowing DOS semantics.
370 *******************************************************************/
371
372 BOOL set_filetime(connection_struct *conn, char *fname, time_t mtime)
373 {
374   struct utimbuf times;
375
376   if (null_mtime(mtime)) return(True);
377
378   times.modtime = times.actime = mtime;
379
380   if (file_utime(conn, fname, &times)) {
381     DEBUG(4,("set_filetime(%s) failed: %s\n",fname,strerror(errno)));
382   }
383   
384   return(True);
385
386
387 /****************************************************************************
388 check if two filenames are equal
389
390 this needs to be careful about whether we are case sensitive
391 ****************************************************************************/
392 static BOOL fname_equal(char *name1, char *name2)
393 {
394   int l1 = strlen(name1);
395   int l2 = strlen(name2);
396
397   /* handle filenames ending in a single dot */
398   if (l1-l2 == 1 && name1[l1-1] == '.' && lp_strip_dot())
399     {
400       BOOL ret;
401       name1[l1-1] = 0;
402       ret = fname_equal(name1,name2);
403       name1[l1-1] = '.';
404       return(ret);
405     }
406
407   if (l2-l1 == 1 && name2[l2-1] == '.' && lp_strip_dot())
408     {
409       BOOL ret;
410       name2[l2-1] = 0;
411       ret = fname_equal(name1,name2);
412       name2[l2-1] = '.';
413       return(ret);
414     }
415
416   /* now normal filename handling */
417   if (case_sensitive)
418     return(strcmp(name1,name2) == 0);
419
420   return(strequal(name1,name2));
421 }
422
423
424 /****************************************************************************
425 mangle the 2nd name and check if it is then equal to the first name
426 ****************************************************************************/
427 static BOOL mangled_equal(char *name1, char *name2)
428 {
429   pstring tmpname;
430
431   if (is_8_3(name2, True))
432     return(False);
433
434   pstrcpy(tmpname,name2);
435   mangle_name_83(tmpname,sizeof(tmpname));
436
437   return(strequal(name1,tmpname));
438 }
439
440
441 /****************************************************************************
442 scan a directory to find a filename, matching without case sensitivity
443
444 If the name looks like a mangled name then try via the mangling functions
445 ****************************************************************************/
446 static BOOL scan_directory(char *path, char *name,connection_struct *conn,BOOL docache)
447 {
448   void *cur_dir;
449   char *dname;
450   BOOL mangled;
451   pstring name2;
452
453   mangled = is_mangled(name);
454
455   /* handle null paths */
456   if (*path == 0)
457     path = ".";
458
459   if (docache && (dname = DirCacheCheck(path,name,SNUM(conn)))) {
460     pstrcpy(name, dname);       
461     return(True);
462   }      
463
464   /*
465    * The incoming name can be mangled, and if we de-mangle it
466    * here it will not compare correctly against the filename (name2)
467    * read from the directory and then mangled by the name_map_mangle()
468    * call. We need to mangle both names or neither.
469    * (JRA).
470    */
471   if (mangled)
472     mangled = !check_mangled_cache( name );
473
474   /* open the directory */
475   if (!(cur_dir = OpenDir(conn, path, True))) 
476     {
477       DEBUG(3,("scan dir didn't open dir [%s]\n",path));
478       return(False);
479     }
480
481   /* now scan for matching names */
482   while ((dname = ReadDirName(cur_dir))) 
483     {
484       if (*dname == '.' &&
485           (strequal(dname,".") || strequal(dname,"..")))
486         continue;
487
488       pstrcpy(name2,dname);
489       if (!name_map_mangle(name2,False,SNUM(conn))) continue;
490
491       if ((mangled && mangled_equal(name,name2))
492           || fname_equal(name, name2))
493         {
494           /* we've found the file, change it's name and return */
495           if (docache) DirCacheAdd(path,name,dname,SNUM(conn));
496           pstrcpy(name, dname);
497           CloseDir(cur_dir);
498           return(True);
499         }
500     }
501
502   CloseDir(cur_dir);
503   return(False);
504 }
505
506 /****************************************************************************
507 This routine is called to convert names from the dos namespace to unix
508 namespace. It needs to handle any case conversions, mangling, format
509 changes etc.
510
511 We assume that we have already done a chdir() to the right "root" directory
512 for this service.
513
514 The function will return False if some part of the name except for the last
515 part cannot be resolved
516
517 If the saved_last_component != 0, then the unmodified last component
518 of the pathname is returned there. This is used in an exceptional
519 case in reply_mv (so far). If saved_last_component == 0 then nothing
520 is returned there.
521
522 The bad_path arg is set to True if the filename walk failed. This is
523 used to pick the correct error code to return between ENOENT and ENOTDIR
524 as Windows applications depend on ERRbadpath being returned if a component
525 of a pathname does not exist.
526 ****************************************************************************/
527 BOOL unix_convert(char *name,connection_struct *conn,char *saved_last_component, BOOL *bad_path)
528 {
529   struct stat st;
530   char *start, *end;
531   pstring dirpath;
532   int saved_errno;
533
534   *dirpath = 0;
535   *bad_path = False;
536
537   if(saved_last_component)
538     *saved_last_component = 0;
539
540   /* convert to basic unix format - removing \ chars and cleaning it up */
541   unix_format(name);
542   unix_clean_name(name);
543
544   /* names must be relative to the root of the service - trim any leading /.
545    also trim trailing /'s */
546   trim_string(name,"/","/");
547
548   /*
549    * Ensure saved_last_component is valid even if file exists.
550    */
551   if(saved_last_component) {
552     end = strrchr(name, '/');
553     if(end)
554       pstrcpy(saved_last_component, end + 1);
555     else
556       pstrcpy(saved_last_component, name);
557   }
558
559   if (!case_sensitive && 
560       (!case_preserve || (is_8_3(name, False) && !short_case_preserve)))
561     strnorm(name);
562
563   /* check if it's a printer file */
564   if (conn->printer)
565     {
566       if ((! *name) || strchr(name,'/') || !is_8_3(name, True))
567         {
568           char *s;
569           fstring name2;
570           slprintf(name2,sizeof(name2)-1,"%.6s.XXXXXX",remote_machine);
571           /* sanitise the name */
572           for (s=name2 ; *s ; s++)
573             if (!issafe(*s)) *s = '_';
574           pstrcpy(name,(char *)mktemp(name2));    
575         }      
576       return(True);
577     }
578
579   /* stat the name - if it exists then we are all done! */
580   if (sys_stat(name,&st) == 0)
581     return(True);
582
583   saved_errno = errno;
584
585   DEBUG(5,("unix_convert(%s)\n",name));
586
587   /* a special case - if we don't have any mangling chars and are case
588      sensitive then searching won't help */
589   if (case_sensitive && !is_mangled(name) && 
590       !lp_strip_dot() && !use_mangled_map && (saved_errno != ENOENT))
591     return(False);
592
593   /* now we need to recursively match the name against the real 
594      directory structure */
595
596   start = name;
597   while (strncmp(start,"./",2) == 0)
598     start += 2;
599
600   /* now match each part of the path name separately, trying the names
601      as is first, then trying to scan the directory for matching names */
602   for (;start;start = (end?end+1:(char *)NULL)) 
603     {
604       /* pinpoint the end of this section of the filename */
605       end = strchr(start, '/');
606
607       /* chop the name at this point */
608       if (end)  *end = 0;
609
610       if(saved_last_component != 0)
611         pstrcpy(saved_last_component, end ? end + 1 : start);
612
613       /* check if the name exists up to this point */
614       if (sys_stat(name, &st) == 0) 
615         {
616           /* it exists. it must either be a directory or this must be
617              the last part of the path for it to be OK */
618           if (end && !(st.st_mode & S_IFDIR)) 
619             {
620               /* an intermediate part of the name isn't a directory */
621               DEBUG(5,("Not a dir %s\n",start));
622               *end = '/';
623               return(False);
624             }
625         }
626       else 
627         {
628           pstring rest;
629
630           *rest = 0;
631
632           /* remember the rest of the pathname so it can be restored
633              later */
634           if (end) pstrcpy(rest,end+1);
635
636           /* try to find this part of the path in the directory */
637           if (strchr(start,'?') || strchr(start,'*') ||
638               !scan_directory(dirpath, start, conn, end?True:False))
639             {
640               if (end) 
641                 {
642                   /* an intermediate part of the name can't be found */
643                   DEBUG(5,("Intermediate not found %s\n",start));
644                   *end = '/';
645                   /* We need to return the fact that the intermediate
646                      name resolution failed. This is used to return an
647                      error of ERRbadpath rather than ERRbadfile. Some
648                      Windows applications depend on the difference between
649                      these two errors.
650                    */
651                   *bad_path = True;
652                   return(False);
653                 }
654               
655               /* just the last part of the name doesn't exist */
656               /* we may need to strupper() or strlower() it in case
657                  this conversion is being used for file creation 
658                  purposes */
659               /* if the filename is of mixed case then don't normalise it */
660               if (!case_preserve && 
661                   (!strhasupper(start) || !strhaslower(start)))         
662                 strnorm(start);
663
664               /* check on the mangled stack to see if we can recover the 
665                  base of the filename */
666               if (is_mangled(start))
667                 check_mangled_cache( start );
668
669               DEBUG(5,("New file %s\n",start));
670               return(True); 
671             }
672
673           /* restore the rest of the string */
674           if (end) 
675             {
676               pstrcpy(start+strlen(start)+1,rest);
677               end = start + strlen(start);
678             }
679         }
680
681       /* add to the dirpath that we have resolved so far */
682       if (*dirpath) pstrcat(dirpath,"/");
683       pstrcat(dirpath,start);
684
685       /* restore the / that we wiped out earlier */
686       if (end) *end = '/';
687     }
688   
689   /* the name has been resolved */
690   DEBUG(5,("conversion finished %s\n",name));
691   return(True);
692 }
693
694
695 /****************************************************************************
696 check a filename - possibly caling reducename
697
698 This is called by every routine before it allows an operation on a filename.
699 It does any final confirmation necessary to ensure that the filename is
700 a valid one for the user to access.
701 ****************************************************************************/
702 BOOL check_name(char *name,connection_struct *conn)
703 {
704   BOOL ret;
705
706   errno = 0;
707
708   if (IS_VETO_PATH(conn, name))  {
709           DEBUG(5,("file path name %s vetoed\n",name));
710           return(0);
711   }
712
713   ret = reduce_name(name,conn->connectpath,lp_widelinks(SNUM(conn)));
714
715   /* Check if we are allowing users to follow symlinks */
716   /* Patch from David Clerc <David.Clerc@cui.unige.ch>
717      University of Geneva */
718
719 #ifdef S_ISLNK
720   if (!lp_symlinks(SNUM(conn)))
721     {
722       struct stat statbuf;
723       if ( (sys_lstat(name,&statbuf) != -1) &&
724           (S_ISLNK(statbuf.st_mode)) )
725         {
726           DEBUG(3,("check_name: denied: file path name %s is a symlink\n",name));
727           ret=0; 
728         }
729     }
730 #endif
731
732   if (!ret)
733     DEBUG(5,("check_name on %s failed\n",name));
734
735   return(ret);
736 }
737
738 /****************************************************************************
739 check a filename - possibly caling reducename
740 ****************************************************************************/
741 static void check_for_pipe(char *fname)
742 {
743   /* special case of pipe opens */
744   char s[10];
745   StrnCpy(s,fname,9);
746   strlower(s);
747   if (strstr(s,"pipe/"))
748     {
749       DEBUG(3,("Rejecting named pipe open for %s\n",fname));
750       unix_ERR_class = ERRSRV;
751       unix_ERR_code = ERRaccess;
752     }
753 }
754
755 /****************************************************************************
756 fd support routines - attempt to do a sys_open
757 ****************************************************************************/
758 static int fd_attempt_open(char *fname, int flags, int mode)
759 {
760   int fd = sys_open(fname,flags,mode);
761
762   /* Fix for files ending in '.' */
763   if((fd == -1) && (errno == ENOENT) &&
764      (strchr(fname,'.')==NULL))
765     {
766       pstrcat(fname,".");
767       fd = sys_open(fname,flags,mode);
768     }
769
770 #if (defined(ENAMETOOLONG) && defined(HAVE_PATHCONF))
771   if ((fd == -1) && (errno == ENAMETOOLONG))
772     {
773       int max_len;
774       char *p = strrchr(fname, '/');
775
776       if (p == fname)   /* name is "/xxx" */
777         {
778           max_len = pathconf("/", _PC_NAME_MAX);
779           p++;
780         }
781       else if ((p == NULL) || (p == fname))
782         {
783           p = fname;
784           max_len = pathconf(".", _PC_NAME_MAX);
785         }
786       else
787         {
788           *p = '\0';
789           max_len = pathconf(fname, _PC_NAME_MAX);
790           *p = '/';
791           p++;
792         }
793       if (strlen(p) > max_len)
794         {
795           char tmp = p[max_len];
796
797           p[max_len] = '\0';
798           if ((fd = sys_open(fname,flags,mode)) == -1)
799             p[max_len] = tmp;
800         }
801     }
802 #endif
803   return fd;
804 }
805
806 /****************************************************************************
807 Cache a uid_t currently with this file open. This is an optimization only
808 used when multiple sessionsetup's have been done to one smbd.
809 ****************************************************************************/
810 static void fd_add_to_uid_cache(file_fd_struct *fd_ptr, uid_t u)
811 {
812   if(fd_ptr->uid_cache_count >= sizeof(fd_ptr->uid_users_cache)/sizeof(uid_t))
813     return;
814   fd_ptr->uid_users_cache[fd_ptr->uid_cache_count++] = u;
815 }
816
817 /****************************************************************************
818 Remove a uid_t that currently has this file open. This is an optimization only
819 used when multiple sessionsetup's have been done to one smbd.
820 ****************************************************************************/
821 static void fd_remove_from_uid_cache(file_fd_struct *fd_ptr, uid_t u)
822 {
823   int i;
824   for(i = 0; i < fd_ptr->uid_cache_count; i++)
825     if(fd_ptr->uid_users_cache[i] == u) {
826       if(i < (fd_ptr->uid_cache_count-1))
827         memmove((char *)&fd_ptr->uid_users_cache[i], (char *)&fd_ptr->uid_users_cache[i+1],
828                sizeof(uid_t)*(fd_ptr->uid_cache_count-1-i) );
829       fd_ptr->uid_cache_count--;
830     }
831   return;
832 }
833
834 /****************************************************************************
835 Check if a uid_t that currently has this file open is present. This is an
836 optimization only used when multiple sessionsetup's have been done to one smbd.
837 ****************************************************************************/
838 static BOOL fd_is_in_uid_cache(file_fd_struct *fd_ptr, uid_t u)
839 {
840   int i;
841   for(i = 0; i < fd_ptr->uid_cache_count; i++)
842     if(fd_ptr->uid_users_cache[i] == u)
843       return True;
844   return False;
845 }
846
847 /****************************************************************************
848 fd support routines - attempt to find an already open file by dev
849 and inode - increments the ref_count of the returned file_fd_struct *.
850 ****************************************************************************/
851 static file_fd_struct *fd_get_already_open(struct stat *sbuf)
852 {
853   int i;
854   file_fd_struct *fd_ptr;
855
856   if(sbuf == 0)
857     return 0;
858
859   for(i = 0; i <= max_file_fd_used; i++) {
860     fd_ptr = &FileFd[i];
861     if((fd_ptr->ref_count > 0) &&
862        (((uint32)sbuf->st_dev) == fd_ptr->dev) &&
863        (((uint32)sbuf->st_ino) == fd_ptr->inode)) {
864       fd_ptr->ref_count++;
865       DEBUG(3,
866        ("Re-used file_fd_struct %d, dev = %x, inode = %x, ref_count = %d\n",
867         i, fd_ptr->dev, fd_ptr->inode, fd_ptr->ref_count));
868       return fd_ptr;
869     }
870   }
871   return 0;
872 }
873
874 /****************************************************************************
875 fd support routines - attempt to find a empty slot in the FileFd array.
876 Increments the ref_count of the returned entry.
877 ****************************************************************************/
878 static file_fd_struct *fd_get_new(void)
879 {
880   extern struct current_user current_user;
881   int i;
882   file_fd_struct *fd_ptr;
883
884   for(i = 0; i < MAX_OPEN_FILES; i++) {
885     fd_ptr = &FileFd[i];
886     if(fd_ptr->ref_count == 0) {
887       fd_ptr->dev = (uint32)-1;
888       fd_ptr->inode = (uint32)-1;
889       fd_ptr->fd = -1;
890       fd_ptr->fd_readonly = -1;
891       fd_ptr->fd_writeonly = -1;
892       fd_ptr->real_open_flags = -1;
893       fd_ptr->uid_cache_count = 0;
894       fd_add_to_uid_cache(fd_ptr, (uid_t)current_user.uid);
895       fd_ptr->ref_count++;
896       /* Increment max used counter if neccessary, cuts down
897          on search time when re-using */
898       if(i > max_file_fd_used)
899         max_file_fd_used = i;
900       DEBUG(3,("Allocated new file_fd_struct %d, dev = %x, inode = %x\n",
901                i, fd_ptr->dev, fd_ptr->inode));
902       return fd_ptr;
903     }
904   }
905   DEBUG(1,("ERROR! Out of file_fd structures - perhaps increase MAX_OPEN_FILES?\n"));
906   return 0;
907 }
908
909 /****************************************************************************
910 fd support routines - attempt to re-open an already open fd as O_RDWR.
911 Save the already open fd (we cannot close due to POSIX file locking braindamage.
912 ****************************************************************************/
913 static void fd_attempt_reopen(char *fname, int mode, file_fd_struct *fd_ptr)
914 {
915   int fd = sys_open( fname, O_RDWR, mode);
916
917   if(fd == -1)
918     return;
919
920   if(fd_ptr->real_open_flags == O_RDONLY)
921     fd_ptr->fd_readonly = fd_ptr->fd;
922   if(fd_ptr->real_open_flags == O_WRONLY)
923     fd_ptr->fd_writeonly = fd_ptr->fd;
924
925   fd_ptr->fd = fd;
926   fd_ptr->real_open_flags = O_RDWR;
927 }
928
929 /****************************************************************************
930 fd support routines - attempt to close the file referenced by this fd.
931 Decrements the ref_count and returns it.
932 ****************************************************************************/
933 static int fd_attempt_close(file_fd_struct *fd_ptr)
934 {
935   extern struct current_user current_user;
936
937   DEBUG(3,("fd_attempt_close on file_fd_struct %d, fd = %d, dev = %x, inode = %x, open_flags = %d, ref_count = %d.\n",
938           fd_ptr - &FileFd[0],
939           fd_ptr->fd, fd_ptr->dev, fd_ptr->inode,
940           fd_ptr->real_open_flags,
941           fd_ptr->ref_count));
942   if(fd_ptr->ref_count > 0) {
943     fd_ptr->ref_count--;
944     if(fd_ptr->ref_count == 0) {
945       if(fd_ptr->fd != -1)
946         close(fd_ptr->fd);
947       if(fd_ptr->fd_readonly != -1)
948         close(fd_ptr->fd_readonly);
949       if(fd_ptr->fd_writeonly != -1)
950         close(fd_ptr->fd_writeonly);
951       fd_ptr->fd = -1;
952       fd_ptr->fd_readonly = -1;
953       fd_ptr->fd_writeonly = -1;
954       fd_ptr->real_open_flags = -1;
955       fd_ptr->dev = (uint32)-1;
956       fd_ptr->inode = (uint32)-1;
957       fd_ptr->uid_cache_count = 0;
958     } else
959       fd_remove_from_uid_cache(fd_ptr, (uid_t)current_user.uid);
960   } 
961  return fd_ptr->ref_count;
962 }
963
964 /****************************************************************************
965 fd support routines - check that current user has permissions
966 to open this file. Used when uid not found in optimization cache.
967 This is really ugly code, as due to POSIX locking braindamage we must
968 fork and then attempt to open the file, and return success or failure
969 via an exit code.
970 ****************************************************************************/
971 static BOOL check_access_allowed_for_current_user( char *fname, int accmode )
972 {
973   pid_t child_pid;
974
975   if((child_pid = fork()) < 0) {
976     DEBUG(0,("check_access_allowed_for_current_user: fork failed.\n"));
977     return False;
978   }
979
980   if(child_pid) {
981     /*
982      * Parent.
983      */
984     pid_t wpid;
985     int status_code;
986     if ((wpid = sys_waitpid(child_pid, &status_code, 0)) < 0) {
987       DEBUG(0,("check_access_allowed_for_current_user: The process is no longer waiting!\n"));
988       return(False);
989     }
990
991     if (child_pid != wpid) {
992       DEBUG(0,("check_access_allowed_for_current_user: We were waiting for the wrong process ID\n"));
993       return(False);
994     }
995 #if defined(WIFEXITED) && defined(WEXITSTATUS)
996     if (WIFEXITED(status_code) == 0) {
997       DEBUG(0,("check_access_allowed_for_current_user: The process exited while we were waiting\n"));
998       return(False);
999     }
1000     if (WEXITSTATUS(status_code) != 0) {
1001       DEBUG(9,("check_access_allowed_for_current_user: The status of the process exiting was %d. Returning access denied.\n", status_code));
1002       return(False);
1003     }
1004 #else /* defined(WIFEXITED) && defined(WEXITSTATUS) */
1005     if(status_code != 0) {
1006       DEBUG(9,("check_access_allowed_for_current_user: The status of the process exiting was %d. Returning access denied.\n", status_code));
1007       return(False);
1008     }
1009 #endif /* defined(WIFEXITED) && defined(WEXITSTATUS) */
1010
1011     /*
1012      * Success - the child could open the file.
1013      */
1014     DEBUG(9,("check_access_allowed_for_current_user: The status of the process exiting was %d. Returning access allowed.\n", status_code));
1015     return True;
1016   } else {
1017     /*
1018      * Child.
1019      */
1020     int fd;
1021     DEBUG(9,("check_access_allowed_for_current_user: Child - attempting to open %s with mode %d.\n", fname, accmode ));
1022     if((fd = fd_attempt_open( fname, accmode, 0)) < 0) {
1023       /* Access denied. */
1024       _exit(EACCES);
1025     }
1026     close(fd);
1027     DEBUG(9,("check_access_allowed_for_current_user: Child - returning ok.\n"));
1028     _exit(0);
1029   }
1030
1031   return False;
1032 }
1033
1034 /****************************************************************************
1035 open a file
1036 ****************************************************************************/
1037 static void open_file(int fnum,connection_struct *conn,
1038                       char *fname1,int flags,int mode, struct stat *sbuf)
1039 {
1040   extern struct current_user current_user;
1041   pstring fname;
1042   struct stat statbuf;
1043   file_fd_struct *fd_ptr;
1044   files_struct *fsp = &Files[fnum];
1045   int accmode = (flags & (O_RDONLY | O_WRONLY | O_RDWR));
1046
1047   fsp->open = False;
1048   fsp->fd_ptr = 0;
1049   fsp->granted_oplock = False;
1050   errno = EPERM;
1051
1052   pstrcpy(fname,fname1);
1053
1054   /* check permissions */
1055
1056   /*
1057    * This code was changed after seeing a client open request 
1058    * containing the open mode of (DENY_WRITE/read-only) with
1059    * the 'create if not exist' bit set. The previous code
1060    * would fail to open the file read only on a read-only share
1061    * as it was checking the flags parameter  directly against O_RDONLY,
1062    * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
1063    * JRA.
1064    */
1065
1066   if (conn->read_only && !conn->printer) {
1067     /* It's a read-only share - fail if we wanted to write. */
1068     if(accmode != O_RDONLY) {
1069       DEBUG(3,("Permission denied opening %s\n",fname));
1070       check_for_pipe(fname);
1071       return;
1072     } else if(flags & O_CREAT) {
1073       /* We don't want to write - but we must make sure that O_CREAT
1074          doesn't create the file if we have write access into the
1075          directory.
1076        */
1077       flags &= ~O_CREAT;
1078     }
1079   }
1080
1081   /* this handles a bug in Win95 - it doesn't say to create the file when it 
1082      should */
1083   if (conn->printer) {
1084           flags |= O_CREAT;
1085   }
1086
1087 /*
1088   if (flags == O_WRONLY)
1089     DEBUG(3,("Bug in client? Set O_WRONLY without O_CREAT\n"));
1090 */
1091
1092   /*
1093    * Ensure we have a valid struct stat so we can search the
1094    * open fd table.
1095    */
1096   if(sbuf == 0) {
1097     if(sys_stat(fname, &statbuf) < 0) {
1098       if(errno != ENOENT) {
1099         DEBUG(3,("Error doing stat on file %s (%s)\n",
1100                  fname,strerror(errno)));
1101
1102         check_for_pipe(fname);
1103         return;
1104       }
1105       sbuf = 0;
1106     } else {
1107       sbuf = &statbuf;
1108     }
1109   }
1110
1111   /*
1112    * Check to see if we have this file already
1113    * open. If we do, just use the already open fd and increment the
1114    * reference count (fd_get_already_open increments the ref_count).
1115    */
1116   if((fd_ptr = fd_get_already_open(sbuf))!= 0) {
1117     /*
1118      * File was already open.
1119      */
1120
1121     /* 
1122      * Check it wasn't open for exclusive use.
1123      */
1124     if((flags & O_CREAT) && (flags & O_EXCL)) {
1125       fd_ptr->ref_count--;
1126       errno = EEXIST;
1127       return;
1128     }
1129
1130     /*
1131      * Ensure that the user attempting to open
1132      * this file has permissions to do so, if
1133      * the user who originally opened the file wasn't
1134      * the same as the current user.
1135      */
1136
1137     if(!fd_is_in_uid_cache(fd_ptr, (uid_t)current_user.uid)) {
1138       if(!check_access_allowed_for_current_user( fname, accmode )) {
1139         /* Error - permission denied. */
1140         DEBUG(3,("Permission denied opening file %s (flags=%d, accmode = %d)\n",
1141               fname, flags, accmode));
1142         /* Ensure the ref_count is decremented. */
1143         fd_ptr->ref_count--;
1144         fd_remove_from_uid_cache(fd_ptr, (uid_t)current_user.uid);
1145         errno = EACCES;
1146         return;
1147       }
1148     }
1149
1150     fd_add_to_uid_cache(fd_ptr, (uid_t)current_user.uid);
1151
1152     /* 
1153      * If not opened O_RDWR try
1154      * and do that here - a chmod may have been done
1155      * between the last open and now. 
1156      */
1157     if(fd_ptr->real_open_flags != O_RDWR)
1158       fd_attempt_reopen(fname, mode, fd_ptr);
1159
1160     /*
1161      * Ensure that if we wanted write access
1162      * it has been opened for write, and if we wanted read it
1163      * was open for read. 
1164      */
1165     if(((accmode == O_WRONLY) && (fd_ptr->real_open_flags == O_RDONLY)) ||
1166        ((accmode == O_RDONLY) && (fd_ptr->real_open_flags == O_WRONLY)) ||
1167        ((accmode == O_RDWR) && (fd_ptr->real_open_flags != O_RDWR))) {
1168       DEBUG(3,("Error opening (already open for flags=%d) file %s (%s) (flags=%d)\n",
1169                fd_ptr->real_open_flags, fname,strerror(EACCES),flags));
1170       check_for_pipe(fname);
1171       fd_remove_from_uid_cache(fd_ptr, (uid_t)current_user.uid);
1172       fd_ptr->ref_count--;
1173       return;
1174     }
1175
1176   } else {
1177     int open_flags;
1178     /* We need to allocate a new file_fd_struct (this increments the
1179        ref_count). */
1180     if((fd_ptr = fd_get_new()) == 0)
1181       return;
1182     /*
1183      * Whatever the requested flags, attempt read/write access,
1184      * as we don't know what flags future file opens may require.
1185      * If this fails, try again with the required flags. 
1186      * Even if we open read/write when only read access was 
1187      * requested the setting of the can_write flag in
1188      * the file_struct will protect us from errant
1189      * write requests. We never need to worry about O_APPEND
1190      * as this is not set anywhere in Samba.
1191      */
1192     fd_ptr->real_open_flags = O_RDWR;
1193     /* Set the flags as needed without the read/write modes. */
1194     open_flags = flags & ~(O_RDWR|O_WRONLY|O_RDONLY);
1195     fd_ptr->fd = fd_attempt_open(fname, open_flags|O_RDWR, mode);
1196     /*
1197      * On some systems opening a file for R/W access on a read only
1198      * filesystems sets errno to EROFS.
1199      */
1200 #ifdef EROFS
1201     if((fd_ptr->fd == -1) && ((errno == EACCES) || (errno == EROFS))) {
1202 #else /* No EROFS */
1203     if((fd_ptr->fd == -1) && (errno == EACCES)) {
1204 #endif /* EROFS */
1205       if(accmode != O_RDWR) {
1206         fd_ptr->fd = fd_attempt_open(fname, open_flags|accmode, mode);
1207         fd_ptr->real_open_flags = accmode;
1208       }
1209     }
1210   }
1211
1212   if ((fd_ptr->fd >=0) && 
1213       conn->printer && lp_minprintspace(SNUM(conn))) {
1214     pstring dname;
1215     int dum1,dum2,dum3;
1216     char *p;
1217     pstrcpy(dname,fname);
1218     p = strrchr(dname,'/');
1219     if (p) *p = 0;
1220     if (sys_disk_free(dname,&dum1,&dum2,&dum3) < 
1221         lp_minprintspace(SNUM(conn))) {
1222       fd_attempt_close(fd_ptr);
1223       fsp->fd_ptr = 0;
1224       if(fd_ptr->ref_count == 0)
1225         sys_unlink(fname);
1226       errno = ENOSPC;
1227       return;
1228     }
1229   }
1230     
1231   if (fd_ptr->fd < 0)
1232   {
1233     DEBUG(3,("Error opening file %s (%s) (flags=%d)\n",
1234       fname,strerror(errno),flags));
1235     /* Ensure the ref_count is decremented. */
1236     fd_attempt_close(fd_ptr);
1237     check_for_pipe(fname);
1238     return;
1239   }
1240
1241   if (fd_ptr->fd >= 0)
1242   {
1243     if(sbuf == 0) {
1244       /* Do the fstat */
1245       if(fstat(fd_ptr->fd, &statbuf) == -1) {
1246         /* Error - backout !! */
1247         DEBUG(3,("Error doing fstat on fd %d, file %s (%s)\n",
1248                  fd_ptr->fd, fname,strerror(errno)));
1249         /* Ensure the ref_count is decremented. */
1250         fd_attempt_close(fd_ptr);
1251         return;
1252       }
1253       sbuf = &statbuf;
1254     }
1255
1256     /* Set the correct entries in fd_ptr. */
1257     fd_ptr->dev = (uint32)sbuf->st_dev;
1258     fd_ptr->inode = (uint32)sbuf->st_ino;
1259
1260     fsp->fd_ptr = fd_ptr;
1261     conn->num_files_open++;
1262     fsp->mode = sbuf->st_mode;
1263     GetTimeOfDay(&fsp->open_time);
1264     fsp->vuid = current_user.vuid;
1265     fsp->size = 0;
1266     fsp->pos = -1;
1267     fsp->open = True;
1268     fsp->mmap_ptr = NULL;
1269     fsp->mmap_size = 0;
1270     fsp->can_lock = True;
1271     fsp->can_read = ((flags & O_WRONLY)==0);
1272     fsp->can_write = ((flags & (O_WRONLY|O_RDWR))!=0);
1273     fsp->share_mode = 0;
1274     fsp->print_file = conn->printer;
1275     fsp->modified = False;
1276     fsp->granted_oplock = False;
1277     fsp->sent_oplock_break = False;
1278     fsp->is_directory = False;
1279     fsp->conn = conn;
1280     /*
1281      * Note that the file name here is the *untranslated* name
1282      * ie. it is still in the DOS codepage sent from the client.
1283      * All use of this filename will pass though the sys_xxxx
1284      * functions which will do the dos_to_unix translation before
1285      * mapping into a UNIX filename. JRA.
1286      */
1287     string_set(&fsp->fsp_name,fname);
1288     fsp->wbmpx_ptr = NULL;      
1289
1290     /*
1291      * If the printer is marked as postscript output a leading
1292      * file identifier to ensure the file is treated as a raw
1293      * postscript file.
1294      * This has a similar effect as CtrlD=0 in WIN.INI file.
1295      * tim@fsg.com 09/06/94
1296      */
1297     if (fsp->print_file && lp_postscript(SNUM(conn)) && fsp->can_write) {
1298             DEBUG(3,("Writing postscript line\n"));
1299             write_file(fnum,"%!\n",3);
1300     }
1301       
1302     DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d fnum=%d)\n",
1303              *sesssetup_user ? sesssetup_user : conn->user,fname,
1304              BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
1305              conn->num_files_open,fnum));
1306
1307   }
1308
1309 #if WITH_MMAP
1310   /* mmap it if read-only */
1311   if (!fsp->can_write) {
1312           fsp->mmap_size = file_size(fname);
1313           fsp->mmap_ptr = (char *)mmap(NULL,fsp->mmap_size,
1314                                        PROT_READ,MAP_SHARED,fsp->fd_ptr->fd,0);
1315
1316           if (fsp->mmap_ptr == (char *)-1 || !fsp->mmap_ptr) {
1317                   DEBUG(3,("Failed to mmap() %s - %s\n",
1318                            fname,strerror(errno)));
1319                   fsp->mmap_ptr = NULL;
1320           }
1321   }
1322 #endif
1323 }
1324
1325 /*******************************************************************
1326 sync a file
1327 ********************************************************************/
1328 void sync_file(connection_struct *conn, int fnum)
1329 {
1330 #ifdef HAVE_FSYNC
1331     if(lp_strict_sync(SNUM(conn)))
1332       fsync(Files[fnum].fd_ptr->fd);
1333 #endif
1334 }
1335
1336 /****************************************************************************
1337 run a file if it is a magic script
1338 ****************************************************************************/
1339 static void check_magic(int fnum,connection_struct *conn)
1340 {
1341   if (!*lp_magicscript(SNUM(conn)))
1342     return;
1343
1344   DEBUG(5,("checking magic for %s\n",Files[fnum].fsp_name));
1345
1346   {
1347     char *p;
1348     if (!(p = strrchr(Files[fnum].fsp_name,'/')))
1349       p = Files[fnum].fsp_name;
1350     else
1351       p++;
1352
1353     if (!strequal(lp_magicscript(SNUM(conn)),p))
1354       return;
1355   }
1356
1357   {
1358     int ret;
1359     pstring magic_output;
1360     pstring fname;
1361     pstrcpy(fname,Files[fnum].fsp_name);
1362
1363     if (*lp_magicoutput(SNUM(conn)))
1364       pstrcpy(magic_output,lp_magicoutput(SNUM(conn)));
1365     else
1366       slprintf(magic_output,sizeof(fname)-1, "%s.out",fname);
1367
1368     chmod(fname,0755);
1369     ret = smbrun(fname,magic_output,False);
1370     DEBUG(3,("Invoking magic command %s gave %d\n",fname,ret));
1371     unlink(fname);
1372   }
1373 }
1374
1375 /****************************************************************************
1376   Common code to close a file or a directory.
1377 ****************************************************************************/
1378 static void close_filestruct(files_struct *fsp)
1379 {   
1380         connection_struct *conn = fsp->conn;
1381     
1382         fsp->reserved = False; 
1383         fsp->open = False;
1384         fsp->is_directory = False; 
1385     
1386         conn->num_files_open--;
1387         if(fsp->wbmpx_ptr) {  
1388                 free((char *)fsp->wbmpx_ptr);
1389                 fsp->wbmpx_ptr = NULL; 
1390         }  
1391      
1392 #if WITH_MMAP
1393         if(fsp->mmap_ptr) {
1394                 munmap(fsp->mmap_ptr,fsp->mmap_size);
1395                 fsp->mmap_ptr = NULL;
1396         }  
1397 #endif 
1398 }    
1399
1400 /****************************************************************************
1401  Close a file - possibly invalidating the read prediction.
1402
1403  If normal_close is 1 then this came from a normal SMBclose (or equivalent)
1404  operation otherwise it came as the result of some other operation such as
1405  the closing of the connection. In the latter case printing and
1406  magic scripts are not run.
1407 ****************************************************************************/
1408 void close_file(int fnum, BOOL normal_close)
1409 {
1410         files_struct *fs_p = &Files[fnum];
1411         uint32 dev = fs_p->fd_ptr->dev;
1412         uint32 inode = fs_p->fd_ptr->inode;
1413         int token;
1414         connection_struct *conn = fs_p->conn;
1415
1416         close_filestruct(fs_p);
1417
1418 #if USE_READ_PREDICTION
1419         invalidate_read_prediction(fs_p->fd_ptr->fd);
1420 #endif
1421
1422         if (lp_share_modes(SNUM(conn))) {
1423                 lock_share_entry(conn, dev, inode, &token);
1424                 del_share_mode(token, fnum);
1425         }
1426
1427         fd_attempt_close(fs_p->fd_ptr);
1428
1429         if (lp_share_modes(SNUM(conn)))
1430                 unlock_share_entry(conn, dev, inode, token);
1431
1432         /* NT uses smbclose to start a print - weird */
1433         if (normal_close && fs_p->print_file)
1434                 print_file(conn, fs_p);
1435
1436         /* check for magic scripts */
1437         if (normal_close) {
1438                 check_magic(fnum,conn);
1439         }
1440
1441         if(fs_p->granted_oplock == True)
1442                 global_oplocks_open--;
1443
1444         fs_p->sent_oplock_break = False;
1445
1446         DEBUG(2,("%s closed file %s (numopen=%d)\n",
1447                  conn->user,fs_p->fsp_name,
1448                  conn->num_files_open));
1449
1450         if (fs_p->fsp_name) {
1451                 string_free(&fs_p->fsp_name);
1452         }
1453
1454         /* we will catch bugs faster by zeroing this structure */
1455         memset(fs_p, 0, sizeof(*fs_p));
1456 }
1457
1458 /****************************************************************************
1459  Close a directory opened by an NT SMB call. 
1460 ****************************************************************************/
1461   
1462 void close_directory(int fnum)
1463 {
1464   files_struct *fsp = &Files[fnum];
1465
1466   /* TODO - walk the list of pending
1467      change notify requests and free
1468      any pertaining to this fnum. */
1469
1470   remove_pending_change_notify_requests_by_fid(fnum);
1471
1472   /*
1473    * Do the code common to files and directories.
1474    */
1475   close_filestruct(fsp);
1476
1477   if (fsp->fsp_name)
1478     string_free(&fsp->fsp_name);
1479
1480   /* we will catch bugs faster by zeroing this structure */
1481   memset(fsp, 0, sizeof(*fsp));
1482 }
1483
1484 /****************************************************************************
1485  Open a directory from an NT SMB call.
1486 ****************************************************************************/
1487 int open_directory(int fnum,connection_struct *conn,
1488                    char *fname, int smb_ofun, int unixmode, int *action)
1489 {
1490         extern struct current_user current_user;
1491         files_struct *fsp = &Files[fnum];
1492         struct stat st;
1493
1494         if (smb_ofun & 0x10) {
1495                 /*
1496                  * Create the directory.
1497                  */
1498
1499                 if(sys_mkdir(fname, unixmode) < 0) {
1500                         DEBUG(0,("open_directory: unable to create %s. Error was %s\n",
1501                                  fname, strerror(errno) ));
1502                         return -1;
1503                 }
1504
1505                 *action = FILE_WAS_CREATED;
1506         } else {
1507                 /*
1508                  * Check that it *was* a directory.
1509                  */
1510
1511                 if(sys_stat(fname, &st) < 0) {
1512                         DEBUG(0,("open_directory: unable to stat name = %s. Error was %s\n",
1513                                  fname, strerror(errno) ));
1514                         return -1;
1515                 }
1516
1517                 if(!S_ISDIR(st.st_mode)) {
1518                         DEBUG(0,("open_directory: %s is not a directory !\n", fname ));
1519                         return -1;
1520                 }
1521                 *action = FILE_WAS_OPENED;
1522         }
1523         
1524         DEBUG(5,("open_directory: opening directory %s, fnum = %d\n",
1525                  fname, fnum ));
1526
1527         /*
1528          * Setup the files_struct for it.
1529          */
1530         
1531         fsp->fd_ptr = NULL;
1532         conn->num_files_open++;
1533         fsp->mode = 0;
1534         GetTimeOfDay(&fsp->open_time);
1535         fsp->vuid = current_user.vuid;
1536         fsp->size = 0;
1537         fsp->pos = -1;
1538         fsp->open = True;
1539         fsp->mmap_ptr = NULL;
1540         fsp->mmap_size = 0;
1541         fsp->can_lock = True;
1542         fsp->can_read = False;
1543         fsp->can_write = False;
1544         fsp->share_mode = 0;
1545         fsp->print_file = False;
1546         fsp->modified = False;
1547         fsp->granted_oplock = False;
1548         fsp->sent_oplock_break = False;
1549         fsp->is_directory = True;
1550         fsp->conn = conn;
1551         /*
1552          * Note that the file name here is the *untranslated* name
1553          * ie. it is still in the DOS codepage sent from the client.
1554          * All use of this filename will pass though the sys_xxxx
1555          * functions which will do the dos_to_unix translation before
1556          * mapping into a UNIX filename. JRA.
1557          */
1558         string_set(&fsp->fsp_name,fname);
1559         fsp->wbmpx_ptr = NULL;
1560
1561         return 0;
1562 }
1563
1564 enum {AFAIL,AREAD,AWRITE,AALL};
1565
1566 /*******************************************************************
1567 reproduce the share mode access table
1568 ********************************************************************/
1569 static int access_table(int new_deny,int old_deny,int old_mode,
1570                         int share_pid,char *fname)
1571 {
1572   if (new_deny == DENY_ALL || old_deny == DENY_ALL) return(AFAIL);
1573
1574   if (new_deny == DENY_DOS || old_deny == DENY_DOS) {
1575     int pid = getpid();
1576     if (old_deny == new_deny && share_pid == pid) 
1577         return(AALL);    
1578
1579     if (old_mode == 0) return(AREAD);
1580
1581     /* the new smbpub.zip spec says that if the file extension is
1582        .com, .dll, .exe or .sym then allow the open. I will force
1583        it to read-only as this seems sensible although the spec is
1584        a little unclear on this. */
1585     if ((fname = strrchr(fname,'.'))) {
1586       if (strequal(fname,".com") ||
1587           strequal(fname,".dll") ||
1588           strequal(fname,".exe") ||
1589           strequal(fname,".sym"))
1590         return(AREAD);
1591     }
1592
1593     return(AFAIL);
1594   }
1595
1596   switch (new_deny) 
1597     {
1598     case DENY_WRITE:
1599       if (old_deny==DENY_WRITE && old_mode==0) return(AREAD);
1600       if (old_deny==DENY_READ && old_mode==0) return(AWRITE);
1601       if (old_deny==DENY_NONE && old_mode==0) return(AALL);
1602       return(AFAIL);
1603     case DENY_READ:
1604       if (old_deny==DENY_WRITE && old_mode==1) return(AREAD);
1605       if (old_deny==DENY_READ && old_mode==1) return(AWRITE);
1606       if (old_deny==DENY_NONE && old_mode==1) return(AALL);
1607       return(AFAIL);
1608     case DENY_NONE:
1609       if (old_deny==DENY_WRITE) return(AREAD);
1610       if (old_deny==DENY_READ) return(AWRITE);
1611       if (old_deny==DENY_NONE) return(AALL);
1612       return(AFAIL);      
1613     }
1614   return(AFAIL);      
1615 }
1616
1617 /*******************************************************************
1618 check if the share mode on a file allows it to be deleted or unlinked
1619 return True if sharing doesn't prevent the operation
1620 ********************************************************************/
1621 BOOL check_file_sharing(connection_struct *conn,char *fname, BOOL rename_op)
1622 {
1623   int i;
1624   int ret = False;
1625   share_mode_entry *old_shares = 0;
1626   int num_share_modes;
1627   struct stat sbuf;
1628   int token;
1629   int pid = getpid();
1630   uint32 dev, inode;
1631
1632   if(!lp_share_modes(SNUM(conn)))
1633     return True;
1634
1635   if (sys_stat(fname,&sbuf) == -1) return(True);
1636
1637   dev = (uint32)sbuf.st_dev;
1638   inode = (uint32)sbuf.st_ino;
1639
1640   lock_share_entry(conn, dev, inode, &token);
1641   num_share_modes = get_share_modes(conn, token, dev, inode, &old_shares);
1642
1643   /*
1644    * Check if the share modes will give us access.
1645    */
1646
1647   if(num_share_modes != 0)
1648   {
1649     BOOL broke_oplock;
1650
1651     do
1652     {
1653
1654       broke_oplock = False;
1655       for(i = 0; i < num_share_modes; i++)
1656       {
1657         share_mode_entry *share_entry = &old_shares[i];
1658
1659         /* 
1660          * Break oplocks before checking share modes. See comment in
1661          * open_file_shared for details. 
1662          * Check if someone has an oplock on this file. If so we must 
1663          * break it before continuing. 
1664          */
1665         if(share_entry->op_type & BATCH_OPLOCK)
1666         {
1667
1668           /*
1669            * It appears that the NT redirector may have a bug, in that
1670            * it tries to do an SMBmv on a file that it has open with a
1671            * batch oplock, and then fails to respond to the oplock break
1672            * request. This only seems to occur when the client is doing an
1673            * SMBmv to the smbd it is using - thus we try and detect this
1674            * condition by checking if the file being moved is open and oplocked by
1675            * this smbd process, and then not sending the oplock break in this
1676            * special case. If the file was open with a deny mode that 
1677            * prevents the move the SMBmv will fail anyway with a share
1678            * violation error. JRA.
1679            */
1680           if(rename_op && (share_entry->pid == pid))
1681           {
1682             DEBUG(0,("check_file_sharing: NT redirector workaround - rename attempted on \
1683 batch oplocked file %s, dev = %x, inode = %x\n", fname, dev, inode));
1684             /* 
1685              * This next line is a test that allows the deny-mode
1686              * processing to be skipped. This seems to be needed as
1687              * NT insists on the rename succeeding (in Office 9x no less !).
1688              * This should be removed as soon as (a) MS fix the redirector
1689              * bug or (b) NT SMB support in Samba makes NT not issue the
1690              * call (as is my fervent hope). JRA.
1691              */ 
1692             continue;
1693           }
1694           else
1695           {
1696             DEBUG(5,("check_file_sharing: breaking oplock (%x) on file %s, \
1697 dev = %x, inode = %x\n", share_entry->op_type, fname, dev, inode));
1698
1699             /* Oplock break.... */
1700             unlock_share_entry(conn, dev, inode, token);
1701             if(request_oplock_break(share_entry, dev, inode) == False)
1702             {
1703               free((char *)old_shares);
1704               DEBUG(0,("check_file_sharing: FAILED when breaking oplock (%x) on file %s, \
1705 dev = %x, inode = %x\n", old_shares[i].op_type, fname, dev, inode));
1706               return False;
1707             }
1708             lock_share_entry(conn, dev, inode, &token);
1709             broke_oplock = True;
1710             break;
1711           }
1712         }
1713
1714         /* someone else has a share lock on it, check to see 
1715            if we can too */
1716         if ((share_entry->share_mode != DENY_DOS) || (share_entry->pid != pid))
1717           goto free_and_exit;
1718
1719       } /* end for */
1720
1721       if(broke_oplock)
1722       {
1723         free((char *)old_shares);
1724         num_share_modes = get_share_modes(conn, token, dev, inode, &old_shares);
1725       }
1726     } while(broke_oplock);
1727   }
1728
1729   /* XXXX exactly what share mode combinations should be allowed for
1730      deleting/renaming? */
1731   /* If we got here then either there were no share modes or
1732      all share modes were DENY_DOS and the pid == getpid() */
1733   ret = True;
1734
1735 free_and_exit:
1736
1737   unlock_share_entry(conn, dev, inode, token);
1738   if(old_shares != NULL)
1739     free((char *)old_shares);
1740   return(ret);
1741 }
1742
1743 /****************************************************************************
1744   C. Hoch 11/22/95
1745   Helper for open_file_shared. 
1746   Truncate a file after checking locking; close file if locked.
1747   **************************************************************************/
1748 static void truncate_unless_locked(int fnum, connection_struct *conn, int token, 
1749                                    BOOL *share_locked)
1750 {
1751   files_struct *fsp = &Files[fnum];
1752
1753   if (fsp->can_write){
1754     if (is_locked(fnum,conn,0x3FFFFFFF,0,F_WRLCK)){
1755       /* If share modes are in force for this connection we
1756          have the share entry locked. Unlock it before closing. */
1757       if (*share_locked && lp_share_modes(SNUM(conn)))
1758         unlock_share_entry( conn, fsp->fd_ptr->dev, 
1759                             fsp->fd_ptr->inode, token);
1760       close_file(fnum,False);   
1761       /* Share mode no longer locked. */
1762       *share_locked = False;
1763       errno = EACCES;
1764       unix_ERR_class = ERRDOS;
1765       unix_ERR_code = ERRlock;
1766     }
1767     else
1768       ftruncate(fsp->fd_ptr->fd,0); 
1769   }
1770 }
1771
1772 /****************************************************************************
1773 check if we can open a file with a share mode
1774 ****************************************************************************/
1775 int check_share_mode( share_mode_entry *share, int deny_mode, char *fname,
1776                       BOOL fcbopen, int *flags)
1777 {
1778   int old_open_mode = share->share_mode &0xF;
1779   int old_deny_mode = (share->share_mode >>4)&7;
1780
1781   if (old_deny_mode > 4 || old_open_mode > 2)
1782   {
1783     DEBUG(0,("Invalid share mode found (%d,%d,%d) on file %s\n",
1784                deny_mode,old_deny_mode,old_open_mode,fname));
1785     return False;
1786   }
1787
1788   {
1789     int access_allowed = access_table(deny_mode,old_deny_mode,old_open_mode,
1790                                 share->pid,fname);
1791
1792     if ((access_allowed == AFAIL) ||
1793         (!fcbopen && (access_allowed == AREAD && *flags == O_RDWR)) ||
1794         (access_allowed == AREAD && *flags == O_WRONLY) ||
1795         (access_allowed == AWRITE && *flags == O_RDONLY))
1796     {
1797       DEBUG(2,("Share violation on file (%d,%d,%d,%d,%s,fcbopen = %d, flags = %d) = %d\n",
1798                 deny_mode,old_deny_mode,old_open_mode,
1799                 share->pid,fname, fcbopen, *flags, access_allowed));
1800       return False;
1801     }
1802
1803     if (access_allowed == AREAD)
1804       *flags = O_RDONLY;
1805
1806     if (access_allowed == AWRITE)
1807       *flags = O_WRONLY;
1808
1809   }
1810   return True;
1811 }
1812
1813 /****************************************************************************
1814 open a file with a share mode
1815 ****************************************************************************/
1816 void open_file_shared(int fnum,connection_struct *conn,char *fname,int share_mode,int ofun,
1817                       int mode,int oplock_request, int *Access,int *action)
1818 {
1819   files_struct *fs_p = &Files[fnum];
1820   int flags=0;
1821   int flags2=0;
1822   int deny_mode = (share_mode>>4)&7;
1823   struct stat sbuf;
1824   BOOL file_existed = file_exist(fname,&sbuf);
1825   BOOL share_locked = False;
1826   BOOL fcbopen = False;
1827   int token;
1828   uint32 dev = 0;
1829   uint32 inode = 0;
1830   int num_share_modes = 0;
1831
1832   fs_p->open = False;
1833   fs_p->fd_ptr = 0;
1834
1835   /* this is for OS/2 EAs - try and say we don't support them */
1836   if (strstr(fname,".+,;=[].")) 
1837   {
1838     unix_ERR_class = ERRDOS;
1839     /* OS/2 Workplace shell fix may be main code stream in a later release. */ 
1840 #if 1 /* OS2_WPS_FIX - Recent versions of OS/2 need this. */
1841     unix_ERR_code = ERRcannotopen;
1842 #else /* OS2_WPS_FIX */
1843     unix_ERR_code = ERROR_EAS_NOT_SUPPORTED;
1844 #endif /* OS2_WPS_FIX */
1845
1846     return;
1847   }
1848
1849   if ((ofun & 0x3) == 0 && file_existed)  
1850   {
1851     errno = EEXIST;
1852     return;
1853   }
1854       
1855   if (ofun & 0x10)
1856     flags2 |= O_CREAT;
1857   if ((ofun & 0x3) == 2)
1858     flags2 |= O_TRUNC;
1859
1860   /* note that we ignore the append flag as 
1861      append does not mean the same thing under dos and unix */
1862
1863   switch (share_mode&0xF)
1864   {
1865     case 1: 
1866       flags = O_WRONLY; 
1867       break;
1868     case 0xF: 
1869       fcbopen = True;
1870       flags = O_RDWR; 
1871       break;
1872     case 2: 
1873       flags = O_RDWR; 
1874       break;
1875     default:
1876       flags = O_RDONLY;
1877       break;
1878   }
1879
1880 #if defined(O_SYNC)
1881   if (share_mode&(1<<14)) {
1882           flags2 |= O_SYNC;
1883   }
1884 #endif /* O_SYNC */
1885   
1886   if (flags != O_RDONLY && file_existed && 
1887       (!CAN_WRITE(conn) || IS_DOS_READONLY(dos_mode(conn,fname,&sbuf)))) 
1888   {
1889     if (!fcbopen) 
1890     {
1891       errno = EACCES;
1892       return;
1893     }
1894     flags = O_RDONLY;
1895   }
1896
1897   if (deny_mode > DENY_NONE && deny_mode!=DENY_FCB) 
1898   {
1899     DEBUG(2,("Invalid deny mode %d on file %s\n",deny_mode,fname));
1900     errno = EINVAL;
1901     return;
1902   }
1903
1904   if (deny_mode == DENY_FCB) deny_mode = DENY_DOS;
1905
1906   if (lp_share_modes(SNUM(conn))) 
1907   {
1908     int i;
1909     share_mode_entry *old_shares = 0;
1910
1911     if (file_existed)
1912     {
1913       dev = (uint32)sbuf.st_dev;
1914       inode = (uint32)sbuf.st_ino;
1915       lock_share_entry(conn, dev, inode, &token);
1916       share_locked = True;
1917       num_share_modes = get_share_modes(conn, token, dev, inode, &old_shares);
1918     }
1919
1920     /*
1921      * Check if the share modes will give us access.
1922      */
1923
1924     if(share_locked && (num_share_modes != 0))
1925     {
1926       BOOL broke_oplock;
1927
1928       do
1929       {
1930
1931         broke_oplock = False;
1932         for(i = 0; i < num_share_modes; i++)
1933         {
1934           share_mode_entry *share_entry = &old_shares[i];
1935
1936           /* 
1937            * By observation of NetBench, oplocks are broken *before* share
1938            * modes are checked. This allows a file to be closed by the client
1939            * if the share mode would deny access and the client has an oplock. 
1940            * Check if someone has an oplock on this file. If so we must break 
1941            * it before continuing. 
1942            */
1943           if(share_entry->op_type & (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
1944           {
1945
1946             DEBUG(5,("open_file_shared: breaking oplock (%x) on file %s, \
1947 dev = %x, inode = %x\n", share_entry->op_type, fname, dev, inode));
1948
1949             /* Oplock break.... */
1950             unlock_share_entry(conn, dev, inode, token);
1951             if(request_oplock_break(share_entry, dev, inode) == False)
1952             {
1953               free((char *)old_shares);
1954               DEBUG(0,("open_file_shared: FAILED when breaking oplock (%x) on file %s, \
1955 dev = %x, inode = %x\n", old_shares[i].op_type, fname, dev, inode));
1956               errno = EACCES;
1957               unix_ERR_class = ERRDOS;
1958               unix_ERR_code = ERRbadshare;
1959               return;
1960             }
1961             lock_share_entry(conn, dev, inode, &token);
1962             broke_oplock = True;
1963             break;
1964           }
1965
1966           /* someone else has a share lock on it, check to see 
1967              if we can too */
1968           if(check_share_mode(share_entry, deny_mode, fname, fcbopen, &flags) == False)
1969           {
1970             free((char *)old_shares);
1971             unlock_share_entry(conn, dev, inode, token);
1972             errno = EACCES;
1973             unix_ERR_class = ERRDOS;
1974             unix_ERR_code = ERRbadshare;
1975             return;
1976           }
1977
1978         } /* end for */
1979
1980         if(broke_oplock)
1981         {
1982           free((char *)old_shares);
1983           num_share_modes = get_share_modes(conn, token, dev, inode, &old_shares);
1984         }
1985       } while(broke_oplock);
1986     }
1987
1988     if(old_shares != 0)
1989       free((char *)old_shares);
1990   }
1991
1992   DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o\n",
1993            flags,flags2,mode));
1994
1995   open_file(fnum,conn,fname,flags|(flags2&~(O_TRUNC)),mode,file_existed ? &sbuf : 0);
1996   if (!fs_p->open && flags==O_RDWR && errno!=ENOENT && fcbopen) 
1997   {
1998     flags = O_RDONLY;
1999     open_file(fnum,conn,fname,flags,mode,file_existed ? &sbuf : 0 );
2000   }
2001
2002   if (fs_p->open) 
2003   {
2004     int open_mode=0;
2005
2006     if((share_locked == False) && lp_share_modes(SNUM(conn)))
2007     {
2008       /* We created the file - thus we must now lock the share entry before creating it. */
2009       dev = fs_p->fd_ptr->dev;
2010       inode = fs_p->fd_ptr->inode;
2011       lock_share_entry(conn, dev, inode, &token);
2012       share_locked = True;
2013     }
2014
2015     switch (flags) 
2016     {
2017       case O_RDONLY:
2018         open_mode = 0;
2019         break;
2020       case O_RDWR:
2021         open_mode = 2;
2022         break;
2023       case O_WRONLY:
2024         open_mode = 1;
2025         break;
2026     }
2027
2028     fs_p->share_mode = (deny_mode<<4) | open_mode;
2029
2030     if (Access)
2031       (*Access) = open_mode;
2032
2033     if (action) 
2034     {
2035       if (file_existed && !(flags2 & O_TRUNC)) *action = FILE_WAS_OPENED;
2036       if (!file_existed) *action = FILE_WAS_CREATED;
2037       if (file_existed && (flags2 & O_TRUNC)) *action = FILE_WAS_OVERWRITTEN;
2038     }
2039     /* We must create the share mode entry before truncate as
2040        truncate can fail due to locking and have to close the
2041        file (which expects the share_mode_entry to be there).
2042      */
2043     if (lp_share_modes(SNUM(conn)))
2044     {
2045       uint16 port = 0;
2046       /* JRA. Currently this only services Exlcusive and batch
2047          oplocks (no other opens on this file). This needs to
2048          be extended to level II oplocks (multiple reader
2049          oplocks). */
2050
2051       if(oplock_request && (num_share_modes == 0) && lp_oplocks(SNUM(conn)) && 
2052               !IS_VETO_OPLOCK_PATH(conn,fname))
2053       {
2054         fs_p->granted_oplock = True;
2055         fs_p->sent_oplock_break = False;
2056         global_oplocks_open++;
2057         port = oplock_port;
2058
2059         DEBUG(5,("open_file_shared: granted oplock (%x) on file %s, \
2060 dev = %x, inode = %x\n", oplock_request, fname, dev, inode));
2061
2062       }
2063       else
2064       {
2065         port = 0;
2066         oplock_request = 0;
2067       }
2068       set_share_mode(token, fnum, port, oplock_request);
2069     }
2070
2071     if ((flags2&O_TRUNC) && file_existed)
2072       truncate_unless_locked(fnum,conn,token,&share_locked);
2073   }
2074
2075   if (share_locked && lp_share_modes(SNUM(conn)))
2076     unlock_share_entry( conn, dev, inode, token);
2077 }
2078
2079 /****************************************************************************
2080 seek a file. Try to avoid the seek if possible
2081 ****************************************************************************/
2082 int seek_file(int fnum,uint32 pos)
2083 {
2084   uint32 offset = 0;
2085   files_struct *fsp = &Files[fnum];
2086
2087   if (fsp->print_file && lp_postscript(fsp->conn->service))
2088     offset = 3;
2089
2090   fsp->pos = (int)(lseek(fsp->fd_ptr->fd,pos+offset,SEEK_SET) - offset);
2091   return(fsp->pos);
2092 }
2093
2094 /****************************************************************************
2095 read from a file
2096 ****************************************************************************/
2097 int read_file(int fnum,char *data,uint32 pos,int n)
2098 {
2099   int ret=0,readret;
2100   files_struct *fsp = &Files[fnum];
2101
2102 #if USE_READ_PREDICTION
2103   if (!fsp->can_write)
2104     {
2105       ret = read_predict(fsp->fd_ptr->fd,pos,data,NULL,n);
2106
2107       data += ret;
2108       n -= ret;
2109       pos += ret;
2110     }
2111 #endif
2112
2113 #if WITH_MMAP
2114   if (fsp->mmap_ptr)
2115     {
2116       int num = (fsp->mmap_size > pos) ? (fsp->mmap_size - pos) : -1;
2117       num = MIN(n,num);
2118       if (num > 0)
2119         {
2120           memcpy(data,fsp->mmap_ptr+pos,num);
2121           data += num;
2122           pos += num;
2123           n -= num;
2124           ret += num;
2125         }
2126     }
2127 #endif
2128
2129   if (n <= 0)
2130     return(ret);
2131
2132   if (seek_file(fnum,pos) != pos)
2133     {
2134       DEBUG(3,("Failed to seek to %d\n",pos));
2135       return(ret);
2136     }
2137   
2138   if (n > 0) {
2139     readret = read(fsp->fd_ptr->fd,data,n);
2140     if (readret > 0) ret += readret;
2141   }
2142
2143   return(ret);
2144 }
2145
2146
2147 /****************************************************************************
2148 write to a file
2149 ****************************************************************************/
2150 int write_file(int fnum,char *data,int n)
2151 {
2152   files_struct *fsp = &Files[fnum];
2153
2154   if (!fsp->can_write) {
2155     errno = EPERM;
2156     return(0);
2157   }
2158
2159   if (!fsp->modified) {
2160     struct stat st;
2161     fsp->modified = True;
2162     if (fstat(fsp->fd_ptr->fd,&st) == 0) {
2163       int dosmode = dos_mode(fsp->conn,fsp->fsp_name,&st);
2164       if (MAP_ARCHIVE(fsp->conn) && !IS_DOS_ARCHIVE(dosmode)) { 
2165         dos_chmod(fsp->conn,fsp->fsp_name,dosmode | aARCH,&st);
2166       }
2167     }  
2168   }
2169
2170   return(write_data(fsp->fd_ptr->fd,data,n));
2171 }
2172
2173
2174 /****************************************************************************
2175 load parameters specific to a connection/service
2176 ****************************************************************************/
2177 BOOL become_service(connection_struct *conn,BOOL do_chdir)
2178 {
2179         extern char magic_char;
2180         static connection_struct *last_conn;
2181         int snum;
2182
2183         if (!conn || !conn->open)  {
2184                 last_conn = NULL;
2185                 return(False);
2186         }
2187
2188         conn->lastused = smb_last_time;
2189
2190         snum = SNUM(conn);
2191   
2192         if (do_chdir &&
2193             ChDir(conn->connectpath) != 0 &&
2194             ChDir(conn->origpath) != 0) {
2195                 DEBUG(0,("chdir (%s) failed\n",
2196                          conn->connectpath));
2197                 return(False);
2198         }
2199
2200         if (conn == last_conn)
2201                 return(True);
2202
2203         last_conn = conn;
2204
2205         case_default = lp_defaultcase(snum);
2206         case_preserve = lp_preservecase(snum);
2207         short_case_preserve = lp_shortpreservecase(snum);
2208         case_mangle = lp_casemangle(snum);
2209         case_sensitive = lp_casesensitive(snum);
2210         magic_char = lp_magicchar(snum);
2211         use_mangled_map = (*lp_mangled_map(snum) ? True:False);
2212         return(True);
2213 }
2214
2215
2216 /****************************************************************************
2217   find a service entry
2218 ****************************************************************************/
2219 int find_service(char *service)
2220 {
2221    int iService;
2222
2223    string_sub(service,"\\","/");
2224
2225    iService = lp_servicenumber(service);
2226
2227    /* now handle the special case of a home directory */
2228    if (iService < 0)
2229    {
2230       char *phome_dir = get_home_dir(service);
2231
2232       if(!phome_dir)
2233       {
2234         /*
2235          * Try mapping the servicename, it may
2236          * be a Windows to unix mapped user name.
2237          */
2238         if(map_username(service))
2239           phome_dir = get_home_dir(service);
2240       }
2241
2242       DEBUG(3,("checking for home directory %s gave %s\n",service,
2243             phome_dir?phome_dir:"(NULL)"));
2244
2245       if (phome_dir)
2246       {   
2247         int iHomeService;
2248         if ((iHomeService = lp_servicenumber(HOMES_NAME)) >= 0)
2249         {
2250           lp_add_home(service,iHomeService,phome_dir);
2251           iService = lp_servicenumber(service);
2252         }
2253       }
2254    }
2255
2256    /* If we still don't have a service, attempt to add it as a printer. */
2257    if (iService < 0)
2258    {
2259       int iPrinterService;
2260
2261       if ((iPrinterService = lp_servicenumber(PRINTERS_NAME)) >= 0)
2262       {
2263          char *pszTemp;
2264
2265          DEBUG(3,("checking whether %s is a valid printer name...\n", service));
2266          pszTemp = PRINTCAP;
2267          if ((pszTemp != NULL) && pcap_printername_ok(service, pszTemp))
2268          {
2269             DEBUG(3,("%s is a valid printer name\n", service));
2270             DEBUG(3,("adding %s as a printer service\n", service));
2271             lp_add_printer(service,iPrinterService);
2272             iService = lp_servicenumber(service);
2273             if (iService < 0)
2274                DEBUG(0,("failed to add %s as a printer service!\n", service));
2275          }
2276          else
2277             DEBUG(3,("%s is not a valid printer name\n", service));
2278       }
2279    }
2280
2281    /* just possibly it's a default service? */
2282    if (iService < 0) 
2283    {
2284      char *pdefservice = lp_defaultservice();
2285      if (pdefservice && *pdefservice && !strequal(pdefservice,service))
2286      {
2287        /*
2288         * We need to do a local copy here as lp_defaultservice() 
2289         * returns one of the rotating lp_string buffers that
2290         * could get overwritten by the recursive find_service() call
2291         * below. Fix from Josef Hinteregger <joehtg@joehtg.co.at>.
2292         */
2293        pstring defservice;
2294        pstrcpy(defservice, pdefservice);
2295        iService = find_service(defservice);
2296        if (iService >= 0)
2297        {
2298          string_sub(service,"_","/");
2299          iService = lp_add_service(service,iService);
2300        }
2301      }
2302    }
2303
2304    if (iService >= 0)
2305      if (!VALID_SNUM(iService))
2306      {
2307        DEBUG(0,("Invalid snum %d for %s\n",iService,service));
2308        iService = -1;
2309      }
2310
2311    if (iService < 0)
2312      DEBUG(3,("find_service() failed to find service %s\n", service));
2313
2314    return (iService);
2315 }
2316
2317
2318 /****************************************************************************
2319   create an error packet from a cached error.
2320 ****************************************************************************/
2321 int cached_error_packet(char *inbuf,char *outbuf,int fnum,int line)
2322 {
2323   write_bmpx_struct *wbmpx = Files[fnum].wbmpx_ptr;
2324
2325   int32 eclass = wbmpx->wr_errclass;
2326   int32 err = wbmpx->wr_error;
2327
2328   /* We can now delete the auxiliary struct */
2329   free((char *)wbmpx);
2330   Files[fnum].wbmpx_ptr = NULL;
2331   return error_packet(inbuf,outbuf,eclass,err,line);
2332 }
2333
2334
2335 struct
2336 {
2337   int unixerror;
2338   int smbclass;
2339   int smbcode;
2340 } unix_smb_errmap[] =
2341 {
2342   {EPERM,ERRDOS,ERRnoaccess},
2343   {EACCES,ERRDOS,ERRnoaccess},
2344   {ENOENT,ERRDOS,ERRbadfile},
2345   {ENOTDIR,ERRDOS,ERRbadpath},
2346   {EIO,ERRHRD,ERRgeneral},
2347   {EBADF,ERRSRV,ERRsrverror},
2348   {EINVAL,ERRSRV,ERRsrverror},
2349   {EEXIST,ERRDOS,ERRfilexists},
2350   {ENFILE,ERRDOS,ERRnofids},
2351   {EMFILE,ERRDOS,ERRnofids},
2352   {ENOSPC,ERRHRD,ERRdiskfull},
2353 #ifdef EDQUOT
2354   {EDQUOT,ERRHRD,ERRdiskfull},
2355 #endif
2356 #ifdef ENOTEMPTY
2357   {ENOTEMPTY,ERRDOS,ERRnoaccess},
2358 #endif
2359 #ifdef EXDEV
2360   {EXDEV,ERRDOS,ERRdiffdevice},
2361 #endif
2362   {EROFS,ERRHRD,ERRnowrite},
2363   {0,0,0}
2364 };
2365
2366 /****************************************************************************
2367   create an error packet from errno
2368 ****************************************************************************/
2369 int unix_error_packet(char *inbuf,char *outbuf,int def_class,uint32 def_code,int line)
2370 {
2371   int eclass=def_class;
2372   int ecode=def_code;
2373   int i=0;
2374
2375   if (unix_ERR_class != SMB_SUCCESS)
2376     {
2377       eclass = unix_ERR_class;
2378       ecode = unix_ERR_code;
2379       unix_ERR_class = SMB_SUCCESS;
2380       unix_ERR_code = 0;
2381     }
2382   else
2383     {
2384       while (unix_smb_errmap[i].smbclass != 0)
2385       {
2386             if (unix_smb_errmap[i].unixerror == errno)
2387             {
2388               eclass = unix_smb_errmap[i].smbclass;
2389               ecode = unix_smb_errmap[i].smbcode;
2390               break;
2391             }
2392           i++;
2393       }
2394     }
2395
2396   return(error_packet(inbuf,outbuf,eclass,ecode,line));
2397 }
2398
2399
2400 /****************************************************************************
2401   create an error packet. Normally called using the ERROR() macro
2402 ****************************************************************************/
2403 int error_packet(char *inbuf,char *outbuf,int error_class,uint32 error_code,int line)
2404 {
2405   int outsize = set_message(outbuf,0,0,True);
2406   int cmd = CVAL(inbuf,smb_com);
2407   int flgs2 = SVAL(outbuf,smb_flg2); 
2408
2409   if ((flgs2 & FLAGS2_32_BIT_ERROR_CODES) == FLAGS2_32_BIT_ERROR_CODES)
2410   {
2411     SIVAL(outbuf,smb_rcls,error_code);
2412     
2413     DEBUG( 3, ( "32 bit error packet at line %d cmd=%d (%s) eclass=%08x [%s]\n",
2414               line, cmd, smb_fn_name(cmd), error_code, smb_errstr(outbuf) ) );
2415   }
2416   else
2417   {
2418     CVAL(outbuf,smb_rcls) = error_class;
2419     SSVAL(outbuf,smb_err,error_code);  
2420     DEBUG( 3, ( "error packet at line %d cmd=%d (%s) eclass=%d ecode=%d\n",
2421               line,
2422               (int)CVAL(inbuf,smb_com),
2423               smb_fn_name(CVAL(inbuf,smb_com)),
2424               error_class,
2425               error_code ) );
2426
2427   }
2428   
2429   if (errno != 0)
2430     DEBUG(3,("error string = %s\n",strerror(errno)));
2431   
2432   return(outsize);
2433 }
2434
2435
2436 /****************************************************************************
2437   this is called when the client exits abruptly
2438   **************************************************************************/
2439 static void sig_pipe(int sig)
2440 {
2441         struct cli_state *cli;
2442         BlockSignals(True,SIGPIPE);
2443
2444         if ((cli = server_client()) && cli->initialised) {
2445                 DEBUG(3,("lost connection to password server\n"));
2446                 cli_shutdown(cli);
2447                 BlockSignals(False,SIGPIPE);
2448                 return;
2449         }
2450
2451         exit_server("Got sigpipe\n");
2452 }
2453
2454 /****************************************************************************
2455   open the socket communication
2456 ****************************************************************************/
2457 static BOOL open_sockets(BOOL is_daemon,int port)
2458 {
2459   extern int Client;
2460
2461   if (is_daemon)
2462   {
2463     int num_interfaces = iface_count();
2464     int fd_listenset[FD_SETSIZE];
2465     fd_set listen_set;
2466     int s;
2467     int i;
2468
2469 #ifdef HAVE_ATEXIT
2470     static int atexit_set;
2471     if(atexit_set == 0) {
2472             atexit_set=1;
2473             atexit(killkids);
2474     }
2475 #endif
2476
2477     /* Stop zombies */
2478     CatchChild();
2479
2480
2481     FD_ZERO(&listen_set);
2482
2483     if(lp_interfaces() && lp_bind_interfaces_only())
2484     {
2485        /* We have been given an interfaces line, and been 
2486           told to only bind to those interfaces. Create a
2487           socket per interface and bind to only these.
2488         */
2489
2490       if(num_interfaces > FD_SETSIZE)
2491       {
2492         DEBUG(0,("open_sockets: Too many interfaces specified to bind to. Number was %d \
2493 max can be %d\n", num_interfaces, FD_SETSIZE));
2494         return False;
2495       }
2496
2497       /* Now open a listen socket for each of the interfaces. */
2498       for(i = 0; i < num_interfaces; i++)
2499       {
2500         struct in_addr *ifip = iface_n_ip(i);
2501
2502         if(ifip == NULL)
2503         {
2504           DEBUG(0,("open_sockets: interface %d has NULL IP address !\n", i));
2505           continue;
2506         }
2507         s = fd_listenset[i] = open_socket_in(SOCK_STREAM, port, 0, ifip->s_addr);
2508         if(s == -1)
2509           return False;
2510         /* ready to listen */
2511         if (listen(s, 5) == -1) 
2512         {
2513           DEBUG(0,("listen: %s\n",strerror(errno)));
2514           close(s);
2515           return False;
2516         }
2517         FD_SET(s,&listen_set);
2518       }
2519     }
2520     else
2521     {
2522       /* Just bind to 0.0.0.0 - accept connections from anywhere. */
2523       num_interfaces = 1;
2524
2525       /* open an incoming socket */
2526       s = open_socket_in(SOCK_STREAM, port, 0,interpret_addr(lp_socket_address()));
2527       if (s == -1)
2528         return(False);
2529
2530       /* ready to listen */
2531       if (listen(s, 5) == -1) 
2532       {
2533         DEBUG(0,("open_sockets: listen: %s\n",strerror(errno)));
2534         close(s);
2535         return False;
2536       }
2537
2538       fd_listenset[0] = s;
2539       FD_SET(s,&listen_set);
2540     }      
2541
2542     /* now accept incoming connections - forking a new process
2543        for each incoming connection */
2544     DEBUG(2,("waiting for a connection\n"));
2545     while (1)
2546     {
2547       fd_set lfds;
2548       int num;
2549
2550       memcpy((char *)&lfds, (char *)&listen_set, sizeof(listen_set));
2551
2552       num = sys_select(&lfds,NULL);
2553
2554       if (num == -1 && errno == EINTR)
2555         continue;
2556
2557       /* Find the sockets that are read-ready - accept on these. */
2558       for( ; num > 0; num--)
2559       {
2560         struct sockaddr addr;
2561         int in_addrlen = sizeof(addr);
2562
2563         s = -1;
2564         for(i = 0; i < num_interfaces; i++)
2565         {
2566           if(FD_ISSET(fd_listenset[i],&lfds))
2567           {
2568             s = fd_listenset[i];
2569             /* Clear this so we don't look at it again. */
2570             FD_CLR(fd_listenset[i],&lfds);
2571             break;
2572           }
2573         }
2574
2575         Client = accept(s,&addr,&in_addrlen);
2576
2577         if (Client == -1 && errno == EINTR)
2578           continue;
2579
2580         if (Client == -1)
2581         {
2582           DEBUG(0,("open_sockets: accept: %s\n",strerror(errno)));
2583           continue;
2584         }
2585
2586         if (Client != -1 && fork()==0)
2587         {
2588           /* Child code ... */
2589
2590           CatchSignal(SIGPIPE, SIGNAL_CAST sig_pipe);
2591
2592           /* close the listening socket(s) */
2593           for(i = 0; i < num_interfaces; i++)
2594             close(fd_listenset[i]);
2595
2596           /* close our standard file descriptors */
2597           close_low_fds();
2598           am_parent = 0;
2599   
2600           set_socket_options(Client,"SO_KEEPALIVE");
2601           set_socket_options(Client,user_socket_options);
2602
2603           /* Reset global variables in util.c so that
2604              client substitutions will be done correctly
2605              in the process.
2606            */
2607           reset_globals_after_fork();
2608           return True; 
2609         }
2610         close(Client); /* The parent doesn't need this socket */
2611
2612         /*
2613          * Force parent to check log size after spawning child.
2614          * Fix from klausr@ITAP.Physik.Uni-Stuttgart.De.
2615          * The parent smbd will log to logserver.smb. 
2616          * It writes only two messages for each child
2617          * started/finished. But each child writes, say, 50 messages also in
2618          * logserver.smb, begining with the debug_count of the parent, before the
2619          * child opens its own log file logserver.client. In a worst case
2620          * scenario the size of logserver.smb would be checked after about
2621          * 50*50=2500 messages (ca. 100kb).
2622          */
2623         force_check_log_size();
2624  
2625       } /* end for num */
2626     } /* end while 1 */
2627   } /* end if is_daemon */
2628   else
2629   {
2630     /* Started from inetd. fd 0 is the socket. */
2631     /* We will abort gracefully when the client or remote system 
2632        goes away */
2633     CatchSignal(SIGPIPE, SIGNAL_CAST sig_pipe);
2634     Client = dup(0);
2635
2636     /* close our standard file descriptors */
2637     close_low_fds();
2638
2639     set_socket_options(Client,"SO_KEEPALIVE");
2640     set_socket_options(Client,user_socket_options);
2641   }
2642
2643   return True;
2644 }
2645
2646 /****************************************************************************
2647   process an smb from the client - split out from the process() code so
2648   it can be used by the oplock break code.
2649 ****************************************************************************/
2650
2651 static void process_smb(char *inbuf, char *outbuf)
2652 {
2653   extern int Client;
2654 #ifdef WITH_SSL
2655   extern BOOL sslEnabled;     /* don't use function for performance reasons */
2656   static int sslConnected = 0;
2657 #endif /* WITH_SSL */
2658   static int trans_num;
2659   int msg_type = CVAL(inbuf,0);
2660   int32 len = smb_len(inbuf);
2661   int nread = len + 4;
2662
2663   if (trans_num == 0) {
2664           /* on the first packet, check the global hosts allow/ hosts
2665              deny parameters before doing any parsing of the packet
2666              passed to us by the client.  This prevents attacks on our
2667              parsing code from hosts not in the hosts allow list */
2668           if (!check_access(Client, lp_hostsallow(-1), lp_hostsdeny(-1))) {
2669                   /* send a negative session response "not listining on calling
2670                    name" */
2671                   static unsigned char buf[5] = {0x83, 0, 0, 1, 0x81};
2672                   DEBUG( 1, ( "Connection denied from %s\n",
2673                               client_addr(Client) ) );
2674                   send_smb(Client,(char *)buf);
2675                   exit_server("connection denied");
2676           }
2677   }
2678
2679   DEBUG( 6, ( "got message type 0x%x of len 0x%x\n", msg_type, len ) );
2680   DEBUG( 3, ( "Transaction %d of length %d\n", trans_num, nread ) );
2681
2682 #ifdef WITH_SSL
2683     if(sslEnabled && !sslConnected){
2684         sslConnected = sslutil_negotiate_ssl(Client, msg_type);
2685         if(sslConnected < 0){   /* an error occured */
2686             exit_server("SSL negotiation failed");
2687         }else if(sslConnected){
2688             trans_num++;
2689             return;
2690         }
2691     }
2692 #endif  /* WITH_SSL */
2693
2694 #ifdef WITH_VTP
2695   if(trans_num == 1 && VT_Check(inbuf)) 
2696   {
2697     VT_Process();
2698     return;
2699   }
2700 #endif
2701
2702   if (msg_type == 0)
2703     show_msg(inbuf);
2704   else if(msg_type == 0x85)
2705     return; /* Keepalive packet. */
2706
2707   nread = construct_reply(inbuf,outbuf,nread,max_send);
2708       
2709   if(nread > 0) 
2710   {
2711     if (CVAL(outbuf,0) == 0)
2712       show_msg(outbuf);
2713         
2714     if (nread != smb_len(outbuf) + 4) 
2715     {
2716       DEBUG(0,("ERROR: Invalid message response size! %d %d\n",
2717                  nread, smb_len(outbuf)));
2718     }
2719     else
2720       send_smb(Client,outbuf);
2721   }
2722   trans_num++;
2723 }
2724
2725 /****************************************************************************
2726   open the oplock IPC socket communication
2727 ****************************************************************************/
2728 static BOOL open_oplock_ipc(void)
2729 {
2730   struct sockaddr_in sock_name;
2731   int len = sizeof(sock_name);
2732
2733   DEBUG(3,("open_oplock_ipc: opening loopback UDP socket.\n"));
2734
2735   /* Open a lookback UDP socket on a random port. */
2736   oplock_sock = open_socket_in(SOCK_DGRAM, 0, 0, htonl(INADDR_LOOPBACK));
2737   if (oplock_sock == -1)
2738   {
2739     DEBUG(0,("open_oplock_ipc: Failed to get local UDP socket for \
2740 address %x. Error was %s\n", htonl(INADDR_LOOPBACK), strerror(errno)));
2741     oplock_port = 0;
2742     return(False);
2743   }
2744
2745   /* Find out the transient UDP port we have been allocated. */
2746   if(getsockname(oplock_sock, (struct sockaddr *)&sock_name, &len)<0)
2747   {
2748     DEBUG(0,("open_oplock_ipc: Failed to get local UDP port. Error was %s\n",
2749             strerror(errno)));
2750     close(oplock_sock);
2751     oplock_sock = -1;
2752     oplock_port = 0;
2753     return False;
2754   }
2755   oplock_port = ntohs(sock_name.sin_port);
2756
2757   DEBUG(3,("open_oplock ipc: pid = %d, oplock_port = %u\n", 
2758             (int)getpid(), oplock_port));
2759
2760   return True;
2761 }
2762
2763 /****************************************************************************
2764   process an oplock break message.
2765 ****************************************************************************/
2766 static BOOL process_local_message(int sock, char *buffer, int buf_size)
2767 {
2768   int32 msg_len;
2769   uint16 from_port;
2770   char *msg_start;
2771
2772   msg_len = IVAL(buffer,UDP_CMD_LEN_OFFSET);
2773   from_port = SVAL(buffer,UDP_CMD_PORT_OFFSET);
2774
2775   msg_start = &buffer[UDP_CMD_HEADER_LEN];
2776
2777   DEBUG(5,("process_local_message: Got a message of length %d from port (%d)\n", 
2778             msg_len, from_port));
2779
2780   /* Switch on message command - currently OPLOCK_BREAK_CMD is the
2781      only valid request. */
2782
2783   switch(SVAL(msg_start,UDP_MESSAGE_CMD_OFFSET))
2784   {
2785     case OPLOCK_BREAK_CMD:
2786       /* Ensure that the msg length is correct. */
2787       if(msg_len != OPLOCK_BREAK_MSG_LEN)
2788       {
2789         DEBUG(0,("process_local_message: incorrect length for OPLOCK_BREAK_CMD (was %d, \
2790 should be %d).\n", msg_len, OPLOCK_BREAK_MSG_LEN));
2791         return False;
2792       }
2793       {
2794         uint32 remotepid = IVAL(msg_start,OPLOCK_BREAK_PID_OFFSET);
2795         uint32 dev = IVAL(msg_start,OPLOCK_BREAK_DEV_OFFSET);
2796         uint32 inode = IVAL(msg_start, OPLOCK_BREAK_INODE_OFFSET);
2797         struct timeval tval;
2798         struct sockaddr_in toaddr;
2799
2800         tval.tv_sec = IVAL(msg_start, OPLOCK_BREAK_SEC_OFFSET);
2801         tval.tv_usec = IVAL(msg_start, OPLOCK_BREAK_USEC_OFFSET);
2802
2803         DEBUG(5,("process_local_message: oplock break request from \
2804 pid %d, port %d, dev = %x, inode = %x\n", remotepid, from_port, dev, inode));
2805
2806         /*
2807          * If we have no record of any currently open oplocks,
2808          * it's not an error, as a close command may have
2809          * just been issued on the file that was oplocked.
2810          * Just return success in this case.
2811          */
2812
2813         if(global_oplocks_open != 0)
2814         {
2815           if(oplock_break(dev, inode, &tval) == False)
2816           {
2817             DEBUG(0,("process_local_message: oplock break failed - \
2818 not returning udp message.\n"));
2819             return False;
2820           }
2821         }
2822         else
2823         {
2824           DEBUG(3,("process_local_message: oplock break requested with no outstanding \
2825 oplocks. Returning success.\n"));
2826         }
2827
2828         /* Send the message back after OR'ing in the 'REPLY' bit. */
2829         SSVAL(msg_start,UDP_MESSAGE_CMD_OFFSET,OPLOCK_BREAK_CMD | CMD_REPLY);
2830   
2831         bzero((char *)&toaddr,sizeof(toaddr));
2832         toaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2833         toaddr.sin_port = htons(from_port);
2834         toaddr.sin_family = AF_INET;
2835
2836         if(sendto( sock, msg_start, OPLOCK_BREAK_MSG_LEN, 0,
2837                 (struct sockaddr *)&toaddr, sizeof(toaddr)) < 0) 
2838         {
2839           DEBUG(0,("process_local_message: sendto process %d failed. Errno was %s\n",
2840                     remotepid, strerror(errno)));
2841           return False;
2842         }
2843
2844         DEBUG(5,("process_local_message: oplock break reply sent to \
2845 pid %d, port %d, for file dev = %x, inode = %x\n", remotepid, 
2846                 from_port, dev, inode));
2847
2848       }
2849       break;
2850     /* 
2851      * Keep this as a debug case - eventually we can remove it.
2852      */
2853     case 0x8001:
2854       DEBUG(0,("process_local_message: Received unsolicited break \
2855 reply - dumping info.\n"));
2856
2857       if(msg_len != OPLOCK_BREAK_MSG_LEN)
2858       {
2859         DEBUG(0,("process_local_message: ubr: incorrect length for reply \
2860 (was %d, should be %d).\n", msg_len, OPLOCK_BREAK_MSG_LEN));
2861         return False;
2862       }
2863
2864       {
2865         uint32 remotepid = IVAL(msg_start,OPLOCK_BREAK_PID_OFFSET);
2866         uint32 dev = IVAL(msg_start,OPLOCK_BREAK_DEV_OFFSET);
2867         uint32 inode = IVAL(msg_start, OPLOCK_BREAK_INODE_OFFSET);
2868
2869         DEBUG(0,("process_local_message: unsolicited oplock break reply from \
2870 pid %d, port %d, dev = %x, inode = %x\n", remotepid, from_port, dev, inode));
2871
2872        }
2873        return False;
2874
2875     default:
2876       DEBUG(0,("process_local_message: unknown UDP message command code (%x) - ignoring.\n",
2877                 (unsigned int)SVAL(msg_start,0)));
2878       return False;
2879   }
2880   return True;
2881 }
2882
2883 /****************************************************************************
2884  Process an oplock break directly.
2885 ****************************************************************************/
2886 BOOL oplock_break(uint32 dev, uint32 inode, struct timeval *tval)
2887 {
2888   extern struct current_user current_user;
2889   extern int Client;
2890   char *inbuf = NULL;
2891   char *outbuf = NULL;
2892   files_struct *fsp = NULL;
2893   int fnum;
2894   time_t start_time;
2895   BOOL shutdown_server = False;
2896   connection_struct *saved_conn;
2897   int saved_vuid;
2898   pstring saved_dir; 
2899
2900   if( DEBUGLVL( 3 ) )
2901     {
2902     dbgtext( "oplock_break: called for dev = %x, inode = %x.\n", dev, inode );
2903     dbgtext( "Current global_oplocks_open = %d\n", global_oplocks_open );
2904     }
2905
2906   /* We need to search the file open table for the
2907      entry containing this dev and inode, and ensure
2908      we have an oplock on it. */
2909   for( fnum = 0; fnum < MAX_FNUMS; fnum++)
2910   {
2911     if(OPEN_FNUM(fnum))
2912     {
2913       if((Files[fnum].fd_ptr->dev == dev) && (Files[fnum].fd_ptr->inode == inode) &&
2914          (Files[fnum].open_time.tv_sec == tval->tv_sec) && 
2915          (Files[fnum].open_time.tv_usec == tval->tv_usec)) {
2916               fsp = &Files[fnum];
2917               break;
2918       }
2919     }
2920   }
2921
2922   if(fsp == NULL)
2923   {
2924     /* The file could have been closed in the meantime - return success. */
2925     if( DEBUGLVL( 0 ) )
2926       {
2927       dbgtext( "oplock_break: cannot find open file with " );
2928       dbgtext( "dev = %x, inode = %x (fnum = %d) ", dev, inode, fnum );
2929       dbgtext( "allowing break to succeed.\n" );
2930       }
2931     return True;
2932   }
2933
2934   /* Ensure we have an oplock on the file */
2935
2936   /* There is a potential race condition in that an oplock could
2937      have been broken due to another udp request, and yet there are
2938      still oplock break messages being sent in the udp message
2939      queue for this file. So return true if we don't have an oplock,
2940      as we may have just freed it.
2941    */
2942
2943   if(!fsp->granted_oplock)
2944   {
2945     if( DEBUGLVL( 0 ) )
2946       {
2947       dbgtext( "oplock_break: file %s (fnum = %d, ", fsp->fsp_name, fnum );
2948       dbgtext( "dev = %x, inode = %x) has no oplock.\n", dev, inode );
2949       dbgtext( "Allowing break to succeed regardless.\n" );
2950       }
2951     return True;
2952   }
2953
2954   /* mark the oplock break as sent - we don't want to send twice! */
2955   if (fsp->sent_oplock_break)
2956   {
2957     if( DEBUGLVL( 0 ) )
2958       {
2959       dbgtext( "oplock_break: ERROR: oplock_break already sent for " );
2960       dbgtext( "file %s (fnum = %d, ", fsp->fsp_name, fnum );
2961       dbgtext( "dev = %x, inode = %x)\n", dev, inode );
2962       }
2963
2964     /* We have to fail the open here as we cannot send another oplock break on
2965        this file whilst we are awaiting a response from the client - neither
2966        can we allow another open to succeed while we are waiting for the
2967        client.
2968      */
2969     return False;
2970   }
2971
2972   /* Now comes the horrid part. We must send an oplock break to the client,
2973      and then process incoming messages until we get a close or oplock release.
2974      At this point we know we need a new inbuf/outbuf buffer pair.
2975      We cannot use these staticaly as we may recurse into here due to
2976      messages crossing on the wire.
2977    */
2978
2979   if((inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN))==NULL)
2980   {
2981     DEBUG(0,("oplock_break: malloc fail for input buffer.\n"));
2982     return False;
2983   }
2984
2985   if((outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN))==NULL)
2986   {
2987     DEBUG(0,("oplock_break: malloc fail for output buffer.\n"));
2988     free(inbuf);
2989     inbuf = NULL;
2990     return False;
2991   }
2992
2993   /* Prepare the SMBlockingX message. */
2994   bzero(outbuf,smb_size);
2995   set_message(outbuf,8,0,True);
2996
2997   SCVAL(outbuf,smb_com,SMBlockingX);
2998   SSVAL(outbuf,smb_tid,fsp->conn->cnum);
2999   SSVAL(outbuf,smb_pid,0xFFFF);
3000   SSVAL(outbuf,smb_uid,0);
3001   SSVAL(outbuf,smb_mid,0xFFFF);
3002   SCVAL(outbuf,smb_vwv0,0xFF);
3003   SSVAL(outbuf,smb_vwv2,fnum);
3004   SCVAL(outbuf,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
3005   /* Change this when we have level II oplocks. */
3006   SCVAL(outbuf,smb_vwv3+1,OPLOCKLEVEL_NONE);
3007  
3008   send_smb(Client, outbuf);
3009
3010   /* Remember we just sent an oplock break on this file. */
3011   fsp->sent_oplock_break = True;
3012
3013   /* We need this in case a readraw crosses on the wire. */
3014   global_oplock_break = True;
3015  
3016   /* Process incoming messages. */
3017
3018   /* JRA - If we don't get a break from the client in OPLOCK_BREAK_TIMEOUT
3019      seconds we should just die.... */
3020
3021   start_time = time(NULL);
3022
3023   /*
3024    * Save the information we need to re-become the
3025    * user, then unbecome the user whilst we're doing this.
3026    */
3027   saved_conn = fsp->conn;
3028   saved_vuid = current_user.vuid;
3029   GetWd(saved_dir);
3030   unbecome_user();
3031
3032   while(OPEN_FNUM(fnum) && fsp->granted_oplock)
3033   {
3034     if(receive_smb(Client,inbuf,OPLOCK_BREAK_TIMEOUT * 1000) == False)
3035     {
3036       /*
3037        * Die if we got an error.
3038        */
3039
3040       if (smb_read_error == READ_EOF)
3041         DEBUG( 0, ( "oplock_break: end of file from client\n" ) );
3042  
3043       if (smb_read_error == READ_ERROR)
3044         DEBUG( 0, ("oplock_break: receive_smb error (%s)\n", strerror(errno)) );
3045
3046       if (smb_read_error == READ_TIMEOUT)
3047         DEBUG( 0, ( "oplock_break: receive_smb timed out after %d seconds.\n",
3048                      OPLOCK_BREAK_TIMEOUT ) );
3049
3050       DEBUGADD( 0, ( "oplock_break failed for file %s ", fsp->fsp_name ) );
3051       DEBUGADD( 0, ( "(fnum = %d, dev = %x, inode = %x).\n", fnum, dev, inode));
3052       shutdown_server = True;
3053       break;
3054     }
3055
3056     /*
3057      * There are certain SMB requests that we shouldn't allow
3058      * to recurse. opens, renames and deletes are the obvious
3059      * ones. This is handled in the switch_message() function.
3060      * If global_oplock_break is set they will push the packet onto
3061      * the pending smb queue and return -1 (no reply).
3062      * JRA.
3063      */
3064
3065     process_smb(inbuf, outbuf);
3066
3067     /*
3068      * Die if we go over the time limit.
3069      */
3070
3071     if((time(NULL) - start_time) > OPLOCK_BREAK_TIMEOUT)
3072     {
3073       if( DEBUGLVL( 0 ) )
3074         {
3075         dbgtext( "oplock_break: no break received from client " );
3076         dbgtext( "within %d seconds.\n", OPLOCK_BREAK_TIMEOUT );
3077         dbgtext( "oplock_break failed for file %s ", fsp->fsp_name );
3078         dbgtext( "(fnum = %d, dev = %x, inode = %x).\n", fnum, dev, inode );
3079         }
3080       shutdown_server = True;
3081       break;
3082     }
3083   }
3084
3085   /*
3086    * Go back to being the user who requested the oplock
3087    * break.
3088    */
3089   if(!become_user(saved_conn, saved_vuid))
3090   {
3091     DEBUG( 0, ( "oplock_break: unable to re-become user!" ) );
3092     DEBUGADD( 0, ( "Shutting down server\n" ) );
3093     close_sockets();
3094     close(oplock_sock);
3095     exit_server("unable to re-become user");
3096   }
3097   /* Including the directory. */
3098   ChDir(saved_dir);
3099
3100   /* Free the buffers we've been using to recurse. */
3101   free(inbuf);
3102   free(outbuf);
3103
3104   /* We need this in case a readraw crossed on the wire. */
3105   if(global_oplock_break)
3106     global_oplock_break = False;
3107
3108   /*
3109    * If the client did not respond we must die.
3110    */
3111
3112   if(shutdown_server)
3113   {
3114     DEBUG( 0, ( "oplock_break: client failure in break - " ) );
3115     DEBUGADD( 0, ( "shutting down this smbd.\n" ) );
3116     close_sockets();
3117     close(oplock_sock);
3118     exit_server("oplock break failure");
3119   }
3120
3121   if(OPEN_FNUM(fnum))
3122   {
3123     /* The lockingX reply will have removed the oplock flag 
3124        from the sharemode. */
3125     /* Paranoia.... */
3126     fsp->granted_oplock = False;
3127     fsp->sent_oplock_break = False;
3128     global_oplocks_open--;
3129   }
3130
3131   /* Santity check - remove this later. JRA */
3132   if(global_oplocks_open < 0)
3133   {
3134     DEBUG(0,("oplock_break: global_oplocks_open < 0 (%d). PANIC ERROR\n",
3135               global_oplocks_open));
3136     exit_server("oplock_break: global_oplocks_open < 0");
3137   }
3138
3139   if( DEBUGLVL( 3 ) )
3140     {
3141     dbgtext( "oplock_break: returning success for " );
3142     dbgtext( "fnum = %d, dev = %x, inode = %x.\n", fnum, dev, inode );
3143     dbgtext( "Current global_oplocks_open = %d\n", global_oplocks_open );
3144     }
3145
3146   return True;
3147 }
3148
3149 /****************************************************************************
3150 Send an oplock break message to another smbd process. If the oplock is held 
3151 by the local smbd then call the oplock break function directly.
3152 ****************************************************************************/
3153
3154 BOOL request_oplock_break(share_mode_entry *share_entry, 
3155                           uint32 dev, uint32 inode)
3156 {
3157   char op_break_msg[OPLOCK_BREAK_MSG_LEN];
3158   struct sockaddr_in addr_out;
3159   int pid = getpid();
3160   time_t start_time;
3161   int time_left;
3162
3163   if(pid == share_entry->pid)
3164   {
3165     /* We are breaking our own oplock, make sure it's us. */
3166     if(share_entry->op_port != oplock_port)
3167     {
3168       DEBUG(0,("request_oplock_break: corrupt share mode entry - pid = %d, port = %d \
3169 should be %d\n", pid, share_entry->op_port, oplock_port));
3170       return False;
3171     }
3172
3173     DEBUG(5,("request_oplock_break: breaking our own oplock\n"));
3174
3175     /* Call oplock break direct. */
3176     return oplock_break(dev, inode, &share_entry->time);
3177   }
3178
3179   /* We need to send a OPLOCK_BREAK_CMD message to the
3180      port in the share mode entry. */
3181
3182   SSVAL(op_break_msg,UDP_MESSAGE_CMD_OFFSET,OPLOCK_BREAK_CMD);
3183   SIVAL(op_break_msg,OPLOCK_BREAK_PID_OFFSET,pid);
3184   SIVAL(op_break_msg,OPLOCK_BREAK_DEV_OFFSET,dev);
3185   SIVAL(op_break_msg,OPLOCK_BREAK_INODE_OFFSET,inode);
3186   SIVAL(op_break_msg,OPLOCK_BREAK_SEC_OFFSET,(uint32)share_entry->time.tv_sec);
3187   SIVAL(op_break_msg,OPLOCK_BREAK_USEC_OFFSET,(uint32)share_entry->time.tv_usec);
3188
3189   /* set the address and port */
3190   bzero((char *)&addr_out,sizeof(addr_out));
3191   addr_out.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
3192   addr_out.sin_port = htons( share_entry->op_port );
3193   addr_out.sin_family = AF_INET;
3194    
3195   if( DEBUGLVL( 3 ) )
3196     {
3197     dbgtext( "request_oplock_break: sending a oplock break message to " );
3198     dbgtext( "pid %d on port %d ", share_entry->pid, share_entry->op_port );
3199     dbgtext( "for dev = %x, inode = %x\n", dev, inode );
3200     }
3201
3202   if(sendto(oplock_sock,op_break_msg,OPLOCK_BREAK_MSG_LEN,0,
3203          (struct sockaddr *)&addr_out,sizeof(addr_out)) < 0)
3204   {
3205     if( DEBUGLVL( 0 ) )
3206       {
3207       dbgtext( "request_oplock_break: failed when sending a oplock " );
3208       dbgtext( "break message to pid %d ", share_entry->pid );
3209       dbgtext( "on port %d ", share_entry->op_port );
3210       dbgtext( "for dev = %x, inode = %x.\n", dev, inode );
3211       dbgtext( "Error was %s\n", strerror(errno) );
3212       }
3213     return False;
3214   }
3215
3216   /*
3217    * Now we must await the oplock broken message coming back
3218    * from the target smbd process. Timeout if it fails to
3219    * return in (OPLOCK_BREAK_TIMEOUT + OPLOCK_BREAK_TIMEOUT_FUDGEFACTOR) seconds.
3220    * While we get messages that aren't ours, loop.
3221    */
3222
3223   start_time = time(NULL);
3224   time_left = OPLOCK_BREAK_TIMEOUT+OPLOCK_BREAK_TIMEOUT_FUDGEFACTOR;
3225
3226   while(time_left >= 0)
3227   {
3228     char op_break_reply[UDP_CMD_HEADER_LEN+OPLOCK_BREAK_MSG_LEN];
3229     int32 reply_msg_len;
3230     uint16 reply_from_port;
3231     char *reply_msg_start;
3232
3233     if(receive_local_message(oplock_sock, op_break_reply, sizeof(op_break_reply),
3234                time_left ? time_left * 1000 : 1) == False)
3235     {
3236       if(smb_read_error == READ_TIMEOUT)
3237       {
3238         if( DEBUGLVL( 0 ) )
3239           {
3240           dbgtext( "request_oplock_break: no response received to oplock " );
3241           dbgtext( "break request to pid %d ", share_entry->pid );
3242           dbgtext( "on port %d ", share_entry->op_port );
3243           dbgtext( "for dev = %x, inode = %x\n", dev, inode );
3244           }
3245         /*
3246          * This is a hack to make handling of failing clients more robust.
3247          * If a oplock break response message is not received in the timeout
3248          * period we may assume that the smbd servicing that client holding
3249          * the oplock has died and the client changes were lost anyway, so
3250          * we should continue to try and open the file.
3251          */
3252         break;
3253       }
3254       else
3255         if( DEBUGLVL( 0 ) )
3256           {
3257           dbgtext( "request_oplock_break: error in response received " );
3258           dbgtext( "to oplock break request to pid %d ", share_entry->pid );
3259           dbgtext( "on port %d ", share_entry->op_port );
3260           dbgtext( "for dev = %x, inode = %x.\n", dev, inode );
3261           dbgtext( "Error was (%s).\n", strerror(errno) );
3262           }
3263       return False;
3264     }
3265
3266     reply_msg_len = IVAL(op_break_reply,UDP_CMD_LEN_OFFSET);
3267     reply_from_port = SVAL(op_break_reply,UDP_CMD_PORT_OFFSET);
3268
3269     reply_msg_start = &op_break_reply[UDP_CMD_HEADER_LEN];
3270
3271     if(reply_msg_len != OPLOCK_BREAK_MSG_LEN)
3272     {
3273       /* Ignore it. */
3274       DEBUG( 0, ( "request_oplock_break: invalid message length received." ) );
3275       DEBUGADD( 0, ( "  Ignoring.\n" ) );
3276       continue;
3277     }
3278
3279     /*
3280      * Test to see if this is the reply we are awaiting.
3281      */
3282
3283     if((SVAL(reply_msg_start,UDP_MESSAGE_CMD_OFFSET) & CMD_REPLY) &&
3284        (reply_from_port == share_entry->op_port) && 
3285        (memcmp(&reply_msg_start[OPLOCK_BREAK_PID_OFFSET], 
3286                &op_break_msg[OPLOCK_BREAK_PID_OFFSET],
3287                OPLOCK_BREAK_MSG_LEN - OPLOCK_BREAK_PID_OFFSET) == 0))
3288     {
3289       /*
3290        * This is the reply we've been waiting for.
3291        */
3292       break;
3293     }
3294     else
3295     {
3296       /*
3297        * This is another message - probably a break request.
3298        * Process it to prevent potential deadlock.
3299        * Note that the code in switch_message() prevents
3300        * us from recursing into here as any SMB requests
3301        * we might process that would cause another oplock
3302        * break request to be made will be queued.
3303        * JRA.
3304        */
3305
3306       process_local_message(oplock_sock, op_break_reply, sizeof(op_break_reply));
3307     }
3308
3309     time_left -= (time(NULL) - start_time);
3310   }
3311
3312   DEBUG(3,("request_oplock_break: broke oplock.\n"));
3313
3314   return True;
3315 }
3316
3317 /****************************************************************************
3318 Get the next SMB packet, doing the local message processing automatically.
3319 ****************************************************************************/
3320
3321 BOOL receive_next_smb(int smbfd, int oplockfd, char *inbuf, int bufsize, int timeout)
3322 {
3323   BOOL got_smb = False;
3324   BOOL ret;
3325
3326   do
3327   {
3328     ret = receive_message_or_smb(smbfd,oplockfd,inbuf,bufsize,
3329                                  timeout,&got_smb);
3330
3331     if(ret && !got_smb)
3332     {
3333       /* Deal with oplock break requests from other smbd's. */
3334       process_local_message(oplock_sock, inbuf, bufsize);
3335       continue;
3336     }
3337
3338     if(ret && (CVAL(inbuf,0) == 0x85))
3339     {
3340       /* Keepalive packet. */
3341       got_smb = False;
3342     }
3343
3344   }
3345   while(ret && !got_smb);
3346
3347   return ret;
3348 }
3349
3350 /****************************************************************************
3351 check if a snum is in use
3352 ****************************************************************************/
3353 BOOL snum_used(int snum)
3354 {
3355         int i;
3356         for (i=0;i<MAX_CONNECTIONS;i++) {
3357                 if (Connections[i].open && (Connections[i].service == snum)) {
3358                         return(True);
3359                 }
3360         }
3361         return(False);
3362 }
3363
3364 /****************************************************************************
3365   reload the services file
3366   **************************************************************************/
3367 BOOL reload_services(BOOL test)
3368 {
3369         BOOL ret;
3370
3371         if (lp_loaded()) {
3372                 pstring fname;
3373                 pstrcpy(fname,lp_configfile());
3374                 if (file_exist(fname,NULL) && !strcsequal(fname,servicesf)) {
3375                         pstrcpy(servicesf,fname);
3376                         test = False;
3377                 }
3378         }
3379
3380         reopen_logs();
3381
3382         if (test && !lp_file_list_changed())
3383                 return(True);
3384
3385         lp_killunused(snum_used);
3386
3387         ret = lp_load(servicesf,False,False,True);
3388
3389         load_printers();
3390
3391         /* perhaps the config filename is now set */
3392         if (!test)
3393                 reload_services(True);
3394
3395         reopen_logs();
3396
3397         load_interfaces();
3398
3399         {
3400                 extern int Client;
3401                 if (Client != -1) {      
3402                         set_socket_options(Client,"SO_KEEPALIVE");
3403                         set_socket_options(Client,user_socket_options);
3404                 }
3405         }
3406
3407         reset_mangled_cache();
3408
3409         /* this forces service parameters to be flushed */
3410         become_service(NULL,True);
3411
3412         return(ret);
3413 }
3414
3415
3416
3417 /****************************************************************************
3418 this prevents zombie child processes
3419 ****************************************************************************/
3420 static BOOL reload_after_sighup = False;
3421
3422 static void sig_hup(int sig)
3423 {
3424   BlockSignals(True,SIGHUP);
3425   DEBUG(0,("Got SIGHUP\n"));
3426
3427   /*
3428    * Fix from <branko.cibej@hermes.si> here.
3429    * We used to reload in the signal handler - this
3430    * is a *BIG* no-no.
3431    */
3432
3433   reload_after_sighup = True;
3434   BlockSignals(False,SIGHUP);
3435 }
3436
3437 /****************************************************************************
3438   find first available connection slot, starting from a random position.
3439 The randomisation stops problems with the server dieing and clients
3440 thinking the server is still available.
3441 ****************************************************************************/
3442 static connection_struct *find_free_connection(int hash)
3443 {
3444         int i;
3445         BOOL used=False;
3446         hash = (hash % (MAX_CONNECTIONS-2))+1;
3447
3448  again:
3449
3450         for (i=hash+1;i!=hash;) {
3451                 if (!Connections[i].open && Connections[i].used == used) {
3452                         DEBUG(3,("found free connection number %d\n",i));
3453                         memset(&Connections[i], 0, sizeof(&Connections[i]));
3454                         Connections[i].cnum = i;
3455                         return &Connections[i];
3456                 }
3457                 i++;
3458                 if (i == MAX_CONNECTIONS) {
3459                         i = 1;
3460                 }
3461         }
3462
3463         if (!used) {
3464                 used = !used;
3465                 goto again;
3466         }
3467
3468         DEBUG(1,("ERROR! Out of connection structures\n"));
3469
3470         return NULL;
3471 }
3472
3473
3474 /****************************************************************************
3475   make a connection to a service
3476 ****************************************************************************/
3477 connection_struct *make_connection(char *service,char *user,char *password, int pwlen, char *dev,uint16 vuid, int *ecode)
3478 {
3479         int snum;
3480         struct passwd *pass = NULL;
3481         BOOL guest = False;
3482         BOOL force = False;
3483         extern int Client;
3484         connection_struct *conn;
3485
3486         strlower(service);
3487
3488         snum = find_service(service);
3489         if (snum < 0) {
3490                 extern int Client;
3491                 if (strequal(service,"IPC$")) {
3492                         DEBUG(3,("refusing IPC connection\n"));
3493                         *ecode = ERRnoipc;
3494                         return NULL;
3495                 }
3496
3497                 DEBUG(0,("%s (%s) couldn't find service %s\n",
3498                          remote_machine, client_addr(Client), service));
3499                 *ecode = ERRinvnetname;
3500                 return NULL;
3501         }
3502
3503         if (strequal(service,HOMES_NAME)) {
3504                 if (*user && Get_Pwnam(user,True))
3505                         return(make_connection(user,user,password,
3506                                                pwlen,dev,vuid,ecode));
3507
3508                 if(lp_security() != SEC_SHARE) {
3509                         if (validated_username(vuid)) {
3510                                 pstrcpy(user,validated_username(vuid));
3511                                 return(make_connection(user,user,password,pwlen,dev,vuid,ecode));
3512                         }
3513                 } else {
3514                         /* Security = share. Try with sesssetup_user
3515                          * as the username.  */
3516                         if(*sesssetup_user) {
3517                                 pstrcpy(user,sesssetup_user);
3518                                 return(make_connection(user,user,password,pwlen,dev,vuid,ecode));
3519                         }
3520                 }
3521         }
3522
3523         if (!lp_snum_ok(snum) || 
3524             !check_access(Client, 
3525                           lp_hostsallow(snum), lp_hostsdeny(snum))) {    
3526                 *ecode = ERRaccess;
3527                 return NULL;
3528         }
3529
3530         /* you can only connect to the IPC$ service as an ipc device */
3531         if (strequal(service,"IPC$"))
3532                 pstrcpy(dev,"IPC");
3533         
3534         if (*dev == '?' || !*dev) {
3535                 if (lp_print_ok(snum)) {
3536                         pstrcpy(dev,"LPT1:");
3537                 } else {
3538                         pstrcpy(dev,"A:");
3539                 }
3540         }
3541
3542         /* if the request is as a printer and you can't print then refuse */
3543         strupper(dev);
3544         if (!lp_print_ok(snum) && (strncmp(dev,"LPT",3) == 0)) {
3545                 DEBUG(1,("Attempt to connect to non-printer as a printer\n"));
3546                 *ecode = ERRinvdevice;
3547                 return NULL;
3548         }
3549
3550         /* lowercase the user name */
3551         strlower(user);
3552
3553         /* add it as a possible user name */
3554         add_session_user(service);
3555
3556         /* shall we let them in? */
3557         if (!authorise_login(snum,user,password,pwlen,&guest,&force,vuid)) {
3558                 DEBUG( 2, ( "Invalid username/password for %s\n", service ) );
3559                 *ecode = ERRbadpw;
3560                 return NULL;
3561         }
3562   
3563         conn = find_free_connection(str_checksum(service) + str_checksum(user));
3564         if (!conn) {
3565                 DEBUG(0,("Couldn't find free connection.\n"));
3566                 *ecode = ERRnoresource;
3567                 return NULL;
3568         }
3569
3570         /* find out some info about the user */
3571         pass = Get_Pwnam(user,True);
3572
3573         if (pass == NULL) {
3574                 DEBUG(0,( "Couldn't find account %s\n",user));
3575                 *ecode = ERRbaduid;
3576                 return NULL;
3577         }
3578
3579         conn->read_only = lp_readonly(snum);
3580
3581         {
3582                 pstring list;
3583                 StrnCpy(list,lp_readlist(snum),sizeof(pstring)-1);
3584                 string_sub(list,"%S",service);
3585
3586                 if (user_in_list(user,list))
3587                         conn->read_only = True;
3588                 
3589                 StrnCpy(list,lp_writelist(snum),sizeof(pstring)-1);
3590                 string_sub(list,"%S",service);
3591                 
3592                 if (user_in_list(user,list))
3593                         conn->read_only = False;    
3594         }
3595
3596         /* admin user check */
3597         
3598         /* JRA - original code denied admin user if the share was
3599            marked read_only. Changed as I don't think this is needed,
3600            but old code left in case there is a problem here.
3601         */
3602         if (user_in_list(user,lp_admin_users(snum)) 
3603 #if 0
3604             && !conn->read_only
3605 #endif
3606             ) {
3607                 conn->admin_user = True;
3608                 DEBUG(0,("%s logged in as admin user (root privileges)\n",user));
3609         } else {
3610                 conn->admin_user = False;
3611         }
3612     
3613         conn->force_user = force;
3614         conn->vuid = vuid;
3615         conn->uid = pass->pw_uid;
3616         conn->gid = pass->pw_gid;
3617         conn->num_files_open = 0;
3618         conn->lastused = time(NULL);
3619         conn->service = snum;
3620         conn->used = True;
3621         conn->printer = (strncmp(dev,"LPT",3) == 0);
3622         conn->ipc = (strncmp(dev,"IPC",3) == 0);
3623         conn->dirptr = NULL;
3624         conn->veto_list = NULL;
3625         conn->hide_list = NULL;
3626         conn->veto_oplock_list = NULL;
3627         string_set(&conn->dirpath,"");
3628         string_set(&conn->user,user);
3629         
3630 #ifdef HAVE_GETGRNAM 
3631         if (*lp_force_group(snum)) {
3632                 struct group *gptr;
3633                 pstring gname;
3634                 
3635                 StrnCpy(gname,lp_force_group(snum),sizeof(pstring)-1);
3636                 /* default service may be a group name          */
3637                 string_sub(gname,"%S",service);
3638                 gptr = (struct group *)getgrnam(gname);
3639                 
3640                 if (gptr) {
3641                         conn->gid = gptr->gr_gid;
3642                         DEBUG(3,("Forced group %s\n",gname));
3643                 } else {
3644                         DEBUG(1,("Couldn't find group %s\n",gname));
3645                 }
3646         }
3647 #endif
3648         
3649         if (*lp_force_user(snum)) {
3650                 struct passwd *pass2;
3651                 fstring fuser;
3652                 fstrcpy(fuser,lp_force_user(snum));
3653                 pass2 = (struct passwd *)Get_Pwnam(fuser,True);
3654                 if (pass2) {
3655                         conn->uid = pass2->pw_uid;
3656                         string_set(&conn->user,fuser);
3657                         fstrcpy(user,fuser);
3658                         conn->force_user = True;
3659                         DEBUG(3,("Forced user %s\n",fuser));      
3660                 } else {
3661                         DEBUG(1,("Couldn't find user %s\n",fuser));
3662                 }
3663         }
3664
3665         {
3666                 pstring s;
3667                 pstrcpy(s,lp_pathname(snum));
3668                 standard_sub(conn,s);
3669                 string_set(&conn->connectpath,s);
3670                 DEBUG(3,("Connect path is %s\n",s));
3671         }
3672
3673         /* groups stuff added by ih */
3674         conn->ngroups = 0;
3675         conn->groups = NULL;
3676         
3677         if (!IS_IPC(conn)) {
3678                 /* Find all the groups this uid is in and
3679                    store them. Used by become_user() */
3680                 setup_groups(conn->user,conn->uid,conn->gid,
3681                              &conn->ngroups,&conn->groups);
3682                 
3683                 /* check number of connections */
3684                 if (!claim_connection(conn,
3685                                       lp_servicename(SNUM(conn)),
3686                                       lp_max_connections(SNUM(conn)),
3687                                       False)) {
3688                         DEBUG(1,("too many connections - rejected\n"));
3689                         *ecode = ERRnoresource;
3690                         return NULL;
3691                 }  
3692                 
3693                 if (lp_status(SNUM(conn)))
3694                         claim_connection(conn,"STATUS.",
3695                                          MAXSTATUS,False);
3696         } /* IS_IPC */
3697         
3698         conn->open = True;
3699         
3700         /* execute any "root preexec = " line */
3701         if (*lp_rootpreexec(SNUM(conn))) {
3702                 pstring cmd;
3703                 pstrcpy(cmd,lp_rootpreexec(SNUM(conn)));
3704                 standard_sub(conn,cmd);
3705                 DEBUG(5,("cmd=%s\n",cmd));
3706                 smbrun(cmd,NULL,False);
3707         }
3708         
3709         if (!become_user(conn, conn->vuid)) {
3710                 DEBUG(0,("Can't become connected user!\n"));
3711                 conn->open = False;
3712                 if (!IS_IPC(conn)) {
3713                         yield_connection(conn,
3714                                          lp_servicename(SNUM(conn)),
3715                                          lp_max_connections(SNUM(conn)));
3716                         if (lp_status(SNUM(conn))) {
3717                                 yield_connection(conn,"STATUS.",MAXSTATUS);
3718                         }
3719                 }
3720                 *ecode = ERRbadpw;
3721                 return NULL;
3722         }
3723         
3724         if (ChDir(conn->connectpath) != 0) {
3725                 DEBUG(0,("Can't change directory to %s (%s)\n",
3726                          conn->connectpath,strerror(errno)));
3727                 conn->open = False;
3728                 unbecome_user();
3729                 if (!IS_IPC(conn)) {
3730                         yield_connection(conn,
3731                                          lp_servicename(SNUM(conn)),
3732                                          lp_max_connections(SNUM(conn)));
3733                         if (lp_status(SNUM(conn))) 
3734                                 yield_connection(conn,"STATUS.",MAXSTATUS);
3735                 }
3736                 *ecode = ERRinvnetname;
3737                 return NULL;
3738         }
3739         
3740         string_set(&conn->origpath,conn->connectpath);
3741         
3742 #if SOFTLINK_OPTIMISATION
3743         /* resolve any soft links early */
3744         {
3745                 pstring s;
3746                 pstrcpy(s,conn->connectpath);
3747                 GetWd(s);
3748                 string_set(&conn->connectpath,s);
3749                 ChDir(conn->connectpath);
3750         }
3751 #endif
3752         
3753         num_connections_open++;
3754         add_session_user(user);
3755                 
3756         /* execute any "preexec = " line */
3757         if (*lp_preexec(SNUM(conn))) {
3758                 pstring cmd;
3759                 pstrcpy(cmd,lp_preexec(SNUM(conn)));
3760                 standard_sub(conn,cmd);
3761                 smbrun(cmd,NULL,False);
3762         }
3763         
3764         /* we've finished with the sensitive stuff */
3765         unbecome_user();
3766         
3767         /* Add veto/hide lists */
3768         if (!IS_IPC(conn) && !IS_PRINT(conn)) {
3769                 set_namearray( &conn->veto_list, lp_veto_files(SNUM(conn)));
3770                 set_namearray( &conn->hide_list, lp_hide_files(SNUM(conn)));
3771                 set_namearray( &conn->veto_oplock_list, lp_veto_oplocks(SNUM(conn)));
3772         }
3773         
3774         if( DEBUGLVL( IS_IPC(conn) ? 3 : 1 ) ) {
3775                 extern int Client;
3776                 
3777                 dbgtext( "%s (%s) ", remote_machine, client_addr(Client) );
3778                 dbgtext( "connect to service %s ", lp_servicename(SNUM(conn)) );
3779                 dbgtext( "as user %s ", user );
3780                 dbgtext( "(uid=%d, gid=%d) ", conn->uid, conn->gid );
3781                 dbgtext( "(pid %d)\n", (int)getpid() );
3782         }
3783         
3784         return(conn);
3785 }
3786
3787 /****************************************************************************
3788   Attempt to break an oplock on a file (if oplocked).
3789   Returns True if the file was closed as a result of
3790   the oplock break, False otherwise.
3791   Used as a last ditch attempt to free a space in the 
3792   file table when we have run out.
3793 ****************************************************************************/
3794
3795 static BOOL attempt_close_oplocked_file(files_struct *fsp)
3796 {
3797
3798   DEBUG(5,("attempt_close_oplocked_file: checking file %s.\n", fsp->fsp_name));
3799
3800   if (fsp->open && fsp->granted_oplock && !fsp->sent_oplock_break) {
3801
3802     /* Try and break the oplock. */
3803     file_fd_struct *fd_ptr = fsp->fd_ptr;
3804     if(oplock_break( fd_ptr->dev, fd_ptr->inode, &fsp->open_time)) {
3805       if(!fsp->open) /* Did the oplock break close the file ? */
3806         return True;
3807     }
3808   }
3809
3810   return False;
3811 }
3812
3813 /****************************************************************************
3814   find first available file slot
3815 ****************************************************************************/
3816 int find_free_file(void )
3817 {
3818         int i;
3819         static int first_file;
3820
3821         /* we want to give out file handles differently on each new
3822            connection because of a common bug in MS clients where they try to
3823            reuse a file descriptor from an earlier smb connection. This code
3824            increases the chance that the errant client will get an error rather
3825            than causing corruption */
3826         if (first_file == 0) {
3827                 first_file = (getpid() ^ (int)time(NULL)) % MAX_FNUMS;
3828                 if (first_file == 0) first_file = 1;
3829         }
3830
3831         if (first_file >= MAX_FNUMS)
3832                 first_file = 1;
3833
3834         for (i=first_file;i<MAX_FNUMS;i++)
3835                 if (!Files[i].open && !Files[i].reserved) {
3836                         memset(&Files[i], 0, sizeof(Files[i]));
3837                         first_file = i+1;
3838                         Files[i].reserved = True;
3839                         return(i);
3840                 }
3841
3842         /* returning a file handle of 0 is a bad idea - so we start at 1 */
3843         for (i=1;i<first_file;i++)
3844                 if (!Files[i].open && !Files[i].reserved) {
3845                         memset(&Files[i], 0, sizeof(Files[i]));
3846                         first_file = i+1;
3847                         Files[i].reserved = True;
3848                         return(i);
3849                 }
3850
3851         /* 
3852          * Before we give up, go through the open files 
3853          * and see if there are any files opened with a
3854          * batch oplock. If so break the oplock and then
3855          * re-use that entry (if it becomes closed).
3856          * This may help as NT/95 clients tend to keep
3857          * files batch oplocked for quite a long time
3858          * after they have finished with them.
3859          */
3860         for (i=first_file;i<MAX_FNUMS;i++) {
3861           if(attempt_close_oplocked_file( &Files[i])) {
3862             memset(&Files[i], 0, sizeof(Files[i]));
3863             first_file = i+1;
3864             Files[i].reserved = True;
3865             return(i);
3866           }
3867         }
3868
3869         for (i=1;i<MAX_FNUMS;i++) {
3870           if(attempt_close_oplocked_file( &Files[i])) {
3871             memset(&Files[i], 0, sizeof(Files[i]));
3872             first_file = i+1;
3873             Files[i].reserved = True;
3874             return(i);
3875           }
3876         }
3877
3878         DEBUG(1,("ERROR! Out of file structures - perhaps increase MAX_OPEN_FILES?\n"));
3879         return(-1);
3880 }
3881
3882
3883 /****************************************************************************
3884 reply for the core protocol
3885 ****************************************************************************/
3886 int reply_corep(char *outbuf)
3887 {
3888   int outsize = set_message(outbuf,1,0,True);
3889
3890   Protocol = PROTOCOL_CORE;
3891
3892   return outsize;
3893 }
3894
3895
3896 /****************************************************************************
3897 reply for the coreplus protocol
3898 ****************************************************************************/
3899 int reply_coreplus(char *outbuf)
3900 {
3901   int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
3902   int outsize = set_message(outbuf,13,0,True);
3903   SSVAL(outbuf,smb_vwv5,raw); /* tell redirector we support
3904                                  readbraw and writebraw (possibly) */
3905   CVAL(outbuf,smb_flg) = 0x81; /* Reply, SMBlockread, SMBwritelock supported */
3906   SSVAL(outbuf,smb_vwv1,0x1); /* user level security, don't encrypt */  
3907
3908   Protocol = PROTOCOL_COREPLUS;
3909
3910   return outsize;
3911 }
3912
3913
3914 /****************************************************************************
3915 reply for the lanman 1.0 protocol
3916 ****************************************************************************/
3917 int reply_lanman1(char *outbuf)
3918 {
3919   int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
3920   int secword=0;
3921   BOOL doencrypt = SMBENCRYPT();
3922   time_t t = time(NULL);
3923
3924   if (lp_security()>=SEC_USER) secword |= 1;
3925   if (doencrypt) secword |= 2;
3926
3927   set_message(outbuf,13,doencrypt?8:0,True);
3928   SSVAL(outbuf,smb_vwv1,secword); 
3929   /* Create a token value and add it to the outgoing packet. */
3930   if (doencrypt) 
3931     generate_next_challenge(smb_buf(outbuf));
3932
3933   Protocol = PROTOCOL_LANMAN1;
3934
3935   CVAL(outbuf,smb_flg) = 0x81; /* Reply, SMBlockread, SMBwritelock supported */
3936   SSVAL(outbuf,smb_vwv2,max_recv);
3937   SSVAL(outbuf,smb_vwv3,lp_maxmux()); /* maxmux */
3938   SSVAL(outbuf,smb_vwv4,1);
3939   SSVAL(outbuf,smb_vwv5,raw); /* tell redirector we support
3940                                  readbraw writebraw (possibly) */
3941   SIVAL(outbuf,smb_vwv6,getpid());
3942   SSVAL(outbuf,smb_vwv10, TimeDiff(t)/60);
3943
3944   put_dos_date(outbuf,smb_vwv8,t);
3945
3946   return (smb_len(outbuf)+4);
3947 }
3948
3949
3950 /****************************************************************************
3951 reply for the lanman 2.0 protocol
3952 ****************************************************************************/
3953 int reply_lanman2(char *outbuf)
3954 {
3955   int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
3956   int secword=0;
3957   BOOL doencrypt = SMBENCRYPT();
3958   time_t t = time(NULL);
3959   struct cli_state *cli = NULL;
3960   char cryptkey[8];
3961   char crypt_len = 0;
3962
3963   if (lp_security() == SEC_SERVER) {
3964           cli = server_cryptkey();
3965   }
3966
3967   if (cli) {
3968           DEBUG(3,("using password server validation\n"));
3969           doencrypt = ((cli->sec_mode & 2) != 0);
3970   }
3971
3972   if (lp_security()>=SEC_USER) secword |= 1;
3973   if (doencrypt) secword |= 2;
3974
3975   if (doencrypt) {
3976           crypt_len = 8;
3977           if (!cli) {
3978                   generate_next_challenge(cryptkey);
3979           } else {
3980                   memcpy(cryptkey, cli->cryptkey, 8);
3981                   set_challenge(cli->cryptkey);
3982           }
3983   }
3984
3985   set_message(outbuf,13,crypt_len,True);
3986   SSVAL(outbuf,smb_vwv1,secword); 
3987   SIVAL(outbuf,smb_vwv6,getpid());
3988   if (doencrypt) 
3989           memcpy(smb_buf(outbuf), cryptkey, 8);
3990
3991   Protocol = PROTOCOL_LANMAN2;
3992
3993   CVAL(outbuf,smb_flg) = 0x81; /* Reply, SMBlockread, SMBwritelock supported */
3994   SSVAL(outbuf,smb_vwv2,max_recv);
3995   SSVAL(outbuf,smb_vwv3,lp_maxmux()); 
3996   SSVAL(outbuf,smb_vwv4,1);
3997   SSVAL(outbuf,smb_vwv5,raw); /* readbraw and/or writebraw */
3998   SSVAL(outbuf,smb_vwv10, TimeDiff(t)/60);
3999   put_dos_date(outbuf,smb_vwv8,t);
4000
4001   return (smb_len(outbuf)+4);
4002 }
4003
4004
4005 /****************************************************************************
4006 reply for the nt protocol
4007 ****************************************************************************/
4008 int reply_nt1(char *outbuf)
4009 {
4010   /* dual names + lock_and_read + nt SMBs + remote API calls */
4011   int capabilities = CAP_NT_FIND|CAP_LOCK_AND_READ|CAP_RPC_REMOTE_APIS |CAP_NT_SMBS;
4012
4013 /*
4014   other valid capabilities which we may support at some time...
4015                      CAP_LARGE_FILES|
4016                      CAP_LARGE_READX|CAP_STATUS32|CAP_LEVEL_II_OPLOCKS;
4017  */
4018
4019   int secword=0;
4020   BOOL doencrypt = SMBENCRYPT();
4021   time_t t = time(NULL);
4022   int data_len;
4023   struct cli_state *cli = NULL;
4024   char cryptkey[8];
4025   char crypt_len = 0;
4026
4027   if (lp_security() == SEC_SERVER) {
4028           cli = server_cryptkey();
4029   }
4030
4031   if (cli) {
4032           DEBUG(3,("using password server validation\n"));
4033           doencrypt = ((cli->sec_mode & 2) != 0);
4034   }
4035
4036   if (doencrypt) {
4037           crypt_len = 8;
4038           if (!cli) {
4039                   generate_next_challenge(cryptkey);
4040           } else {
4041                   memcpy(cryptkey, cli->cryptkey, 8);
4042                   set_challenge(cli->cryptkey);
4043           }
4044   }
4045
4046   if (lp_readraw() && lp_writeraw()) {
4047           capabilities |= CAP_RAW_MODE;
4048   }
4049
4050   if (lp_security() >= SEC_USER) secword |= 1;
4051   if (doencrypt) secword |= 2;
4052
4053   /* decide where (if) to put the encryption challenge, and
4054      follow it with the OEM'd domain name
4055    */
4056   data_len = crypt_len + strlen(global_myworkgroup) + 1;
4057
4058   set_message(outbuf,17,data_len,True);
4059   pstrcpy(smb_buf(outbuf)+crypt_len, global_myworkgroup);
4060
4061   CVAL(outbuf,smb_vwv1) = secword;
4062   SSVALS(outbuf,smb_vwv16+1,crypt_len);
4063   if (doencrypt) 
4064           memcpy(smb_buf(outbuf), cryptkey, 8);
4065
4066   Protocol = PROTOCOL_NT1;
4067
4068   SSVAL(outbuf,smb_vwv1+1,lp_maxmux()); /* maxmpx */
4069   SSVAL(outbuf,smb_vwv2+1,1); /* num vcs */
4070   SIVAL(outbuf,smb_vwv3+1,0xffff); /* max buffer. LOTS! */
4071   SIVAL(outbuf,smb_vwv5+1,0x10000); /* raw size. full 64k */
4072   SIVAL(outbuf,smb_vwv7+1,getpid()); /* session key */
4073   SIVAL(outbuf,smb_vwv9+1,capabilities); /* capabilities */
4074   put_long_date(outbuf+smb_vwv11+1,t);
4075   SSVALS(outbuf,smb_vwv15+1,TimeDiff(t)/60);
4076   SSVAL(outbuf,smb_vwv17,data_len); /* length of challenge+domain strings */
4077
4078   return (smb_len(outbuf)+4);
4079 }
4080
4081 /* these are the protocol lists used for auto architecture detection:
4082
4083 WinNT 3.51:
4084 protocol [PC NETWORK PROGRAM 1.0]
4085 protocol [XENIX CORE]
4086 protocol [MICROSOFT NETWORKS 1.03]
4087 protocol [LANMAN1.0]
4088 protocol [Windows for Workgroups 3.1a]
4089 protocol [LM1.2X002]
4090 protocol [LANMAN2.1]
4091 protocol [NT LM 0.12]
4092
4093 Win95:
4094 protocol [PC NETWORK PROGRAM 1.0]
4095 protocol [XENIX CORE]
4096 protocol [MICROSOFT NETWORKS 1.03]
4097 protocol [LANMAN1.0]
4098 protocol [Windows for Workgroups 3.1a]
4099 protocol [LM1.2X002]
4100 protocol [LANMAN2.1]
4101 protocol [NT LM 0.12]
4102
4103 OS/2:
4104 protocol [PC NETWORK PROGRAM 1.0]
4105 protocol [XENIX CORE]
4106 protocol [LANMAN1.0]
4107 protocol [LM1.2X002]
4108 protocol [LANMAN2.1]
4109 */
4110
4111 /*
4112   * Modified to recognize the architecture of the remote machine better.
4113   *
4114   * This appears to be the matrix of which protocol is used by which
4115   * MS product.
4116        Protocol                       WfWg    Win95   WinNT  OS/2
4117        PC NETWORK PROGRAM 1.0          1       1       1      1
4118        XENIX CORE                                      2      2
4119        MICROSOFT NETWORKS 3.0          2       2       
4120        DOS LM1.2X002                   3       3       
4121        MICROSOFT NETWORKS 1.03                         3
4122        DOS LANMAN2.1                   4       4       
4123        LANMAN1.0                                       4      3
4124        Windows for Workgroups 3.1a     5       5       5
4125        LM1.2X002                                       6      4
4126        LANMAN2.1                                       7      5
4127        NT LM 0.12                              6       8
4128   *
4129   *  tim@fsg.com 09/29/95
4130   */
4131   
4132 #define ARCH_WFWG     0x3      /* This is a fudge because WfWg is like Win95 */
4133 #define ARCH_WIN95    0x2
4134 #define ARCH_OS2      0xC      /* Again OS/2 is like NT */
4135 #define ARCH_WINNT    0x8
4136 #define ARCH_SAMBA    0x10
4137  
4138 #define ARCH_ALL      0x1F
4139  
4140 /* List of supported protocols, most desired first */
4141 struct {
4142   char *proto_name;
4143   char *short_name;
4144   int (*proto_reply_fn)(char *);
4145   int protocol_level;
4146 } supported_protocols[] = {
4147   {"NT LANMAN 1.0",           "NT1",      reply_nt1,      PROTOCOL_NT1},
4148   {"NT LM 0.12",              "NT1",      reply_nt1,      PROTOCOL_NT1},
4149   {"LM1.2X002",               "LANMAN2",  reply_lanman2,  PROTOCOL_LANMAN2},
4150   {"Samba",                   "LANMAN2",  reply_lanman2,  PROTOCOL_LANMAN2},
4151   {"DOS LM1.2X002",           "LANMAN2",  reply_lanman2,  PROTOCOL_LANMAN2},
4152   {"LANMAN1.0",               "LANMAN1",  reply_lanman1,  PROTOCOL_LANMAN1},
4153   {"MICROSOFT NETWORKS 3.0",  "LANMAN1",  reply_lanman1,  PROTOCOL_LANMAN1},
4154   {"MICROSOFT NETWORKS 1.03", "COREPLUS", reply_coreplus, PROTOCOL_COREPLUS},
4155   {"PC NETWORK PROGRAM 1.0",  "CORE",     reply_corep,    PROTOCOL_CORE}, 
4156   {NULL,NULL},
4157 };
4158
4159
4160 /****************************************************************************
4161   reply to a negprot
4162 ****************************************************************************/
4163 static int reply_negprot(connection_struct *conn, 
4164                          char *inbuf,char *outbuf, int dum_size, 
4165                          int dum_buffsize)
4166 {
4167   int outsize = set_message(outbuf,1,0,True);
4168   int Index=0;
4169   int choice= -1;
4170   int protocol;
4171   char *p;
4172   int bcc = SVAL(smb_buf(inbuf),-2);
4173   int arch = ARCH_ALL;
4174
4175   p = smb_buf(inbuf)+1;
4176   while (p < (smb_buf(inbuf) + bcc))
4177     { 
4178       Index++;
4179       DEBUG(3,("Requested protocol [%s]\n",p));
4180       if (strcsequal(p,"Windows for Workgroups 3.1a"))
4181         arch &= ( ARCH_WFWG | ARCH_WIN95 | ARCH_WINNT );
4182       else if (strcsequal(p,"DOS LM1.2X002"))
4183         arch &= ( ARCH_WFWG | ARCH_WIN95 );
4184       else if (strcsequal(p,"DOS LANMAN2.1"))
4185         arch &= ( ARCH_WFWG | ARCH_WIN95 );
4186       else if (strcsequal(p,"NT LM 0.12"))
4187         arch &= ( ARCH_WIN95 | ARCH_WINNT );
4188       else if (strcsequal(p,"LANMAN2.1"))
4189         arch &= ( ARCH_WINNT | ARCH_OS2 );
4190       else if (strcsequal(p,"LM1.2X002"))
4191         arch &= ( ARCH_WINNT | ARCH_OS2 );
4192       else if (strcsequal(p,"MICROSOFT NETWORKS 1.03"))
4193         arch &= ARCH_WINNT;
4194       else if (strcsequal(p,"XENIX CORE"))
4195         arch &= ( ARCH_WINNT | ARCH_OS2 );
4196       else if (strcsequal(p,"Samba")) {
4197         arch = ARCH_SAMBA;
4198         break;
4199       }
4200  
4201       p += strlen(p) + 2;
4202     }
4203     
4204   switch ( arch ) {
4205   case ARCH_SAMBA:
4206     set_remote_arch(RA_SAMBA);
4207     break;
4208   case ARCH_WFWG:
4209     set_remote_arch(RA_WFWG);
4210     break;
4211   case ARCH_WIN95:
4212     set_remote_arch(RA_WIN95);
4213     break;
4214   case ARCH_WINNT:
4215     set_remote_arch(RA_WINNT);
4216     break;
4217   case ARCH_OS2:
4218     set_remote_arch(RA_OS2);
4219     break;
4220   default:
4221     set_remote_arch(RA_UNKNOWN);
4222     break;
4223   }
4224  
4225   /* possibly reload - change of architecture */
4226   reload_services(True);      
4227     
4228   /* a special case to stop password server loops */
4229   if (Index == 1 && strequal(remote_machine,myhostname) && 
4230       (lp_security()==SEC_SERVER || lp_security()==SEC_DOMAIN))
4231     exit_server("Password server loop!");
4232   
4233   /* Check for protocols, most desirable first */
4234   for (protocol = 0; supported_protocols[protocol].proto_name; protocol++)
4235     {
4236       p = smb_buf(inbuf)+1;
4237       Index = 0;
4238       if (lp_maxprotocol() >= supported_protocols[protocol].protocol_level)
4239         while (p < (smb_buf(inbuf) + bcc))
4240           { 
4241             if (strequal(p,supported_protocols[protocol].proto_name))
4242               choice = Index;
4243             Index++;
4244             p += strlen(p) + 2;
4245           }
4246       if(choice != -1)
4247         break;
4248     }
4249   
4250   SSVAL(outbuf,smb_vwv0,choice);
4251   if(choice != -1) {
4252     extern fstring remote_proto;
4253     fstrcpy(remote_proto,supported_protocols[protocol].short_name);
4254     reload_services(True);          
4255     outsize = supported_protocols[protocol].proto_reply_fn(outbuf);
4256     DEBUG(3,("Selected protocol %s\n",supported_protocols[protocol].proto_name));
4257   }
4258   else {
4259     DEBUG(0,("No protocol supported !\n"));
4260   }
4261   SSVAL(outbuf,smb_vwv0,choice);
4262   
4263   DEBUG( 5, ( "negprot index=%d\n", choice ) );
4264
4265   return(outsize);
4266 }
4267
4268
4269 /****************************************************************************
4270 close all open files for a connection
4271 ****************************************************************************/
4272 static void close_open_files(connection_struct *conn)
4273 {
4274   int i;
4275   for (i=0;i<MAX_FNUMS;i++)
4276     if (Files[i].conn == conn && Files[i].open) {
4277       if(Files[i].is_directory)
4278         close_directory(i); 
4279       else                  
4280         close_file(i,False); 
4281     }
4282 }
4283
4284
4285
4286 /****************************************************************************
4287 close a cnum
4288 ****************************************************************************/
4289 void close_cnum(connection_struct *conn, uint16 vuid)
4290 {
4291         extern int Client;
4292         DirCacheFlush(SNUM(conn));
4293
4294         unbecome_user();
4295
4296         if (!conn->open) {
4297                 DEBUG(0,("cnum not open\n"));
4298                 return;
4299         }
4300
4301         DEBUG(IS_IPC(conn)?3:1, ("%s (%s) closed connection to service %s\n",
4302                                  remote_machine,client_addr(Client),
4303                                  lp_servicename(SNUM(conn))));
4304
4305         yield_connection(conn,
4306                          lp_servicename(SNUM(conn)),
4307                          lp_max_connections(SNUM(conn)));
4308
4309         if (lp_status(SNUM(conn)))
4310                 yield_connection(conn,"STATUS.",MAXSTATUS);
4311
4312         close_open_files(conn);
4313         dptr_closecnum(conn);
4314
4315         /* execute any "postexec = " line */
4316         if (*lp_postexec(SNUM(conn)) && 
4317             become_user(conn, vuid))  {
4318                 pstring cmd;
4319                 pstrcpy(cmd,lp_postexec(SNUM(conn)));
4320                 standard_sub(conn,cmd);
4321                 smbrun(cmd,NULL,False);
4322                 unbecome_user();
4323         }
4324
4325         unbecome_user();
4326         /* execute any "root postexec = " line */
4327         if (*lp_rootpostexec(SNUM(conn)))  {
4328                 pstring cmd;
4329                 pstrcpy(cmd,lp_rootpostexec(SNUM(conn)));
4330                 standard_sub(conn,cmd);
4331                 smbrun(cmd,NULL,False);
4332         }
4333         
4334         conn->open = False;
4335         num_connections_open--;
4336         if (conn->ngroups && conn->groups) {
4337                 free(conn->groups);
4338                 conn->groups = NULL;
4339                 conn->ngroups = 0;
4340         }
4341
4342         free_namearray(conn->veto_list);
4343         free_namearray(conn->hide_list);
4344         free_namearray(conn->veto_oplock_list);
4345         
4346         string_set(&conn->user,"");
4347         string_set(&conn->dirpath,"");
4348         string_set(&conn->connectpath,"");
4349 }
4350
4351
4352
4353 #if DUMP_CORE
4354 /*******************************************************************
4355 prepare to dump a core file - carefully!
4356 ********************************************************************/
4357 static BOOL dump_core(void)
4358 {
4359   char *p;
4360   pstring dname;
4361   pstrcpy(dname,debugf);
4362   if ((p=strrchr(dname,'/'))) *p=0;
4363   pstrcat(dname,"/corefiles");
4364   mkdir(dname,0700);
4365   sys_chown(dname,getuid(),getgid());
4366   chmod(dname,0700);
4367   if (chdir(dname)) return(False);
4368   umask(~(0700));
4369
4370 #ifdef HAVE_GETRLIMIT
4371 #ifdef RLIMIT_CORE
4372   {
4373     struct rlimit rlp;
4374     getrlimit(RLIMIT_CORE, &rlp);
4375     rlp.rlim_cur = MAX(4*1024*1024,rlp.rlim_cur);
4376     setrlimit(RLIMIT_CORE, &rlp);
4377     getrlimit(RLIMIT_CORE, &rlp);
4378     DEBUG(3,("Core limits now %d %d\n",(int)rlp.rlim_cur,(int)rlp.rlim_max));
4379   }
4380 #endif
4381 #endif
4382
4383
4384   DEBUG(0,("Dumping core in %s\n",dname));
4385   abort();
4386   return(True);
4387 }
4388 #endif
4389
4390 /****************************************************************************
4391 exit the server
4392 ****************************************************************************/
4393 void exit_server(char *reason)
4394 {
4395   static int firsttime=1;
4396   int i;
4397
4398   if (!firsttime) exit(0);
4399   firsttime = 0;
4400
4401   unbecome_user();
4402   DEBUG(2,("Closing connections\n"));
4403   for (i=0;i<MAX_CONNECTIONS;i++)
4404     if (Connections[i].open)
4405       close_cnum(&Connections[i],(uint16)-1);
4406 #ifdef WITH_DFS
4407   if (dcelogin_atmost_once) {
4408     dfs_unlogin();
4409   }
4410 #endif
4411   if (!reason) {   
4412     int oldlevel = DEBUGLEVEL;
4413     DEBUGLEVEL = 10;
4414     DEBUG(0,("Last message was %s\n",smb_fn_name(last_message)));
4415     if (last_inbuf)
4416       show_msg(last_inbuf);
4417     DEBUGLEVEL = oldlevel;
4418     DEBUG(0,("===============================================================\n"));
4419 #if DUMP_CORE
4420     if (dump_core()) return;
4421 #endif
4422   }    
4423
4424   locking_end();
4425
4426   DEBUG( 3, ( "Server exit (%s)\n", (reason ? reason : "") ) );
4427   exit(0);
4428 }
4429
4430 /*
4431 These flags determine some of the permissions required to do an operation 
4432
4433 Note that I don't set NEED_WRITE on some write operations because they
4434 are used by some brain-dead clients when printing, and I don't want to
4435 force write permissions on print services.
4436 */
4437 #define AS_USER (1<<0)
4438 #define NEED_WRITE (1<<1)
4439 #define TIME_INIT (1<<2)
4440 #define CAN_IPC (1<<3)
4441 #define AS_GUEST (1<<5)
4442 #define QUEUE_IN_OPLOCK (1<<6)
4443
4444 /* 
4445    define a list of possible SMB messages and their corresponding
4446    functions. Any message that has a NULL function is unimplemented -
4447    please feel free to contribute implementations!
4448 */
4449 struct smb_message_struct
4450 {
4451   int code;
4452   char *name;
4453   int (*fn)(connection_struct *conn, char *, char *, int, int);
4454   int flags;
4455 #if PROFILING
4456   unsigned long time;
4457 #endif
4458 }
4459  smb_messages[] = {
4460
4461     /* CORE PROTOCOL */
4462
4463    {SMBnegprot,"SMBnegprot",reply_negprot,0},
4464    {SMBtcon,"SMBtcon",reply_tcon,0},
4465    {SMBtdis,"SMBtdis",reply_tdis,0},
4466    {SMBexit,"SMBexit",reply_exit,0},
4467    {SMBioctl,"SMBioctl",reply_ioctl,0},
4468    {SMBecho,"SMBecho",reply_echo,0},
4469    {SMBsesssetupX,"SMBsesssetupX",reply_sesssetup_and_X,0},
4470    {SMBtconX,"SMBtconX",reply_tcon_and_X,0},
4471    {SMBulogoffX, "SMBulogoffX", reply_ulogoffX, 0}, /* ulogoff doesn't give a valid TID */
4472    {SMBgetatr,"SMBgetatr",reply_getatr,AS_USER},
4473    {SMBsetatr,"SMBsetatr",reply_setatr,AS_USER | NEED_WRITE},
4474    {SMBchkpth,"SMBchkpth",reply_chkpth,AS_USER},
4475    {SMBsearch,"SMBsearch",reply_search,AS_USER},
4476    {SMBopen,"SMBopen",reply_open,AS_USER | QUEUE_IN_OPLOCK },
4477
4478    /* note that SMBmknew and SMBcreate are deliberately overloaded */   
4479    {SMBcreate,"SMBcreate",reply_mknew,AS_USER},
4480    {SMBmknew,"SMBmknew",reply_mknew,AS_USER}, 
4481
4482    {SMBunlink,"SMBunlink",reply_unlink,AS_USER | NEED_WRITE | QUEUE_IN_OPLOCK},
4483    {SMBread,"SMBread",reply_read,AS_USER},
4484    {SMBwrite,"SMBwrite",reply_write,AS_USER},
4485    {SMBclose,"SMBclose",reply_close,AS_USER | CAN_IPC},
4486    {SMBmkdir,"SMBmkdir",reply_mkdir,AS_USER | NEED_WRITE},
4487    {SMBrmdir,"SMBrmdir",reply_rmdir,AS_USER | NEED_WRITE},
4488    {SMBdskattr,"SMBdskattr",reply_dskattr,AS_USER},
4489    {SMBmv,"SMBmv",reply_mv,AS_USER | NEED_WRITE | QUEUE_IN_OPLOCK},
4490
4491    /* this is a Pathworks specific call, allowing the 
4492       changing of the root path */
4493    {pSETDIR,"pSETDIR",reply_setdir,AS_USER}, 
4494
4495    {SMBlseek,"SMBlseek",reply_lseek,AS_USER},
4496    {SMBflush,"SMBflush",reply_flush,AS_USER},
4497    {SMBctemp,"SMBctemp",reply_ctemp,AS_USER | QUEUE_IN_OPLOCK },
4498    {SMBsplopen,"SMBsplopen",reply_printopen,AS_USER | QUEUE_IN_OPLOCK },
4499    {SMBsplclose,"SMBsplclose",reply_printclose,AS_USER},
4500    {SMBsplretq,"SMBsplretq",reply_printqueue,AS_USER},
4501    {SMBsplwr,"SMBsplwr",reply_printwrite,AS_USER},
4502    {SMBlock,"SMBlock",reply_lock,AS_USER},
4503    {SMBunlock,"SMBunlock",reply_unlock,AS_USER},
4504    
4505    /* CORE+ PROTOCOL FOLLOWS */
4506    
4507    {SMBreadbraw,"SMBreadbraw",reply_readbraw,AS_USER},
4508    {SMBwritebraw,"SMBwritebraw",reply_writebraw,AS_USER},
4509    {SMBwriteclose,"SMBwriteclose",reply_writeclose,AS_USER},
4510    {SMBlockread,"SMBlockread",reply_lockread,AS_USER},
4511    {SMBwriteunlock,"SMBwriteunlock",reply_writeunlock,AS_USER},
4512    
4513    /* LANMAN1.0 PROTOCOL FOLLOWS */
4514    
4515    {SMBreadBmpx,"SMBreadBmpx",reply_readbmpx,AS_USER},
4516    {SMBreadBs,"SMBreadBs",NULL,AS_USER},
4517    {SMBwriteBmpx,"SMBwriteBmpx",reply_writebmpx,AS_USER},
4518    {SMBwriteBs,"SMBwriteBs",reply_writebs,AS_USER},
4519    {SMBwritec,"SMBwritec",NULL,AS_USER},
4520    {SMBsetattrE,"SMBsetattrE",reply_setattrE,AS_USER | NEED_WRITE},
4521    {SMBgetattrE,"SMBgetattrE",reply_getattrE,AS_USER},
4522    {SMBtrans,"SMBtrans",reply_trans,AS_USER | CAN_IPC},
4523    {SMBtranss,"SMBtranss",NULL,AS_USER | CAN_IPC},
4524    {SMBioctls,"SMBioctls",NULL,AS_USER},
4525    {SMBcopy,"SMBcopy",reply_copy,AS_USER | NEED_WRITE | QUEUE_IN_OPLOCK },
4526    {SMBmove,"SMBmove",NULL,AS_USER | NEED_WRITE | QUEUE_IN_OPLOCK },
4527    
4528    {SMBopenX,"SMBopenX",reply_open_and_X,AS_USER | CAN_IPC | QUEUE_IN_OPLOCK },
4529    {SMBreadX,"SMBreadX",reply_read_and_X,AS_USER | CAN_IPC },
4530    {SMBwriteX,"SMBwriteX",reply_write_and_X,AS_USER},
4531    {SMBlockingX,"SMBlockingX",reply_lockingX,AS_USER},
4532    
4533    {SMBffirst,"SMBffirst",reply_search,AS_USER},
4534    {SMBfunique,"SMBfunique",reply_search,AS_USER},
4535    {SMBfclose,"SMBfclose",reply_fclose,AS_USER},
4536
4537    /* LANMAN2.0 PROTOCOL FOLLOWS */
4538    {SMBfindnclose, "SMBfindnclose", reply_findnclose, AS_USER},
4539    {SMBfindclose, "SMBfindclose", reply_findclose,AS_USER},
4540    {SMBtrans2, "SMBtrans2", reply_trans2, AS_USER },
4541    {SMBtranss2, "SMBtranss2", reply_transs2, AS_USER},
4542
4543    /* NT PROTOCOL FOLLOWS */
4544    {SMBntcreateX, "SMBntcreateX", reply_ntcreate_and_X, AS_USER | CAN_IPC | QUEUE_IN_OPLOCK },
4545    {SMBnttrans, "SMBnttrans", reply_nttrans, AS_USER | CAN_IPC },
4546    {SMBnttranss, "SMBnttranss", reply_nttranss, AS_USER | CAN_IPC },
4547    {SMBntcancel, "SMBntcancel", reply_ntcancel, AS_USER },
4548
4549    /* messaging routines */
4550    {SMBsends,"SMBsends",reply_sends,AS_GUEST},
4551    {SMBsendstrt,"SMBsendstrt",reply_sendstrt,AS_GUEST},
4552    {SMBsendend,"SMBsendend",reply_sendend,AS_GUEST},
4553    {SMBsendtxt,"SMBsendtxt",reply_sendtxt,AS_GUEST},
4554
4555    /* NON-IMPLEMENTED PARTS OF THE CORE PROTOCOL */
4556    
4557    {SMBsendb,"SMBsendb",NULL,AS_GUEST},
4558    {SMBfwdname,"SMBfwdname",NULL,AS_GUEST},
4559    {SMBcancelf,"SMBcancelf",NULL,AS_GUEST},
4560    {SMBgetmac,"SMBgetmac",NULL,AS_GUEST}
4561  };
4562
4563 /****************************************************************************
4564 return a string containing the function name of a SMB command
4565 ****************************************************************************/
4566 char *smb_fn_name(int type)
4567 {
4568         static char *unknown_name = "SMBunknown";
4569         static int num_smb_messages = 
4570                 sizeof(smb_messages) / sizeof(struct smb_message_struct);
4571         int match;
4572
4573         for (match=0;match<num_smb_messages;match++)
4574                 if (smb_messages[match].code == type)
4575                         break;
4576
4577         if (match == num_smb_messages)
4578                 return(unknown_name);
4579
4580         return(smb_messages[match].name);
4581 }
4582
4583
4584 /****************************************************************************
4585 do a switch on the message type, and return the response size
4586 ****************************************************************************/
4587 static int switch_message(int type,char *inbuf,char *outbuf,int size,int bufsize)
4588 {
4589   static int pid= -1;
4590   int outsize = 0;
4591   static int num_smb_messages = 
4592     sizeof(smb_messages) / sizeof(struct smb_message_struct);
4593   int match;
4594   extern int Client;
4595
4596 #if PROFILING
4597   struct timeval msg_start_time;
4598   struct timeval msg_end_time;
4599   static unsigned long total_time = 0;
4600
4601   GetTimeOfDay(&msg_start_time);
4602 #endif
4603
4604   if (pid == -1)
4605     pid = getpid();
4606
4607   errno = 0;
4608   last_message = type;
4609
4610   /* make sure this is an SMB packet */
4611   if (strncmp(smb_base(inbuf),"\377SMB",4) != 0)
4612   {
4613     DEBUG(2,("Non-SMB packet of length %d\n",smb_len(inbuf)));
4614     return(-1);
4615   }
4616
4617   for (match=0;match<num_smb_messages;match++)
4618     if (smb_messages[match].code == type)
4619       break;
4620
4621   if (match == num_smb_messages)
4622   {
4623     DEBUG(0,("Unknown message type %d!\n",type));
4624     outsize = reply_unknown(inbuf,outbuf);
4625   }
4626   else
4627   {
4628     DEBUG(3,("switch message %s (pid %d)\n",smb_messages[match].name,pid));
4629
4630     if(global_oplock_break && (smb_messages[match].flags & QUEUE_IN_OPLOCK))
4631     {
4632       /* 
4633        * Queue this message as we are the process of an oplock break.
4634        */
4635
4636       DEBUG( 2, ( "switch_message: queueing message due to being in " ) );
4637       DEBUGADD( 2, ( "oplock break state.\n" ) );
4638
4639       push_oplock_pending_smb_message( inbuf, size );
4640       return -1;
4641     }          
4642
4643     if (smb_messages[match].fn)
4644     {
4645       int cnum = SVAL(inbuf,smb_tid);
4646       int flags = smb_messages[match].flags;
4647       static uint16 last_session_tag = UID_FIELD_INVALID;
4648       /* In share mode security we must ignore the vuid. */
4649       uint16 session_tag = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID : SVAL(inbuf,smb_uid);
4650       connection_struct *conn = NULL;
4651
4652       if (VALID_CNUM(cnum) && Connections[cnum].open) {
4653               conn = &Connections[cnum];
4654       }
4655
4656       /* Ensure this value is replaced in the incoming packet. */
4657       SSVAL(inbuf,smb_uid,session_tag);
4658
4659       /*
4660        * Ensure the correct username is in sesssetup_user.
4661        * This is a really ugly bugfix for problems with
4662        * multiple session_setup_and_X's being done and
4663        * allowing %U and %G substitutions to work correctly.
4664        * There is a reason this code is done here, don't
4665        * move it unless you know what you're doing... :-).
4666        * JRA.
4667        */
4668       if (session_tag != last_session_tag) {
4669         user_struct *vuser = NULL;
4670
4671         last_session_tag = session_tag;
4672         if(session_tag != UID_FIELD_INVALID)
4673           vuser = get_valid_user_struct(session_tag);           
4674         if(vuser != NULL)
4675           pstrcpy( sesssetup_user, vuser->requested_name);
4676       }
4677
4678       /* does this protocol need to be run as root? */
4679       if (!(flags & AS_USER))
4680         unbecome_user();
4681
4682       /* does this protocol need to be run as the connected user? */
4683       if ((flags & AS_USER) && !become_user(conn,session_tag)) {
4684         if (flags & AS_GUEST) 
4685           flags &= ~AS_USER;
4686         else
4687           return(ERROR(ERRSRV,ERRinvnid));
4688       }
4689       /* this code is to work around a bug is MS client 3 without
4690          introducing a security hole - it needs to be able to do
4691          print queue checks as guest if it isn't logged in properly */
4692       if (flags & AS_USER)
4693         flags &= ~AS_GUEST;
4694
4695       /* does it need write permission? */
4696       if ((flags & NEED_WRITE) && !CAN_WRITE(conn))
4697         return(ERROR(ERRSRV,ERRaccess));
4698
4699       /* ipc services are limited */
4700       if (IS_IPC(conn) && (flags & AS_USER) && !(flags & CAN_IPC)) {
4701         return(ERROR(ERRSRV,ERRaccess));            
4702       }
4703
4704       /* load service specific parameters */
4705       if (OPEN_CNUM(conn) && 
4706           !become_service(conn,(flags & AS_USER)?True:False)) {
4707         return(ERROR(ERRSRV,ERRaccess));
4708       }
4709
4710       /* does this protocol need to be run as guest? */
4711       if ((flags & AS_GUEST) && 
4712           (!become_guest() || 
4713            !check_access(Client, lp_hostsallow(-1), lp_hostsdeny(-1)))) {
4714         return(ERROR(ERRSRV,ERRaccess));
4715       }
4716
4717       last_inbuf = inbuf;
4718
4719       outsize = smb_messages[match].fn(conn, inbuf,outbuf,size,bufsize);
4720     }
4721     else
4722     {
4723       outsize = reply_unknown(inbuf,outbuf);
4724     }
4725   }
4726
4727 #if PROFILING
4728   GetTimeOfDay(&msg_end_time);
4729   if (!(smb_messages[match].flags & TIME_INIT))
4730   {
4731     smb_messages[match].time = 0;
4732     smb_messages[match].flags |= TIME_INIT;
4733   }
4734   {
4735     unsigned long this_time =     
4736       (msg_end_time.tv_sec - msg_start_time.tv_sec)*1e6 +
4737       (msg_end_time.tv_usec - msg_start_time.tv_usec);
4738     smb_messages[match].time += this_time;
4739     total_time += this_time;
4740   }
4741   DEBUG(2,("TIME %s  %d usecs   %g pct\n",
4742         smb_fn_name(type),smb_messages[match].time,
4743         (100.0*smb_messages[match].time) / total_time));
4744 #endif
4745
4746   return(outsize);
4747 }
4748
4749
4750 /****************************************************************************
4751   construct a chained reply and add it to the already made reply
4752   **************************************************************************/
4753 int chain_reply(char *inbuf,char *outbuf,int size,int bufsize)
4754 {
4755   static char *orig_inbuf;
4756   static char *orig_outbuf;
4757   int smb_com1, smb_com2 = CVAL(inbuf,smb_vwv0);
4758   unsigned smb_off2 = SVAL(inbuf,smb_vwv1);
4759   char *inbuf2, *outbuf2;
4760   int outsize2;
4761   char inbuf_saved[smb_wct];
4762   char outbuf_saved[smb_wct];
4763   extern int chain_size;
4764   int wct = CVAL(outbuf,smb_wct);
4765   int outsize = smb_size + 2*wct + SVAL(outbuf,smb_vwv0+2*wct);
4766
4767   /* maybe its not chained */
4768   if (smb_com2 == 0xFF) {
4769     CVAL(outbuf,smb_vwv0) = 0xFF;
4770     return outsize;
4771   }
4772
4773   if (chain_size == 0) {
4774     /* this is the first part of the chain */
4775     orig_inbuf = inbuf;
4776     orig_outbuf = outbuf;
4777   }
4778
4779   /* we need to tell the client where the next part of the reply will be */
4780   SSVAL(outbuf,smb_vwv1,smb_offset(outbuf+outsize,outbuf));
4781   CVAL(outbuf,smb_vwv0) = smb_com2;
4782
4783   /* remember how much the caller added to the chain, only counting stuff
4784      after the parameter words */
4785   chain_size += outsize - smb_wct;
4786
4787   /* work out pointers into the original packets. The
4788      headers on these need to be filled in */
4789   inbuf2 = orig_inbuf + smb_off2 + 4 - smb_wct;
4790   outbuf2 = orig_outbuf + SVAL(outbuf,smb_vwv1) + 4 - smb_wct;
4791
4792   /* remember the original command type */
4793   smb_com1 = CVAL(orig_inbuf,smb_com);
4794
4795   /* save the data which will be overwritten by the new headers */
4796   memcpy(inbuf_saved,inbuf2,smb_wct);
4797   memcpy(outbuf_saved,outbuf2,smb_wct);
4798
4799   /* give the new packet the same header as the last part of the SMB */
4800   memmove(inbuf2,inbuf,smb_wct);
4801
4802   /* create the in buffer */
4803   CVAL(inbuf2,smb_com) = smb_com2;
4804
4805   /* create the out buffer */
4806   bzero(outbuf2,smb_size);
4807   set_message(outbuf2,0,0,True);
4808   CVAL(outbuf2,smb_com) = CVAL(inbuf2,smb_com);
4809   
4810   memcpy(outbuf2+4,inbuf2+4,4);
4811   CVAL(outbuf2,smb_rcls) = SMB_SUCCESS;
4812   CVAL(outbuf2,smb_reh) = 0;
4813   CVAL(outbuf2,smb_flg) = 0x80 | (CVAL(inbuf2,smb_flg) & 0x8); /* bit 7 set 
4814                                                                   means a reply */
4815   SSVAL(outbuf2,smb_flg2,1); /* say we support long filenames */
4816   SSVAL(outbuf2,smb_err,SMB_SUCCESS);
4817   SSVAL(outbuf2,smb_tid,SVAL(inbuf2,smb_tid));
4818   SSVAL(outbuf2,smb_pid,SVAL(inbuf2,smb_pid));
4819   SSVAL(outbuf2,smb_uid,SVAL(inbuf2,smb_uid));
4820   SSVAL(outbuf2,smb_mid,SVAL(inbuf2,smb_mid));
4821
4822   DEBUG(3,("Chained message\n"));
4823   show_msg(inbuf2);
4824
4825   /* process the request */
4826   outsize2 = switch_message(smb_com2,inbuf2,outbuf2,size-chain_size,
4827                             bufsize-chain_size);
4828
4829   /* copy the new reply and request headers over the old ones, but
4830      preserve the smb_com field */
4831   memmove(orig_outbuf,outbuf2,smb_wct);
4832   CVAL(orig_outbuf,smb_com) = smb_com1;
4833
4834   /* restore the saved data, being careful not to overwrite any
4835    data from the reply header */
4836   memcpy(inbuf2,inbuf_saved,smb_wct);
4837   {
4838     int ofs = smb_wct - PTR_DIFF(outbuf2,orig_outbuf);
4839     if (ofs < 0) ofs = 0;
4840     memmove(outbuf2+ofs,outbuf_saved+ofs,smb_wct-ofs);
4841   }
4842
4843   return outsize2;
4844 }
4845
4846
4847 /****************************************************************************
4848  Helper function for contruct_reply.
4849 ****************************************************************************/
4850
4851 void construct_reply_common(char *inbuf,char *outbuf)
4852 {
4853   bzero(outbuf,smb_size);
4854
4855   CVAL(outbuf,smb_com) = CVAL(inbuf,smb_com);
4856   set_message(outbuf,0,0,True);
4857
4858   memcpy(outbuf+4,inbuf+4,4);
4859   CVAL(outbuf,smb_rcls) = SMB_SUCCESS;
4860   CVAL(outbuf,smb_reh) = 0;
4861   CVAL(outbuf,smb_flg) = 0x80 | (CVAL(inbuf,smb_flg) & 0x8); /* bit 7 set
4862                                  means a reply */
4863   SSVAL(outbuf,smb_flg2,1); /* say we support long filenames */
4864   SSVAL(outbuf,smb_err,SMB_SUCCESS);
4865   SSVAL(outbuf,smb_tid,SVAL(inbuf,smb_tid));
4866   SSVAL(outbuf,smb_pid,SVAL(inbuf,smb_pid));
4867   SSVAL(outbuf,smb_uid,SVAL(inbuf,smb_uid));
4868   SSVAL(outbuf,smb_mid,SVAL(inbuf,smb_mid));
4869 }
4870
4871 /****************************************************************************
4872   construct a reply to the incoming packet
4873 ****************************************************************************/
4874 int construct_reply(char *inbuf,char *outbuf,int size,int bufsize)
4875 {
4876   int type = CVAL(inbuf,smb_com);
4877   int outsize = 0;
4878   int msg_type = CVAL(inbuf,0);
4879   extern int chain_size;
4880
4881   smb_last_time = time(NULL);
4882
4883   chain_size = 0;
4884   chain_fnum = -1;
4885   reset_chain_pnum();
4886
4887   if (msg_type != 0)
4888     return(reply_special(inbuf,outbuf));  
4889
4890   construct_reply_common(inbuf, outbuf);
4891
4892   outsize = switch_message(type,inbuf,outbuf,size,bufsize);
4893
4894   outsize += chain_size;
4895
4896   if(outsize > 4)
4897     smb_setlen(outbuf,outsize - 4);
4898   return(outsize);
4899 }
4900
4901 /****************************************************************************
4902   process commands from the client
4903 ****************************************************************************/
4904 static void process(void)
4905 {
4906   extern int Client;
4907
4908   InBuffer = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
4909   OutBuffer = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
4910   if ((InBuffer == NULL) || (OutBuffer == NULL)) 
4911     return;
4912
4913   InBuffer += SMB_ALIGNMENT;
4914   OutBuffer += SMB_ALIGNMENT;
4915
4916 #if PRIME_NMBD
4917   DEBUG(3,("priming nmbd\n"));
4918   {
4919     struct in_addr ip;
4920     ip = *interpret_addr2("localhost");
4921     if (zero_ip(ip)) ip = *interpret_addr2("127.0.0.1");
4922     *OutBuffer = 0;
4923     send_one_packet(OutBuffer,1,ip,NMB_PORT,SOCK_DGRAM);
4924   }
4925 #endif    
4926
4927   /* re-initialise the timezone */
4928   TimeInit();
4929
4930   while (True)
4931   {
4932     int deadtime = lp_deadtime()*60;
4933     int counter;
4934     int last_keepalive=0;
4935     int service_load_counter = 0;
4936     BOOL got_smb = False;
4937
4938     if (deadtime <= 0)
4939       deadtime = DEFAULT_SMBD_TIMEOUT;
4940
4941 #if USE_READ_PREDICTION
4942     if (lp_readprediction())
4943       do_read_prediction();
4944 #endif
4945
4946     errno = 0;      
4947
4948     for (counter=SMBD_SELECT_LOOP; 
4949           !receive_message_or_smb(Client,oplock_sock,
4950                       InBuffer,BUFFER_SIZE,SMBD_SELECT_LOOP*1000,&got_smb); 
4951           counter += SMBD_SELECT_LOOP)
4952     {
4953       int i;
4954       time_t t;
4955       BOOL allidle = True;
4956       extern int keepalive;
4957
4958       if (counter > 365 * 3600) /* big number of seconds. */
4959       {
4960         counter = 0;
4961         service_load_counter = 0;
4962       }
4963
4964       if (smb_read_error == READ_EOF) 
4965       {
4966         DEBUG(3,("end of file from client\n"));
4967         return;
4968       }
4969
4970       if (smb_read_error == READ_ERROR) 
4971       {
4972         DEBUG(3,("receive_smb error (%s) exiting\n",
4973                   strerror(errno)));
4974         return;
4975       }
4976
4977       t = time(NULL);
4978
4979       /* become root again if waiting */
4980       unbecome_user();
4981
4982       /* check for smb.conf reload */
4983       if (counter >= service_load_counter + SMBD_RELOAD_CHECK)
4984       {
4985         service_load_counter = counter;
4986
4987         /* reload services, if files have changed. */
4988         reload_services(True);
4989       }
4990
4991       /*
4992        * If reload_after_sighup == True then we got a SIGHUP
4993        * and are being asked to reload. Fix from <branko.cibej@hermes.si>
4994        */
4995
4996       if (reload_after_sighup)
4997       {
4998         DEBUG(0,("Reloading services after SIGHUP\n"));
4999         reload_services(False);
5000         reload_after_sighup = False;
5001       }
5002
5003       /* automatic timeout if all connections are closed */      
5004       if (num_connections_open==0 && counter >= IDLE_CLOSED_TIMEOUT) 
5005       {
5006         DEBUG( 2, ( "Closing idle connection\n" ) );
5007         return;
5008       }
5009
5010       if (keepalive && (counter-last_keepalive)>keepalive) 
5011       {
5012               struct cli_state *cli = server_client();
5013               if (!send_keepalive(Client)) {
5014                       DEBUG( 2, ( "Keepalive failed - exiting.\n" ) );
5015                       return;
5016               }     
5017               /* also send a keepalive to the password server if its still
5018                  connected */
5019               if (cli && cli->initialised)
5020                       send_keepalive(cli->fd);
5021               last_keepalive = counter;
5022       }
5023
5024       /* check for connection timeouts */
5025       for (i=0;i<MAX_CONNECTIONS;i++) {
5026               if (Connections[i].open) {
5027                       /* close dirptrs on connections that are idle */
5028                       if ((t-Connections[i].lastused)>DPTR_IDLE_TIMEOUT)
5029                               dptr_idlecnum(&Connections[i]);
5030
5031                       if (Connections[i].num_files_open > 0 ||
5032                           (t-Connections[i].lastused)<deadtime)
5033                               allidle = False;
5034               }
5035       }
5036
5037       if (allidle && num_connections_open>0) {
5038               DEBUG(2,("Closing idle connection 2.\n"));
5039               return;
5040       }
5041
5042       if(global_machine_pasword_needs_changing)
5043       {
5044         unsigned char trust_passwd_hash[16];
5045         time_t lct;
5046         pstring remote_machine_list;
5047
5048         /*
5049          * We're in domain level security, and the code that
5050          * read the machine password flagged that the machine
5051          * password needs changing.
5052          */
5053
5054         /*
5055          * First, open the machine password file with an exclusive lock.
5056          */
5057
5058         if(!trust_password_lock( global_myworkgroup, global_myname, True)) {
5059           DEBUG(0,("process: unable to open the machine account password file for \
5060 machine %s in domain %s.\n", global_myname, global_myworkgroup ));
5061           continue;
5062         }
5063
5064         if(!get_trust_account_password( trust_passwd_hash, &lct)) {
5065           DEBUG(0,("process: unable to read the machine account password for \
5066 machine %s in domain %s.\n", global_myname, global_myworkgroup ));
5067           trust_password_unlock();
5068           continue;
5069         }
5070
5071         /*
5072          * Make sure someone else hasn't already done this.
5073          */
5074
5075         if(t < lct + lp_machine_password_timeout()) {
5076           trust_password_unlock();
5077           global_machine_pasword_needs_changing = False;
5078           continue;
5079         }
5080
5081         pstrcpy(remote_machine_list, lp_passwordserver());
5082
5083         change_trust_account_password( global_myworkgroup, remote_machine_list);
5084         trust_password_unlock();
5085         global_machine_pasword_needs_changing = False;
5086       }
5087
5088       /*
5089        * Check to see if we have any change notifies 
5090        * outstanding on the queue.
5091        */
5092       process_pending_change_notify_queue(t);
5093     }
5094
5095     if(got_smb)
5096       process_smb(InBuffer, OutBuffer);
5097     else
5098       process_local_message(oplock_sock, InBuffer, BUFFER_SIZE);
5099   }
5100 }
5101
5102
5103 /****************************************************************************
5104   initialise connect, service and file structs
5105 ****************************************************************************/
5106 static void init_structs(void )
5107 {
5108   int i;
5109   get_myname(myhostname,NULL);
5110
5111   /*
5112    * Set the machine NETBIOS name if not already
5113    * set from the config file.
5114    */
5115
5116   if (!*global_myname)
5117   {
5118     char *p;
5119     fstrcpy( global_myname, myhostname );
5120     p = strchr( global_myname, '.' );
5121     if (p) 
5122       *p = 0;
5123   }
5124   strupper( global_myname );
5125
5126   for (i=0;i<MAX_CONNECTIONS;i++)
5127     {
5128       Connections[i].open = False;
5129       Connections[i].num_files_open=0;
5130       Connections[i].lastused=0;
5131       Connections[i].used=False;
5132       string_init(&Connections[i].user,"");
5133       string_init(&Connections[i].dirpath,"");
5134       string_init(&Connections[i].connectpath,"");
5135       string_init(&Connections[i].origpath,"");
5136     }
5137
5138   for (i=0;i<MAX_FNUMS;i++)
5139     {
5140       Files[i].open = False;
5141       string_init(&Files[i].fsp_name,"");
5142     }
5143
5144   for (i=0;i<MAX_OPEN_FILES;i++)
5145     {
5146       file_fd_struct *fd_ptr = &FileFd[i];
5147       fd_ptr->ref_count = 0;
5148       fd_ptr->dev = (int32)-1;
5149       fd_ptr->inode = (int32)-1;
5150       fd_ptr->fd = -1;
5151       fd_ptr->fd_readonly = -1;
5152       fd_ptr->fd_writeonly = -1;
5153       fd_ptr->real_open_flags = -1;
5154     }
5155
5156   /* for RPC pipes */
5157   init_rpc_pipe_hnd();
5158
5159   /* for LSA handles */
5160   init_lsa_policy_hnd();
5161
5162   init_dptrs();
5163 }
5164
5165 /****************************************************************************
5166 usage on the program
5167 ****************************************************************************/
5168 static void usage(char *pname)
5169 {
5170   DEBUG(0,("Incorrect program usage - are you sure the command line is correct?\n"));
5171
5172   printf("Usage: %s [-D] [-p port] [-d debuglevel] [-l log basename] [-s services file]\n",pname);
5173   printf("Version %s\n",VERSION);
5174   printf("\t-D                    become a daemon\n");
5175   printf("\t-p port               listen on the specified port\n");
5176   printf("\t-d debuglevel         set the debuglevel\n");
5177   printf("\t-l log basename.      Basename for log/debug files\n");
5178   printf("\t-s services file.     Filename of services file\n");
5179   printf("\t-P                    passive only\n");
5180   printf("\t-a                    overwrite log file, don't append\n");
5181   printf("\n");
5182 }
5183
5184
5185 /****************************************************************************
5186   main program
5187 ****************************************************************************/
5188  int main(int argc,char *argv[])
5189 {
5190   extern BOOL append_log;
5191   /* shall I run as a daemon */
5192   BOOL is_daemon = False;
5193   int port = SMB_PORT;
5194   int opt;
5195   extern char *optarg;
5196
5197 #ifdef HAVE_SET_AUTH_PARAMETERS
5198   set_auth_parameters(argc,argv);
5199 #endif
5200
5201 #ifdef HAVE_SETLUID
5202   /* needed for SecureWare on SCO */
5203   setluid(0);
5204 #endif
5205
5206   append_log = True;
5207
5208   TimeInit();
5209
5210   pstrcpy(debugf,SMBLOGFILE);  
5211
5212   pstrcpy(remote_machine, "smb");
5213
5214   setup_logging(argv[0],False);
5215
5216   charset_initialise();
5217
5218   /* make absolutely sure we run as root - to handle cases where people
5219      are crazy enough to have it setuid */
5220 #ifdef HAVE_SETRESUID
5221   setresuid(0,0,0);
5222 #else
5223   setuid(0);
5224   seteuid(0);
5225   setuid(0);
5226   seteuid(0);
5227 #endif
5228
5229   fault_setup((void (*)(void *))exit_server);
5230   CatchSignal(SIGTERM , SIGNAL_CAST dflt_sig);
5231
5232   /* we want total control over the permissions on created files,
5233      so set our umask to 0 */
5234   umask(0);
5235
5236   GetWd(OriginalDir);
5237
5238   init_uid();
5239
5240   /* this is for people who can't start the program correctly */
5241   while (argc > 1 && (*argv[1] != '-'))
5242     {
5243       argv++;
5244       argc--;
5245     }
5246
5247   while ((opt = getopt(argc, argv, "O:i:l:s:d:Dp:hPaf:")) != EOF)
5248     switch (opt)
5249       {
5250       case 'O':
5251         pstrcpy(user_socket_options,optarg);
5252         break;
5253       case 'i':
5254         pstrcpy(scope,optarg);
5255         break;
5256       case 'P':
5257         {
5258           extern BOOL passive;
5259           passive = True;
5260         }
5261         break;  
5262       case 's':
5263         pstrcpy(servicesf,optarg);
5264         break;
5265       case 'l':
5266         pstrcpy(debugf,optarg);
5267         break;
5268       case 'a':
5269         {
5270           extern BOOL append_log;
5271           append_log = !append_log;
5272         }
5273         break;
5274       case 'D':
5275         is_daemon = True;
5276         break;
5277       case 'd':
5278         if (*optarg == 'A')
5279           DEBUGLEVEL = 10000;
5280         else
5281           DEBUGLEVEL = atoi(optarg);
5282         break;
5283       case 'p':
5284         port = atoi(optarg);
5285         break;
5286       case 'h':
5287         usage(argv[0]);
5288         exit(0);
5289         break;
5290       default:
5291         usage(argv[0]);
5292         exit(1);
5293       }
5294
5295   reopen_logs();
5296
5297   DEBUG( 1, ( "smbd version %s started.\n", VERSION ) );
5298   DEBUGADD( 1, ( "Copyright Andrew Tridgell 1992-1997\n" ) );
5299
5300 #ifdef HAVE_GETRLIMIT
5301 #ifdef RLIMIT_NOFILE
5302   {
5303     struct rlimit rlp;
5304     getrlimit(RLIMIT_NOFILE, &rlp);
5305     /*
5306      * Set the fd limit to be MAX_OPEN_FILES + 10 to account for the
5307      * extra fd we need to read directories, as well as the log files
5308      * and standard handles etc.
5309      */
5310     rlp.rlim_cur = (MAX_OPEN_FILES+10>rlp.rlim_max)? rlp.rlim_max:MAX_OPEN_FILES+10;
5311     setrlimit(RLIMIT_NOFILE, &rlp);
5312     getrlimit(RLIMIT_NOFILE, &rlp);
5313     DEBUG(3,("Maximum number of open files per session is %d\n",(int)rlp.rlim_cur));
5314   }
5315 #endif
5316 #endif
5317
5318   
5319   DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
5320         (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));
5321
5322   if (sizeof(uint16) < 2 || sizeof(uint32) < 4)
5323     {
5324       DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
5325       exit(1);
5326     }
5327
5328   init_structs();
5329
5330   if (!reload_services(False))
5331     return(-1); 
5332
5333 #ifdef WITH_SSL
5334   {
5335     extern BOOL sslEnabled;
5336     sslEnabled = lp_ssl_enabled();
5337     if(sslEnabled)
5338       sslutil_init(True);
5339   }
5340 #endif        /* WITH_SSL */
5341
5342   codepage_initialise(lp_client_code_page());
5343
5344   pstrcpy(global_myworkgroup, lp_workgroup());
5345
5346   if(!pdb_generate_machine_sid()) {
5347           DEBUG(0,("ERROR: Samba cannot get a machine SID.\n"));
5348           exit(1);
5349   }
5350
5351   CatchSignal(SIGHUP,SIGNAL_CAST sig_hup);
5352
5353   /* Setup the signals that allow the debug log level
5354      to by dynamically changed. */
5355  
5356   /* If we are using the malloc debug code we can't use
5357      SIGUSR1 and SIGUSR2 to do debug level changes. */
5358
5359 #ifndef MEM_MAN
5360 #if defined(SIGUSR1)
5361   CatchSignal( SIGUSR1, SIGNAL_CAST sig_usr1 );
5362 #endif /* SIGUSR1 */
5363    
5364 #if defined(SIGUSR2)
5365   CatchSignal( SIGUSR2, SIGNAL_CAST sig_usr2 );
5366 #endif /* SIGUSR2 */
5367 #endif /* MEM_MAN */
5368
5369   DEBUG( 3, ( "loaded services\n" ) );
5370
5371   if (!is_daemon && !is_a_socket(0))
5372     {
5373       DEBUG(0,("standard input is not a socket, assuming -D option\n"));
5374       is_daemon = True;
5375     }
5376
5377   if (is_daemon)
5378     {
5379       DEBUG( 3, ( "Becoming a daemon.\n" ) );
5380       become_daemon();
5381     }
5382
5383   if (!directory_exist(lp_lockdir(), NULL)) {
5384           mkdir(lp_lockdir(), 0755);
5385   }
5386
5387   if (is_daemon) {
5388           pidfile_create("smbd");
5389   }
5390
5391   if (!open_sockets(is_daemon,port))
5392     exit(1);
5393
5394   if (!locking_init(0))
5395     exit(1);
5396
5397   if(!initialize_password_db())
5398     exit(1);
5399
5400   /* possibly reload the services file. */
5401   reload_services(True);
5402
5403   max_recv = MIN(lp_maxxmit(),BUFFER_SIZE);
5404
5405   if (*lp_rootdir())
5406     {
5407       if (sys_chroot(lp_rootdir()) == 0)
5408         DEBUG( 2, ( "Changed root to %s\n", lp_rootdir() ) );
5409     }
5410
5411   /* Setup the oplock IPC socket. */
5412   if( !open_oplock_ipc() )
5413     exit(1);
5414
5415   process();
5416   close_sockets();
5417
5418   exit_server("normal exit");
5419   return(0);
5420 }