2 Unix SMB/Netbios implementation.
4 Main SMB server routines
5 Copyright (C) Andrew Tridgell 1992-1997
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.
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.
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.
25 pstring servicesf = CONFIGFILE;
26 extern pstring debugf;
27 extern pstring sesssetup_user;
28 extern fstring myworkgroup;
30 char *InBuffer = NULL;
31 char *OutBuffer = NULL;
32 char *last_inbuf = NULL;
37 /* the last message the was processed */
38 int last_message = -1;
40 /* a useful macro to debug the last message processed */
41 #define LAST_MESSAGE() smb_fn_name(last_message)
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 extern time_t smb_last_time;
53 extern int smb_read_error;
55 extern pstring user_socket_options;
57 connection_struct Connections[MAX_CONNECTIONS];
58 files_struct Files[MAX_OPEN_FILES];
61 * Indirection for file fd's. Needed as POSIX locking
62 * is based on file/process, not fd/process.
64 file_fd_struct FileFd[MAX_OPEN_FILES];
65 int max_file_fd_used = 0;
70 * Size of data we can send to client. Set
71 * by the client for all protocols above CORE.
72 * Set by us for CORE protocol.
74 int max_send = BUFFER_SIZE;
76 * Size of the data we can receive. Set by us.
77 * Can be modified by the max xmit parameter.
79 int max_recv = BUFFER_SIZE;
81 /* a fnum to use when chaining */
84 /* number of open connections */
85 static int num_connections_open = 0;
87 extern fstring remote_machine;
91 /* these can be set by some functions to override the error codes */
92 int unix_ERR_class=SUCCESS;
96 extern int extra_time_offset;
98 extern pstring myhostname;
100 static int find_free_connection(int hash);
102 /* for readability... */
103 #define IS_DOS_READONLY(test_mode) (((test_mode) & aRONLY) != 0)
104 #define IS_DOS_DIR(test_mode) (((test_mode) & aDIR) != 0)
105 #define IS_DOS_ARCHIVE(test_mode) (((test_mode) & aARCH) != 0)
106 #define IS_DOS_SYSTEM(test_mode) (((test_mode) & aSYSTEM) != 0)
107 #define IS_DOS_HIDDEN(test_mode) (((test_mode) & aHIDDEN) != 0)
109 /****************************************************************************
110 when exiting, take the whole family
111 ****************************************************************************/
114 exit_server("caught signal");
115 return 0; /* Keep -Wall happy :-) */
117 /****************************************************************************
118 Send a SIGTERM to our process group.
119 *****************************************************************************/
122 if(am_parent) kill(0,SIGTERM);
125 /****************************************************************************
126 change a dos mode to a unix mode
127 base permission for files:
128 everybody gets read bit set
129 dos readonly is represented in unix by removing everyone's write bit
130 dos archive is represented in unix by the user's execute bit
131 dos system is represented in unix by the group's execute bit
132 dos hidden is represented in unix by the other's execute bit
133 Then apply create mask,
135 base permission for directories:
136 dos directory is represented in unix by unix's dir bit and the exec bit
137 Then apply create mask,
139 ****************************************************************************/
140 mode_t unix_mode(int cnum,int dosmode)
142 mode_t result = (S_IRUSR | S_IRGRP | S_IROTH);
144 if ( !IS_DOS_READONLY(dosmode) )
145 result |= (S_IWUSR | S_IWGRP | S_IWOTH);
147 if (IS_DOS_DIR(dosmode)) {
148 /* We never make directories read only for the owner as under DOS a user
149 can always create a file in a read-only directory. */
150 result |= (S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH | S_IWUSR);
151 /* Apply directory mask */
152 result &= lp_dir_mode(SNUM(cnum));
153 /* Add in force bits */
154 result |= lp_force_dir_mode(SNUM(cnum));
156 if (MAP_ARCHIVE(cnum) && IS_DOS_ARCHIVE(dosmode))
159 if (MAP_SYSTEM(cnum) && IS_DOS_SYSTEM(dosmode))
162 if (MAP_HIDDEN(cnum) && IS_DOS_HIDDEN(dosmode))
165 /* Apply mode mask */
166 result &= lp_create_mode(SNUM(cnum));
167 /* Add in force bits */
168 result |= lp_force_create_mode(SNUM(cnum));
174 /****************************************************************************
175 change a unix mode to a dos mode
176 ****************************************************************************/
177 int dos_mode(int cnum,char *path,struct stat *sbuf)
180 extern struct current_user current_user;
182 DEBUG(5,("dos_mode: %d %s\n", cnum, path));
184 if (CAN_WRITE(cnum) && !lp_alternate_permissions(SNUM(cnum))) {
185 if (!((sbuf->st_mode & S_IWOTH) ||
186 Connections[cnum].admin_user ||
187 ((sbuf->st_mode & S_IWUSR) && current_user.uid==sbuf->st_uid) ||
188 ((sbuf->st_mode & S_IWGRP) &&
189 in_group(sbuf->st_gid,current_user.gid,
190 current_user.ngroups,current_user.igroups))))
193 if ((sbuf->st_mode & S_IWUSR) == 0)
197 if ((sbuf->st_mode & S_IXUSR) != 0)
200 if (MAP_SYSTEM(cnum) && ((sbuf->st_mode & S_IXGRP) != 0))
203 if (MAP_HIDDEN(cnum) && ((sbuf->st_mode & S_IXOTH) != 0))
206 if (S_ISDIR(sbuf->st_mode))
207 result = aDIR | (result & aRONLY);
210 if (S_ISLNK(sbuf->st_mode) && S_ISDIR(sbuf->st_mode))
214 /* hide files with a name starting with a . */
215 if (lp_hide_dot_files(SNUM(cnum)))
217 char *p = strrchr(path,'/');
223 if (p[0] == '.' && p[1] != '.' && p[1] != 0)
227 /* Optimization : Only call is_hidden_path if it's not already
229 if (!(result & aHIDDEN) && IS_HIDDEN_PATH(cnum,path))
234 DEBUG(5,("dos_mode returning "));
236 if (result & aHIDDEN) DEBUG(5, ("h"));
237 if (result & aRONLY ) DEBUG(5, ("r"));
238 if (result & aSYSTEM) DEBUG(5, ("s"));
239 if (result & aDIR ) DEBUG(5, ("d"));
240 if (result & aARCH ) DEBUG(5, ("a"));
248 /*******************************************************************
249 chmod a file - but preserve some bits
250 ********************************************************************/
251 int dos_chmod(int cnum,char *fname,int dosmode,struct stat *st)
260 if (sys_stat(fname,st)) return(-1);
263 if (S_ISDIR(st->st_mode)) dosmode |= aDIR;
265 if (dos_mode(cnum,fname,st) == dosmode) return(0);
267 unixmode = unix_mode(cnum,dosmode);
269 /* preserve the s bits */
270 mask |= (S_ISUID | S_ISGID);
272 /* preserve the t bit */
277 /* possibly preserve the x bits */
278 if (!MAP_ARCHIVE(cnum)) mask |= S_IXUSR;
279 if (!MAP_SYSTEM(cnum)) mask |= S_IXGRP;
280 if (!MAP_HIDDEN(cnum)) mask |= S_IXOTH;
282 unixmode |= (st->st_mode & mask);
284 /* if we previously had any r bits set then leave them alone */
285 if ((tmp = st->st_mode & (S_IRUSR|S_IRGRP|S_IROTH))) {
286 unixmode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
290 /* if we previously had any w bits set then leave them alone
291 if the new mode is not rdonly */
292 if (!IS_DOS_READONLY(dosmode) &&
293 (tmp = st->st_mode & (S_IWUSR|S_IWGRP|S_IWOTH))) {
294 unixmode &= ~(S_IWUSR|S_IWGRP|S_IWOTH);
298 return(sys_chmod(fname,unixmode));
302 /****************************************************************************
303 check if two filenames are equal
305 this needs to be careful about whether we are case sensitive
306 ****************************************************************************/
307 static BOOL fname_equal(char *name1, char *name2)
309 int l1 = strlen(name1);
310 int l2 = strlen(name2);
312 /* handle filenames ending in a single dot */
313 if (l1-l2 == 1 && name1[l1-1] == '.' && lp_strip_dot())
317 ret = fname_equal(name1,name2);
322 if (l2-l1 == 1 && name2[l2-1] == '.' && lp_strip_dot())
326 ret = fname_equal(name1,name2);
331 /* now normal filename handling */
333 return(strcmp(name1,name2) == 0);
335 return(strequal(name1,name2));
339 /****************************************************************************
340 mangle the 2nd name and check if it is then equal to the first name
341 ****************************************************************************/
342 static BOOL mangled_equal(char *name1, char *name2)
346 if (is_8_3(name2, True))
349 strcpy(tmpname,name2);
350 mangle_name_83(tmpname);
352 return(strequal(name1,tmpname));
356 /****************************************************************************
357 scan a directory to find a filename, matching without case sensitivity
359 If the name looks like a mangled name then try via the mangling functions
360 ****************************************************************************/
361 static BOOL scan_directory(char *path, char *name,int cnum,BOOL docache)
368 mangled = is_mangled(name);
370 /* handle null paths */
374 if (docache && (dname = DirCacheCheck(path,name,SNUM(cnum)))) {
380 check_mangled_stack(name);
382 /* open the directory */
383 if (!(cur_dir = OpenDir(cnum, path, True)))
385 DEBUG(3,("scan dir didn't open dir [%s]\n",path));
389 /* now scan for matching names */
390 while ((dname = ReadDirName(cur_dir)))
393 (strequal(dname,".") || strequal(dname,"..")))
397 if (!name_map_mangle(name2,False,SNUM(cnum))) continue;
399 if ((mangled && mangled_equal(name,name2))
400 || fname_equal(name, dname))
402 /* we've found the file, change it's name and return */
403 if (docache) DirCacheAdd(path,name,dname,SNUM(cnum));
414 /****************************************************************************
415 This routine is called to convert names from the dos namespace to unix
416 namespace. It needs to handle any case conversions, mangling, format
419 We assume that we have already done a chdir() to the right "root" directory
422 The function will return False if some part of the name except for the last
423 part cannot be resolved
425 If the saved_last_component != 0, then the unmodified last component
426 of the pathname is returned there. This is used in an exceptional
427 case in reply_mv (so far). If saved_last_component == 0 then nothing
429 ****************************************************************************/
430 BOOL unix_convert(char *name,int cnum,pstring saved_last_component)
437 if(saved_last_component)
438 *saved_last_component = 0;
440 /* convert to basic unix format - removing \ chars and cleaning it up */
442 unix_clean_name(name);
444 /* names must be relative to the root of the service - trim any leading /.
445 also trim trailing /'s */
446 trim_string(name,"/","/");
449 * Ensure saved_last_component is valid even if file exists.
451 if(saved_last_component) {
452 end = strrchr(name, '/');
454 strcpy(saved_last_component, end + 1);
456 strcpy(saved_last_component, name);
459 if (!case_sensitive &&
460 (!case_preserve || (is_8_3(name, False) && !short_case_preserve)))
463 /* check if it's a printer file */
464 if (Connections[cnum].printer)
466 if ((! *name) || strchr(name,'/') || !is_8_3(name, True))
470 sprintf(name2,"%.6s.XXXXXX",remote_machine);
471 /* sanitise the name */
472 for (s=name2 ; *s ; s++)
473 if (!issafe(*s)) *s = '_';
474 strcpy(name,(char *)mktemp(name2));
479 /* stat the name - if it exists then we are all done! */
480 if (sys_stat(name,&st) == 0)
483 DEBUG(5,("unix_convert(%s,%d)\n",name,cnum));
485 /* a special case - if we don't have any mangling chars and are case
486 sensitive then searching won't help */
487 if (case_sensitive && !is_mangled(name) &&
488 !lp_strip_dot() && !use_mangled_map)
491 /* now we need to recursively match the name against the real
492 directory structure */
495 while (strncmp(start,"./",2) == 0)
498 /* now match each part of the path name separately, trying the names
499 as is first, then trying to scan the directory for matching names */
500 for (;start;start = (end?end+1:(char *)NULL))
502 /* pinpoint the end of this section of the filename */
503 end = strchr(start, '/');
505 /* chop the name at this point */
508 if(saved_last_component != 0)
509 strcpy(saved_last_component, end ? end + 1 : start);
511 /* check if the name exists up to this point */
512 if (sys_stat(name, &st) == 0)
514 /* it exists. it must either be a directory or this must be
515 the last part of the path for it to be OK */
516 if (end && !(st.st_mode & S_IFDIR))
518 /* an intermediate part of the name isn't a directory */
519 DEBUG(5,("Not a dir %s\n",start));
530 /* remember the rest of the pathname so it can be restored
532 if (end) strcpy(rest,end+1);
534 /* try to find this part of the path in the directory */
535 if (strchr(start,'?') || strchr(start,'*') ||
536 !scan_directory(dirpath, start, cnum, end?True:False))
540 /* an intermediate part of the name can't be found */
541 DEBUG(5,("Intermediate not found %s\n",start));
546 /* just the last part of the name doesn't exist */
547 /* we may need to strupper() or strlower() it in case
548 this conversion is being used for file creation
550 /* if the filename is of mixed case then don't normalise it */
551 if (!case_preserve &&
552 (!strhasupper(start) || !strhaslower(start)))
555 /* check on the mangled stack to see if we can recover the
556 base of the filename */
557 if (is_mangled(start))
558 check_mangled_stack(start);
560 DEBUG(5,("New file %s\n",start));
564 /* restore the rest of the string */
567 strcpy(start+strlen(start)+1,rest);
568 end = start + strlen(start);
572 /* add to the dirpath that we have resolved so far */
573 if (*dirpath) strcat(dirpath,"/");
574 strcat(dirpath,start);
576 /* restore the / that we wiped out earlier */
580 /* the name has been resolved */
581 DEBUG(5,("conversion finished %s\n",name));
586 /****************************************************************************
587 normalise for DOS usage
588 ****************************************************************************/
589 static void disk_norm(int *bsize,int *dfree,int *dsize)
591 /* check if the disk is beyond the max disk size */
592 int maxdisksize = lp_maxdisksize();
594 /* convert to blocks - and don't overflow */
595 maxdisksize = ((maxdisksize*1024)/(*bsize))*1024;
596 if (*dsize > maxdisksize) *dsize = maxdisksize;
597 if (*dfree > maxdisksize) *dfree = maxdisksize-1; /* the -1 should stop
602 while (*dfree > WORDMAX || *dsize > WORDMAX || *bsize < 512)
607 if (*bsize > WORDMAX )
610 if (*dsize > WORDMAX)
612 if (*dfree > WORDMAX)
619 /****************************************************************************
620 return number of 1K blocks available on a path and total number
621 ****************************************************************************/
622 int disk_free(char *path,int *bsize,int *dfree,int *dsize)
624 char *df_command = lp_dfree_command();
645 /* possibly use system() to get the result */
646 if (df_command && *df_command)
652 sprintf(outfile,"%s/dfree.smb.%d",tmpdir(),(int)getpid());
653 sprintf(syscmd,"%s %s",df_command,path);
654 standard_sub_basic(syscmd);
656 ret = smbrun(syscmd,outfile,False);
657 DEBUG(3,("Running the command `%s' gave %d\n",syscmd,ret));
660 FILE *f = fopen(outfile,"r");
666 fscanf(f,"%d %d %d",dsize,dfree,bsize);
670 DEBUG(0,("Can't open %s\n",outfile));
674 disk_norm(bsize,dfree,dsize);
675 dfree_retval = ((*bsize)/1024)*(*dfree);
677 /* Ensure we return the min value between the users quota and
678 what's free on the disk. Thanks to Albrecht Gebhardt
679 <albrecht.gebhardt@uni-klu.ac.at> for this fix.
681 if (disk_quotas(path, &bsizeq, &dfreeq, &dsizeq))
683 disk_norm(&bsizeq, &dfreeq, &dsizeq);
684 dfreeq_retval = ((bsizeq)/1024)*(dfreeq);
685 dfree_retval = ( dfree_retval < dfreeq_retval ) ?
686 dfree_retval : dfreeq_retval ;
687 /* maybe dfree and dfreeq are calculated using different bsizes
688 so convert dfree from bsize into bsizeq */
689 *dfree = ((*dfree) * (*bsize)) / (bsizeq);
690 *dfree = ( *dfree < dfreeq ) ? *dfree : dfreeq ;
695 return(dfree_retval);
699 DEBUG(1,("Warning - no statfs function\n"));
703 if (statfs(path,&fs,sizeof(fs),0) != 0)
706 if (statvfs(path, &fs))
709 if (statfs(path,&fs,sizeof(fs)) == -1)
711 if (statfs(path,&fs) == -1)
713 #endif /* USE_STATVFS */
716 DEBUG(3,("dfree call failed code errno=%d\n",errno));
720 return(((*bsize)/1024)*(*dfree));
725 *dfree = fs.fd_req.bfree;
726 *dsize = fs.fd_req.btot;
729 *bsize = fs.f_frsize;
732 /* eg: osf1 has f_fsize = fundamental filesystem block size,
733 f_bsize = optimal transfer block size (MX: 94-04-19) */
738 #endif /* USE_STATVFS */
743 *dfree = fs.f_bavail;
745 *dsize = fs.f_blocks;
748 #if defined(SCO) || defined(ISC) || defined(MIPS)
752 /* handle rediculous bsize values - some OSes are broken */
753 if ((*bsize) < 512 || (*bsize)>0xFFFF) *bsize = 1024;
755 disk_norm(bsize,dfree,dsize);
761 DEBUG(0,("dfree seems to be broken on your system\n"));
762 *dsize = 20*1024*1024/(*bsize);
763 *dfree = MAX(1,*dfree);
765 dfree_retval = ((*bsize)/1024)*(*dfree);
767 /* Ensure we return the min value between the users quota and
768 what's free on the disk. Thanks to Albrecht Gebhardt
769 <albrecht.gebhardt@uni-klu.ac.at> for this fix.
771 if (disk_quotas(path, &bsizeq, &dfreeq, &dsizeq))
773 disk_norm(&bsizeq, &dfreeq, &dsizeq);
774 dfreeq_retval = ((bsizeq)/1024)*(dfreeq);
775 dfree_retval = ( dfree_retval < dfreeq_retval ) ?
776 dfree_retval : dfreeq_retval ;
777 /* maybe dfree and dfreeq are calculated using different bsizes
778 so convert dfree from bsize into bsizeq */
779 *dfree = ((*dfree) * (*bsize)) / (bsizeq);
780 *dfree = ( *dfree < dfreeq ) ? *dfree : dfreeq ;
785 return(dfree_retval);
790 /****************************************************************************
791 wrap it to get filenames right
792 ****************************************************************************/
793 int sys_disk_free(char *path,int *bsize,int *dfree,int *dsize)
795 return(disk_free(dos_to_unix(path,False),bsize,dfree,dsize));
800 /****************************************************************************
801 check a filename - possibly caling reducename
803 This is called by every routine before it allows an operation on a filename.
804 It does any final confirmation necessary to ensure that the filename is
805 a valid one for the user to access.
806 ****************************************************************************/
807 BOOL check_name(char *name,int cnum)
813 if( IS_VETO_PATH(cnum, name))
815 DEBUG(5,("file path name %s vetoed\n",name));
819 ret = reduce_name(name,Connections[cnum].connectpath,lp_widelinks(SNUM(cnum)));
821 /* Check if we are allowing users to follow symlinks */
822 /* Patch from David Clerc <David.Clerc@cui.unige.ch>
823 University of Geneva */
825 if (!lp_symlinks(SNUM(cnum)))
828 if ( (sys_lstat(name,&statbuf) != -1) &&
829 (S_ISLNK(statbuf.st_mode)) )
831 DEBUG(3,("check_name: denied: file path name %s is a symlink\n",name));
837 DEBUG(5,("check_name on %s failed\n",name));
842 /****************************************************************************
843 check a filename - possibly caling reducename
844 ****************************************************************************/
845 static void check_for_pipe(char *fname)
847 /* special case of pipe opens */
851 if (strstr(s,"pipe/"))
853 DEBUG(3,("Rejecting named pipe open for %s\n",fname));
854 unix_ERR_class = ERRSRV;
855 unix_ERR_code = ERRaccess;
859 /****************************************************************************
860 fd support routines - attempt to do a sys_open
861 ****************************************************************************/
863 int fd_attempt_open(char *fname, int flags, int mode)
865 int fd = sys_open(fname,flags,mode);
867 /* Fix for files ending in '.' */
868 if((fd == -1) && (errno == ENOENT) &&
869 (strchr(fname,'.')==NULL))
872 fd = sys_open(fname,flags,mode);
875 #if (defined(ENAMETOOLONG) && defined(HAVE_PATHCONF))
876 if ((fd == -1) && (errno == ENAMETOOLONG))
879 char *p = strrchr(fname, '/');
881 if (p == fname) /* name is "/xxx" */
883 max_len = pathconf("/", _PC_NAME_MAX);
886 else if ((p == NULL) || (p == fname))
889 max_len = pathconf(".", _PC_NAME_MAX);
894 max_len = pathconf(fname, _PC_NAME_MAX);
898 if (strlen(p) > max_len)
900 char tmp = p[max_len];
903 if ((fd = sys_open(fname,flags,mode)) == -1)
911 /****************************************************************************
912 fd support routines - attempt to find an already open file by dev
913 and inode - increments the ref_count of the returned file_fd_struct *.
914 ****************************************************************************/
915 file_fd_struct *fd_get_already_open(struct stat *sbuf)
918 file_fd_struct *fd_ptr;
923 for(i = 0; i <= max_file_fd_used; i++) {
925 if((fd_ptr->ref_count > 0) &&
926 (((uint32)sbuf->st_dev) == fd_ptr->dev) &&
927 (((uint32)sbuf->st_ino) == fd_ptr->inode)) {
930 ("Re-used file_fd_struct %d, dev = %x, inode = %x, ref_count = %d\n",
931 i, fd_ptr->dev, fd_ptr->inode, fd_ptr->ref_count));
938 /****************************************************************************
939 fd support routines - attempt to find a empty slot in the FileFd array.
940 Increments the ref_count of the returned entry.
941 ****************************************************************************/
942 file_fd_struct *fd_get_new()
945 file_fd_struct *fd_ptr;
947 for(i = 0; i < MAX_OPEN_FILES; i++) {
949 if(fd_ptr->ref_count == 0) {
950 fd_ptr->dev = (uint32)-1;
951 fd_ptr->inode = (uint32)-1;
953 fd_ptr->fd_readonly = -1;
954 fd_ptr->fd_writeonly = -1;
955 fd_ptr->real_open_flags = -1;
957 /* Increment max used counter if neccessary, cuts down
958 on search time when re-using */
959 if(i > max_file_fd_used)
960 max_file_fd_used = i;
961 DEBUG(3,("Allocated new file_fd_struct %d, dev = %x, inode = %x\n",
962 i, fd_ptr->dev, fd_ptr->inode));
966 DEBUG(1,("ERROR! Out of file_fd structures - perhaps increase MAX_OPEN_FILES?\
971 /****************************************************************************
972 fd support routines - attempt to re-open an already open fd as O_RDWR.
973 Save the already open fd (we cannot close due to POSIX file locking braindamage.
974 ****************************************************************************/
976 void fd_attempt_reopen(char *fname, int mode, file_fd_struct *fd_ptr)
978 int fd = sys_open( fname, O_RDWR, mode);
983 if(fd_ptr->real_open_flags == O_RDONLY)
984 fd_ptr->fd_readonly = fd_ptr->fd;
985 if(fd_ptr->real_open_flags == O_WRONLY)
986 fd_ptr->fd_writeonly = fd_ptr->fd;
989 fd_ptr->real_open_flags = O_RDWR;
992 /****************************************************************************
993 fd support routines - attempt to close the file referenced by this fd.
994 Decrements the ref_count and returns it.
995 ****************************************************************************/
996 int fd_attempt_close(file_fd_struct *fd_ptr)
998 DEBUG(3,("fd_attempt_close on file_fd_struct %d, fd = %d, dev = %x, inode = %x, open_flags = %d, ref_count = %d.\n",
1000 fd_ptr->fd, fd_ptr->dev, fd_ptr->inode,
1001 fd_ptr->real_open_flags,
1002 fd_ptr->ref_count));
1003 if(fd_ptr->ref_count > 0) {
1004 fd_ptr->ref_count--;
1005 if(fd_ptr->ref_count == 0) {
1006 if(fd_ptr->fd != -1)
1008 if(fd_ptr->fd_readonly != -1)
1009 close(fd_ptr->fd_readonly);
1010 if(fd_ptr->fd_writeonly != -1)
1011 close(fd_ptr->fd_writeonly);
1013 fd_ptr->fd_readonly = -1;
1014 fd_ptr->fd_writeonly = -1;
1015 fd_ptr->real_open_flags = -1;
1016 fd_ptr->dev = (uint32)-1;
1017 fd_ptr->inode = (uint32)-1;
1020 return fd_ptr->ref_count;
1023 /****************************************************************************
1025 ****************************************************************************/
1026 static void open_file(int fnum,int cnum,char *fname1,int flags,int mode, struct stat *sbuf)
1028 extern struct current_user current_user;
1030 struct stat statbuf;
1031 file_fd_struct *fd_ptr;
1033 Files[fnum].open = False;
1034 Files[fnum].fd_ptr = 0;
1037 strcpy(fname,fname1);
1039 /* check permissions */
1040 if ((flags != O_RDONLY) && !CAN_WRITE(cnum) && !Connections[cnum].printer)
1042 DEBUG(3,("Permission denied opening %s\n",fname));
1043 check_for_pipe(fname);
1047 /* this handles a bug in Win95 - it doesn't say to create the file when it
1049 if (Connections[cnum].printer)
1053 if (flags == O_WRONLY)
1054 DEBUG(3,("Bug in client? Set O_WRONLY without O_CREAT\n"));
1057 #if UTIME_WORKAROUND
1058 /* XXXX - is this OK?? */
1059 /* this works around a utime bug but can cause other problems */
1060 if ((flags & (O_WRONLY|O_RDWR)) && (flags & O_CREAT) && !(flags & O_APPEND))
1065 * Ensure we have a valid struct stat so we can search the
1069 if(stat(fname, &statbuf) < 0) {
1070 if(errno != ENOENT) {
1071 DEBUG(3,("Error doing stat on file %s (%s)\n",
1072 fname,strerror(errno)));
1074 check_for_pipe(fname);
1084 * Check to see if we have this file already
1085 * open. If we do, just use the already open fd and increment the
1086 * reference count (fd_get_already_open increments the ref_count).
1088 if((fd_ptr = fd_get_already_open(sbuf))!= 0) {
1090 int accmode = (flags & (O_RDONLY | O_WRONLY | O_RDWR));
1092 /* File was already open. */
1093 if((flags & O_CREAT) && (flags & O_EXCL)) {
1094 fd_ptr->ref_count--;
1100 * If not opened O_RDWR try
1101 * and do that here - a chmod may have been done
1102 * between the last open and now.
1104 if(fd_ptr->real_open_flags != O_RDWR)
1105 fd_attempt_reopen(fname, mode, fd_ptr);
1108 * Ensure that if we wanted write access
1109 * it has been opened for write, and if we wanted read it
1110 * was open for read.
1112 if(((accmode == O_WRONLY) && (fd_ptr->real_open_flags == O_RDONLY)) ||
1113 ((accmode == O_RDONLY) && (fd_ptr->real_open_flags == O_WRONLY)) ||
1114 ((accmode == O_RDWR) && (fd_ptr->real_open_flags != O_RDWR))) {
1115 DEBUG(3,("Error opening (already open for flags=%d) file %s (%s) (flags=%d)\n",
1116 fd_ptr->real_open_flags, fname,strerror(EACCES),flags));
1117 check_for_pipe(fname);
1118 fd_ptr->ref_count--;
1124 /* We need to allocate a new file_fd_struct (this increments the
1126 if((fd_ptr = fd_get_new()) == 0)
1129 * Whatever the requested flags, attempt read/write access,
1130 * as we don't know what flags future file opens may require.
1131 * If this fails, try again with the required flags.
1132 * Even if we open read/write when only read access was
1133 * requested the setting of the can_write flag in
1134 * the file_struct will protect us from errant
1135 * write requests. We never need to worry about O_APPEND
1136 * as this is not set anywhere in Samba.
1138 fd_ptr->real_open_flags = O_RDWR;
1139 /* Set the flags as needed without the read/write modes. */
1140 open_flags = flags & ~(O_RDWR|O_WRONLY|O_RDONLY);
1141 fd_ptr->fd = fd_attempt_open(fname, open_flags|O_RDWR, mode);
1143 * On some systems opening a file for R/W access on a read only
1144 * filesystems sets errno to EROFS.
1147 if((fd_ptr->fd == -1) && ((errno == EACCES) || (errno == EROFS))) {
1148 #else /* No EROFS */
1149 if((fd_ptr->fd == -1) && (errno == EACCES)) {
1151 if(flags & O_WRONLY) {
1152 fd_ptr->fd = fd_attempt_open(fname, open_flags|O_WRONLY, mode);
1153 fd_ptr->real_open_flags = O_WRONLY;
1155 fd_ptr->fd = fd_attempt_open(fname, open_flags|O_RDONLY, mode);
1156 fd_ptr->real_open_flags = O_RDONLY;
1161 if ((fd_ptr->fd >=0) &&
1162 Connections[cnum].printer && lp_minprintspace(SNUM(cnum))) {
1166 strcpy(dname,fname);
1167 p = strrchr(dname,'/');
1169 if (sys_disk_free(dname,&dum1,&dum2,&dum3) <
1170 lp_minprintspace(SNUM(cnum))) {
1171 fd_attempt_close(fd_ptr);
1172 Files[fnum].fd_ptr = 0;
1173 if(fd_ptr->ref_count == 0)
1182 DEBUG(3,("Error opening file %s (%s) (flags=%d)\n",
1183 fname,strerror(errno),flags));
1184 /* Ensure the ref_count is decremented. */
1185 fd_attempt_close(fd_ptr);
1186 check_for_pipe(fname);
1190 if (fd_ptr->fd >= 0)
1194 if(fstat(fd_ptr->fd, &statbuf) == -1) {
1195 /* Error - backout !! */
1196 DEBUG(3,("Error doing fstat on fd %d, file %s (%s)\n",
1197 fd_ptr->fd, fname,strerror(errno)));
1198 /* Ensure the ref_count is decremented. */
1199 fd_attempt_close(fd_ptr);
1204 /* Set the correct entries in fd_ptr. */
1205 fd_ptr->dev = (uint32)sbuf->st_dev;
1206 fd_ptr->inode = (uint32)sbuf->st_ino;
1208 Files[fnum].fd_ptr = fd_ptr;
1209 Connections[cnum].num_files_open++;
1210 Files[fnum].mode = sbuf->st_mode;
1211 GetTimeOfDay(&Files[fnum].open_time);
1212 Files[fnum].uid = current_user.id;
1213 Files[fnum].size = 0;
1214 Files[fnum].pos = -1;
1215 Files[fnum].open = True;
1216 Files[fnum].mmap_ptr = NULL;
1217 Files[fnum].mmap_size = 0;
1218 Files[fnum].can_lock = True;
1219 Files[fnum].can_read = ((flags & O_WRONLY)==0);
1220 Files[fnum].can_write = ((flags & (O_WRONLY|O_RDWR))!=0);
1221 Files[fnum].share_mode = 0;
1222 Files[fnum].print_file = Connections[cnum].printer;
1223 Files[fnum].modified = False;
1224 Files[fnum].cnum = cnum;
1225 string_set(&Files[fnum].name,dos_to_unix(fname,False));
1226 Files[fnum].wbmpx_ptr = NULL;
1229 * If the printer is marked as postscript output a leading
1230 * file identifier to ensure the file is treated as a raw
1232 * This has a similar effect as CtrlD=0 in WIN.INI file.
1233 * tim@fsg.com 09/06/94
1235 if (Files[fnum].print_file && POSTSCRIPT(cnum) &&
1236 Files[fnum].can_write)
1238 DEBUG(3,("Writing postscript line\n"));
1239 write_file(fnum,"%!\n",3);
1242 DEBUG(2,("%s %s opened file %s read=%s write=%s (numopen=%d fnum=%d)\n",
1243 timestring(),Connections[cnum].user,fname,
1244 BOOLSTR(Files[fnum].can_read),BOOLSTR(Files[fnum].can_write),
1245 Connections[cnum].num_files_open,fnum));
1250 /* mmap it if read-only */
1251 if (!Files[fnum].can_write)
1253 Files[fnum].mmap_size = file_size(fname);
1254 Files[fnum].mmap_ptr = (char *)mmap(NULL,Files[fnum].mmap_size,
1255 PROT_READ,MAP_SHARED,Files[fnum].fd_ptr->fd,0);
1257 if (Files[fnum].mmap_ptr == (char *)-1 || !Files[fnum].mmap_ptr)
1259 DEBUG(3,("Failed to mmap() %s - %s\n",fname,strerror(errno)));
1260 Files[fnum].mmap_ptr = NULL;
1266 /*******************************************************************
1268 ********************************************************************/
1269 void sync_file(int fnum)
1272 fsync(Files[fnum].fd_ptr->fd);
1276 /****************************************************************************
1277 run a file if it is a magic script
1278 ****************************************************************************/
1279 static void check_magic(int fnum,int cnum)
1281 if (!*lp_magicscript(SNUM(cnum)))
1284 DEBUG(5,("checking magic for %s\n",Files[fnum].name));
1288 if (!(p = strrchr(Files[fnum].name,'/')))
1289 p = Files[fnum].name;
1293 if (!strequal(lp_magicscript(SNUM(cnum)),p))
1299 pstring magic_output;
1301 strcpy(fname,Files[fnum].name);
1303 if (*lp_magicoutput(SNUM(cnum)))
1304 strcpy(magic_output,lp_magicoutput(SNUM(cnum)));
1306 sprintf(magic_output,"%s.out",fname);
1309 ret = smbrun(fname,magic_output,False);
1310 DEBUG(3,("Invoking magic command %s gave %d\n",fname,ret));
1316 /****************************************************************************
1317 close a file - possibly invalidating the read prediction
1318 ****************************************************************************/
1319 void close_file(int fnum)
1321 files_struct *fs_p = &Files[fnum];
1322 int cnum = fs_p->cnum;
1323 uint32 dev = fs_p->fd_ptr->dev;
1324 uint32 inode = fs_p->fd_ptr->inode;
1325 share_lock_token token;
1327 invalidate_read_prediction(fs_p->fd_ptr->fd);
1329 Connections[cnum].num_files_open--;
1332 free((char *)fs_p->wbmpx_ptr);
1333 fs_p->wbmpx_ptr = NULL;
1339 munmap(fs_p->mmap_ptr,fs_p->mmap_size);
1340 fs_p->mmap_ptr = NULL;
1344 if (lp_share_modes(SNUM(cnum)))
1346 lock_share_entry( cnum, dev, inode, &token);
1347 del_share_mode(token, fnum);
1350 fd_attempt_close(fs_p->fd_ptr);
1352 if (lp_share_modes(SNUM(cnum)))
1353 unlock_share_entry( cnum, dev, inode, token);
1355 /* NT uses smbclose to start a print - weird */
1356 if (fs_p->print_file)
1359 /* check for magic scripts */
1360 check_magic(fnum,cnum);
1362 DEBUG(2,("%s %s closed file %s (numopen=%d)\n",
1363 timestring(),Connections[cnum].user,fs_p->name,
1364 Connections[cnum].num_files_open));
1367 enum {AFAIL,AREAD,AWRITE,AALL};
1369 /*******************************************************************
1370 reproduce the share mode access table
1371 ********************************************************************/
1372 static int access_table(int new_deny,int old_deny,int old_mode,
1373 int share_pid,char *fname)
1375 if (new_deny == DENY_ALL || old_deny == DENY_ALL) return(AFAIL);
1377 if (new_deny == DENY_DOS || old_deny == DENY_DOS) {
1378 if (old_deny == new_deny && share_pid == getpid())
1381 if (old_mode == 0) return(AREAD);
1383 /* the new smbpub.zip spec says that if the file extension is
1384 .com, .dll, .exe or .sym then allow the open. I will force
1385 it to read-only as this seems sensible although the spec is
1386 a little unclear on this. */
1387 if ((fname = strrchr(fname,'.'))) {
1388 if (strequal(fname,".com") ||
1389 strequal(fname,".dll") ||
1390 strequal(fname,".exe") ||
1391 strequal(fname,".sym"))
1401 if (old_deny==DENY_WRITE && old_mode==0) return(AREAD);
1402 if (old_deny==DENY_READ && old_mode==0) return(AWRITE);
1403 if (old_deny==DENY_NONE && old_mode==0) return(AALL);
1406 if (old_deny==DENY_WRITE && old_mode==1) return(AREAD);
1407 if (old_deny==DENY_READ && old_mode==1) return(AWRITE);
1408 if (old_deny==DENY_NONE && old_mode==1) return(AALL);
1411 if (old_deny==DENY_WRITE) return(AREAD);
1412 if (old_deny==DENY_READ) return(AWRITE);
1413 if (old_deny==DENY_NONE) return(AALL);
1419 /*******************************************************************
1420 check if the share mode on a file allows it to be deleted or unlinked
1421 return True if sharing doesn't prevent the operation
1422 ********************************************************************/
1423 BOOL check_file_sharing(int cnum,char *fname)
1427 min_share_mode_entry *old_shares = 0;
1428 int num_share_modes;
1430 share_lock_token token;
1433 if(!lp_share_modes(SNUM(cnum)))
1436 if (stat(fname,&sbuf) == -1) return(True);
1438 lock_share_entry(cnum, (uint32)sbuf.st_dev, (uint32)sbuf.st_ino, &token);
1439 num_share_modes = get_share_modes(cnum, token,
1440 (uint32)sbuf.st_dev, (uint32)sbuf.st_ino, &old_shares);
1442 for( i = 0; i < num_share_modes; i++)
1444 if (old_shares[i].share_mode != DENY_DOS)
1447 if(old_shares[i].pid != pid)
1451 /* XXXX exactly what share mode combinations should be allowed for
1452 deleting/renaming? */
1453 /* If we got here then either there were no share modes or
1454 all share modes were DENY_DOS and the pid == getpid() */
1459 unlock_share_entry(cnum, (uint32)sbuf.st_dev, (uint32)sbuf.st_ino, token);
1460 if(old_shares != NULL)
1461 free((char *)old_shares);
1465 /****************************************************************************
1467 Helper for open_file_shared.
1468 Truncate a file after checking locking; close file if locked.
1469 **************************************************************************/
1470 static void truncate_unless_locked(int fnum, int cnum, share_lock_token token,
1473 if (Files[fnum].can_write){
1474 if (is_locked(fnum,cnum,0x3FFFFFFF,0)){
1475 /* If share modes are in force for this connection we
1476 have the share entry locked. Unlock it before closing. */
1477 if (*share_locked && lp_share_modes(SNUM(cnum)))
1478 unlock_share_entry( cnum, Files[fnum].fd_ptr->dev,
1479 Files[fnum].fd_ptr->inode, token);
1481 /* Share mode no longer locked. */
1482 *share_locked = False;
1484 unix_ERR_class = ERRDOS;
1485 unix_ERR_code = ERRlock;
1488 ftruncate(Files[fnum].fd_ptr->fd,0);
1493 /****************************************************************************
1494 open a file with a share mode
1495 ****************************************************************************/
1496 void open_file_shared(int fnum,int cnum,char *fname,int share_mode,int ofun,
1497 int mode,int *Access,int *action)
1499 files_struct *fs_p = &Files[fnum];
1502 int deny_mode = (share_mode>>4)&7;
1504 BOOL file_existed = file_exist(fname,&sbuf);
1505 BOOL share_locked = False;
1506 BOOL fcbopen = False;
1507 share_lock_token token;
1514 /* this is for OS/2 EAs - try and say we don't support them */
1515 if (strstr(fname,".+,;=[]."))
1517 unix_ERR_class = ERRDOS;
1518 unix_ERR_code = ERROR_EAS_NOT_SUPPORTED;
1522 if ((ofun & 0x3) == 0 && file_existed)
1530 if ((ofun & 0x3) == 2)
1533 /* note that we ignore the append flag as
1534 append does not mean the same thing under dos and unix */
1536 switch (share_mode&0xF)
1553 if (flags != O_RDONLY && file_existed &&
1554 (!CAN_WRITE(cnum) || IS_DOS_READONLY(dos_mode(cnum,fname,&sbuf))))
1564 if (deny_mode > DENY_NONE && deny_mode!=DENY_FCB)
1566 DEBUG(2,("Invalid deny mode %d on file %s\n",deny_mode,fname));
1571 if (deny_mode == DENY_FCB) deny_mode = DENY_DOS;
1573 if (lp_share_modes(SNUM(cnum)))
1577 min_share_mode_entry *old_shares = 0;
1582 dev = (uint32)sbuf.st_dev;
1583 inode = (uint32)sbuf.st_ino;
1584 lock_share_entry(cnum, dev, inode, &token);
1585 share_locked = True;
1586 num_shares = get_share_modes(cnum, token, dev, inode, &old_shares);
1589 for(i = 0; i < num_shares; i++)
1591 /* someone else has a share lock on it, check to see
1593 int old_open_mode = old_shares[i].share_mode &0xF;
1594 int old_deny_mode = (old_shares[i].share_mode >>4)&7;
1596 if (old_deny_mode > 4 || old_open_mode > 2)
1598 DEBUG(0,("Invalid share mode found (%d,%d,%d) on file %s\n",
1599 deny_mode,old_deny_mode,old_open_mode,fname));
1600 free((char *)old_shares);
1602 unlock_share_entry(cnum, dev, inode, token);
1604 unix_ERR_class = ERRDOS;
1605 unix_ERR_code = ERRbadshare;
1610 int access_allowed = access_table(deny_mode,old_deny_mode,old_open_mode,
1611 old_shares[i].pid,fname);
1613 if ((access_allowed == AFAIL) ||
1614 (!fcbopen && (access_allowed == AREAD && flags == O_RDWR)) ||
1615 (access_allowed == AREAD && flags == O_WRONLY) ||
1616 (access_allowed == AWRITE && flags == O_RDONLY))
1618 DEBUG(2,("Share violation on file (%d,%d,%d,%d,%s) = %d\n",
1619 deny_mode,old_deny_mode,old_open_mode,
1620 old_shares[i].pid,fname,
1622 free((char *)old_shares);
1624 unlock_share_entry(cnum, dev, inode, token);
1626 unix_ERR_class = ERRDOS;
1627 unix_ERR_code = ERRbadshare;
1631 if (access_allowed == AREAD)
1634 if (access_allowed == AWRITE)
1639 free((char *)old_shares);
1642 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o\n",
1643 flags,flags2,mode));
1645 open_file(fnum,cnum,fname,flags|(flags2&~(O_TRUNC)),mode,file_existed ? &sbuf : 0);
1646 if (!fs_p->open && flags==O_RDWR && errno!=ENOENT && fcbopen)
1649 open_file(fnum,cnum,fname,flags,mode,file_existed ? &sbuf : 0 );
1656 if((share_locked == False) && lp_share_modes(SNUM(cnum)))
1658 /* We created the file - thus we must now lock the share entry before creating it. */
1659 dev = fs_p->fd_ptr->dev;
1660 inode = fs_p->fd_ptr->inode;
1661 lock_share_entry(cnum, dev, inode, &token);
1662 share_locked = True;
1678 fs_p->share_mode = (deny_mode<<4) | open_mode;
1681 (*Access) = open_mode;
1685 if (file_existed && !(flags2 & O_TRUNC)) *action = 1;
1686 if (!file_existed) *action = 2;
1687 if (file_existed && (flags2 & O_TRUNC)) *action = 3;
1689 /* We must create the share mode entry before truncate as
1690 truncate can fail due to locking and have to close the
1691 file (which expects the share_mode_entry to be there).
1693 if (lp_share_modes(SNUM(cnum)))
1694 set_share_mode(token, fnum);
1696 if ((flags2&O_TRUNC) && file_existed)
1697 truncate_unless_locked(fnum,cnum,token,&share_locked);
1700 if (share_locked && lp_share_modes(SNUM(cnum)))
1701 unlock_share_entry( cnum, dev, inode, token);
1704 /****************************************************************************
1705 seek a file. Try to avoid the seek if possible
1706 ****************************************************************************/
1707 int seek_file(int fnum,int pos)
1710 if (Files[fnum].print_file && POSTSCRIPT(Files[fnum].cnum))
1713 Files[fnum].pos = lseek(Files[fnum].fd_ptr->fd,pos+offset,SEEK_SET) - offset;
1714 return(Files[fnum].pos);
1717 /****************************************************************************
1719 ****************************************************************************/
1720 int read_file(int fnum,char *data,int pos,int n)
1724 if (!Files[fnum].can_write)
1726 ret = read_predict(Files[fnum].fd_ptr->fd,pos,data,NULL,n);
1734 if (Files[fnum].mmap_ptr)
1736 int num = MIN(n,Files[fnum].mmap_size-pos);
1739 memcpy(data,Files[fnum].mmap_ptr+pos,num);
1751 if (seek_file(fnum,pos) != pos)
1753 DEBUG(3,("Failed to seek to %d\n",pos));
1758 readret = read(Files[fnum].fd_ptr->fd,data,n);
1759 if (readret > 0) ret += readret;
1766 /****************************************************************************
1768 ****************************************************************************/
1769 int write_file(int fnum,char *data,int n)
1771 if (!Files[fnum].can_write) {
1776 if (!Files[fnum].modified) {
1778 Files[fnum].modified = True;
1779 if (fstat(Files[fnum].fd_ptr->fd,&st) == 0) {
1780 int dosmode = dos_mode(Files[fnum].cnum,Files[fnum].name,&st);
1781 if (MAP_ARCHIVE(Files[fnum].cnum) && !IS_DOS_ARCHIVE(dosmode)) {
1782 dos_chmod(Files[fnum].cnum,Files[fnum].name,dosmode | aARCH,&st);
1787 return(write_data(Files[fnum].fd_ptr->fd,data,n));
1791 /****************************************************************************
1792 load parameters specific to a connection/service
1793 ****************************************************************************/
1794 BOOL become_service(int cnum,BOOL do_chdir)
1796 extern char magic_char;
1797 static int last_cnum = -1;
1800 if (!OPEN_CNUM(cnum))
1806 Connections[cnum].lastused = smb_last_time;
1811 ChDir(Connections[cnum].connectpath) != 0 &&
1812 ChDir(Connections[cnum].origpath) != 0)
1814 DEBUG(0,("%s chdir (%s) failed cnum=%d\n",timestring(),
1815 Connections[cnum].connectpath,cnum));
1819 if (cnum == last_cnum)
1824 case_default = lp_defaultcase(snum);
1825 case_preserve = lp_preservecase(snum);
1826 short_case_preserve = lp_shortpreservecase(snum);
1827 case_mangle = lp_casemangle(snum);
1828 case_sensitive = lp_casesensitive(snum);
1829 magic_char = lp_magicchar(snum);
1830 use_mangled_map = (*lp_mangled_map(snum) ? True:False);
1835 /****************************************************************************
1836 find a service entry
1837 ****************************************************************************/
1838 int find_service(char *service)
1842 string_sub(service,"\\","/");
1844 iService = lp_servicenumber(service);
1846 /* now handle the special case of a home directory */
1849 char *phome_dir = get_home_dir(service);
1850 DEBUG(3,("checking for home directory %s gave %s\n",service,
1851 phome_dir?phome_dir:"(NULL)"));
1855 if ((iHomeService = lp_servicenumber(HOMES_NAME)) >= 0)
1857 lp_add_home(service,iHomeService,phome_dir);
1858 iService = lp_servicenumber(service);
1863 /* If we still don't have a service, attempt to add it as a printer. */
1866 int iPrinterService;
1868 if ((iPrinterService = lp_servicenumber(PRINTERS_NAME)) >= 0)
1872 DEBUG(3,("checking whether %s is a valid printer name...\n", service));
1874 if ((pszTemp != NULL) && pcap_printername_ok(service, pszTemp))
1876 DEBUG(3,("%s is a valid printer name\n", service));
1877 DEBUG(3,("adding %s as a printer service\n", service));
1878 lp_add_printer(service,iPrinterService);
1879 iService = lp_servicenumber(service);
1881 DEBUG(0,("failed to add %s as a printer service!\n", service));
1884 DEBUG(3,("%s is not a valid printer name\n", service));
1888 /* just possibly it's a default service? */
1891 char *defservice = lp_defaultservice();
1892 if (defservice && *defservice && !strequal(defservice,service)) {
1893 iService = find_service(defservice);
1894 if (iService >= 0) {
1895 string_sub(service,"_","/");
1896 iService = lp_add_service(service,iService);
1902 if (!VALID_SNUM(iService))
1904 DEBUG(0,("Invalid snum %d for %s\n",iService,service));
1909 DEBUG(3,("find_service() failed to find service %s\n", service));
1915 /****************************************************************************
1916 create an error packet from a cached error.
1917 ****************************************************************************/
1918 int cached_error_packet(char *inbuf,char *outbuf,int fnum,int line)
1920 write_bmpx_struct *wbmpx = Files[fnum].wbmpx_ptr;
1922 int32 eclass = wbmpx->wr_errclass;
1923 int32 err = wbmpx->wr_error;
1925 /* We can now delete the auxiliary struct */
1926 free((char *)wbmpx);
1927 Files[fnum].wbmpx_ptr = NULL;
1928 return error_packet(inbuf,outbuf,eclass,err,line);
1937 } unix_smb_errmap[] =
1939 {EPERM,ERRDOS,ERRnoaccess},
1940 {EACCES,ERRDOS,ERRnoaccess},
1941 {ENOENT,ERRDOS,ERRbadfile},
1942 {EIO,ERRHRD,ERRgeneral},
1943 {EBADF,ERRSRV,ERRsrverror},
1944 {EINVAL,ERRSRV,ERRsrverror},
1945 {EEXIST,ERRDOS,ERRfilexists},
1946 {ENFILE,ERRDOS,ERRnofids},
1947 {EMFILE,ERRDOS,ERRnofids},
1948 {ENOSPC,ERRHRD,ERRdiskfull},
1950 {EDQUOT,ERRHRD,ERRdiskfull},
1953 {ENOTEMPTY,ERRDOS,ERRnoaccess},
1956 {EXDEV,ERRDOS,ERRdiffdevice},
1958 {EROFS,ERRHRD,ERRnowrite},
1963 /****************************************************************************
1964 create an error packet from errno
1965 ****************************************************************************/
1966 int unix_error_packet(char *inbuf,char *outbuf,int def_class,uint32 def_code,int line)
1968 int eclass=def_class;
1972 if (unix_ERR_class != SUCCESS)
1974 eclass = unix_ERR_class;
1975 ecode = unix_ERR_code;
1976 unix_ERR_class = SUCCESS;
1981 while (unix_smb_errmap[i].smbclass != 0)
1983 if (unix_smb_errmap[i].unixerror == errno)
1985 eclass = unix_smb_errmap[i].smbclass;
1986 ecode = unix_smb_errmap[i].smbcode;
1993 return(error_packet(inbuf,outbuf,eclass,ecode,line));
1997 /****************************************************************************
1998 create an error packet. Normally called using the ERROR() macro
1999 ****************************************************************************/
2000 int error_packet(char *inbuf,char *outbuf,int error_class,uint32 error_code,int line)
2002 int outsize = set_message(outbuf,0,0,True);
2004 cmd = CVAL(inbuf,smb_com);
2006 CVAL(outbuf,smb_rcls) = error_class;
2007 SSVAL(outbuf,smb_err,error_code);
2009 DEBUG(3,("%s error packet at line %d cmd=%d (%s) eclass=%d ecode=%d\n",
2012 (int)CVAL(inbuf,smb_com),
2013 smb_fn_name(CVAL(inbuf,smb_com)),
2018 DEBUG(3,("error string = %s\n",strerror(errno)));
2024 #ifndef SIGCLD_IGNORE
2025 /****************************************************************************
2026 this prevents zombie child processes
2027 ****************************************************************************/
2028 static int sig_cld()
2030 static int depth = 0;
2033 DEBUG(0,("ERROR: Recursion in sig_cld? Perhaps you need `#define USE_WAITPID'?\n"));
2039 BlockSignals(True,SIGCLD);
2040 DEBUG(5,("got SIGCLD\n"));
2043 while (sys_waitpid((pid_t)-1,(int *)NULL, WNOHANG) > 0);
2047 /* Stevens, Adv. Unix Prog. says that on system V you must call
2048 wait before reinstalling the signal handler, because the kernel
2049 calls the handler from within the signal-call when there is a
2050 child that has exited. This would lead to an infinite recursion
2051 if done vice versa. */
2053 #ifndef DONT_REINSTALL_SIG
2054 #ifdef SIGCLD_IGNORE
2055 signal(SIGCLD, SIG_IGN);
2057 signal(SIGCLD, SIGNAL_CAST sig_cld);
2062 while (wait3(WAIT3_CAST1 NULL, WNOHANG, WAIT3_CAST2 NULL) > 0);
2065 BlockSignals(False,SIGCLD);
2070 /****************************************************************************
2071 this is called when the client exits abruptly
2072 **************************************************************************/
2073 static int sig_pipe()
2075 extern int password_client;
2076 BlockSignals(True,SIGPIPE);
2078 if (password_client != -1) {
2079 DEBUG(3,("lost connection to password server\n"));
2080 close(password_client);
2081 password_client = -1;
2082 #ifndef DONT_REINSTALL_SIG
2083 signal(SIGPIPE, SIGNAL_CAST sig_pipe);
2085 BlockSignals(False,SIGPIPE);
2089 exit_server("Got sigpipe\n");
2093 /****************************************************************************
2094 open the socket communication
2095 ****************************************************************************/
2096 static BOOL open_sockets(BOOL is_daemon,int port)
2103 struct sockaddr addr;
2104 int in_addrlen = sizeof(addr);
2107 #ifdef SIGCLD_IGNORE
2108 signal(SIGCLD, SIG_IGN);
2110 signal(SIGCLD, SIGNAL_CAST sig_cld);
2113 /* open an incoming socket */
2114 s = open_socket_in(SOCK_STREAM, port, 0,interpret_addr(lp_socket_address()));
2118 /* ready to listen */
2119 if (listen(s, 5) == -1)
2121 DEBUG(0,("listen: %s\n",strerror(errno)));
2129 /* now accept incoming connections - forking a new process
2130 for each incoming connection */
2131 DEBUG(2,("waiting for a connection\n"));
2134 Client = accept(s,&addr,&in_addrlen);
2136 if (Client == -1 && errno == EINTR)
2141 DEBUG(0,("accept: %s\n",strerror(errno)));
2145 #ifdef NO_FORK_DEBUG
2146 #ifndef NO_SIGNAL_TEST
2147 signal(SIGPIPE, SIGNAL_CAST sig_pipe);
2148 signal(SIGCLD, SIGNAL_CAST SIG_DFL);
2152 if (Client != -1 && fork()==0)
2154 /* Child code ... */
2155 #ifndef NO_SIGNAL_TEST
2156 signal(SIGPIPE, SIGNAL_CAST sig_pipe);
2157 signal(SIGCLD, SIGNAL_CAST SIG_DFL);
2159 /* close the listening socket */
2162 /* close our standard file descriptors */
2166 set_socket_options(Client,"SO_KEEPALIVE");
2167 set_socket_options(Client,user_socket_options);
2169 /* Reset global variables in util.c so that
2170 client substitutions will be done correctly
2173 reset_globals_after_fork();
2176 close(Client); /* The parent doesn't need this socket */
2182 /* We will abort gracefully when the client or remote system
2184 #ifndef NO_SIGNAL_TEST
2185 signal(SIGPIPE, SIGNAL_CAST sig_pipe);
2189 /* close our standard file descriptors */
2192 set_socket_options(Client,"SO_KEEPALIVE");
2193 set_socket_options(Client,user_socket_options);
2200 /****************************************************************************
2201 check if a snum is in use
2202 ****************************************************************************/
2203 BOOL snum_used(int snum)
2206 for (i=0;i<MAX_CONNECTIONS;i++)
2207 if (OPEN_CNUM(i) && (SNUM(i) == snum))
2212 /****************************************************************************
2213 reload the services file
2214 **************************************************************************/
2215 BOOL reload_services(BOOL test)
2222 strcpy(fname,lp_configfile());
2223 if (file_exist(fname,NULL) && !strcsequal(fname,servicesf))
2225 strcpy(servicesf,fname);
2232 if (test && !lp_file_list_changed())
2235 lp_killunused(snum_used);
2237 ret = lp_load(servicesf,False);
2239 /* perhaps the config filename is now set */
2241 reload_services(True);
2250 set_socket_options(Client,"SO_KEEPALIVE");
2251 set_socket_options(Client,user_socket_options);
2255 create_mangled_stack(lp_mangledstack());
2257 /* this forces service parameters to be flushed */
2258 become_service(-1,True);
2265 /****************************************************************************
2266 this prevents zombie child processes
2267 ****************************************************************************/
2268 static int sig_hup()
2270 BlockSignals(True,SIGHUP);
2271 DEBUG(0,("Got SIGHUP\n"));
2272 reload_services(False);
2273 #ifndef DONT_REINSTALL_SIG
2274 signal(SIGHUP,SIGNAL_CAST sig_hup);
2276 BlockSignals(False,SIGHUP);
2280 /****************************************************************************
2281 Setup the groups a user belongs to.
2282 ****************************************************************************/
2283 int setup_groups(char *user, int uid, int gid, int *p_ngroups,
2284 int **p_igroups, gid_t **p_groups)
2286 if (-1 == initgroups(user,gid))
2290 DEBUG(0,("Unable to initgroups!\n"));
2291 if (gid < 0 || gid > 16000 || uid < 0 || uid > 16000)
2292 DEBUG(0,("This is probably a problem with the account %s\n",user));
2300 ngroups = getgroups(0,&grp);
2303 igroups = (int *)malloc(sizeof(int)*ngroups);
2304 for (i=0;i<ngroups;i++)
2305 igroups[i] = 0x42424242;
2306 ngroups = getgroups(ngroups,(gid_t *)igroups);
2308 if (igroups[0] == 0x42424242)
2311 *p_ngroups = ngroups;
2313 /* The following bit of code is very strange. It is due to the
2314 fact that some OSes use int* and some use gid_t* for
2315 getgroups, and some (like SunOS) use both, one in prototypes,
2316 and one in man pages and the actual code. Thus we detect it
2317 dynamically using some very ugly code */
2320 /* does getgroups return ints or gid_t ?? */
2321 static BOOL groups_use_ints = True;
2323 if (groups_use_ints &&
2325 SVAL(igroups,2) == 0x4242)
2326 groups_use_ints = False;
2328 for (i=0;groups_use_ints && i<ngroups;i++)
2329 if (igroups[i] == 0x42424242)
2330 groups_use_ints = False;
2332 if (groups_use_ints)
2334 *p_igroups = igroups;
2335 *p_groups = (gid_t *)igroups;
2339 gid_t *groups = (gid_t *)igroups;
2340 igroups = (int *)malloc(sizeof(int)*ngroups);
2341 for (i=0;i<ngroups;i++)
2342 igroups[i] = groups[i];
2343 *p_igroups = igroups;
2344 *p_groups = (gid_t *)groups;
2347 DEBUG(3,("%s is in %d groups\n",user,ngroups));
2348 for (i=0;i<ngroups;i++)
2349 DEBUG(3,("%d ",igroups[i]));
2355 /****************************************************************************
2356 make a connection to a service
2357 ****************************************************************************/
2358 int make_connection(char *service,char *user,char *password, int pwlen, char *dev,uint16 vuid)
2362 struct passwd *pass = NULL;
2363 connection_struct *pcon;
2366 static BOOL first_connection = True;
2370 snum = find_service(service);
2373 if (strequal(service,"IPC$"))
2375 DEBUG(3,("%s refusing IPC connection\n",timestring()));
2379 DEBUG(0,("%s couldn't find service %s\n",timestring(),service));
2383 if (strequal(service,HOMES_NAME))
2385 if (*user && Get_Pwnam(user,True))
2386 return(make_connection(user,user,password,pwlen,dev,vuid));
2388 if (validated_username(vuid))
2390 strcpy(user,validated_username(vuid));
2391 return(make_connection(user,user,password,pwlen,dev,vuid));
2395 if (!lp_snum_ok(snum) || !check_access(snum)) {
2399 /* you can only connect to the IPC$ service as an ipc device */
2400 if (strequal(service,"IPC$"))
2403 if (*dev == '?' || !*dev)
2405 if (lp_print_ok(snum))
2406 strcpy(dev,"LPT1:");
2411 /* if the request is as a printer and you can't print then refuse */
2413 if (!lp_print_ok(snum) && (strncmp(dev,"LPT",3) == 0)) {
2414 DEBUG(1,("Attempt to connect to non-printer as a printer\n"));
2418 /* lowercase the user name */
2421 /* add it as a possible user name */
2422 add_session_user(service);
2424 /* shall we let them in? */
2425 if (!authorise_login(snum,user,password,pwlen,&guest,&force,vuid))
2427 DEBUG(2,("%s invalid username/password for %s\n",timestring(),service));
2431 cnum = find_free_connection(str_checksum(service) + str_checksum(user));
2434 DEBUG(0,("%s couldn't find free connection\n",timestring()));
2438 pcon = &Connections[cnum];
2439 bzero((char *)pcon,sizeof(*pcon));
2441 /* find out some info about the user */
2442 pass = Get_Pwnam(user,True);
2446 DEBUG(0,("%s couldn't find account %s\n",timestring(),user));
2450 pcon->read_only = lp_readonly(snum);
2454 StrnCpy(list,lp_readlist(snum),sizeof(pstring)-1);
2455 string_sub(list,"%S",service);
2457 if (user_in_list(user,list))
2458 pcon->read_only = True;
2460 StrnCpy(list,lp_writelist(snum),sizeof(pstring)-1);
2461 string_sub(list,"%S",service);
2463 if (user_in_list(user,list))
2464 pcon->read_only = False;
2467 /* admin user check */
2468 if (user_in_list(user,lp_admin_users(snum)) &&
2471 pcon->admin_user = True;
2472 DEBUG(0,("%s logged in as admin user (root privileges)\n",user));
2475 pcon->admin_user = False;
2477 pcon->force_user = force;
2479 pcon->uid = pass->pw_uid;
2480 pcon->gid = pass->pw_gid;
2481 pcon->num_files_open = 0;
2482 pcon->lastused = time(NULL);
2483 pcon->service = snum;
2485 pcon->printer = (strncmp(dev,"LPT",3) == 0);
2486 pcon->ipc = (strncmp(dev,"IPC",3) == 0);
2487 pcon->dirptr = NULL;
2488 pcon->veto_list = NULL;
2489 pcon->hide_list = NULL;
2490 string_set(&pcon->dirpath,"");
2491 string_set(&pcon->user,user);
2494 if (*lp_force_group(snum))
2499 StrnCpy(gname,lp_force_group(snum),sizeof(pstring)-1);
2500 /* default service may be a group name */
2501 string_sub(gname,"%S",service);
2502 gptr = (struct group *)getgrnam(gname);
2506 pcon->gid = gptr->gr_gid;
2507 DEBUG(3,("Forced group %s\n",gname));
2510 DEBUG(1,("Couldn't find group %s\n",gname));
2514 if (*lp_force_user(snum))
2516 struct passwd *pass2;
2518 strcpy(fuser,lp_force_user(snum));
2519 pass2 = (struct passwd *)Get_Pwnam(fuser,True);
2522 pcon->uid = pass2->pw_uid;
2523 string_set(&pcon->user,fuser);
2525 pcon->force_user = True;
2526 DEBUG(3,("Forced user %s\n",fuser));
2529 DEBUG(1,("Couldn't find user %s\n",fuser));
2534 strcpy(s,lp_pathname(snum));
2535 standard_sub(cnum,s);
2536 string_set(&pcon->connectpath,s);
2537 DEBUG(3,("Connect path is %s\n",s));
2540 /* groups stuff added by ih */
2542 pcon->groups = NULL;
2546 /* Find all the groups this uid is in and store them. Used by become_user() */
2547 setup_groups(pcon->user,pcon->uid,pcon->gid,&pcon->ngroups,&pcon->igroups,&pcon->groups);
2549 /* check number of connections */
2550 if (!claim_connection(cnum,
2551 lp_servicename(SNUM(cnum)),
2552 lp_max_connections(SNUM(cnum)),False))
2554 DEBUG(1,("too many connections - rejected\n"));
2558 if (lp_status(SNUM(cnum)))
2559 claim_connection(cnum,"STATUS.",MAXSTATUS,first_connection);
2561 first_connection = False;
2566 /* execute any "root preexec = " line */
2567 if (*lp_rootpreexec(SNUM(cnum)))
2570 strcpy(cmd,lp_rootpreexec(SNUM(cnum)));
2571 standard_sub(cnum,cmd);
2572 DEBUG(5,("cmd=%s\n",cmd));
2573 smbrun(cmd,NULL,False);
2576 if (!become_user(cnum,pcon->vuid))
2578 DEBUG(0,("Can't become connected user!\n"));
2580 if (!IS_IPC(cnum)) {
2581 yield_connection(cnum,
2582 lp_servicename(SNUM(cnum)),
2583 lp_max_connections(SNUM(cnum)));
2584 if (lp_status(SNUM(cnum))) yield_connection(cnum,"STATUS.",MAXSTATUS);
2589 if (ChDir(pcon->connectpath) != 0)
2591 DEBUG(0,("Can't change directory to %s (%s)\n",
2592 pcon->connectpath,strerror(errno)));
2595 if (!IS_IPC(cnum)) {
2596 yield_connection(cnum,
2597 lp_servicename(SNUM(cnum)),
2598 lp_max_connections(SNUM(cnum)));
2599 if (lp_status(SNUM(cnum))) yield_connection(cnum,"STATUS.",MAXSTATUS);
2604 string_set(&pcon->origpath,pcon->connectpath);
2606 #if SOFTLINK_OPTIMISATION
2607 /* resolve any soft links early */
2610 strcpy(s,pcon->connectpath);
2612 string_set(&pcon->connectpath,s);
2613 ChDir(pcon->connectpath);
2617 num_connections_open++;
2618 add_session_user(user);
2620 /* execute any "preexec = " line */
2621 if (*lp_preexec(SNUM(cnum)))
2624 strcpy(cmd,lp_preexec(SNUM(cnum)));
2625 standard_sub(cnum,cmd);
2626 smbrun(cmd,NULL,False);
2629 /* we've finished with the sensitive stuff */
2632 /* Add veto/hide lists */
2633 if (!IS_IPC(cnum) && !IS_PRINT(cnum))
2635 set_namearray( &pcon->veto_list, lp_veto_files(SNUM(cnum)));
2636 set_namearray( &pcon->hide_list, lp_hide_files(SNUM(cnum)));
2640 DEBUG(IS_IPC(cnum)?3:1,("%s %s (%s) connect to service %s as user %s (uid=%d,gid=%d) (pid %d)\n",
2644 lp_servicename(SNUM(cnum)),user,
2654 /****************************************************************************
2655 find first available file slot
2656 ****************************************************************************/
2657 int find_free_file(void )
2660 /* we start at 1 here for an obscure reason I can't now remember,
2661 but I think is important :-) */
2662 for (i=1;i<MAX_OPEN_FILES;i++)
2665 DEBUG(1,("ERROR! Out of file structures - perhaps increase MAX_OPEN_FILES?\n"));
2669 /****************************************************************************
2670 find first available connection slot, starting from a random position.
2671 The randomisation stops problems with the server dieing and clients
2672 thinking the server is still available.
2673 ****************************************************************************/
2674 static int find_free_connection(int hash )
2678 hash = (hash % (MAX_CONNECTIONS-2))+1;
2682 for (i=hash+1;i!=hash;)
2684 if (!Connections[i].open && Connections[i].used == used)
2686 DEBUG(3,("found free connection number %d\n",i));
2690 if (i == MAX_CONNECTIONS)
2700 DEBUG(1,("ERROR! Out of connection structures\n"));
2705 /****************************************************************************
2706 reply for the core protocol
2707 ****************************************************************************/
2708 int reply_corep(char *outbuf)
2710 int outsize = set_message(outbuf,1,0,True);
2712 Protocol = PROTOCOL_CORE;
2718 /****************************************************************************
2719 reply for the coreplus protocol
2720 ****************************************************************************/
2721 int reply_coreplus(char *outbuf)
2723 int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
2724 int outsize = set_message(outbuf,13,0,True);
2725 SSVAL(outbuf,smb_vwv5,raw); /* tell redirector we support
2726 readbraw and writebraw (possibly) */
2727 CVAL(outbuf,smb_flg) = 0x81; /* Reply, SMBlockread, SMBwritelock supported */
2728 SSVAL(outbuf,smb_vwv1,0x1); /* user level security, don't encrypt */
2730 Protocol = PROTOCOL_COREPLUS;
2736 /****************************************************************************
2737 reply for the lanman 1.0 protocol
2738 ****************************************************************************/
2739 int reply_lanman1(char *outbuf)
2741 int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
2743 BOOL doencrypt = SMBENCRYPT();
2744 time_t t = time(NULL);
2745 /* We need to save and restore this as it can be destroyed
2746 if we call another server if security=server
2747 Thanks to Paul Nelson @ Thursby for pointing this out.
2749 uint16 mid = SVAL(outbuf, smb_mid);
2751 if (lp_security()>=SEC_USER) secword |= 1;
2752 if (doencrypt) secword |= 2;
2754 set_message(outbuf,13,doencrypt?8:0,True);
2755 SSVAL(outbuf,smb_vwv1,secword);
2757 /* Create a token value and add it to the outgoing packet. */
2759 generate_next_challenge(smb_buf(outbuf));
2762 Protocol = PROTOCOL_LANMAN1;
2764 if (lp_security() == SEC_SERVER && server_cryptkey(outbuf)) {
2765 DEBUG(3,("using password server validation\n"));
2767 if (doencrypt) set_challenge(smb_buf(outbuf));
2771 CVAL(outbuf,smb_flg) = 0x81; /* Reply, SMBlockread, SMBwritelock supported */
2772 SSVAL(outbuf,smb_mid,mid); /* Restore possibly corrupted mid */
2773 SSVAL(outbuf,smb_vwv2,max_recv);
2774 SSVAL(outbuf,smb_vwv3,lp_maxmux()); /* maxmux */
2775 SSVAL(outbuf,smb_vwv4,1);
2776 SSVAL(outbuf,smb_vwv5,raw); /* tell redirector we support
2777 readbraw writebraw (possibly) */
2778 SIVAL(outbuf,smb_vwv6,getpid());
2779 SSVAL(outbuf,smb_vwv10, TimeDiff(t)/60);
2781 put_dos_date(outbuf,smb_vwv8,t);
2783 return (smb_len(outbuf)+4);
2787 /****************************************************************************
2788 reply for the lanman 2.0 protocol
2789 ****************************************************************************/
2790 int reply_lanman2(char *outbuf)
2792 int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
2794 BOOL doencrypt = SMBENCRYPT();
2795 time_t t = time(NULL);
2796 /* We need to save and restore this as it can be destroyed
2797 if we call another server if security=server
2798 Thanks to Paul Nelson @ Thursby for pointing this out.
2800 uint16 mid = SVAL(outbuf, smb_mid);
2802 if (lp_security()>=SEC_USER) secword |= 1;
2803 if (doencrypt) secword |= 2;
2805 set_message(outbuf,13,doencrypt?8:0,True);
2806 SSVAL(outbuf,smb_vwv1,secword);
2808 /* Create a token value and add it to the outgoing packet. */
2810 generate_next_challenge(smb_buf(outbuf));
2813 SIVAL(outbuf,smb_vwv6,getpid());
2815 Protocol = PROTOCOL_LANMAN2;
2817 if (lp_security() == SEC_SERVER && server_cryptkey(outbuf)) {
2818 DEBUG(3,("using password server validation\n"));
2820 if (doencrypt) set_challenge(smb_buf(outbuf));
2824 CVAL(outbuf,smb_flg) = 0x81; /* Reply, SMBlockread, SMBwritelock supported */
2825 SSVAL(outbuf,smb_mid,mid); /* Restore possibly corrupted mid */
2826 SSVAL(outbuf,smb_vwv2,max_recv);
2827 SSVAL(outbuf,smb_vwv3,lp_maxmux());
2828 SSVAL(outbuf,smb_vwv4,1);
2829 SSVAL(outbuf,smb_vwv5,raw); /* readbraw and/or writebraw */
2830 SSVAL(outbuf,smb_vwv10, TimeDiff(t)/60);
2831 put_dos_date(outbuf,smb_vwv8,t);
2833 return (smb_len(outbuf)+4);
2837 /****************************************************************************
2838 reply for the nt protocol
2839 ****************************************************************************/
2840 int reply_nt1(char *outbuf)
2842 /* dual names + lock_and_read + nt SMBs + remote API calls */
2843 int capabilities = CAP_NT_FIND|CAP_LOCK_AND_READ;
2845 other valid capabilities which we may support at some time...
2846 CAP_LARGE_FILES|CAP_NT_SMBS|CAP_RPC_REMOTE_APIS;
2847 CAP_LARGE_FILES|CAP_LARGE_READX|
2848 CAP_STATUS32|CAP_LEVEL_II_OPLOCKS;
2852 BOOL doencrypt = SMBENCRYPT();
2853 time_t t = time(NULL);
2856 char challenge_len = 8;
2857 /* We need to save and restore this as it can be destroyed
2858 if we call another server if security=server
2859 Thanks to Paul Nelson @ Thursby for pointing this out.
2861 uint16 mid = SVAL(outbuf, smb_mid);
2863 if (lp_readraw() && lp_writeraw())
2865 capabilities |= CAP_RAW_MODE;
2868 if (lp_security()>=SEC_USER) secword |= 1;
2869 if (doencrypt) secword |= 2;
2871 /* decide where (if) to put the encryption challenge, and
2872 follow it with the OEM'd domain name
2874 encrypt_len = doencrypt?challenge_len:0;
2876 data_len = encrypt_len + 2*(strlen(myworkgroup)+1);
2878 data_len = encrypt_len + strlen(myworkgroup) + 1;
2881 set_message(outbuf,17,data_len,True);
2884 /* put the OEM'd domain name */
2885 PutUniCode(smb_buf(outbuf)+encrypt_len,myworkgroup);
2887 strcpy(smb_buf(outbuf)+encrypt_len, myworkgroup);
2890 CVAL(outbuf,smb_vwv1) = secword;
2892 /* Create a token value and add it to the outgoing packet. */
2895 generate_next_challenge(smb_buf(outbuf));
2897 /* Tell the nt machine how long the challenge is. */
2898 SSVALS(outbuf,smb_vwv16+1,challenge_len);
2902 Protocol = PROTOCOL_NT1;
2904 if (lp_security() == SEC_SERVER && server_cryptkey(outbuf)) {
2905 DEBUG(3,("using password server validation\n"));
2907 if (doencrypt) set_challenge(smb_buf(outbuf));
2911 SSVAL(outbuf,smb_mid,mid); /* Restore possibly corrupted mid */
2912 SSVAL(outbuf,smb_vwv1+1,lp_maxmux()); /* maxmpx */
2913 SSVAL(outbuf,smb_vwv2+1,1); /* num vcs */
2914 SIVAL(outbuf,smb_vwv3+1,0xffff); /* max buffer. LOTS! */
2915 SIVAL(outbuf,smb_vwv5+1,0xffff); /* raw size. LOTS! */
2916 SIVAL(outbuf,smb_vwv7+1,getpid()); /* session key */
2917 SIVAL(outbuf,smb_vwv9+1,capabilities); /* capabilities */
2918 put_long_date(outbuf+smb_vwv11+1,t);
2919 SSVALS(outbuf,smb_vwv15+1,TimeDiff(t)/60);
2920 SSVAL(outbuf,smb_vwv17,data_len); /* length of challenge+domain strings */
2922 return (smb_len(outbuf)+4);
2925 /* these are the protocol lists used for auto architecture detection:
2928 protocol [PC NETWORK PROGRAM 1.0]
2929 protocol [XENIX CORE]
2930 protocol [MICROSOFT NETWORKS 1.03]
2931 protocol [LANMAN1.0]
2932 protocol [Windows for Workgroups 3.1a]
2933 protocol [LM1.2X002]
2934 protocol [LANMAN2.1]
2935 protocol [NT LM 0.12]
2938 protocol [PC NETWORK PROGRAM 1.0]
2939 protocol [XENIX CORE]
2940 protocol [MICROSOFT NETWORKS 1.03]
2941 protocol [LANMAN1.0]
2942 protocol [Windows for Workgroups 3.1a]
2943 protocol [LM1.2X002]
2944 protocol [LANMAN2.1]
2945 protocol [NT LM 0.12]
2948 protocol [PC NETWORK PROGRAM 1.0]
2949 protocol [XENIX CORE]
2950 protocol [LANMAN1.0]
2951 protocol [LM1.2X002]
2952 protocol [LANMAN2.1]
2956 * Modified to recognize the architecture of the remote machine better.
2958 * This appears to be the matrix of which protocol is used by which
2960 Protocol WfWg Win95 WinNT OS/2
2961 PC NETWORK PROGRAM 1.0 1 1 1 1
2963 MICROSOFT NETWORKS 3.0 2 2
2965 MICROSOFT NETWORKS 1.03 3
2968 Windows for Workgroups 3.1a 5 5 5
2973 * tim@fsg.com 09/29/95
2976 #define ARCH_WFWG 0x3 /* This is a fudge because WfWg is like Win95 */
2977 #define ARCH_WIN95 0x2
2978 #define ARCH_OS2 0xC /* Again OS/2 is like NT */
2979 #define ARCH_WINNT 0x8
2980 #define ARCH_SAMBA 0x10
2982 #define ARCH_ALL 0x1F
2984 /* List of supported protocols, most desired first */
2988 int (*proto_reply_fn)(char *);
2990 } supported_protocols[] = {
2991 {"NT LANMAN 1.0", "NT1", reply_nt1, PROTOCOL_NT1},
2992 {"NT LM 0.12", "NT1", reply_nt1, PROTOCOL_NT1},
2993 {"LM1.2X002", "LANMAN2", reply_lanman2, PROTOCOL_LANMAN2},
2994 {"Samba", "LANMAN2", reply_lanman2, PROTOCOL_LANMAN2},
2995 {"DOS LM1.2X002", "LANMAN2", reply_lanman2, PROTOCOL_LANMAN2},
2996 {"LANMAN1.0", "LANMAN1", reply_lanman1, PROTOCOL_LANMAN1},
2997 {"MICROSOFT NETWORKS 3.0", "LANMAN1", reply_lanman1, PROTOCOL_LANMAN1},
2998 {"MICROSOFT NETWORKS 1.03", "COREPLUS", reply_coreplus, PROTOCOL_COREPLUS},
2999 {"PC NETWORK PROGRAM 1.0", "CORE", reply_corep, PROTOCOL_CORE},
3004 /****************************************************************************
3006 ****************************************************************************/
3007 static int reply_negprot(char *inbuf,char *outbuf)
3009 extern fstring remote_arch;
3010 int outsize = set_message(outbuf,1,0,True);
3015 int bcc = SVAL(smb_buf(inbuf),-2);
3016 int arch = ARCH_ALL;
3018 p = smb_buf(inbuf)+1;
3019 while (p < (smb_buf(inbuf) + bcc))
3022 DEBUG(3,("Requested protocol [%s]\n",p));
3023 if (strcsequal(p,"Windows for Workgroups 3.1a"))
3024 arch &= ( ARCH_WFWG | ARCH_WIN95 | ARCH_WINNT );
3025 else if (strcsequal(p,"DOS LM1.2X002"))
3026 arch &= ( ARCH_WFWG | ARCH_WIN95 );
3027 else if (strcsequal(p,"DOS LANMAN2.1"))
3028 arch &= ( ARCH_WFWG | ARCH_WIN95 );
3029 else if (strcsequal(p,"NT LM 0.12"))
3030 arch &= ( ARCH_WIN95 | ARCH_WINNT );
3031 else if (strcsequal(p,"LANMAN2.1"))
3032 arch &= ( ARCH_WINNT | ARCH_OS2 );
3033 else if (strcsequal(p,"LM1.2X002"))
3034 arch &= ( ARCH_WINNT | ARCH_OS2 );
3035 else if (strcsequal(p,"MICROSOFT NETWORKS 1.03"))
3037 else if (strcsequal(p,"XENIX CORE"))
3038 arch &= ( ARCH_WINNT | ARCH_OS2 );
3039 else if (strcsequal(p,"Samba")) {
3049 strcpy(remote_arch,"Samba");
3052 strcpy(remote_arch,"WfWg");
3055 strcpy(remote_arch,"Win95");
3058 strcpy(remote_arch,"WinNT");
3061 strcpy(remote_arch,"OS2");
3064 strcpy(remote_arch,"UNKNOWN");
3068 /* possibly reload - change of architecture */
3069 reload_services(True);
3071 /* a special case to stop password server loops */
3072 if (Index == 1 && strequal(remote_machine,myhostname) &&
3073 lp_security()==SEC_SERVER)
3074 exit_server("Password server loop!");
3076 /* Check for protocols, most desirable first */
3077 for (protocol = 0; supported_protocols[protocol].proto_name; protocol++)
3079 p = smb_buf(inbuf)+1;
3081 if (lp_maxprotocol() >= supported_protocols[protocol].protocol_level)
3082 while (p < (smb_buf(inbuf) + bcc))
3084 if (strequal(p,supported_protocols[protocol].proto_name))
3093 SSVAL(outbuf,smb_vwv0,choice);
3095 extern fstring remote_proto;
3096 strcpy(remote_proto,supported_protocols[protocol].short_name);
3097 reload_services(True);
3098 outsize = supported_protocols[protocol].proto_reply_fn(outbuf);
3099 DEBUG(3,("Selected protocol %s\n",supported_protocols[protocol].proto_name));
3102 DEBUG(0,("No protocol supported !\n"));
3104 SSVAL(outbuf,smb_vwv0,choice);
3106 DEBUG(5,("%s negprot index=%d\n",timestring(),choice));
3112 /****************************************************************************
3113 close all open files for a connection
3114 ****************************************************************************/
3115 static void close_open_files(int cnum)
3118 for (i=0;i<MAX_OPEN_FILES;i++)
3119 if( Files[i].cnum == cnum && Files[i].open) {
3126 /****************************************************************************
3128 ****************************************************************************/
3129 void close_cnum(int cnum, uint16 vuid)
3131 DirCacheFlush(SNUM(cnum));
3135 if (!OPEN_CNUM(cnum))
3137 DEBUG(0,("Can't close cnum %d\n",cnum));
3141 DEBUG(IS_IPC(cnum)?3:1,("%s %s (%s) closed connection to service %s\n",
3143 remote_machine,client_addr(),
3144 lp_servicename(SNUM(cnum))));
3146 yield_connection(cnum,
3147 lp_servicename(SNUM(cnum)),
3148 lp_max_connections(SNUM(cnum)));
3150 if (lp_status(SNUM(cnum)))
3151 yield_connection(cnum,"STATUS.",MAXSTATUS);
3153 close_open_files(cnum);
3154 dptr_closecnum(cnum);
3156 /* execute any "postexec = " line */
3157 if (*lp_postexec(SNUM(cnum)) && become_user(cnum,vuid))
3160 strcpy(cmd,lp_postexec(SNUM(cnum)));
3161 standard_sub(cnum,cmd);
3162 smbrun(cmd,NULL,False);
3167 /* execute any "root postexec = " line */
3168 if (*lp_rootpostexec(SNUM(cnum)))
3171 strcpy(cmd,lp_rootpostexec(SNUM(cnum)));
3172 standard_sub(cnum,cmd);
3173 smbrun(cmd,NULL,False);
3176 Connections[cnum].open = False;
3177 num_connections_open--;
3178 if (Connections[cnum].ngroups && Connections[cnum].groups)
3180 if (Connections[cnum].igroups != (int *)Connections[cnum].groups)
3181 free(Connections[cnum].groups);
3182 free(Connections[cnum].igroups);
3183 Connections[cnum].groups = NULL;
3184 Connections[cnum].igroups = NULL;
3185 Connections[cnum].ngroups = 0;
3188 free_namearray(Connections[cnum].veto_list);
3189 free_namearray(Connections[cnum].hide_list);
3191 string_set(&Connections[cnum].user,"");
3192 string_set(&Connections[cnum].dirpath,"");
3193 string_set(&Connections[cnum].connectpath,"");
3197 /****************************************************************************
3198 simple routines to do connection counting
3199 ****************************************************************************/
3200 BOOL yield_connection(int cnum,char *name,int max_connections)
3202 struct connect_record crec;
3205 int mypid = getpid();
3208 DEBUG(3,("Yielding connection to %d %s\n",cnum,name));
3210 if (max_connections <= 0)
3213 bzero(&crec,sizeof(crec));
3215 strcpy(fname,lp_lockdir());
3216 standard_sub(cnum,fname);
3217 trim_string(fname,"","/");
3221 strcat(fname,".LCK");
3223 f = fopen(fname,"r+");
3226 DEBUG(2,("Couldn't open lock file %s (%s)\n",fname,strerror(errno)));
3230 fseek(f,0,SEEK_SET);
3232 /* find a free spot */
3233 for (i=0;i<max_connections;i++)
3235 if (fread(&crec,sizeof(crec),1,f) != 1)
3237 DEBUG(2,("Entry not found in lock file %s\n",fname));
3241 if (crec.pid == mypid && crec.cnum == cnum)
3245 if (crec.pid != mypid || crec.cnum != cnum)
3248 DEBUG(2,("Entry not found in lock file %s\n",fname));
3252 bzero((void *)&crec,sizeof(crec));
3254 /* remove our mark */
3255 if (fseek(f,i*sizeof(crec),SEEK_SET) != 0 ||
3256 fwrite(&crec,sizeof(crec),1,f) != 1)
3258 DEBUG(2,("Couldn't update lock file %s (%s)\n",fname,strerror(errno)));
3263 DEBUG(3,("Yield successful\n"));
3270 /****************************************************************************
3271 simple routines to do connection counting
3272 ****************************************************************************/
3273 BOOL claim_connection(int cnum,char *name,int max_connections,BOOL Clear)
3275 struct connect_record crec;
3278 int snum = SNUM(cnum);
3282 if (max_connections <= 0)
3285 DEBUG(5,("trying claim %s %s %d\n",lp_lockdir(),name,max_connections));
3287 strcpy(fname,lp_lockdir());
3288 standard_sub(cnum,fname);
3289 trim_string(fname,"","/");
3291 if (!directory_exist(fname,NULL))
3296 strcat(fname,".LCK");
3298 if (!file_exist(fname,NULL))
3300 int oldmask = umask(022);
3301 f = fopen(fname,"w");
3306 total_recs = file_size(fname) / sizeof(crec);
3308 f = fopen(fname,"r+");
3312 DEBUG(1,("couldn't open lock file %s\n",fname));
3316 /* find a free spot */
3317 for (i=0;i<max_connections;i++)
3320 if (i>=total_recs ||
3321 fseek(f,i*sizeof(crec),SEEK_SET) != 0 ||
3322 fread(&crec,sizeof(crec),1,f) != 1)
3324 if (foundi < 0) foundi = i;
3328 if (Clear && crec.pid && !process_exists(crec.pid))
3330 fseek(f,i*sizeof(crec),SEEK_SET);
3331 bzero((void *)&crec,sizeof(crec));
3332 fwrite(&crec,sizeof(crec),1,f);
3333 if (foundi < 0) foundi = i;
3336 if (foundi < 0 && (!crec.pid || !process_exists(crec.pid)))
3345 DEBUG(3,("no free locks in %s\n",fname));
3350 /* fill in the crec */
3351 bzero((void *)&crec,sizeof(crec));
3352 crec.magic = 0x280267;
3353 crec.pid = getpid();
3355 crec.uid = Connections[cnum].uid;
3356 crec.gid = Connections[cnum].gid;
3357 StrnCpy(crec.name,lp_servicename(snum),sizeof(crec.name)-1);
3358 crec.start = time(NULL);
3360 StrnCpy(crec.machine,remote_machine,sizeof(crec.machine)-1);
3361 StrnCpy(crec.addr,client_addr(),sizeof(crec.addr)-1);
3364 if (fseek(f,foundi*sizeof(crec),SEEK_SET) != 0 ||
3365 fwrite(&crec,sizeof(crec),1,f) != 1)
3376 /*******************************************************************
3377 prepare to dump a core file - carefully!
3378 ********************************************************************/
3379 static BOOL dump_core(void)
3383 strcpy(dname,debugf);
3384 if ((p=strrchr(dname,'/'))) *p=0;
3385 strcat(dname,"/corefiles");
3387 sys_chown(dname,getuid(),getgid());
3389 if (chdir(dname)) return(False);
3392 #ifndef NO_GETRLIMIT
3396 getrlimit(RLIMIT_CORE, &rlp);
3397 rlp.rlim_cur = MAX(4*1024*1024,rlp.rlim_cur);
3398 setrlimit(RLIMIT_CORE, &rlp);
3399 getrlimit(RLIMIT_CORE, &rlp);
3400 DEBUG(3,("Core limits now %d %d\n",rlp.rlim_cur,rlp.rlim_max));
3406 DEBUG(0,("Dumping core in %s\n",dname));
3411 /****************************************************************************
3413 ****************************************************************************/
3414 void exit_server(char *reason)
3416 static int firsttime=1;
3419 if (!firsttime) exit(0);
3423 DEBUG(2,("Closing connections\n"));
3424 for (i=0;i<MAX_CONNECTIONS;i++)
3425 if (Connections[i].open)
3428 if (dcelogin_atmost_once)
3432 int oldlevel = DEBUGLEVEL;
3434 DEBUG(0,("Last message was %s\n",smb_fn_name(last_message)));
3436 show_msg(last_inbuf);
3437 DEBUGLEVEL = oldlevel;
3438 DEBUG(0,("===============================================================\n"));
3440 if (dump_core()) return;
3444 #ifdef FAST_SHARE_MODES
3445 stop_share_mode_mgmt();
3446 #endif /* FAST_SHARE_MODES */
3448 DEBUG(3,("%s Server exit (%s)\n",timestring(),reason?reason:""));
3452 /****************************************************************************
3453 do some standard substitutions in a string
3454 ****************************************************************************/
3455 void standard_sub(int cnum,char *string)
3457 if (VALID_CNUM(cnum)) {
3460 for ( s=string ; (p=strchr(s, '%')) != NULL ; s=p ) {
3462 case 'H' : if ((home = get_home_dir(Connections[cnum].user))!=NULL)
3463 string_sub(p,"%H",home);
3467 case 'P' : string_sub(p,"%P",Connections[cnum].connectpath); break;
3468 case 'S' : string_sub(p,"%S",lp_servicename(Connections[cnum].service)); break;
3469 case 'g' : string_sub(p,"%g",gidtoname(Connections[cnum].gid)); break;
3470 case 'u' : string_sub(p,"%u",Connections[cnum].user); break;
3471 case '\0' : p++; break; /* don't run off the end of the string */
3472 default : p+=2; break;
3476 standard_sub_basic(string);
3480 These flags determine some of the permissions required to do an operation
3482 Note that I don't set NEED_WRITE on some write operations because they
3483 are used by some brain-dead clients when printing, and I don't want to
3484 force write permissions on print services.
3486 #define AS_USER (1<<0)
3487 #define NEED_WRITE (1<<1)
3488 #define TIME_INIT (1<<2)
3489 #define CAN_IPC (1<<3)
3490 #define AS_GUEST (1<<5)
3494 define a list of possible SMB messages and their corresponding
3495 functions. Any message that has a NULL function is unimplemented -
3496 please feel free to contribute implementations!
3498 struct smb_message_struct
3512 {SMBnegprot,"SMBnegprot",reply_negprot,0},
3513 {SMBtcon,"SMBtcon",reply_tcon,0},
3514 {SMBtdis,"SMBtdis",reply_tdis,0},
3515 {SMBexit,"SMBexit",reply_exit,0},
3516 {SMBioctl,"SMBioctl",reply_ioctl,0},
3517 {SMBecho,"SMBecho",reply_echo,0},
3518 {SMBsesssetupX,"SMBsesssetupX",reply_sesssetup_and_X,0},
3519 {SMBtconX,"SMBtconX",reply_tcon_and_X,0},
3520 {SMBulogoffX, "SMBulogoffX", reply_ulogoffX, 0}, /* ulogoff doesn't give a valid TID */
3521 {SMBgetatr,"SMBgetatr",reply_getatr,AS_USER},
3522 {SMBsetatr,"SMBsetatr",reply_setatr,AS_USER | NEED_WRITE},
3523 {SMBchkpth,"SMBchkpth",reply_chkpth,AS_USER},
3524 {SMBsearch,"SMBsearch",reply_search,AS_USER},
3525 {SMBopen,"SMBopen",reply_open,AS_USER},
3527 /* note that SMBmknew and SMBcreate are deliberately overloaded */
3528 {SMBcreate,"SMBcreate",reply_mknew,AS_USER},
3529 {SMBmknew,"SMBmknew",reply_mknew,AS_USER},
3531 {SMBunlink,"SMBunlink",reply_unlink,AS_USER | NEED_WRITE},
3532 {SMBread,"SMBread",reply_read,AS_USER},
3533 {SMBwrite,"SMBwrite",reply_write,AS_USER},
3534 {SMBclose,"SMBclose",reply_close,AS_USER | CAN_IPC},
3535 {SMBmkdir,"SMBmkdir",reply_mkdir,AS_USER | NEED_WRITE},
3536 {SMBrmdir,"SMBrmdir",reply_rmdir,AS_USER | NEED_WRITE},
3537 {SMBdskattr,"SMBdskattr",reply_dskattr,AS_USER},
3538 {SMBmv,"SMBmv",reply_mv,AS_USER | NEED_WRITE},
3540 /* this is a Pathworks specific call, allowing the
3541 changing of the root path */
3542 {pSETDIR,"pSETDIR",reply_setdir,AS_USER},
3544 {SMBlseek,"SMBlseek",reply_lseek,AS_USER},
3545 {SMBflush,"SMBflush",reply_flush,AS_USER},
3546 {SMBctemp,"SMBctemp",reply_ctemp,AS_USER},
3547 {SMBsplopen,"SMBsplopen",reply_printopen,AS_USER},
3548 {SMBsplclose,"SMBsplclose",reply_printclose,AS_USER},
3549 {SMBsplretq,"SMBsplretq",reply_printqueue,AS_USER|AS_GUEST},
3550 {SMBsplwr,"SMBsplwr",reply_printwrite,AS_USER},
3551 {SMBlock,"SMBlock",reply_lock,AS_USER},
3552 {SMBunlock,"SMBunlock",reply_unlock,AS_USER},
3554 /* CORE+ PROTOCOL FOLLOWS */
3556 {SMBreadbraw,"SMBreadbraw",reply_readbraw,AS_USER},
3557 {SMBwritebraw,"SMBwritebraw",reply_writebraw,AS_USER},
3558 {SMBwriteclose,"SMBwriteclose",reply_writeclose,AS_USER},
3559 {SMBlockread,"SMBlockread",reply_lockread,AS_USER},
3560 {SMBwriteunlock,"SMBwriteunlock",reply_writeunlock,AS_USER},
3562 /* LANMAN1.0 PROTOCOL FOLLOWS */
3564 {SMBreadBmpx,"SMBreadBmpx",reply_readbmpx,AS_USER},
3565 {SMBreadBs,"SMBreadBs",NULL,AS_USER},
3566 {SMBwriteBmpx,"SMBwriteBmpx",reply_writebmpx,AS_USER},
3567 {SMBwriteBs,"SMBwriteBs",reply_writebs,AS_USER},
3568 {SMBwritec,"SMBwritec",NULL,AS_USER},
3569 {SMBsetattrE,"SMBsetattrE",reply_setattrE,AS_USER | NEED_WRITE},
3570 {SMBgetattrE,"SMBgetattrE",reply_getattrE,AS_USER},
3571 {SMBtrans,"SMBtrans",reply_trans,AS_USER | CAN_IPC},
3572 {SMBtranss,"SMBtranss",NULL,AS_USER | CAN_IPC},
3573 {SMBioctls,"SMBioctls",NULL,AS_USER},
3574 {SMBcopy,"SMBcopy",reply_copy,AS_USER | NEED_WRITE},
3575 {SMBmove,"SMBmove",NULL,AS_USER | NEED_WRITE},
3577 {SMBopenX,"SMBopenX",reply_open_and_X,AS_USER | CAN_IPC},
3578 {SMBreadX,"SMBreadX",reply_read_and_X,AS_USER},
3579 {SMBwriteX,"SMBwriteX",reply_write_and_X,AS_USER},
3580 {SMBlockingX,"SMBlockingX",reply_lockingX,AS_USER},
3582 {SMBffirst,"SMBffirst",reply_search,AS_USER},
3583 {SMBfunique,"SMBfunique",reply_search,AS_USER},
3584 {SMBfclose,"SMBfclose",reply_fclose,AS_USER},
3586 /* LANMAN2.0 PROTOCOL FOLLOWS */
3587 {SMBfindnclose, "SMBfindnclose", reply_findnclose, AS_USER},
3588 {SMBfindclose, "SMBfindclose", reply_findclose,AS_USER},
3589 {SMBtrans2, "SMBtrans2", reply_trans2, AS_USER},
3590 {SMBtranss2, "SMBtranss2", reply_transs2, AS_USER},
3592 /* messaging routines */
3593 {SMBsends,"SMBsends",reply_sends,AS_GUEST},
3594 {SMBsendstrt,"SMBsendstrt",reply_sendstrt,AS_GUEST},
3595 {SMBsendend,"SMBsendend",reply_sendend,AS_GUEST},
3596 {SMBsendtxt,"SMBsendtxt",reply_sendtxt,AS_GUEST},
3598 /* NON-IMPLEMENTED PARTS OF THE CORE PROTOCOL */
3600 {SMBsendb,"SMBsendb",NULL,AS_GUEST},
3601 {SMBfwdname,"SMBfwdname",NULL,AS_GUEST},
3602 {SMBcancelf,"SMBcancelf",NULL,AS_GUEST},
3603 {SMBgetmac,"SMBgetmac",NULL,AS_GUEST}
3606 /****************************************************************************
3607 return a string containing the function name of a SMB command
3608 ****************************************************************************/
3609 char *smb_fn_name(int type)
3611 static char *unknown_name = "SMBunknown";
3612 static int num_smb_messages =
3613 sizeof(smb_messages) / sizeof(struct smb_message_struct);
3616 for (match=0;match<num_smb_messages;match++)
3617 if (smb_messages[match].code == type)
3620 if (match == num_smb_messages)
3621 return(unknown_name);
3623 return(smb_messages[match].name);
3627 /****************************************************************************
3628 do a switch on the message type, and return the response size
3629 ****************************************************************************/
3630 static int switch_message(int type,char *inbuf,char *outbuf,int size,int bufsize)
3634 static int num_smb_messages =
3635 sizeof(smb_messages) / sizeof(struct smb_message_struct);
3639 struct timeval msg_start_time;
3640 struct timeval msg_end_time;
3641 static unsigned long total_time = 0;
3643 GetTimeOfDay(&msg_start_time);
3650 last_message = type;
3652 /* make sure this is an SMB packet */
3653 if (strncmp(smb_base(inbuf),"\377SMB",4) != 0)
3655 DEBUG(2,("Non-SMB packet of length %d\n",smb_len(inbuf)));
3659 for (match=0;match<num_smb_messages;match++)
3660 if (smb_messages[match].code == type)
3663 if (match == num_smb_messages)
3665 DEBUG(0,("Unknown message type %d!\n",type));
3666 outsize = reply_unknown(inbuf,outbuf);
3670 DEBUG(3,("switch message %s (pid %d)\n",smb_messages[match].name,pid));
3671 if (smb_messages[match].fn)
3673 int cnum = SVAL(inbuf,smb_tid);
3674 int flags = smb_messages[match].flags;
3675 uint16 session_tag = SVAL(inbuf,smb_uid);
3677 /* does this protocol need to be run as root? */
3678 if (!(flags & AS_USER))
3681 /* does this protocol need to be run as the connected user? */
3682 if ((flags & AS_USER) && !become_user(cnum,session_tag)) {
3683 if (flags & AS_GUEST)
3686 return(ERROR(ERRSRV,ERRinvnid));
3688 /* this code is to work around a bug is MS client 3 without
3689 introducing a security hole - it needs to be able to do
3690 print queue checks as guest if it isn't logged in properly */
3691 if (flags & AS_USER)
3694 /* does it need write permission? */
3695 if ((flags & NEED_WRITE) && !CAN_WRITE(cnum))
3696 return(ERROR(ERRSRV,ERRaccess));
3698 /* ipc services are limited */
3699 if (IS_IPC(cnum) && (flags & AS_USER) && !(flags & CAN_IPC))
3700 return(ERROR(ERRSRV,ERRaccess));
3702 /* load service specific parameters */
3703 if (OPEN_CNUM(cnum) && !become_service(cnum,(flags & AS_USER)?True:False))
3704 return(ERROR(ERRSRV,ERRaccess));
3706 /* does this protocol need to be run as guest? */
3707 if ((flags & AS_GUEST) && (!become_guest() || !check_access(-1)))
3708 return(ERROR(ERRSRV,ERRaccess));
3712 outsize = smb_messages[match].fn(inbuf,outbuf,size,bufsize);
3716 outsize = reply_unknown(inbuf,outbuf);
3721 GetTimeOfDay(&msg_end_time);
3722 if (!(smb_messages[match].flags & TIME_INIT))
3724 smb_messages[match].time = 0;
3725 smb_messages[match].flags |= TIME_INIT;
3728 unsigned long this_time =
3729 (msg_end_time.tv_sec - msg_start_time.tv_sec)*1e6 +
3730 (msg_end_time.tv_usec - msg_start_time.tv_usec);
3731 smb_messages[match].time += this_time;
3732 total_time += this_time;
3734 DEBUG(2,("TIME %s %d usecs %g pct\n",
3735 smb_fn_name(type),smb_messages[match].time,
3736 (100.0*smb_messages[match].time) / total_time));
3743 /****************************************************************************
3744 construct a chained reply and add it to the already made reply
3745 **************************************************************************/
3746 int chain_reply(char *inbuf,char *outbuf,int size,int bufsize)