home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / samba-1.9.18p7.tar.gz / samba-1.9.18p7.tar / samba-1.9.18p7 / source / reply.c < prev    next >
C/C++ Source or Header  |  1998-05-12  |  103KB  |  3,954 lines

  1. /* 
  2.    Unix SMB/Netbios implementation.
  3.    Version 1.9.
  4.    Main SMB reply 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.    This file handles most of the reply_ calls that the server
  23.    makes to handle specific protocols
  24. */
  25.  
  26.  
  27. #include "includes.h"
  28. #include "trans2.h"
  29. #include "nterr.h"
  30.  
  31. /* look in server.c for some explanation of these variables */
  32. extern int Protocol;
  33. extern int DEBUGLEVEL;
  34. extern int max_send;
  35. extern int max_recv;
  36. extern int chain_fnum;
  37. extern char magic_char;
  38. extern connection_struct Connections[];
  39. extern files_struct Files[];
  40. extern BOOL case_sensitive;
  41. extern BOOL case_preserve;
  42. extern BOOL short_case_preserve;
  43. extern pstring sesssetup_user;
  44. extern fstring myworkgroup;
  45. extern int Client;
  46. extern int global_oplock_break;
  47.  
  48. /* this macro should always be used to extract an fnum (smb_fid) from
  49. a packet to ensure chaining works correctly */
  50. #define GETFNUM(buf,where) (chain_fnum!= -1?chain_fnum:SVAL(buf,where))
  51.  
  52.  
  53. /****************************************************************************
  54. report a possible attack via the password buffer overflow bug
  55. ****************************************************************************/
  56. static void overflow_attack(int len)
  57. {
  58.     DEBUG(0,("%s: ERROR: Invalid password length %d\n", timestring(), len));
  59.     DEBUG(0,("your machine may be under attack by a user exploiting an old bug\n"));
  60.     DEBUG(0,("Attack was from IP=%s\n", client_addr()));
  61.     exit_server("possible attack");
  62. }
  63.  
  64.  
  65. /****************************************************************************
  66.   reply to an special message 
  67. ****************************************************************************/
  68. int reply_special(char *inbuf,char *outbuf)
  69. {
  70.     int outsize = 4;
  71.     int msg_type = CVAL(inbuf,0);
  72.     int msg_flags = CVAL(inbuf,1);
  73.     pstring name1,name2;
  74.     extern fstring remote_machine;
  75.     extern fstring local_machine;
  76.     int len;
  77.     char name_type = 0;
  78.     
  79.     *name1 = *name2 = 0;
  80.     
  81.     smb_setlen(outbuf,0);
  82.     
  83.     switch (msg_type) {
  84.     case 0x81: /* session request */
  85.         CVAL(outbuf,0) = 0x82;
  86.         CVAL(outbuf,3) = 0;
  87.         if (name_len(inbuf+4) > 50 || 
  88.             name_len(inbuf+4 + name_len(inbuf + 4)) > 50) {
  89.             DEBUG(0,("Invalid name length in session request\n"));
  90.             return(0);
  91.         }
  92.         name_extract(inbuf,4,name1);
  93.         name_extract(inbuf,4 + name_len(inbuf + 4),name2);
  94.         DEBUG(2,("netbios connect: name1=%s name2=%s\n",
  95.              name1,name2));      
  96.  
  97.         fstrcpy(remote_machine,name2);
  98.         remote_machine[15] = 0;
  99.         trim_string(remote_machine," "," ");
  100.         strlower(remote_machine);
  101.  
  102.         fstrcpy(local_machine,name1);
  103.         len = strlen(local_machine);
  104.         if (len == 16) {
  105.             name_type = local_machine[15];
  106.             local_machine[15] = 0;
  107.         }
  108.         trim_string(local_machine," "," ");
  109.         strlower(local_machine);
  110.  
  111.         if (name_type == 'R') {
  112.             /* We are being asked for a pathworks session --- 
  113.                no thanks! */
  114.             CVAL(outbuf, 0) = 0x83;
  115.             break;
  116.         }
  117.  
  118.         add_session_user(remote_machine);
  119.  
  120.         reload_services(True);
  121.         reopen_logs();
  122.  
  123.         break;
  124.         
  125.     case 0x89: /* session keepalive request 
  126.               (some old clients produce this?) */
  127.         CVAL(outbuf,0) = 0x85;
  128.         CVAL(outbuf,3) = 0;
  129.         break;
  130.         
  131.     case 0x82: /* positive session response */
  132.     case 0x83: /* negative session response */
  133.     case 0x84: /* retarget session response */
  134.         DEBUG(0,("Unexpected session response\n"));
  135.         break;
  136.         
  137.     case 0x85: /* session keepalive */
  138.     default:
  139.         return(0);
  140.     }
  141.     
  142.     DEBUG(5,("%s init msg_type=0x%x msg_flags=0x%x\n",
  143.          timestring(),msg_type,msg_flags));
  144.     
  145.     return(outsize);
  146. }
  147.  
  148.  
  149. /*******************************************************************
  150. work out what error to give to a failed connection
  151. ********************************************************************/
  152. static int connection_error(char *inbuf,char *outbuf,int connection_num)
  153. {
  154.   switch (connection_num)
  155.     {
  156.     case -8:
  157.       return(ERROR(ERRSRV,ERRnoresource));
  158.     case -7:
  159.       return(ERROR(ERRSRV,ERRbaduid));
  160.     case -6:
  161.       return(ERROR(ERRSRV,ERRinvdevice));
  162.     case -5:
  163.       return(ERROR(ERRSRV,ERRinvnetname));
  164.     case -4:
  165.       return(ERROR(ERRSRV,ERRaccess));
  166.     case -3:
  167.       return(ERROR(ERRDOS,ERRnoipc));
  168.     case -2:
  169.       return(ERROR(ERRSRV,ERRinvnetname));
  170.     }
  171.   return(ERROR(ERRSRV,ERRbadpw));
  172. }
  173.  
  174.  
  175.  
  176. /****************************************************************************
  177.   parse a share descriptor string
  178. ****************************************************************************/
  179. static void parse_connect(char *p,char *service,char *user,
  180.               char *password,int *pwlen,char *dev)
  181. {
  182.   char *p2;
  183.  
  184.   DEBUG(4,("parsing connect string %s\n",p));
  185.     
  186.   p2 = strrchr(p,'\\');
  187.   if (p2 == NULL)
  188.     fstrcpy(service,p);
  189.   else
  190.     fstrcpy(service,p2+1);
  191.   
  192.   p += strlen(p) + 2;
  193.   
  194.   fstrcpy(password,p);
  195.   *pwlen = strlen(password);
  196.  
  197.   p += strlen(p) + 2;
  198.  
  199.   fstrcpy(dev,p);
  200.   
  201.   *user = 0;
  202.   p = strchr(service,'%');
  203.   if (p != NULL)
  204.     {
  205.       *p = 0;
  206.       fstrcpy(user,p+1);
  207.     }
  208. }
  209.  
  210.  
  211.  
  212.  
  213. /****************************************************************************
  214.   reply to a tcon
  215. ****************************************************************************/
  216. int reply_tcon(char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
  217. {
  218.   pstring service;
  219.   pstring user;
  220.   pstring password;
  221.   pstring dev;
  222.   int connection_num;
  223.   int outsize = 0;
  224.   uint16 vuid = SVAL(inbuf,smb_uid);
  225.   int pwlen=0;
  226.  
  227.   *service = *user = *password = *dev = 0;
  228.  
  229.   parse_connect(smb_buf(inbuf)+1,service,user,password,&pwlen,dev);
  230.  
  231.   connection_num = make_connection(service,user,password,pwlen,dev,vuid);
  232.   
  233.   if (connection_num < 0)
  234.     return(connection_error(inbuf,outbuf,connection_num));
  235.   
  236.   outsize = set_message(outbuf,2,0,True);
  237.   SSVAL(outbuf,smb_vwv0,max_recv);
  238.   SSVAL(outbuf,smb_vwv1,connection_num);
  239.   SSVAL(outbuf,smb_tid,connection_num);
  240.   
  241.   DEBUG(3,("%s tcon service=%s user=%s cnum=%d\n",timestring(),service,user,connection_num));
  242.   
  243.   return(outsize);
  244. }
  245.  
  246.  
  247. /****************************************************************************
  248.   reply to a tcon and X
  249. ****************************************************************************/
  250. int reply_tcon_and_X(char *inbuf,char *outbuf,int length,int bufsize)
  251. {
  252.   pstring service;
  253.   pstring user;
  254.   pstring password;
  255.   pstring devicename;
  256.   int connection_num;
  257.   uint16 vuid = SVAL(inbuf,smb_uid);
  258.   int passlen = SVAL(inbuf,smb_vwv3);
  259.  
  260.   *service = *user = *password = *devicename = 0;
  261.  
  262.   /* we might have to close an old one */
  263.   if ((SVAL(inbuf,smb_vwv2) & 0x1) != 0)
  264.     close_cnum(SVAL(inbuf,smb_tid),vuid);
  265.  
  266.   if (passlen > MAX_PASS_LEN) {
  267.       overflow_attack(passlen);
  268.   }
  269.   
  270.   {
  271.     char *path;
  272.     char *p;
  273.     memcpy(password,smb_buf(inbuf),passlen);
  274.     password[passlen]=0;    
  275.     path = smb_buf(inbuf) + passlen;
  276.  
  277.     if (passlen != 24) {
  278.       if (strequal(password," "))
  279.     *password = 0;
  280.       passlen = strlen(password);
  281.     }
  282.     
  283.     fstrcpy(service,path+2);
  284.     p = strchr(service,'\\');
  285.     if (!p)
  286.       return(ERROR(ERRSRV,ERRinvnetname));
  287.     *p = 0;
  288.     fstrcpy(service,p+1);
  289.     p = strchr(service,'%');
  290.     if (p)
  291.       {
  292.     *p++ = 0;
  293.     fstrcpy(user,p);
  294.       }
  295.     StrnCpy(devicename,path + strlen(path) + 1,6);
  296.     DEBUG(4,("Got device type %s\n",devicename));
  297.   }
  298.  
  299.   connection_num = make_connection(service,user,password,passlen,devicename,vuid);
  300.   
  301.   if (connection_num < 0)
  302.     return(connection_error(inbuf,outbuf,connection_num));
  303.  
  304.   if (Protocol < PROTOCOL_NT1)
  305.   {
  306.     set_message(outbuf,2,strlen(devicename)+1,True);
  307.     pstrcpy(smb_buf(outbuf),devicename);
  308.   }
  309.   else
  310.   {
  311.     char *fsname = "SAMBA";
  312.     char *p;
  313.  
  314.     set_message(outbuf,3,3,True);
  315.  
  316.     p = smb_buf(outbuf);
  317.     pstrcpy(p,devicename); p = skip_string(p,1); /* device name */
  318.     pstrcpy(p,fsname); p = skip_string(p,1); /* filesystem type e.g NTFS */
  319.  
  320.     set_message(outbuf,3,PTR_DIFF(p,smb_buf(outbuf)),False);
  321.  
  322.     SSVAL(outbuf, smb_vwv2, 0x0); /* optional support */
  323.   }
  324.   
  325.   DEBUG(3,("%s tconX service=%s user=%s cnum=%d\n",timestring(),service,user,connection_num));
  326.   
  327.   /* set the incoming and outgoing tid to the just created one */
  328.   SSVAL(inbuf,smb_tid,connection_num);
  329.   SSVAL(outbuf,smb_tid,connection_num);
  330.  
  331.   return chain_reply(inbuf,outbuf,length,bufsize);
  332. }
  333.  
  334.  
  335. /****************************************************************************
  336.   reply to an unknown type
  337. ****************************************************************************/
  338. int reply_unknown(char *inbuf,char *outbuf)
  339. {
  340.   int cnum;
  341.   int type;
  342.   cnum = SVAL(inbuf,smb_tid);
  343.   type = CVAL(inbuf,smb_com);
  344.   
  345.   DEBUG(0,("%s unknown command type (%s): cnum=%d type=%d (0x%X)\n",
  346.     timestring(),
  347.     smb_fn_name(type),
  348.     cnum,type,type));
  349.   
  350.   return(ERROR(ERRSRV,ERRunknownsmb));
  351. }
  352.  
  353.  
  354. /****************************************************************************
  355.   reply to an ioctl
  356. ****************************************************************************/
  357. int reply_ioctl(char *inbuf,char *outbuf, int size, int bufsize)
  358. {
  359.   DEBUG(3,("ignoring ioctl\n"));
  360. #if 0
  361.   /* we just say it succeeds and hope its all OK. 
  362.      some day it would be nice to interpret them individually */
  363.   return set_message(outbuf,1,0,True); 
  364. #else
  365.   return(ERROR(ERRSRV,ERRnosupport));
  366. #endif
  367. }
  368.  
  369.  
  370. /****************************************************************************
  371. reply to a session setup command
  372. ****************************************************************************/
  373. int reply_sesssetup_and_X(char *inbuf,char *outbuf,int length,int bufsize)
  374. {
  375.   uint16 sess_vuid;
  376.   int gid;
  377.   int uid;
  378.   int   smb_bufsize;    
  379.   int   smb_mpxmax;     
  380.   int   smb_vc_num;     
  381.   uint32   smb_sesskey;    
  382.   int   smb_apasslen = 0;   
  383.   pstring smb_apasswd;
  384.   int   smb_ntpasslen = 0;   
  385.   pstring smb_ntpasswd;
  386.   BOOL valid_nt_password = False;
  387.   pstring user;
  388.   BOOL guest=False;
  389.   BOOL computer_id=False;
  390.   static BOOL done_sesssetup = False;
  391.   BOOL doencrypt = SMBENCRYPT();
  392.   char *domain = "";
  393.  
  394.   *smb_apasswd = 0;
  395.   *smb_ntpasswd = 0;
  396.   
  397.   smb_bufsize = SVAL(inbuf,smb_vwv2);
  398.   smb_mpxmax = SVAL(inbuf,smb_vwv3);
  399.   smb_vc_num = SVAL(inbuf,smb_vwv4);
  400.   smb_sesskey = IVAL(inbuf,smb_vwv5);
  401.  
  402.   if (Protocol < PROTOCOL_NT1) {
  403.     smb_apasslen = SVAL(inbuf,smb_vwv7);
  404.     if (smb_apasslen > MAX_PASS_LEN)
  405.     {
  406.         overflow_attack(smb_apasslen);
  407.     }
  408.  
  409.     memcpy(smb_apasswd,smb_buf(inbuf),smb_apasslen);
  410.     smb_apasswd[smb_apasslen] = 0;
  411.     pstrcpy(user,smb_buf(inbuf)+smb_apasslen);
  412.  
  413.     if (!doencrypt && (lp_security() != SEC_SERVER)) {
  414.         smb_apasslen = strlen(smb_apasswd);
  415.     }
  416.   } else {
  417.     uint16 passlen1 = SVAL(inbuf,smb_vwv7);
  418.     uint16 passlen2 = SVAL(inbuf,smb_vwv8);
  419.     uint32 client_caps = IVAL(inbuf,smb_vwv11);
  420.     enum remote_arch_types ra_type = get_remote_arch();
  421.  
  422.     char *p = smb_buf(inbuf);    
  423.  
  424.     /* client_caps is used as final determination if client is NT or Win95. 
  425.        This is needed to return the correct error codes in some
  426.        circumstances.
  427.      */
  428.     
  429.     if(ra_type == RA_WINNT || ra_type == RA_WIN95)
  430.     {
  431.       if(client_caps & (CAP_NT_SMBS | CAP_STATUS32))
  432.         set_remote_arch( RA_WINNT);
  433.       else
  434.         set_remote_arch( RA_WIN95);
  435.     }
  436.  
  437.     if (passlen1 != 24 && passlen2 != 24)
  438.       doencrypt = False;
  439.  
  440.     if (passlen1 > MAX_PASS_LEN) {
  441.         overflow_attack(passlen1);
  442.     }
  443.  
  444.     passlen1 = MIN(passlen1, MAX_PASS_LEN);
  445.     passlen2 = MIN(passlen2, MAX_PASS_LEN);
  446.  
  447.     if(doencrypt || (lp_security() == SEC_SERVER)) {
  448.       /* Save the lanman2 password and the NT md4 password. */
  449.       smb_apasslen = passlen1;
  450.       memcpy(smb_apasswd,p,smb_apasslen);
  451.       smb_apasswd[smb_apasslen] = 0;
  452.       smb_ntpasslen = passlen2;
  453.       memcpy(smb_ntpasswd,p+passlen1,smb_ntpasslen);
  454.       smb_ntpasswd[smb_ntpasslen] = 0;
  455.     } else {
  456.       /* both Win95 and WinNT stuff up the password lengths for
  457.      non-encrypting systems. Uggh. 
  458.       
  459.      if passlen1==24 its a win95 system, and its setting the
  460.      password length incorrectly. Luckily it still works with the
  461.      default code because Win95 will null terminate the password
  462.      anyway 
  463.  
  464.      if passlen1>0 and passlen2>0 then maybe its a NT box and its
  465.      setting passlen2 to some random value which really stuffs
  466.      things up. we need to fix that one.  */
  467.       if (passlen1 > 0 && passlen2 > 0 && passlen2 != 24 &&
  468.       passlen2 != 1) {
  469.     passlen2 = 0;
  470.       }
  471.       /* we use the first password that they gave */
  472.       smb_apasslen = passlen1;
  473.       StrnCpy(smb_apasswd,p,smb_apasslen);      
  474.       
  475.       /* trim the password */
  476.       smb_apasslen = strlen(smb_apasswd);
  477.  
  478.       /* wfwg sometimes uses a space instead of a null */
  479.       if (strequal(smb_apasswd," ")) {
  480.     smb_apasslen = 0;
  481.     *smb_apasswd = 0;
  482.       }
  483.     }
  484.     
  485.     p += passlen1 + passlen2;
  486.     fstrcpy(user,p); p = skip_string(p,1);
  487.     domain = p;
  488.  
  489.     DEBUG(3,("Domain=[%s]  NativeOS=[%s] NativeLanMan=[%s]\n",
  490.          domain,skip_string(p,1),skip_string(p,2)));
  491.   }
  492.  
  493.  
  494.   DEBUG(3,("sesssetupX:name=[%s]\n",user));
  495.  
  496.   /* If name ends in $ then I think it's asking about whether a */
  497.   /* computer with that name (minus the $) has access. For now */
  498.   /* say yes to everything ending in $. */
  499.   if (user[strlen(user) - 1] == '$')
  500.   {
  501. #ifdef NTDOMAIN
  502.     struct smb_passwd *smb_pass; /* To check if machine account exists */
  503. /* 
  504.    PAXX: Ack. We don't want to do this. The workstation trust account
  505.    with a $ on the end should exist in the local password database
  506.    or be mapped to something generic, but not modified. For NT
  507.    domain support we must reject this used in certain circumstances
  508.    with a code to indicate to the client that it is an invalid use
  509.    of a workstation trust account. NTWKS needs this error to join
  510.    a domain. This may be the source of future bugs if we cannot
  511.    be sure whether to reject this or not.
  512. */
  513.    /* non-null user name indicates search by username not by smb userid */
  514.    smb_pass = get_smbpwd_entry(user, 0);
  515.  
  516.    if (!smb_pass)
  517.    {
  518.      /* lkclXXXX: if workstation entry doesn't exist, indicate logon failure */
  519.      DEBUG(4,("Workstation trust account %s doesn't exist.",user));
  520.      SSVAL(outbuf, smb_flg2, 0xc003); /* PAXX: Someone please unhack this */
  521.      CVAL(outbuf, smb_reh) = 1; /* PAXX: Someone please unhack this */
  522.      return(ERROR(NT_STATUS_LOGON_FAILURE, 0xc000)); /* decimal 109 NT error, 0xc000 */
  523.    }
  524.    else
  525.    {
  526.      /* PAXX: This is the NO LOGON workstation trust account stuff */
  527.      /* lkclXXXX: if the workstation *does* exist, indicate failure differently! */
  528.      DEBUG(4,("No Workstation trust account %s",user));
  529.      SSVAL(outbuf, smb_flg2, 0xc003); /* PAXX: Someone please unhack this */
  530.      CVAL(outbuf, smb_reh) = 1; /* PAXX: Someone please unhack this */
  531.      return(ERROR(NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT, 0xc000)); /* decimal 409 NT error, 0xc000 */
  532.    }
  533.  
  534.    computer_id = True;
  535. #else /* not NTDOMAIN, leave this in. PAXX: Someone get rid of this */
  536.     user[strlen(user) - 1] = '\0';
  537. #endif
  538.   }
  539.  
  540.  
  541.   /* If no username is sent use the guest account */
  542.   if (!*user)
  543.     {
  544.       pstrcpy(user,lp_guestaccount(-1));
  545.       /* If no user and no password then set guest flag. */
  546.       if( *smb_apasswd == 0)
  547.         guest = True;
  548.     }
  549.  
  550.   strlower(user);
  551.  
  552.   /* 
  553.    * In share level security, only overwrite sesssetup_use if
  554.    * it's a non null-session share. Helps keep %U and %G
  555.    * working.
  556.    */
  557.  
  558.   if((lp_security() != SEC_SHARE) || *user)
  559.     pstrcpy(sesssetup_user,user);
  560.  
  561.   reload_services(True);
  562.  
  563.   add_session_user(user);
  564.  
  565.   /* Check if the given username was the guest user with no password.
  566.      We need to do this check after add_session_user() as that
  567.      call can potentially change the username (via map_user).
  568.    */
  569.  
  570.   if(!guest && strequal(user,lp_guestaccount(-1)) && (*smb_apasswd == 0))
  571.     guest = True;
  572.  
  573.   if (!guest && !(lp_security() == SEC_SERVER && 
  574.           server_validate(user, domain, 
  575.                   smb_apasswd, smb_apasslen, 
  576.                   smb_ntpasswd, smb_ntpasslen)) &&
  577.       !check_hosts_equiv(user))
  578.     {
  579.  
  580.       /* now check if it's a valid username/password */
  581.       /* If an NT password was supplied try and validate with that
  582.      first. This is superior as the passwords are mixed case 
  583.          128 length unicode */
  584.       if(smb_ntpasslen)
  585.     {
  586.       if(!password_ok(user,smb_ntpasswd,smb_ntpasslen,NULL))
  587.         DEBUG(0,("NT Password did not match ! Defaulting to Lanman\n"));
  588.       else
  589.         valid_nt_password = True;
  590.     } 
  591.       if (!valid_nt_password && !password_ok(user,smb_apasswd,smb_apasslen,NULL))
  592.     {
  593.       if (!computer_id && lp_security() >= SEC_USER) {
  594. #if (GUEST_SESSSETUP == 0)
  595.         return(ERROR(ERRSRV,ERRbadpw));
  596. #endif
  597. #if (GUEST_SESSSETUP == 1)
  598.         if (Get_Pwnam(user,True))
  599.           return(ERROR(ERRSRV,ERRbadpw));
  600. #endif
  601.       }
  602.        if (*smb_apasswd || !Get_Pwnam(user,True))
  603.         pstrcpy(user,lp_guestaccount(-1));
  604.       DEBUG(3,("Registered username %s for guest access\n",user));
  605.       guest = True;
  606.     }
  607.     }
  608.  
  609.   if (!Get_Pwnam(user,True)) {
  610.     DEBUG(3,("No such user %s - using guest account\n",user));
  611.     pstrcpy(user,lp_guestaccount(-1));
  612.     guest = True;
  613.   }
  614.  
  615.   if (!strequal(user,lp_guestaccount(-1)) &&
  616.       lp_servicenumber(user) < 0)      
  617.     {
  618.       int homes = lp_servicenumber(HOMES_NAME);
  619.       char *home = get_home_dir(user);
  620.       if (homes >= 0 && home)
  621.     lp_add_home(user,homes,home);
  622.     }
  623.  
  624.  
  625.   /* it's ok - setup a reply */
  626.   if (Protocol < PROTOCOL_NT1) {
  627.     set_message(outbuf,3,0,True);
  628.   } else {
  629.     char *p;
  630.     set_message(outbuf,3,3,True);
  631.     p = smb_buf(outbuf);
  632.     pstrcpy(p,"Unix"); p = skip_string(p,1);
  633.     pstrcpy(p,"Samba "); pstrcat(p,VERSION); p = skip_string(p,1);
  634.     pstrcpy(p,myworkgroup); p = skip_string(p,1);
  635.     set_message(outbuf,3,PTR_DIFF(p,smb_buf(outbuf)),False);
  636.     /* perhaps grab OS version here?? */
  637.   }
  638.  
  639.   /* Set the correct uid in the outgoing and incoming packets
  640.      We will use this on future requests to determine which
  641.      user we should become.
  642.      */
  643.   {
  644.     struct passwd *pw = Get_Pwnam(user,False);
  645.     if (!pw) {
  646.       DEBUG(1,("Username %s is invalid on this system\n",user));
  647.       return(ERROR(ERRSRV,ERRbadpw));
  648.     }
  649.     gid = pw->pw_gid;
  650.     uid = pw->pw_uid;
  651.   }
  652.  
  653.   if (guest && !computer_id)
  654.     SSVAL(outbuf,smb_vwv2,1);
  655.  
  656.   /* register the name and uid as being validated, so further connections
  657.      to a uid can get through without a password, on the same VC */
  658.   sess_vuid = register_vuid(uid,gid,user,sesssetup_user,guest);
  659.  
  660.   SSVAL(outbuf,smb_uid,sess_vuid);
  661.   SSVAL(inbuf,smb_uid,sess_vuid);
  662.  
  663.   if (!done_sesssetup)
  664.     max_send = MIN(max_send,smb_bufsize);
  665.  
  666.   DEBUG(6,("Client requested max send size of %d\n", max_send));
  667.  
  668.   done_sesssetup = True;
  669.  
  670.   return chain_reply(inbuf,outbuf,length,bufsize);
  671. }
  672.  
  673.  
  674. /****************************************************************************
  675.   reply to a chkpth
  676. ****************************************************************************/
  677. int reply_chkpth(char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
  678. {
  679.   int outsize = 0;
  680.   int cnum,mode;
  681.   pstring name;
  682.   BOOL ok = False;
  683.   BOOL bad_path = False;
  684.  
  685.   cnum = SVAL(inbuf,smb_tid);
  686.   
  687.   pstrcpy(name,smb_buf(inbuf) + 1);
  688.   unix_convert(name,cnum,0,&bad_path);
  689.  
  690.   mode = SVAL(inbuf,smb_vwv0);
  691.  
  692.   if (check_name(name,cnum))
  693.     ok = directory_exist(name,NULL);
  694.  
  695.   if (!ok)
  696.   {
  697.     /* We special case this - as when a Windows machine
  698.        is parsing a path is steps through the components
  699.        one at a time - if a component fails it expects
  700.        ERRbadpath, not ERRbadfile.
  701.      */
  702.     if(errno == ENOENT)
  703.     {
  704.       unix_ERR_class = ERRDOS;
  705.       unix_ERR_code = ERRbadpath;
  706.     }
  707.  
  708. #if 0
  709.     /* Ugly - NT specific hack - maybe not needed ? (JRA) */
  710.     if((errno == ENOTDIR) && (Protocol >= PROTOCOL_NT1) &&
  711.        (get_remote_arch() == RA_WINNT))
  712.     {
  713.       unix_ERR_class = ERRDOS;
  714.       unix_ERR_code = ERRbaddirectory;
  715.     }
  716. #endif
  717.  
  718.     return(UNIXERROR(ERRDOS,ERRbadpath));
  719.   }
  720.  
  721.   outsize = set_message(outbuf,0,0,True);
  722.   
  723.   DEBUG(3,("%s chkpth %s cnum=%d mode=%d\n",timestring(),name,cnum,mode));
  724.   
  725.   return(outsize);
  726. }
  727.  
  728.  
  729. /****************************************************************************
  730.   reply to a getatr
  731. ****************************************************************************/
  732. int reply_getatr(char *inbuf,char *outbuf, int in_size, int buffsize)
  733. {
  734.   pstring fname;
  735.   int cnum;
  736.   int outsize = 0;
  737.   struct stat sbuf;
  738.   BOOL ok = False;
  739.   int mode=0;
  740.   uint32 size=0;
  741.   time_t mtime=0;
  742.   BOOL bad_path = False;
  743.  
  744.   cnum = SVAL(inbuf,smb_tid);
  745.  
  746.   pstrcpy(fname,smb_buf(inbuf) + 1);
  747.   unix_convert(fname,cnum,0,&bad_path);
  748.  
  749.   /* dos smetimes asks for a stat of "" - it returns a "hidden directory"
  750.      under WfWg - weird! */
  751.   if (! (*fname))
  752.     {
  753.       mode = aHIDDEN | aDIR;
  754.       if (!CAN_WRITE(cnum)) mode |= aRONLY;
  755.       size = 0;
  756.       mtime = 0;
  757.       ok = True;
  758.     }
  759.   else
  760.     if (check_name(fname,cnum))
  761.     {
  762.       if (sys_stat(fname,&sbuf) == 0)
  763.       {
  764.         mode = dos_mode(cnum,fname,&sbuf);
  765.         size = sbuf.st_size;
  766.         mtime = sbuf.st_mtime;
  767.         if (mode & aDIR)
  768.           size = 0;
  769.         ok = True;
  770.       }
  771.       else
  772.         DEBUG(3,("stat of %s failed (%s)\n",fname,strerror(errno)));
  773.     }
  774.   
  775.   if (!ok)
  776.   {
  777.     if((errno == ENOENT) && bad_path)
  778.     {
  779.       unix_ERR_class = ERRDOS;
  780.       unix_ERR_code = ERRbadpath;
  781.     }
  782.  
  783.     return(UNIXERROR(ERRDOS,ERRbadfile));
  784.   }
  785.  
  786.   outsize = set_message(outbuf,10,0,True);
  787.  
  788.   SSVAL(outbuf,smb_vwv0,mode);
  789.   if(lp_dos_filetime_resolution(SNUM(cnum)) )
  790.     put_dos_date3(outbuf,smb_vwv1,mtime & ~1);
  791.   else
  792.     put_dos_date3(outbuf,smb_vwv1,mtime);
  793.   SIVAL(outbuf,smb_vwv3,size);
  794.  
  795.   if (Protocol >= PROTOCOL_NT1) {
  796.     char *p = strrchr(fname,'/');
  797.     uint16 flg2 = SVAL(outbuf,smb_flg2);
  798.     if (!p) p = fname;
  799.     if (!is_8_3(fname, True))
  800.       SSVAL(outbuf,smb_flg2,flg2 | 0x40); /* IS_LONG_NAME */
  801.   }
  802.   
  803.   DEBUG(3,("%s getatr name=%s mode=%d size=%d\n",timestring(),fname,mode,size));
  804.   
  805.   return(outsize);
  806. }
  807.  
  808.  
  809. /****************************************************************************
  810.   reply to a setatr
  811. ****************************************************************************/
  812. int reply_setatr(char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
  813. {
  814.   pstring fname;
  815.   int cnum;
  816.   int outsize = 0;
  817.   BOOL ok=False;
  818.   int mode;
  819.   time_t mtime;
  820.   BOOL bad_path = False;
  821.  
  822.   cnum = SVAL(inbuf,smb_tid);
  823.   
  824.   pstrcpy(fname,smb_buf(inbuf) + 1);
  825.   unix_convert(fname,cnum,0,&bad_path);
  826.  
  827.   mode = SVAL(inbuf,smb_vwv0);
  828.   mtime = make_unix_date3(inbuf+smb_vwv1);
  829.   
  830.   if (directory_exist(fname,NULL))
  831.     mode |= aDIR;
  832.   if (check_name(fname,cnum))
  833.     ok =  (dos_chmod(cnum,fname,mode,NULL) == 0);
  834.   if (ok)
  835.     ok = set_filetime(cnum,fname,mtime);
  836.   
  837.   if (!ok)
  838.   {
  839.     if((errno == ENOENT) && bad_path)
  840.     {
  841.       unix_ERR_class = ERRDOS;
  842.       unix_ERR_code = ERRbadpath;
  843.     }
  844.  
  845.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  846.   }
  847.  
  848.   outsize = set_message(outbuf,0,0,True);
  849.   
  850.   DEBUG(3,("%s setatr name=%s mode=%d\n",timestring(),fname,mode));
  851.   
  852.   return(outsize);
  853. }
  854.  
  855.  
  856. /****************************************************************************
  857.   reply to a dskattr
  858. ****************************************************************************/
  859. int reply_dskattr(char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
  860. {
  861.   int cnum;
  862.   int outsize = 0;
  863.   int dfree,dsize,bsize;
  864.   
  865.   cnum = SVAL(inbuf,smb_tid);
  866.   
  867.   sys_disk_free(".",&bsize,&dfree,&dsize);
  868.   
  869.   outsize = set_message(outbuf,5,0,True);
  870.   
  871.   SSVAL(outbuf,smb_vwv0,dsize);
  872.   SSVAL(outbuf,smb_vwv1,bsize/512);
  873.   SSVAL(outbuf,smb_vwv2,512);
  874.   SSVAL(outbuf,smb_vwv3,dfree);
  875.   
  876.   DEBUG(3,("%s dskattr cnum=%d dfree=%d\n",timestring(),cnum,dfree));
  877.   
  878.   return(outsize);
  879. }
  880.  
  881.  
  882. /****************************************************************************
  883.   reply to a search
  884.   Can be called from SMBsearch, SMBffirst or SMBfunique.
  885. ****************************************************************************/
  886. int reply_search(char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
  887. {
  888.   pstring mask;
  889.   pstring directory;
  890.   pstring fname;
  891.   int size,mode;
  892.   time_t date;
  893.   int dirtype;
  894.   int cnum;
  895.   int outsize = 0;
  896.   int numentries = 0;
  897.   BOOL finished = False;
  898.   int maxentries;
  899.   int i;
  900.   char *p;
  901.   BOOL ok = False;
  902.   int status_len;
  903.   char *path;
  904.   char status[21];
  905.   int dptr_num= -1;
  906.   BOOL check_descend = False;
  907.   BOOL expect_close = False;
  908.   BOOL can_open = True;
  909.   BOOL bad_path = False;
  910.  
  911.   *mask = *directory = *fname = 0;
  912.  
  913.   /* If we were called as SMBffirst then we must expect close. */
  914.   if(CVAL(inbuf,smb_com) == SMBffirst)
  915.     expect_close = True;
  916.   
  917.   cnum = SVAL(inbuf,smb_tid);
  918.  
  919.   outsize = set_message(outbuf,1,3,True);
  920.   maxentries = SVAL(inbuf,smb_vwv0); 
  921.   dirtype = SVAL(inbuf,smb_vwv1);
  922.   path = smb_buf(inbuf) + 1;
  923.   status_len = SVAL(smb_buf(inbuf),3 + strlen(path));
  924.  
  925.   
  926.   /* dirtype &= ~aDIR; */
  927.   
  928.   DEBUG(5,("path=%s status_len=%d\n",path,status_len));
  929.  
  930.   
  931.   if (status_len == 0)
  932.     {
  933.       pstring dir2;
  934.  
  935.       pstrcpy(directory,smb_buf(inbuf)+1);
  936.       pstrcpy(dir2,smb_buf(inbuf)+1);
  937.       unix_convert(directory,cnum,0,&bad_path);
  938.       unix_format(dir2);
  939.  
  940.       if (!check_name(directory,cnum))
  941.         can_open = False;
  942.  
  943.       p = strrchr(dir2,'/');
  944.       if (p == NULL) 
  945.       {
  946.         pstrcpy(mask,dir2);
  947.         *dir2 = 0;
  948.       }
  949.       else
  950.       {
  951.         *p = 0;
  952.         pstrcpy(mask,p+1);
  953.       }
  954.  
  955.       p = strrchr(directory,'/');
  956.       if (!p) 
  957.         *directory = 0;
  958.       else
  959.         *p = 0;
  960.  
  961.       if (strlen(directory) == 0)
  962.         pstrcpy(directory,"./");
  963.       bzero(status,21);
  964.       CVAL(status,0) = dirtype;
  965.     }
  966.   else
  967.     {
  968.       memcpy(status,smb_buf(inbuf) + 1 + strlen(path) + 4,21);
  969.       memcpy(mask,status+1,11);
  970.       mask[11] = 0;
  971.       dirtype = CVAL(status,0) & 0x1F;
  972.       Connections[cnum].dirptr = dptr_fetch(status+12,&dptr_num);      
  973.       if (!Connections[cnum].dirptr)
  974.     goto SearchEmpty;
  975.       string_set(&Connections[cnum].dirpath,dptr_path(dptr_num));
  976.       if (!case_sensitive)
  977.     strnorm(mask);
  978.     }
  979.  
  980.   /* turn strings of spaces into a . */  
  981.   {
  982.     trim_string(mask,NULL," ");
  983.     if ((p = strrchr(mask,' ')))
  984.       {
  985.     fstring ext;
  986.     fstrcpy(ext,p+1);
  987.     *p = 0;
  988.     trim_string(mask,NULL," ");
  989.     pstrcat(mask,".");
  990.     pstrcat(mask,ext);
  991.       }
  992.   }
  993.  
  994.   {
  995.     for (p=mask; *p; p++)
  996.       {
  997.     if (*p != '?' && *p != '*' && !isdoschar(*p))
  998.       {
  999.         DEBUG(5,("Invalid char [%c] in search mask?\n",*p));
  1000.         *p = '?';
  1001.       }
  1002.       }
  1003.   }
  1004.  
  1005.   if (!strchr(mask,'.') && strlen(mask)>8)
  1006.     {
  1007.       fstring tmp;
  1008.       fstrcpy(tmp,&mask[8]);
  1009.       mask[8] = '.';
  1010.       mask[9] = 0;
  1011.       pstrcat(mask,tmp);
  1012.     }
  1013.  
  1014.   DEBUG(5,("mask=%s directory=%s\n",mask,directory));
  1015.   
  1016.   if (can_open)
  1017.     {
  1018.       p = smb_buf(outbuf) + 3;
  1019.       
  1020.       ok = True;
  1021.       
  1022.       if (status_len == 0)
  1023.     {
  1024.       dptr_num = dptr_create(cnum,directory,expect_close,SVAL(inbuf,smb_pid));
  1025.       if (dptr_num < 0)
  1026.         {
  1027.           if(dptr_num == -2)
  1028.           {
  1029.             if((errno == ENOENT) && bad_path)
  1030.             {
  1031.               unix_ERR_class = ERRDOS;
  1032.               unix_ERR_code = ERRbadpath;
  1033.             }
  1034.             return (UNIXERROR(ERRDOS,ERRnofids));
  1035.           }
  1036.           return(ERROR(ERRDOS,ERRnofids));
  1037.         }
  1038.     }
  1039.  
  1040.       DEBUG(4,("dptr_num is %d\n",dptr_num));
  1041.  
  1042.       if (ok)
  1043.     {
  1044.       if ((dirtype&0x1F) == aVOLID)
  1045.         {      
  1046.           memcpy(p,status,21);
  1047.           make_dir_struct(p,"???????????",volume_label(SNUM(cnum)),0,aVOLID,0);
  1048.           dptr_fill(p+12,dptr_num);
  1049.           if (dptr_zero(p+12) && (status_len==0))
  1050.         numentries = 1;
  1051.           else
  1052.         numentries = 0;
  1053.           p += DIR_STRUCT_SIZE;
  1054.         }
  1055.       else 
  1056.         {
  1057.           DEBUG(8,("dirpath=<%s> dontdescend=<%s>\n",Connections[cnum].dirpath,lp_dontdescend(SNUM(cnum))));
  1058.           if (in_list(Connections[cnum].dirpath,
  1059.               lp_dontdescend(SNUM(cnum)),True))
  1060.         check_descend = True;
  1061.  
  1062.           for (i=numentries;(i<maxentries) && !finished;i++)
  1063.         {
  1064.           finished = 
  1065.             !get_dir_entry(cnum,mask,dirtype,fname,&size,&mode,&date,check_descend);
  1066.           if (!finished)
  1067.             {
  1068.               memcpy(p,status,21);
  1069.               make_dir_struct(p,mask,fname,size,mode,date);
  1070.               dptr_fill(p+12,dptr_num);
  1071.               numentries++;
  1072.             }
  1073.           p += DIR_STRUCT_SIZE;
  1074.         }
  1075.         }
  1076.     }
  1077.     }
  1078.  
  1079.  
  1080.  SearchEmpty:
  1081.  
  1082.   if (numentries == 0 || !ok)
  1083.     {
  1084.       CVAL(outbuf,smb_rcls) = ERRDOS;
  1085.       SSVAL(outbuf,smb_err,ERRnofiles);
  1086.     }
  1087.  
  1088.   /* If we were called as SMBffirst with smb_search_id == NULL
  1089.      and no entries were found then return error and close dirptr 
  1090.      (X/Open spec) */
  1091.  
  1092.   if(ok && expect_close && numentries == 0 && status_len == 0)
  1093.     {
  1094.       CVAL(outbuf,smb_rcls) = ERRDOS;
  1095.       SSVAL(outbuf,smb_err,ERRnofiles);
  1096.       /* Also close the dptr - we know it's gone */
  1097.       dptr_close(dptr_num);
  1098.     }
  1099.  
  1100.   /* If we were called as SMBfunique, then we can close the dirptr now ! */
  1101.   if(dptr_num >= 0 && CVAL(inbuf,smb_com) == SMBfunique)
  1102.     dptr_close(dptr_num);
  1103.  
  1104.   SSVAL(outbuf,smb_vwv0,numentries);
  1105.   SSVAL(outbuf,smb_vwv1,3 + numentries * DIR_STRUCT_SIZE);
  1106.   CVAL(smb_buf(outbuf),0) = 5;
  1107.   SSVAL(smb_buf(outbuf),1,numentries*DIR_STRUCT_SIZE);
  1108.  
  1109.   if (Protocol >= PROTOCOL_NT1) {
  1110.     uint16 flg2 = SVAL(outbuf,smb_flg2);
  1111.     SSVAL(outbuf,smb_flg2,flg2 | 0x40); /* IS_LONG_NAME */
  1112.   }
  1113.   
  1114.   outsize += DIR_STRUCT_SIZE*numentries;
  1115.   smb_setlen(outbuf,outsize - 4);
  1116.   
  1117.   if ((! *directory) && dptr_path(dptr_num))
  1118.     slprintf(directory, sizeof(directory)-1, "(%s)",dptr_path(dptr_num));
  1119.  
  1120.   DEBUG(4,("%s %s mask=%s path=%s cnum=%d dtype=%d nument=%d of %d\n",
  1121.     timestring(),
  1122.     smb_fn_name(CVAL(inbuf,smb_com)), 
  1123.     mask,directory,cnum,dirtype,numentries,maxentries));
  1124.  
  1125.   return(outsize);
  1126. }
  1127.  
  1128.  
  1129. /****************************************************************************
  1130.   reply to a fclose (stop directory search)
  1131. ****************************************************************************/
  1132. int reply_fclose(char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
  1133. {
  1134.   int cnum;
  1135.   int outsize = 0;
  1136.   int status_len;
  1137.   char *path;
  1138.   char status[21];
  1139.   int dptr_num= -1;
  1140.  
  1141.   cnum = SVAL(inbuf,smb_tid);
  1142.  
  1143.   outsize = set_message(outbuf,1,0,True);
  1144.   path = smb_buf(inbuf) + 1;
  1145.   status_len = SVAL(smb_buf(inbuf),3 + strlen(path));
  1146.  
  1147.   
  1148.   if (status_len == 0)
  1149.     return(ERROR(ERRSRV,ERRsrverror));
  1150.  
  1151.   memcpy(status,smb_buf(inbuf) + 1 + strlen(path) + 4,21);
  1152.  
  1153.   if(dptr_fetch(status+12,&dptr_num)) {
  1154.     /*  Close the dptr - we know it's gone */
  1155.     dptr_close(dptr_num);
  1156.   }
  1157.  
  1158.   SSVAL(outbuf,smb_vwv0,0);
  1159.  
  1160.   DEBUG(3,("%s search close cnum=%d\n",timestring(),cnum));
  1161.  
  1162.   return(outsize);
  1163. }
  1164.  
  1165.  
  1166. /****************************************************************************
  1167.   reply to an open
  1168. ****************************************************************************/
  1169. int reply_open(char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
  1170. {
  1171.   pstring fname;
  1172.   int cnum;
  1173.   int fnum = -1;
  1174.   int outsize = 0;
  1175.   int fmode=0;
  1176.   int share_mode;
  1177.   int size = 0;
  1178.   time_t mtime=0;
  1179.   int unixmode;
  1180.   int rmode=0;
  1181.   struct stat sbuf;
  1182.   BOOL bad_path = False;
  1183.   files_struct *fsp;
  1184.   int oplock_request = CORE_OPLOCK_REQUEST(inbuf);
  1185.  
  1186.   cnum = SVAL(inbuf,smb_tid);
  1187.  
  1188.   share_mode = SVAL(inbuf,smb_vwv0);
  1189.  
  1190.   pstrcpy(fname,smb_buf(inbuf)+1);
  1191.   unix_convert(fname,cnum,0,&bad_path);
  1192.     
  1193.   fnum = find_free_file();
  1194.   if (fnum < 0)
  1195.     return(ERROR(ERRSRV,ERRnofids));
  1196.  
  1197.   if (!check_name(fname,cnum))
  1198.   {
  1199.     if((errno == ENOENT) && bad_path)
  1200.     {
  1201.       unix_ERR_class = ERRDOS;
  1202.       unix_ERR_code = ERRbadpath;
  1203.     }
  1204.     Files[fnum].reserved = False;
  1205.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  1206.   }
  1207.  
  1208.   unixmode = unix_mode(cnum,aARCH);
  1209.       
  1210.   open_file_shared(fnum,cnum,fname,share_mode,3,unixmode,
  1211.                    oplock_request,&rmode,NULL);
  1212.  
  1213.   fsp = &Files[fnum];
  1214.  
  1215.   if (!fsp->open)
  1216.   {
  1217.     if((errno == ENOENT) && bad_path)
  1218.     {
  1219.       unix_ERR_class = ERRDOS;
  1220.       unix_ERR_code = ERRbadpath;
  1221.     }
  1222.     Files[fnum].reserved = False;
  1223.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  1224.   }
  1225.  
  1226.   if (fstat(fsp->fd_ptr->fd,&sbuf) != 0) {
  1227.     close_file(fnum,False);
  1228.     return(ERROR(ERRDOS,ERRnoaccess));
  1229.   }
  1230.     
  1231.   size = sbuf.st_size;
  1232.   fmode = dos_mode(cnum,fname,&sbuf);
  1233.   mtime = sbuf.st_mtime;
  1234.  
  1235.   if (fmode & aDIR) {
  1236.     DEBUG(3,("attempt to open a directory %s\n",fname));
  1237.     close_file(fnum,False);
  1238.     return(ERROR(ERRDOS,ERRnoaccess));
  1239.   }
  1240.   
  1241.   outsize = set_message(outbuf,7,0,True);
  1242.   SSVAL(outbuf,smb_vwv0,fnum);
  1243.   SSVAL(outbuf,smb_vwv1,fmode);
  1244.   if(lp_dos_filetime_resolution(SNUM(cnum)) )
  1245.     put_dos_date3(outbuf,smb_vwv2,mtime & ~1);
  1246.   else
  1247.     put_dos_date3(outbuf,smb_vwv2,mtime);
  1248.   SIVAL(outbuf,smb_vwv4,size);
  1249.   SSVAL(outbuf,smb_vwv6,rmode);
  1250.  
  1251.   if (oplock_request && lp_fake_oplocks(SNUM(cnum))) {
  1252.     CVAL(outbuf,smb_flg) |= CORE_OPLOCK_GRANTED;
  1253.   }
  1254.     
  1255.   if(fsp->granted_oplock)
  1256.     CVAL(outbuf,smb_flg) |= CORE_OPLOCK_GRANTED;
  1257.   return(outsize);
  1258. }
  1259.  
  1260.  
  1261. /****************************************************************************
  1262.   reply to an open and X
  1263. ****************************************************************************/
  1264. int reply_open_and_X(char *inbuf,char *outbuf,int length,int bufsize)
  1265. {
  1266.   pstring fname;
  1267.   int cnum = SVAL(inbuf,smb_tid);
  1268.   int fnum = -1;
  1269.   int smb_mode = SVAL(inbuf,smb_vwv3);
  1270.   int smb_attr = SVAL(inbuf,smb_vwv5);
  1271.   /* Breakout the oplock request bits so we can set the
  1272.      reply bits separately. */
  1273.   BOOL ex_oplock_request = EXTENDED_OPLOCK_REQUEST(inbuf);
  1274.   BOOL core_oplock_request = CORE_OPLOCK_REQUEST(inbuf);
  1275.   BOOL oplock_request = ex_oplock_request | core_oplock_request;
  1276. #if 0
  1277.   int open_flags = SVAL(inbuf,smb_vwv2);
  1278.   int smb_sattr = SVAL(inbuf,smb_vwv4); 
  1279.   uint32 smb_time = make_unix_date3(inbuf+smb_vwv6);
  1280. #endif
  1281.   int smb_ofun = SVAL(inbuf,smb_vwv8);
  1282.   int unixmode;
  1283.   int size=0,fmode=0,mtime=0,rmode=0;
  1284.   struct stat sbuf;
  1285.   int smb_action = 0;
  1286.   BOOL bad_path = False;
  1287.   files_struct *fsp;
  1288.  
  1289.   /* If it's an IPC, pass off the pipe handler. */
  1290.   if (IS_IPC(cnum))
  1291.     return reply_open_pipe_and_X(inbuf,outbuf,length,bufsize);
  1292.  
  1293.   /* XXXX we need to handle passed times, sattr and flags */
  1294.  
  1295.   pstrcpy(fname,smb_buf(inbuf));
  1296.   unix_convert(fname,cnum,0,&bad_path);
  1297.     
  1298.   fnum = find_free_file();
  1299.   if (fnum < 0)
  1300.     return(ERROR(ERRSRV,ERRnofids));
  1301.  
  1302.   if (!check_name(fname,cnum))
  1303.   {
  1304.     if((errno == ENOENT) && bad_path)
  1305.     {
  1306.       unix_ERR_class = ERRDOS;
  1307.       unix_ERR_code = ERRbadpath;
  1308.     }
  1309.     Files[fnum].reserved = False;
  1310.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  1311.   }
  1312.  
  1313.   unixmode = unix_mode(cnum,smb_attr | aARCH);
  1314.       
  1315.   open_file_shared(fnum,cnum,fname,smb_mode,smb_ofun,unixmode,
  1316.            oplock_request, &rmode,&smb_action);
  1317.       
  1318.   fsp = &Files[fnum];
  1319.  
  1320.   if (!fsp->open)
  1321.   {
  1322.     if((errno == ENOENT) && bad_path)
  1323.     {
  1324.       unix_ERR_class = ERRDOS;
  1325.       unix_ERR_code = ERRbadpath;
  1326.     }
  1327.     Files[fnum].reserved = False;
  1328.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  1329.   }
  1330.  
  1331.   if (fstat(fsp->fd_ptr->fd,&sbuf) != 0) {
  1332.     close_file(fnum,False);
  1333.     return(ERROR(ERRDOS,ERRnoaccess));
  1334.   }
  1335.  
  1336.   size = sbuf.st_size;
  1337.   fmode = dos_mode(cnum,fname,&sbuf);
  1338.   mtime = sbuf.st_mtime;
  1339.   if (fmode & aDIR) {
  1340.     close_file(fnum,False);
  1341.     return(ERROR(ERRDOS,ERRnoaccess));
  1342.   }
  1343.  
  1344.   /* If the caller set the extended oplock request bit
  1345.      and we granted one (by whatever means) - set the
  1346.      correct bit for extended oplock reply.
  1347.    */
  1348.  
  1349.   if (ex_oplock_request && lp_fake_oplocks(SNUM(cnum))) {
  1350.     smb_action |= EXTENDED_OPLOCK_GRANTED;
  1351.   }
  1352.  
  1353.   if(ex_oplock_request && fsp->granted_oplock) {
  1354.     smb_action |= EXTENDED_OPLOCK_GRANTED;
  1355.   }
  1356.  
  1357.   /* If the caller set the core oplock request bit
  1358.      and we granted one (by whatever means) - set the
  1359.      correct bit for core oplock reply.
  1360.    */
  1361.  
  1362.   if (core_oplock_request && lp_fake_oplocks(SNUM(cnum))) {
  1363.     CVAL(outbuf,smb_flg) |= CORE_OPLOCK_GRANTED;
  1364.   }
  1365.  
  1366.   if(core_oplock_request && fsp->granted_oplock) {
  1367.     CVAL(outbuf,smb_flg) |= CORE_OPLOCK_GRANTED;
  1368.   }
  1369.  
  1370.   set_message(outbuf,15,0,True);
  1371.   SSVAL(outbuf,smb_vwv2,fnum);
  1372.   SSVAL(outbuf,smb_vwv3,fmode);
  1373.   if(lp_dos_filetime_resolution(SNUM(cnum)) )
  1374.     put_dos_date3(outbuf,smb_vwv4,mtime & ~1);
  1375.   else
  1376.     put_dos_date3(outbuf,smb_vwv4,mtime);
  1377.   SIVAL(outbuf,smb_vwv6,size);
  1378.   SSVAL(outbuf,smb_vwv8,rmode);
  1379.   SSVAL(outbuf,smb_vwv11,smb_action);
  1380.  
  1381.   chain_fnum = fnum;
  1382.  
  1383.   return chain_reply(inbuf,outbuf,length,bufsize);
  1384. }
  1385.  
  1386.  
  1387. /****************************************************************************
  1388.   reply to a SMBulogoffX
  1389. ****************************************************************************/
  1390. int reply_ulogoffX(char *inbuf,char *outbuf,int length,int bufsize)
  1391. {
  1392.   uint16 vuid = SVAL(inbuf,smb_uid);
  1393.   user_struct *vuser = get_valid_user_struct(vuid);
  1394.  
  1395.   if(vuser == 0) {
  1396.     DEBUG(3,("ulogoff, vuser id %d does not map to user.\n", vuid));
  1397.   }
  1398.  
  1399.   /* in user level security we are supposed to close any files
  1400.      open by this user */
  1401.   if ((vuser != 0) && (lp_security() != SEC_SHARE)) {
  1402.     int i;
  1403.     for (i=0;i<MAX_OPEN_FILES;i++)
  1404.       if ((Files[i].vuid == vuid) && Files[i].open) {
  1405.     close_file(i,False);
  1406.       }
  1407.   }
  1408.  
  1409.   invalidate_vuid(vuid);
  1410.  
  1411.   set_message(outbuf,2,0,True);
  1412.  
  1413.   DEBUG(3,("%s ulogoffX vuid=%d\n",timestring(),vuid));
  1414.  
  1415.   return chain_reply(inbuf,outbuf,length,bufsize);
  1416. }
  1417.  
  1418.  
  1419. /****************************************************************************
  1420.   reply to a mknew or a create
  1421. ****************************************************************************/
  1422. int reply_mknew(char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
  1423. {
  1424.   pstring fname;
  1425.   int cnum,com;
  1426.   int fnum = -1;
  1427.   int outsize = 0;
  1428.   int createmode;
  1429.   mode_t unixmode;
  1430.   int ofun = 0;
  1431.   BOOL bad_path = False;
  1432.   files_struct *fsp;
  1433.   int oplock_request = CORE_OPLOCK_REQUEST(inbuf);
  1434.  
  1435.   com = SVAL(inbuf,smb_com);
  1436.   cnum = SVAL(inbuf,smb_tid);
  1437.  
  1438.   createmode = SVAL(inbuf,smb_vwv0);
  1439.   pstrcpy(fname,smb_buf(inbuf)+1);
  1440.   unix_convert(fname,cnum,0,&bad_path);
  1441.  
  1442.   if (createmode & aVOLID)
  1443.     {
  1444.       DEBUG(0,("Attempt to create file (%s) with volid set - please report this\n",fname));
  1445.     }
  1446.   
  1447.   unixmode = unix_mode(cnum,createmode);
  1448.   
  1449.   fnum = find_free_file();
  1450.   if (fnum < 0)
  1451.     return(ERROR(ERRSRV,ERRnofids));
  1452.  
  1453.   if (!check_name(fname,cnum))
  1454.   {
  1455.     if((errno == ENOENT) && bad_path)
  1456.     {
  1457.       unix_ERR_class = ERRDOS;
  1458.       unix_ERR_code = ERRbadpath;
  1459.     }
  1460.     Files[fnum].reserved = False;
  1461.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  1462.   }
  1463.  
  1464.   if(com == SMBmknew)
  1465.   {
  1466.     /* We should fail if file exists. */
  1467.     ofun = 0x10;
  1468.   }
  1469.   else
  1470.   {
  1471.     /* SMBcreate - Create if file doesn't exist, truncate if it does. */
  1472.     ofun = 0x12;
  1473.   }
  1474.  
  1475.   /* Open file in dos compatibility share mode. */
  1476.   open_file_shared(fnum,cnum,fname,(DENY_FCB<<4)|0xF, ofun, unixmode, 
  1477.                    oplock_request, NULL, NULL);
  1478.   
  1479.   fsp = &Files[fnum];
  1480.  
  1481.   if (!fsp->open)
  1482.   {
  1483.     if((errno == ENOENT) && bad_path) 
  1484.     {
  1485.       unix_ERR_class = ERRDOS;
  1486.       unix_ERR_code = ERRbadpath;
  1487.     }
  1488.     Files[fnum].reserved = False;
  1489.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  1490.   }
  1491.  
  1492.   outsize = set_message(outbuf,1,0,True);
  1493.   SSVAL(outbuf,smb_vwv0,fnum);
  1494.  
  1495.   if (oplock_request && lp_fake_oplocks(SNUM(cnum))) {
  1496.     CVAL(outbuf,smb_flg) |= CORE_OPLOCK_GRANTED;
  1497.   }
  1498.  
  1499.   if(fsp->granted_oplock)
  1500.     CVAL(outbuf,smb_flg) |= CORE_OPLOCK_GRANTED;
  1501.  
  1502.   DEBUG(2,("new file %s\n",fname));
  1503.   DEBUG(3,("%s mknew %s fd=%d fnum=%d cnum=%d dmode=%d umode=%o\n",timestring(),fname,Files[fnum].fd_ptr->fd,fnum,cnum,createmode,unixmode));
  1504.   
  1505.   return(outsize);
  1506. }
  1507.  
  1508.  
  1509. /****************************************************************************
  1510.   reply to a create temporary file
  1511. ****************************************************************************/
  1512. int reply_ctemp(char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
  1513. {
  1514.   pstring fname;
  1515.   pstring fname2;
  1516.   int cnum;
  1517.   int fnum = -1;
  1518.   int outsize = 0;
  1519.   int createmode;
  1520.   mode_t unixmode;
  1521.   BOOL bad_path = False;
  1522.   files_struct *fsp;
  1523.   int oplock_request = CORE_OPLOCK_REQUEST(inbuf);
  1524.  
  1525.   cnum = SVAL(inbuf,smb_tid);
  1526.   createmode = SVAL(inbuf,smb_vwv0);
  1527.   pstrcpy(fname,smb_buf(inbuf)+1);
  1528.   pstrcat(fname,"/TMXXXXXX");
  1529.   unix_convert(fname,cnum,0,&bad_path);
  1530.   
  1531.   unixmode = unix_mode(cnum,createmode);
  1532.   
  1533.   fnum = find_free_file();
  1534.   if (fnum < 0)
  1535.     return(ERROR(ERRSRV,ERRnofids));
  1536.  
  1537.   if (!check_name(fname,cnum))
  1538.   {
  1539.     if((errno == ENOENT) && bad_path)
  1540.     {
  1541.       unix_ERR_class = ERRDOS;
  1542.       unix_ERR_code = ERRbadpath;
  1543.     }
  1544.     Files[fnum].reserved = False;
  1545.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  1546.   }
  1547.  
  1548.   pstrcpy(fname2,(char *)mktemp(fname));
  1549.  
  1550.   /* Open file in dos compatibility share mode. */
  1551.   /* We should fail if file exists. */
  1552.   open_file_shared(fnum,cnum,fname2,(DENY_FCB<<4)|0xF, 0x10, unixmode, 
  1553.                    oplock_request, NULL, NULL);
  1554.  
  1555.   fsp = &Files[fnum];
  1556.  
  1557.   if (!fsp->open)
  1558.   {
  1559.     if((errno == ENOENT) && bad_path)
  1560.     {
  1561.       unix_ERR_class = ERRDOS;
  1562.       unix_ERR_code = ERRbadpath;
  1563.     }
  1564.     Files[fnum].reserved = False;
  1565.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  1566.   }
  1567.  
  1568.   outsize = set_message(outbuf,1,2 + strlen(fname2),True);
  1569.   SSVAL(outbuf,smb_vwv0,fnum);
  1570.   CVAL(smb_buf(outbuf),0) = 4;
  1571.   pstrcpy(smb_buf(outbuf) + 1,fname2);
  1572.  
  1573.   if (oplock_request && lp_fake_oplocks(SNUM(cnum))) {
  1574.     CVAL(outbuf,smb_flg) |= CORE_OPLOCK_GRANTED;
  1575.   }
  1576.   
  1577.   if(fsp->granted_oplock)
  1578.     CVAL(outbuf,smb_flg) |= CORE_OPLOCK_GRANTED;
  1579.  
  1580.   DEBUG(2,("created temp file %s\n",fname2));
  1581.   DEBUG(3,("%s ctemp %s fd=%d fnum=%d cnum=%d dmode=%d umode=%o\n",timestring(),fname2,Files[fnum].fd_ptr->fd,fnum,cnum,createmode,unixmode));
  1582.   
  1583.   return(outsize);
  1584. }
  1585.  
  1586.  
  1587. /*******************************************************************
  1588. check if a user is allowed to delete a file
  1589. ********************************************************************/
  1590. static BOOL can_delete(char *fname,int cnum,int dirtype)
  1591. {
  1592.   struct stat sbuf;
  1593.   int fmode;
  1594.  
  1595.   if (!CAN_WRITE(cnum)) return(False);
  1596.  
  1597.   if (sys_lstat(fname,&sbuf) != 0) return(False);
  1598.   fmode = dos_mode(cnum,fname,&sbuf);
  1599.   if (fmode & aDIR) return(False);
  1600.   if (!lp_delete_readonly(SNUM(cnum))) {
  1601.     if (fmode & aRONLY) return(False);
  1602.   }
  1603.   if ((fmode & ~dirtype) & (aHIDDEN | aSYSTEM))
  1604.     return(False);
  1605.   if (!check_file_sharing(cnum,fname,False)) return(False);
  1606.   return(True);
  1607. }
  1608.  
  1609. /****************************************************************************
  1610.   reply to a unlink
  1611. ****************************************************************************/
  1612. int reply_unlink(char *inbuf,char *outbuf, int dum_size, int dum_bufsize)
  1613. {
  1614.   int outsize = 0;
  1615.   pstring name;
  1616.   int cnum;
  1617.   int dirtype;
  1618.   pstring directory;
  1619.   pstring mask;
  1620.   char *p;
  1621.   int count=0;
  1622.   int error = ERRnoaccess;
  1623.   BOOL has_wild;
  1624.   BOOL exists=False;
  1625.   BOOL bad_path = False;
  1626.  
  1627.   *directory = *mask = 0;
  1628.  
  1629.   cnum = SVAL(inbuf,smb_tid);
  1630.   dirtype = SVAL(inbuf,smb_vwv0);
  1631.   
  1632.   pstrcpy(name,smb_buf(inbuf) + 1);
  1633.    
  1634.   DEBUG(3,("reply_unlink : %s\n",name));
  1635.    
  1636.   unix_convert(name,cnum,0,&bad_path);
  1637.  
  1638.   p = strrchr(name,'/');
  1639.   if (!p) {
  1640.     pstrcpy(directory,"./");
  1641.     pstrcpy(mask,name);
  1642.   } else {
  1643.     *p = 0;
  1644.     pstrcpy(directory,name);
  1645.     pstrcpy(mask,p+1);
  1646.   }
  1647.  
  1648.   if (is_mangled(mask))
  1649.     check_mangled_stack(mask);
  1650.  
  1651.   has_wild = strchr(mask,'*') || strchr(mask,'?');
  1652.  
  1653.   if (!has_wild) {
  1654.     pstrcat(directory,"/");
  1655.     pstrcat(directory,mask);
  1656.     if (can_delete(directory,cnum,dirtype) && !sys_unlink(directory)) count++;
  1657.     if (!count) exists = file_exist(directory,NULL);    
  1658.   } else {
  1659.     void *dirptr = NULL;
  1660.     char *dname;
  1661.  
  1662.     if (check_name(directory,cnum))
  1663.       dirptr = OpenDir(cnum, directory, True);
  1664.  
  1665.     /* XXXX the CIFS spec says that if bit0 of the flags2 field is set then
  1666.        the pattern matches against the long name, otherwise the short name 
  1667.        We don't implement this yet XXXX
  1668.        */
  1669.  
  1670.     if (dirptr)
  1671.       {
  1672.     error = ERRbadfile;
  1673.  
  1674.     if (strequal(mask,"????????.???"))
  1675.       pstrcpy(mask,"*");
  1676.  
  1677.     while ((dname = ReadDirName(dirptr)))
  1678.       {
  1679.         pstring fname;
  1680.         pstrcpy(fname,dname);
  1681.         
  1682.         if(!mask_match(fname, mask, case_sensitive, False)) continue;
  1683.  
  1684.         error = ERRnoaccess;
  1685.         slprintf(fname,sizeof(fname)-1, "%s/%s",directory,dname);
  1686.         if (!can_delete(fname,cnum,dirtype)) continue;
  1687.         if (!sys_unlink(fname)) count++;
  1688.         DEBUG(3,("reply_unlink : doing unlink on %s\n",fname));
  1689.       }
  1690.     CloseDir(dirptr);
  1691.       }
  1692.   }
  1693.   
  1694.   if (count == 0) {
  1695.     if (exists)
  1696.       return(ERROR(ERRDOS,error));
  1697.     else
  1698.     {
  1699.       if((errno == ENOENT) && bad_path)
  1700.       {
  1701.         unix_ERR_class = ERRDOS;
  1702.         unix_ERR_code = ERRbadpath;
  1703.       }
  1704.       return(UNIXERROR(ERRDOS,error));
  1705.     }
  1706.   }
  1707.   
  1708.   outsize = set_message(outbuf,0,0,True);
  1709.   
  1710.   return(outsize);
  1711. }
  1712.  
  1713.  
  1714. /****************************************************************************
  1715.    reply to a readbraw (core+ protocol)
  1716. ****************************************************************************/
  1717. int reply_readbraw(char *inbuf, char *outbuf, int dum_size, int dum_buffsize)
  1718. {
  1719.   int cnum,maxcount,mincount,fnum;
  1720.   int nread = 0;
  1721.   uint32 startpos;
  1722.   char *header = outbuf;
  1723.   int ret=0;
  1724.   int fd;
  1725.   char *fname;
  1726.  
  1727.   /*
  1728.    * Special check if an oplock break has been issued
  1729.    * and the readraw request croses on the wire, we must
  1730.    * return a zero length response here.
  1731.    */
  1732.  
  1733.   if(global_oplock_break)
  1734.   {
  1735.     _smb_setlen(header,0);
  1736.     transfer_file(0,Client,0,header,4,0);
  1737.     DEBUG(5,("readbraw - oplock break finished\n"));
  1738.     return -1;
  1739.   }
  1740.  
  1741.   cnum = SVAL(inbuf,smb_tid);
  1742.   fnum = GETFNUM(inbuf,smb_vwv0);
  1743.  
  1744.   startpos = IVAL(inbuf,smb_vwv1);
  1745.   maxcount = SVAL(inbuf,smb_vwv3);
  1746.   mincount = SVAL(inbuf,smb_vwv4);
  1747.  
  1748.   /* ensure we don't overrun the packet size */
  1749.   maxcount = MIN(65535,maxcount);
  1750.   maxcount = MAX(mincount,maxcount);
  1751.  
  1752.   if (!FNUM_OK(fnum,cnum) || !Files[fnum].can_read)
  1753.     {
  1754.       DEBUG(3,("fnum %d not open in readbraw - cache prime?\n",fnum));
  1755.       _smb_setlen(header,0);
  1756.       transfer_file(0,Client,0,header,4,0);
  1757.       return(-1);
  1758.     }
  1759.   else
  1760.     {
  1761.       fd = Files[fnum].fd_ptr->fd;
  1762.       fname = Files[fnum].name;
  1763.     }
  1764.  
  1765.  
  1766.   if (!is_locked(fnum,cnum,maxcount,startpos))
  1767.     {
  1768.       int size = Files[fnum].size;
  1769.       int sizeneeded = startpos + maxcount;
  1770.         
  1771.       if (size < sizeneeded) {
  1772.     struct stat st;
  1773.     if (fstat(Files[fnum].fd_ptr->fd,&st) == 0)
  1774.       size = st.st_size;
  1775.     if (!Files[fnum].can_write) 
  1776.       Files[fnum].size = size;
  1777.       }
  1778.  
  1779.       nread = MIN(maxcount,(int)(size - startpos));      
  1780.     }
  1781.  
  1782.   if (nread < mincount)
  1783.     nread = 0;
  1784.   
  1785.   DEBUG(3,("%s readbraw fnum=%d cnum=%d start=%d max=%d min=%d nread=%d\n",
  1786.        timestring(),
  1787.        fnum,cnum,startpos,
  1788.        maxcount,mincount,nread));
  1789.   
  1790. #if UNSAFE_READRAW
  1791.   {
  1792.     int predict=0;
  1793.     _smb_setlen(header,nread);
  1794.  
  1795. #if USE_READ_PREDICTION
  1796.     if (!Files[fnum].can_write)
  1797.       predict = read_predict(fd,startpos,header+4,NULL,nread);
  1798. #endif
  1799.  
  1800.     if ((nread-predict) > 0)
  1801.       seek_file(fnum,startpos + predict);
  1802.     
  1803.     ret = transfer_file(fd,Client,nread-predict,header,4+predict,
  1804.             startpos+predict);
  1805.   }
  1806.  
  1807.   if (ret != nread+4)
  1808.     DEBUG(0,("ERROR: file read failure on %s at %d for %d bytes (%d)\n",
  1809.          fname,startpos,nread,ret));
  1810.  
  1811. #else
  1812.   ret = read_file(fnum,header+4,startpos,nread);
  1813.   if (ret < mincount) ret = 0;
  1814.  
  1815.   _smb_setlen(header,ret);
  1816.   transfer_file(0,Client,0,header,4+ret,0);
  1817. #endif
  1818.  
  1819.   DEBUG(5,("readbraw finished\n"));
  1820.   return -1;
  1821. }
  1822.  
  1823.  
  1824. /****************************************************************************
  1825.   reply to a lockread (core+ protocol)
  1826. ****************************************************************************/
  1827. int reply_lockread(char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
  1828. {
  1829.   int cnum,fnum;
  1830.   int nread = -1;
  1831.   char *data;
  1832.   int outsize = 0;
  1833.   uint32 startpos, numtoread;
  1834.   int eclass;
  1835.   uint32 ecode;
  1836.   
  1837.   cnum = SVAL(inbuf,smb_tid);
  1838.   fnum = GETFNUM(inbuf,smb_vwv0);
  1839.  
  1840.   CHECK_FNUM(fnum,cnum);
  1841.   CHECK_READ(fnum);
  1842.   CHECK_ERROR(fnum);
  1843.  
  1844.   numtoread = SVAL(inbuf,smb_vwv1);
  1845.   startpos = IVAL(inbuf,smb_vwv2);
  1846.   
  1847.   outsize = set_message(outbuf,5,3,True);
  1848.   numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
  1849.   data = smb_buf(outbuf) + 3;
  1850.   
  1851.   if(!do_lock( fnum, cnum, numtoread, startpos, &eclass, &ecode))
  1852.     return (ERROR(eclass,ecode));
  1853.  
  1854.   nread = read_file(fnum,data,startpos,numtoread);
  1855.   
  1856.   if (nread < 0)
  1857.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  1858.   
  1859.   outsize += nread;
  1860.   SSVAL(outbuf,smb_vwv0,nread);
  1861.   SSVAL(outbuf,smb_vwv5,nread+3);
  1862.   SSVAL(smb_buf(outbuf),1,nread);
  1863.   
  1864.   DEBUG(3,("%s lockread fnum=%d cnum=%d num=%d nread=%d\n",timestring(),fnum,cnum,numtoread,nread));
  1865.   
  1866.   return(outsize);
  1867. }
  1868.  
  1869.  
  1870. /****************************************************************************
  1871.   reply to a read
  1872. ****************************************************************************/
  1873. int reply_read(char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
  1874. {
  1875.   int cnum,numtoread,fnum;
  1876.   int nread = 0;
  1877.   char *data;
  1878.   uint32 startpos;
  1879.   int outsize = 0;
  1880.   
  1881.   cnum = SVAL(inbuf,smb_tid);
  1882.   fnum = GETFNUM(inbuf,smb_vwv0);
  1883.  
  1884.   CHECK_FNUM(fnum,cnum);
  1885.   CHECK_READ(fnum);
  1886.   CHECK_ERROR(fnum);
  1887.  
  1888.   numtoread = SVAL(inbuf,smb_vwv1);
  1889.   startpos = IVAL(inbuf,smb_vwv2);
  1890.   
  1891.   outsize = set_message(outbuf,5,3,True);
  1892.   numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
  1893.   data = smb_buf(outbuf) + 3;
  1894.   
  1895.   if (is_locked(fnum,cnum,numtoread,startpos))
  1896.     return(ERROR(ERRDOS,ERRlock));    
  1897.  
  1898.   if (numtoread > 0)
  1899.     nread = read_file(fnum,data,startpos,numtoread);
  1900.   
  1901.   if (nread < 0)
  1902.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  1903.   
  1904.   outsize += nread;
  1905.   SSVAL(outbuf,smb_vwv0,nread);
  1906.   SSVAL(outbuf,smb_vwv5,nread+3);
  1907.   CVAL(smb_buf(outbuf),0) = 1;
  1908.   SSVAL(smb_buf(outbuf),1,nread);
  1909.   
  1910.   DEBUG(3,("%s read fnum=%d cnum=%d num=%d nread=%d\n",timestring(),fnum,cnum,numtoread,nread));
  1911.   
  1912.   return(outsize);
  1913. }
  1914.  
  1915.  
  1916. /****************************************************************************
  1917.   reply to a read and X
  1918. ****************************************************************************/
  1919. int reply_read_and_X(char *inbuf,char *outbuf,int length,int bufsize)
  1920. {
  1921.   int fnum = GETFNUM(inbuf,smb_vwv2);
  1922.   uint32 smb_offs = IVAL(inbuf,smb_vwv3);
  1923.   int smb_maxcnt = SVAL(inbuf,smb_vwv5);
  1924.   int smb_mincnt = SVAL(inbuf,smb_vwv6);
  1925.   int cnum;
  1926.   int nread = -1;
  1927.   char *data;
  1928.   BOOL ok = False;
  1929.  
  1930.   cnum = SVAL(inbuf,smb_tid);
  1931.  
  1932.   CHECK_FNUM(fnum,cnum);
  1933.   CHECK_READ(fnum);
  1934.   CHECK_ERROR(fnum);
  1935.  
  1936.   set_message(outbuf,12,0,True);
  1937.   data = smb_buf(outbuf);
  1938.  
  1939.   if (is_locked(fnum,cnum,smb_maxcnt,smb_offs))
  1940.     return(ERROR(ERRDOS,ERRlock));
  1941.   nread = read_file(fnum,data,smb_offs,smb_maxcnt);
  1942.   ok = True;
  1943.   
  1944.   if (nread < 0)
  1945.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  1946.   
  1947.   SSVAL(outbuf,smb_vwv5,nread);
  1948.   SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
  1949.   SSVAL(smb_buf(outbuf),-2,nread);
  1950.   
  1951.   DEBUG(3,("%s readX fnum=%d cnum=%d min=%d max=%d nread=%d\n",
  1952.     timestring(),fnum,cnum,
  1953.     smb_mincnt,smb_maxcnt,nread));
  1954.  
  1955.   chain_fnum = fnum;
  1956.  
  1957.   return chain_reply(inbuf,outbuf,length,bufsize);
  1958. }
  1959.  
  1960.  
  1961. /****************************************************************************
  1962.   reply to a writebraw (core+ or LANMAN1.0 protocol)
  1963. ****************************************************************************/
  1964. int reply_writebraw(char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
  1965. {
  1966.   int nwritten=0;
  1967.   int total_written=0;
  1968.   int numtowrite=0;
  1969.   int cnum,fnum;
  1970.   int outsize = 0;
  1971.   long startpos;
  1972.   char *data=NULL;
  1973.   BOOL write_through;
  1974.   int tcount;
  1975.  
  1976.   cnum = SVAL(inbuf,smb_tid);
  1977.   fnum = GETFNUM(inbuf,smb_vwv0);
  1978.  
  1979.   CHECK_FNUM(fnum,cnum);
  1980.   CHECK_WRITE(fnum);
  1981.   CHECK_ERROR(fnum);
  1982.   
  1983.   tcount = IVAL(inbuf,smb_vwv1);
  1984.   startpos = IVAL(inbuf,smb_vwv3);
  1985.   write_through = BITSETW(inbuf+smb_vwv7,0);
  1986.  
  1987.   /* We have to deal with slightly different formats depending
  1988.      on whether we are using the core+ or lanman1.0 protocol */
  1989.   if(Protocol <= PROTOCOL_COREPLUS) {
  1990.     numtowrite = SVAL(smb_buf(inbuf),-2);
  1991.     data = smb_buf(inbuf);
  1992.   } else {
  1993.     numtowrite = SVAL(inbuf,smb_vwv10);
  1994.     data = smb_base(inbuf) + SVAL(inbuf, smb_vwv11);
  1995.   }
  1996.  
  1997.   /* force the error type */
  1998.   CVAL(inbuf,smb_com) = SMBwritec;
  1999.   CVAL(outbuf,smb_com) = SMBwritec;
  2000.  
  2001.   if (is_locked(fnum,cnum,tcount,startpos))
  2002.     return(ERROR(ERRDOS,ERRlock));
  2003.  
  2004.   if (seek_file(fnum,startpos) != startpos)
  2005.     DEBUG(0,("couldn't seek to %d in writebraw\n",startpos));
  2006.  
  2007.   if (numtowrite>0)
  2008.     nwritten = write_file(fnum,data,numtowrite);
  2009.   
  2010.   DEBUG(3,("%s writebraw1 fnum=%d cnum=%d start=%d num=%d wrote=%d sync=%d\n",
  2011.        timestring(),fnum,cnum,startpos,numtowrite,nwritten,write_through));
  2012.  
  2013.   if (nwritten < numtowrite) 
  2014.     return(UNIXERROR(ERRHRD,ERRdiskfull));
  2015.  
  2016.   total_written = nwritten;
  2017.  
  2018.   /* Return a message to the redirector to tell it
  2019.      to send more bytes */
  2020.   CVAL(outbuf,smb_com) = SMBwritebraw;
  2021.   SSVALS(outbuf,smb_vwv0,-1);
  2022.   outsize = set_message(outbuf,Protocol>PROTOCOL_COREPLUS?1:0,0,True);
  2023.   send_smb(Client,outbuf);
  2024.   
  2025.   /* Now read the raw data into the buffer and write it */
  2026.   if (read_smb_length(Client,inbuf,SMB_SECONDARY_WAIT) == -1) {
  2027.     exit_server("secondary writebraw failed");
  2028.   }
  2029.   
  2030.   /* Even though this is not an smb message, smb_len
  2031.      returns the generic length of an smb message */
  2032.   numtowrite = smb_len(inbuf);
  2033.  
  2034.   if (tcount > nwritten+numtowrite) {
  2035.     DEBUG(3,("Client overestimated the write %d %d %d\n",
  2036.          tcount,nwritten,numtowrite));
  2037.   }
  2038.  
  2039.   nwritten = transfer_file(Client,Files[fnum].fd_ptr->fd,numtowrite,NULL,0,
  2040.                startpos+nwritten);
  2041.   total_written += nwritten;
  2042.   
  2043.   /* Set up outbuf to return the correct return */
  2044.   outsize = set_message(outbuf,1,0,True);
  2045.   CVAL(outbuf,smb_com) = SMBwritec;
  2046.   SSVAL(outbuf,smb_vwv0,total_written);
  2047.  
  2048.   if (nwritten < numtowrite) {
  2049.     CVAL(outbuf,smb_rcls) = ERRHRD;
  2050.     SSVAL(outbuf,smb_err,ERRdiskfull);      
  2051.   }
  2052.  
  2053.   if (lp_syncalways(SNUM(cnum)) || write_through)
  2054.     sync_file(fnum);
  2055.  
  2056.   DEBUG(3,("%s writebraw2 fnum=%d cnum=%d start=%d num=%d wrote=%d\n",
  2057.        timestring(),fnum,cnum,startpos,numtowrite,total_written));
  2058.  
  2059.   /* we won't return a status if write through is not selected - this 
  2060.      follows what WfWg does */
  2061.   if (!write_through && total_written==tcount)
  2062.     return(-1);
  2063.  
  2064.   return(outsize);
  2065. }
  2066.  
  2067.  
  2068. /****************************************************************************
  2069.   reply to a writeunlock (core+)
  2070. ****************************************************************************/
  2071. int reply_writeunlock(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  2072. {
  2073.   int cnum,fnum;
  2074.   int nwritten = -1;
  2075.   int outsize = 0;
  2076.   char *data;
  2077.   uint32 numtowrite,startpos;
  2078.   int eclass;
  2079.   uint32 ecode;
  2080.   
  2081.   cnum = SVAL(inbuf,smb_tid);
  2082.   fnum = GETFNUM(inbuf,smb_vwv0);
  2083.  
  2084.   CHECK_FNUM(fnum,cnum);
  2085.   CHECK_WRITE(fnum);
  2086.   CHECK_ERROR(fnum);
  2087.  
  2088.   numtowrite = SVAL(inbuf,smb_vwv1);
  2089.   startpos = IVAL(inbuf,smb_vwv2);
  2090.   data = smb_buf(inbuf) + 3;
  2091.   
  2092.   if (is_locked(fnum,cnum,numtowrite,startpos))
  2093.     return(ERROR(ERRDOS,ERRlock));
  2094.  
  2095.   seek_file(fnum,startpos);
  2096.  
  2097.   /* The special X/Open SMB protocol handling of
  2098.      zero length writes is *NOT* done for
  2099.      this call */
  2100.   if(numtowrite == 0)
  2101.     nwritten = 0;
  2102.   else
  2103.     nwritten = write_file(fnum,data,numtowrite);
  2104.   
  2105.   if (lp_syncalways(SNUM(cnum)))
  2106.     sync_file(fnum);
  2107.  
  2108.   if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0))
  2109.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  2110.  
  2111.   if(!do_unlock(fnum, cnum, numtowrite, startpos, &eclass, &ecode))
  2112.     return(ERROR(eclass,ecode));
  2113.  
  2114.   outsize = set_message(outbuf,1,0,True);
  2115.   
  2116.   SSVAL(outbuf,smb_vwv0,nwritten);
  2117.   
  2118.   DEBUG(3,("%s writeunlock fnum=%d cnum=%d num=%d wrote=%d\n",
  2119.        timestring(),fnum,cnum,numtowrite,nwritten));
  2120.   
  2121.   return(outsize);
  2122. }
  2123.  
  2124.  
  2125. /****************************************************************************
  2126.   reply to a write
  2127. ****************************************************************************/
  2128. int reply_write(char *inbuf,char *outbuf,int dum1,int dum2)
  2129. {
  2130.   int cnum,numtowrite,fnum;
  2131.   int nwritten = -1;
  2132.   int outsize = 0;
  2133.   int startpos;
  2134.   char *data;
  2135.  
  2136.   dum1 = dum2 = 0;
  2137.  
  2138.   
  2139.   cnum = SVAL(inbuf,smb_tid);
  2140.   fnum = GETFNUM(inbuf,smb_vwv0);
  2141.  
  2142.   CHECK_FNUM(fnum,cnum);
  2143.   CHECK_WRITE(fnum);
  2144.   CHECK_ERROR(fnum);
  2145.  
  2146.   numtowrite = SVAL(inbuf,smb_vwv1);
  2147.   startpos = IVAL(inbuf,smb_vwv2);
  2148.   data = smb_buf(inbuf) + 3;
  2149.   
  2150.   if (is_locked(fnum,cnum,numtowrite,startpos))
  2151.     return(ERROR(ERRDOS,ERRlock));
  2152.  
  2153.   seek_file(fnum,startpos);
  2154.  
  2155.   /* X/Open SMB protocol says that if smb_vwv1 is
  2156.      zero then the file size should be extended or
  2157.      truncated to the size given in smb_vwv[2-3] */
  2158.   if(numtowrite == 0)
  2159.     nwritten = set_filelen(Files[fnum].fd_ptr->fd, startpos);
  2160.   else
  2161.     nwritten = write_file(fnum,data,numtowrite);
  2162.   
  2163.   if (lp_syncalways(SNUM(cnum)))
  2164.     sync_file(fnum);
  2165.  
  2166.   if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0))
  2167.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  2168.  
  2169.   outsize = set_message(outbuf,1,0,True);
  2170.   
  2171.   SSVAL(outbuf,smb_vwv0,nwritten);
  2172.  
  2173.   if (nwritten < numtowrite) {
  2174.     CVAL(outbuf,smb_rcls) = ERRHRD;
  2175.     SSVAL(outbuf,smb_err,ERRdiskfull);      
  2176.   }
  2177.   
  2178.   DEBUG(3,("%s write fnum=%d cnum=%d num=%d wrote=%d\n",timestring(),fnum,cnum,numtowrite,nwritten));
  2179.   
  2180.   return(outsize);
  2181. }
  2182.  
  2183.  
  2184. /****************************************************************************
  2185.   reply to a write and X
  2186. ****************************************************************************/
  2187. int reply_write_and_X(char *inbuf,char *outbuf,int length,int bufsize)
  2188. {
  2189.   int fnum = GETFNUM(inbuf,smb_vwv2);
  2190.   uint32 smb_offs = IVAL(inbuf,smb_vwv3);
  2191.   int smb_dsize = SVAL(inbuf,smb_vwv10);
  2192.   int smb_doff = SVAL(inbuf,smb_vwv11);
  2193.   BOOL write_through = BITSETW(inbuf+smb_vwv7,0);
  2194.   int cnum;
  2195.   int nwritten = -1;
  2196.   char *data;
  2197.  
  2198.   cnum = SVAL(inbuf,smb_tid);
  2199.  
  2200.   CHECK_FNUM(fnum,cnum);
  2201.   CHECK_WRITE(fnum);
  2202.   CHECK_ERROR(fnum);
  2203.  
  2204.   data = smb_base(inbuf) + smb_doff;
  2205.  
  2206.   if (is_locked(fnum,cnum,smb_dsize,smb_offs))
  2207.     return(ERROR(ERRDOS,ERRlock));
  2208.  
  2209.   seek_file(fnum,smb_offs);
  2210.   
  2211.   /* X/Open SMB protocol says that, unlike SMBwrite
  2212.      if the length is zero then NO truncation is
  2213.      done, just a write of zero. To truncate a file,
  2214.      use SMBwrite. */
  2215.   if(smb_dsize == 0)
  2216.     nwritten = 0;
  2217.   else
  2218.     nwritten = write_file(fnum,data,smb_dsize);
  2219.   
  2220.   if(((nwritten == 0) && (smb_dsize != 0))||(nwritten < 0))
  2221.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  2222.  
  2223.   set_message(outbuf,6,0,True);
  2224.   
  2225.   SSVAL(outbuf,smb_vwv2,nwritten);
  2226.   
  2227.   if (nwritten < smb_dsize) {
  2228.     CVAL(outbuf,smb_rcls) = ERRHRD;
  2229.     SSVAL(outbuf,smb_err,ERRdiskfull);      
  2230.   }
  2231.  
  2232.   DEBUG(3,("%s writeX fnum=%d cnum=%d num=%d wrote=%d\n",timestring(),fnum,cnum,smb_dsize,nwritten));
  2233.  
  2234.   chain_fnum = fnum;
  2235.  
  2236.   if (lp_syncalways(SNUM(cnum)) || write_through)
  2237.     sync_file(fnum);
  2238.  
  2239.   return chain_reply(inbuf,outbuf,length,bufsize);
  2240. }
  2241.  
  2242.  
  2243. /****************************************************************************
  2244.   reply to a lseek
  2245. ****************************************************************************/
  2246. int reply_lseek(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  2247. {
  2248.   int cnum,fnum;
  2249.   uint32 startpos;
  2250.   int32 res= -1;
  2251.   int mode,umode;
  2252.   int outsize = 0;
  2253.   
  2254.   cnum = SVAL(inbuf,smb_tid);
  2255.   fnum = GETFNUM(inbuf,smb_vwv0);
  2256.  
  2257.   CHECK_FNUM(fnum,cnum);
  2258.   CHECK_ERROR(fnum);
  2259.  
  2260.   mode = SVAL(inbuf,smb_vwv1) & 3;
  2261.   startpos = IVAL(inbuf,smb_vwv2);
  2262.  
  2263.   switch (mode & 3) 
  2264.     {
  2265.     case 0: umode = SEEK_SET; break;
  2266.     case 1: umode = SEEK_CUR; break;
  2267.     case 2: umode = SEEK_END; break;
  2268.     default:
  2269.       umode = SEEK_SET; break;
  2270.     }
  2271.   
  2272.   res = lseek(Files[fnum].fd_ptr->fd,startpos,umode);
  2273.   Files[fnum].pos = res;
  2274.   
  2275.   outsize = set_message(outbuf,2,0,True);
  2276.   SIVALS(outbuf,smb_vwv0,res);
  2277.   
  2278.   DEBUG(3,("%s lseek fnum=%d cnum=%d ofs=%d mode=%d\n",timestring(),fnum,cnum,startpos,mode));
  2279.   
  2280.   return(outsize);
  2281. }
  2282.  
  2283.  
  2284. /****************************************************************************
  2285.   reply to a flush
  2286. ****************************************************************************/
  2287. int reply_flush(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  2288. {
  2289.   int cnum, fnum;
  2290.   int outsize = set_message(outbuf,0,0,True);
  2291.  
  2292.   cnum = SVAL(inbuf,smb_tid);
  2293.   fnum = GETFNUM(inbuf,smb_vwv0);
  2294.  
  2295.   if (fnum != 0xFFFF) {
  2296.     CHECK_FNUM(fnum,cnum);
  2297.     CHECK_ERROR(fnum);
  2298.   }
  2299.  
  2300.   if (fnum == 0xFFFF)
  2301.     {
  2302.       int i;
  2303.       for (i=0;i<MAX_OPEN_FILES;i++)
  2304.     if (OPEN_FNUM(i))
  2305.       sync_file(i);
  2306.     }
  2307.   else
  2308.     sync_file(fnum);
  2309.  
  2310.   DEBUG(3,("%s flush fnum=%d\n",timestring(),fnum));
  2311.   return(outsize);
  2312. }
  2313.  
  2314.  
  2315. /****************************************************************************
  2316.   reply to a exit
  2317. ****************************************************************************/
  2318. int reply_exit(char *inbuf,char *outbuf, int size, int bufsize)
  2319. {
  2320.   int outsize = set_message(outbuf,0,0,True);
  2321.   DEBUG(3,("%s exit\n",timestring()));
  2322.   
  2323.   return(outsize);
  2324. }
  2325.  
  2326.  
  2327. /****************************************************************************
  2328.   reply to a close
  2329. ****************************************************************************/
  2330. int reply_close(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  2331. {
  2332.   int fnum,cnum;
  2333.   int outsize = 0;
  2334.   time_t mtime;
  2335.   int32 eclass = 0, err = 0;
  2336.  
  2337.   outsize = set_message(outbuf,0,0,True);
  2338.  
  2339.   cnum = SVAL(inbuf,smb_tid);
  2340.  
  2341.   /* If it's an IPC, pass off to the pipe handler. */
  2342.   if (IS_IPC(cnum))
  2343.     return reply_pipe_close(inbuf,outbuf);
  2344.  
  2345.   fnum = GETFNUM(inbuf,smb_vwv0);
  2346.  
  2347.   CHECK_FNUM(fnum,cnum);
  2348.  
  2349.   if(HAS_CACHED_ERROR(fnum)) {
  2350.     eclass = Files[fnum].wbmpx_ptr->wr_errclass;
  2351.     err = Files[fnum].wbmpx_ptr->wr_error;
  2352.   }
  2353.  
  2354.   mtime = make_unix_date3(inbuf+smb_vwv1);
  2355.  
  2356.   /* try and set the date */
  2357.   set_filetime(cnum, Files[fnum].name,mtime);
  2358.  
  2359.   DEBUG(3,("%s close fd=%d fnum=%d cnum=%d (numopen=%d)\n",
  2360.        timestring(),Files[fnum].fd_ptr->fd,fnum,cnum,
  2361.        Connections[cnum].num_files_open));
  2362.   
  2363.   close_file(fnum,True);
  2364.  
  2365.   /* We have a cached error */
  2366.   if(eclass || err)
  2367.     return(ERROR(eclass,err));
  2368.  
  2369.   return(outsize);
  2370. }
  2371.  
  2372.  
  2373. /****************************************************************************
  2374.   reply to a writeclose (Core+ protocol)
  2375. ****************************************************************************/
  2376. int reply_writeclose(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  2377. {
  2378.   int cnum,numtowrite,fnum;
  2379.   int nwritten = -1;
  2380.   int outsize = 0;
  2381.   int startpos;
  2382.   char *data;
  2383.   time_t mtime;
  2384.   
  2385.   cnum = SVAL(inbuf,smb_tid);
  2386.   fnum = GETFNUM(inbuf,smb_vwv0);
  2387.  
  2388.   CHECK_FNUM(fnum,cnum);
  2389.   CHECK_WRITE(fnum);
  2390.   CHECK_ERROR(fnum);
  2391.  
  2392.   numtowrite = SVAL(inbuf,smb_vwv1);
  2393.   startpos = IVAL(inbuf,smb_vwv2);
  2394.   mtime = make_unix_date3(inbuf+smb_vwv4);
  2395.   data = smb_buf(inbuf) + 1;
  2396.   
  2397.   if (is_locked(fnum,cnum,numtowrite,startpos))
  2398.     return(ERROR(ERRDOS,ERRlock));
  2399.       
  2400.   seek_file(fnum,startpos);
  2401.       
  2402.   nwritten = write_file(fnum,data,numtowrite);
  2403.  
  2404.   set_filetime(cnum, Files[fnum].name,mtime);
  2405.   
  2406.   DEBUG(3,("%s writeclose fnum=%d cnum=%d num=%d wrote=%d (numopen=%d)\n",
  2407.        timestring(),fnum,cnum,numtowrite,nwritten,
  2408.        Connections[cnum].num_files_open));
  2409.   
  2410.   close_file(fnum,True);
  2411.  
  2412.   if (nwritten <= 0)
  2413.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  2414.   
  2415.   outsize = set_message(outbuf,1,0,True);
  2416.   
  2417.   SSVAL(outbuf,smb_vwv0,nwritten);
  2418.   return(outsize);
  2419. }
  2420.  
  2421.  
  2422. /****************************************************************************
  2423.   reply to a lock
  2424. ****************************************************************************/
  2425. int reply_lock(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  2426. {
  2427.   int fnum,cnum;
  2428.   int outsize = set_message(outbuf,0,0,True);
  2429.   uint32 count,offset;
  2430.   int eclass;
  2431.   uint32 ecode;
  2432.  
  2433.   cnum = SVAL(inbuf,smb_tid);
  2434.   fnum = GETFNUM(inbuf,smb_vwv0);
  2435.  
  2436.   CHECK_FNUM(fnum,cnum);
  2437.   CHECK_ERROR(fnum);
  2438.  
  2439.   count = IVAL(inbuf,smb_vwv1);
  2440.   offset = IVAL(inbuf,smb_vwv3);
  2441.  
  2442.   DEBUG(3,("%s lock fd=%d fnum=%d cnum=%d ofs=%d cnt=%d\n",timestring(),Files[fnum].fd_ptr->fd,fnum,cnum,offset,count));
  2443.  
  2444.   if(!do_lock( fnum, cnum, count, offset, &eclass, &ecode))
  2445.     return (ERROR(eclass,ecode));
  2446.   
  2447.   return(outsize);
  2448. }
  2449.  
  2450.  
  2451. /****************************************************************************
  2452.   reply to a unlock
  2453. ****************************************************************************/
  2454. int reply_unlock(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  2455. {
  2456.   int fnum,cnum;
  2457.   int outsize = set_message(outbuf,0,0,True);
  2458.   uint32 count,offset;
  2459.   int eclass;
  2460.   uint32 ecode;
  2461.   
  2462.   cnum = SVAL(inbuf,smb_tid);
  2463.   fnum = GETFNUM(inbuf,smb_vwv0);
  2464.  
  2465.   CHECK_FNUM(fnum,cnum);
  2466.   CHECK_ERROR(fnum);
  2467.  
  2468.   count = IVAL(inbuf,smb_vwv1);
  2469.   offset = IVAL(inbuf,smb_vwv3);
  2470.  
  2471.   if(!do_unlock(fnum, cnum, count, offset, &eclass, &ecode))
  2472.     return (ERROR(eclass,ecode));
  2473.  
  2474.   DEBUG(3,("%s unlock fd=%d fnum=%d cnum=%d ofs=%d cnt=%d\n",timestring(),Files[fnum].fd_ptr->fd,fnum,cnum,offset,count));
  2475.   
  2476.   return(outsize);
  2477. }
  2478.  
  2479.  
  2480. /****************************************************************************
  2481.   reply to a tdis
  2482. ****************************************************************************/
  2483. int reply_tdis(char *inbuf,char *outbuf, int size, int bufsize)
  2484. {
  2485.   int cnum;
  2486.   int outsize = set_message(outbuf,0,0,True);
  2487.   uint16 vuid;
  2488.  
  2489.   cnum = SVAL(inbuf,smb_tid);
  2490.   vuid = SVAL(inbuf,smb_uid);
  2491.  
  2492.   if (!OPEN_CNUM(cnum)) {
  2493.     DEBUG(4,("Invalid cnum in tdis (%d)\n",cnum));
  2494.     return(ERROR(ERRSRV,ERRinvnid));
  2495.   }
  2496.  
  2497.   Connections[cnum].used = False;
  2498.  
  2499.   close_cnum(cnum,vuid);
  2500.   
  2501.   DEBUG(3,("%s tdis cnum=%d\n",timestring(),cnum));
  2502.  
  2503.   return outsize;
  2504. }
  2505.  
  2506.  
  2507.  
  2508. /****************************************************************************
  2509.   reply to a echo
  2510. ****************************************************************************/
  2511. int reply_echo(char *inbuf,char *outbuf, int size, int bufsize)
  2512. {
  2513.   int cnum;
  2514.   int smb_reverb = SVAL(inbuf,smb_vwv0);
  2515.   int seq_num;
  2516.   int data_len = smb_buflen(inbuf);
  2517.   int outsize = set_message(outbuf,1,data_len,True);
  2518.  
  2519.   cnum = SVAL(inbuf,smb_tid);
  2520.  
  2521.   /* According to the latest CIFS spec we shouldn't
  2522.      care what the TID is.
  2523.    */
  2524.  
  2525. #if 0
  2526.   if (cnum != 0xFFFF && !OPEN_CNUM(cnum))
  2527.     {
  2528.       DEBUG(4,("Invalid cnum in echo (%d)\n",cnum));
  2529.       return(ERROR(ERRSRV,ERRinvnid));
  2530.     }
  2531. #endif
  2532.  
  2533.   /* copy any incoming data back out */
  2534.   if (data_len > 0)
  2535.     memcpy(smb_buf(outbuf),smb_buf(inbuf),data_len);
  2536.  
  2537.   if (smb_reverb > 100)
  2538.     {
  2539.       DEBUG(0,("large reverb (%d)?? Setting to 100\n",smb_reverb));
  2540.       smb_reverb = 100;
  2541.     }
  2542.  
  2543.   for (seq_num =1 ; seq_num <= smb_reverb ; seq_num++)
  2544.     {
  2545.       SSVAL(outbuf,smb_vwv0,seq_num);
  2546.  
  2547.       smb_setlen(outbuf,outsize - 4);
  2548.  
  2549.       send_smb(Client,outbuf);
  2550.     }
  2551.  
  2552.   DEBUG(3,("%s echo %d times cnum=%d\n",timestring(),smb_reverb,cnum));
  2553.  
  2554.   return -1;
  2555. }
  2556.  
  2557.  
  2558. /****************************************************************************
  2559.   reply to a printopen
  2560. ****************************************************************************/
  2561. int reply_printopen(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  2562. {
  2563.   pstring fname;
  2564.   pstring fname2;
  2565.   int cnum;
  2566.   int fnum = -1;
  2567.   int outsize = 0;
  2568.  
  2569.   *fname = *fname2 = 0;
  2570.  
  2571.   cnum = SVAL(inbuf,smb_tid);
  2572.  
  2573.   if (!CAN_PRINT(cnum))
  2574.     return(ERROR(ERRDOS,ERRnoaccess));
  2575.  
  2576.   {
  2577.     pstring s;
  2578.     char *p;
  2579.     pstrcpy(s,smb_buf(inbuf)+1);
  2580.     p = s;
  2581.     while (*p)
  2582.       {
  2583.     if (!(isalnum(*p) || strchr("._-",*p)))
  2584.       *p = 'X';
  2585.     p++;
  2586.       }
  2587.  
  2588.     if (strlen(s) > 10) s[10] = 0;
  2589.  
  2590.     slprintf(fname,sizeof(fname)-1, "%s.XXXXXX",s);  
  2591.   }
  2592.  
  2593.   fnum = find_free_file();
  2594.   if (fnum < 0)
  2595.     return(ERROR(ERRSRV,ERRnofids));
  2596.  
  2597.   pstrcpy(fname2,(char *)mktemp(fname));
  2598.  
  2599.   if (!check_name(fname2,cnum)) {
  2600.       Files[fnum].reserved = False;
  2601.       return(ERROR(ERRDOS,ERRnoaccess));
  2602.   }
  2603.  
  2604.   /* Open for exclusive use, write only. */
  2605.   open_file_shared(fnum,cnum,fname2,(DENY_ALL<<4)|1, 0x12, unix_mode(cnum,0), 
  2606.                    0, NULL, NULL);
  2607.  
  2608.   if (!Files[fnum].open) {
  2609.       Files[fnum].reserved = False;
  2610.       return(UNIXERROR(ERRDOS,ERRnoaccess));
  2611.   }
  2612.  
  2613.   /* force it to be a print file */
  2614.   Files[fnum].print_file = True;
  2615.   
  2616.   outsize = set_message(outbuf,1,0,True);
  2617.   SSVAL(outbuf,smb_vwv0,fnum);
  2618.   
  2619.   DEBUG(3,("%s openprint %s fd=%d fnum=%d cnum=%d\n",timestring(),fname2,Files[fnum].fd_ptr->fd,fnum,cnum));
  2620.   
  2621.   return(outsize);
  2622. }
  2623.  
  2624.  
  2625. /****************************************************************************
  2626.   reply to a printclose
  2627. ****************************************************************************/
  2628. int reply_printclose(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  2629. {
  2630.   int fnum,cnum;
  2631.   int outsize = set_message(outbuf,0,0,True);
  2632.   
  2633.   cnum = SVAL(inbuf,smb_tid);
  2634.   fnum = GETFNUM(inbuf,smb_vwv0);
  2635.  
  2636.   CHECK_FNUM(fnum,cnum);
  2637.   CHECK_ERROR(fnum);
  2638.  
  2639.   if (!CAN_PRINT(cnum))
  2640.     return(ERROR(ERRDOS,ERRnoaccess));
  2641.   
  2642.   DEBUG(3,("%s printclose fd=%d fnum=%d cnum=%d\n",timestring(),Files[fnum].fd_ptr->fd,fnum,cnum));
  2643.   
  2644.   close_file(fnum,True);
  2645.   
  2646.   return(outsize);
  2647. }
  2648.  
  2649.  
  2650. /****************************************************************************
  2651.   reply to a printqueue
  2652. ****************************************************************************/
  2653. int reply_printqueue(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  2654. {
  2655.   int cnum;
  2656.   int outsize = set_message(outbuf,2,3,True);
  2657.   int max_count = SVAL(inbuf,smb_vwv0);
  2658.   int start_index = SVAL(inbuf,smb_vwv1);
  2659.   uint16 vuid;
  2660.  
  2661.   cnum = SVAL(inbuf,smb_tid);
  2662.   vuid = SVAL(inbuf,smb_uid);
  2663.  
  2664. /* allow checking the queue for anyone */
  2665. #if 0
  2666.   if (!CAN_PRINT(cnum))
  2667.     return(ERROR(ERRDOS,ERRnoaccess));
  2668. #endif
  2669.  
  2670.   SSVAL(outbuf,smb_vwv0,0);
  2671.   SSVAL(outbuf,smb_vwv1,0);
  2672.   CVAL(smb_buf(outbuf),0) = 1;
  2673.   SSVAL(smb_buf(outbuf),1,0);
  2674.   
  2675.   DEBUG(3,("%s printqueue cnum=%d start_index=%d max_count=%d\n",
  2676.     timestring(),cnum,start_index,max_count));
  2677.  
  2678.   if (!OPEN_CNUM(cnum) || !Connections[cnum].printer)
  2679.     {
  2680.       int i;
  2681.       cnum = -1;
  2682.  
  2683.       for (i=0;i<MAX_CONNECTIONS;i++)
  2684.     if (CAN_PRINT(i) && Connections[i].printer)
  2685.       cnum = i;
  2686.  
  2687.       if (cnum == -1)
  2688.     for (i=0;i<MAX_CONNECTIONS;i++)
  2689.       if (OPEN_CNUM(i))
  2690.         cnum = i;
  2691.  
  2692.       if (!OPEN_CNUM(cnum))
  2693.     return(ERROR(ERRSRV,ERRinvnid));
  2694.  
  2695.       DEBUG(5,("connection not open or not a printer, using cnum %d\n",cnum));
  2696.     }
  2697.  
  2698.   if (!become_user(&Connections[cnum], cnum, vuid))
  2699.     return(ERROR(ERRSRV,ERRinvnid));
  2700.  
  2701.   {
  2702.     print_queue_struct *queue = NULL;
  2703.     char *p = smb_buf(outbuf) + 3;
  2704.     int count = get_printqueue(SNUM(cnum),cnum,&queue,NULL);
  2705.     int num_to_get = ABS(max_count);
  2706.     int first = (max_count>0?start_index:start_index+max_count+1);
  2707.     int i;
  2708.  
  2709.     if (first >= count)
  2710.       num_to_get = 0;
  2711.     else
  2712.       num_to_get = MIN(num_to_get,count-first);
  2713.     
  2714.  
  2715.     for (i=first;i<first+num_to_get;i++)
  2716.       {
  2717.     put_dos_date2(p,0,queue[i].time);
  2718.     CVAL(p,4) = (queue[i].status==LPQ_PRINTING?2:3);
  2719.     SSVAL(p,5,printjob_encode(SNUM(cnum), queue[i].job));
  2720.     SIVAL(p,7,queue[i].size);
  2721.     CVAL(p,11) = 0;
  2722.     StrnCpy(p+12,queue[i].user,16);
  2723.     p += 28;
  2724.       }
  2725.  
  2726.     if (count > 0)
  2727.       {
  2728.     outsize = set_message(outbuf,2,28*count+3,False);      
  2729.     SSVAL(outbuf,smb_vwv0,count);
  2730.     SSVAL(outbuf,smb_vwv1,(max_count>0?first+count:first-1));
  2731.     CVAL(smb_buf(outbuf),0) = 1;
  2732.     SSVAL(smb_buf(outbuf),1,28*count);
  2733.       }
  2734.  
  2735.     if (queue) free(queue);
  2736.       
  2737.     DEBUG(3,("%d entries returned in queue\n",count));
  2738.   }
  2739.   
  2740.   return(outsize);
  2741. }
  2742.  
  2743.  
  2744. /****************************************************************************
  2745.   reply to a printwrite
  2746. ****************************************************************************/
  2747. int reply_printwrite(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  2748. {
  2749.   int cnum,numtowrite,fnum;
  2750.   int outsize = set_message(outbuf,0,0,True);
  2751.   char *data;
  2752.   
  2753.   cnum = SVAL(inbuf,smb_tid);
  2754.  
  2755.   if (!CAN_PRINT(cnum))
  2756.     return(ERROR(ERRDOS,ERRnoaccess));
  2757.  
  2758.   fnum = GETFNUM(inbuf,smb_vwv0);
  2759.  
  2760.   CHECK_FNUM(fnum,cnum);
  2761.   CHECK_WRITE(fnum);
  2762.   CHECK_ERROR(fnum);
  2763.  
  2764.   numtowrite = SVAL(smb_buf(inbuf),1);
  2765.   data = smb_buf(inbuf) + 3;
  2766.   
  2767.   if (write_file(fnum,data,numtowrite) != numtowrite)
  2768.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  2769.   
  2770.   DEBUG(3,("%s printwrite fnum=%d cnum=%d num=%d\n",timestring(),fnum,cnum,numtowrite));
  2771.   
  2772.   return(outsize);
  2773. }
  2774.  
  2775.  
  2776. /****************************************************************************
  2777.   reply to a mkdir
  2778. ****************************************************************************/
  2779. int reply_mkdir(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  2780. {
  2781.   pstring directory;
  2782.   int cnum;
  2783.   int outsize,ret= -1;
  2784.   BOOL bad_path = False;
  2785.  
  2786.   pstrcpy(directory,smb_buf(inbuf) + 1);
  2787.   cnum = SVAL(inbuf,smb_tid);
  2788.   unix_convert(directory,cnum,0,&bad_path);
  2789.   
  2790.   if (check_name(directory,cnum))
  2791.     ret = sys_mkdir(directory,unix_mode(cnum,aDIR));
  2792.   
  2793.   if (ret < 0)
  2794.   {
  2795.     if((errno == ENOENT) && bad_path)
  2796.     {
  2797.       unix_ERR_class = ERRDOS;
  2798.       unix_ERR_code = ERRbadpath;
  2799.     }
  2800.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  2801.   }
  2802.  
  2803.   outsize = set_message(outbuf,0,0,True);
  2804.   
  2805.   DEBUG(3,("%s mkdir %s cnum=%d ret=%d\n",timestring(),directory,cnum,ret));
  2806.   
  2807.   return(outsize);
  2808. }
  2809.  
  2810. /****************************************************************************
  2811. Static function used by reply_rmdir to delete an entire directory
  2812. tree recursively.
  2813. ****************************************************************************/
  2814. static BOOL recursive_rmdir(char *directory)
  2815. {
  2816.   char *dname = NULL;
  2817.   BOOL ret = False;
  2818.   void *dirptr = OpenDir(-1, directory, False);
  2819.  
  2820.   if(dirptr == NULL)
  2821.     return True;
  2822.  
  2823.   while((dname = ReadDirName(dirptr)))
  2824.   {
  2825.     pstring fullname;
  2826.     struct stat st;
  2827.  
  2828.     if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0))
  2829.       continue;
  2830.  
  2831.     /* Construct the full name. */
  2832.     if(strlen(directory) + strlen(dname) + 1 >= sizeof(fullname))
  2833.     {
  2834.       errno = ENOMEM;
  2835.       ret = True;
  2836.       break;
  2837.     }
  2838.     pstrcpy(fullname, directory);
  2839.     pstrcat(fullname, "/");
  2840.     pstrcat(fullname, dname);
  2841.  
  2842.     if(sys_lstat(fullname, &st) != 0)
  2843.     {
  2844.       ret = True;
  2845.       break;
  2846.     }
  2847.  
  2848.     if(st.st_mode & S_IFDIR)
  2849.     {
  2850.       if(recursive_rmdir(fullname)!=0)
  2851.       {
  2852.         ret = True;
  2853.         break;
  2854.       }
  2855.       if(sys_rmdir(fullname) != 0)
  2856.       {
  2857.         ret = True;
  2858.         break;
  2859.       }
  2860.     }
  2861.     else if(sys_unlink(fullname) != 0)
  2862.     {
  2863.       ret = True;
  2864.       break;
  2865.     }
  2866.   }
  2867.   CloseDir(dirptr);
  2868.   return ret;
  2869. }
  2870.  
  2871. /****************************************************************************
  2872.   reply to a rmdir
  2873. ****************************************************************************/
  2874. int reply_rmdir(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  2875. {
  2876.   pstring directory;
  2877.   int cnum;
  2878.   int outsize = 0;
  2879.   BOOL ok = False;
  2880.   BOOL bad_path = False;
  2881.  
  2882.   cnum = SVAL(inbuf,smb_tid);
  2883.   pstrcpy(directory,smb_buf(inbuf) + 1);
  2884.   unix_convert(directory,cnum,0,&bad_path);
  2885.   
  2886.   if (check_name(directory,cnum))
  2887.     {
  2888.  
  2889.       dptr_closepath(directory,SVAL(inbuf,smb_pid));
  2890.       ok = (sys_rmdir(directory) == 0);
  2891.       if(!ok && (errno == ENOTEMPTY) && lp_veto_files(SNUM(cnum)))
  2892.         {
  2893.           /* Check to see if the only thing in this directory are
  2894.              vetoed files/directories. If so then delete them and
  2895.              retry. If we fail to delete any of them (and we *don't*
  2896.              do a recursive delete) then fail the rmdir. */
  2897.           BOOL all_veto_files = True;
  2898.           char *dname;
  2899.           void *dirptr = OpenDir(cnum, directory, False);
  2900.  
  2901.           if(dirptr != NULL)
  2902.             {
  2903.               int dirpos = TellDir(dirptr);
  2904.               while ((dname = ReadDirName(dirptr)))
  2905.                 {
  2906.                   if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0))
  2907.                     continue;
  2908.                   if(!IS_VETO_PATH(cnum, dname))
  2909.                     {
  2910.                       all_veto_files = False;
  2911.                       break;
  2912.                     }
  2913.                 }
  2914.               if(all_veto_files)
  2915.                 {
  2916.                   SeekDir(dirptr,dirpos);
  2917.                   while ((dname = ReadDirName(dirptr)))
  2918.                     {
  2919.                       pstring fullname;
  2920.                       struct stat st;
  2921.  
  2922.                       if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0))
  2923.                         continue;
  2924.  
  2925.                       /* Construct the full name. */
  2926.                       if(strlen(directory) + strlen(dname) + 1 >= sizeof(fullname))
  2927.                         {
  2928.                           errno = ENOMEM;
  2929.                           break;
  2930.                         }
  2931.                       pstrcpy(fullname, directory);
  2932.                       pstrcat(fullname, "/");
  2933.                       pstrcat(fullname, dname);
  2934.                       
  2935.                       if(sys_lstat(fullname, &st) != 0)
  2936.                         break;
  2937.                       if(st.st_mode & S_IFDIR)
  2938.                       {
  2939.                         if(lp_recursive_veto_delete(SNUM(cnum)))
  2940.                         {
  2941.                           if(recursive_rmdir(fullname) != 0)
  2942.                             break;
  2943.                         }
  2944.                         if(sys_rmdir(fullname) != 0)
  2945.                           break;
  2946.                       }
  2947.                       else if(sys_unlink(fullname) != 0)
  2948.                         break;
  2949.                     }
  2950.                   CloseDir(dirptr);
  2951.                   /* Retry the rmdir */
  2952.                   ok = (sys_rmdir(directory) == 0);
  2953.                 }
  2954.               else
  2955.                 CloseDir(dirptr);
  2956.             }
  2957.           else
  2958.             errno = ENOTEMPTY;
  2959.          }
  2960.           
  2961.       if (!ok)
  2962.         DEBUG(3,("couldn't remove directory %s : %s\n",
  2963.          directory,strerror(errno)));
  2964.     }
  2965.   
  2966.   if (!ok)
  2967.   {
  2968.     if((errno == ENOENT) && bad_path)
  2969.     {
  2970.       unix_ERR_class = ERRDOS;
  2971.       unix_ERR_code = ERRbadpath;
  2972.     }
  2973.     return(UNIXERROR(ERRDOS,ERRbadpath));
  2974.   }
  2975.  
  2976.   outsize = set_message(outbuf,0,0,True);
  2977.   
  2978.   DEBUG(3,("%s rmdir %s\n",timestring(),directory));
  2979.   
  2980.   return(outsize);
  2981. }
  2982.  
  2983.  
  2984. /*******************************************************************
  2985. resolve wildcards in a filename rename
  2986. ********************************************************************/
  2987. static BOOL resolve_wildcards(char *name1,char *name2)
  2988. {
  2989.   fstring root1,root2;
  2990.   fstring ext1,ext2;
  2991.   char *p,*p2;
  2992.  
  2993.   name1 = strrchr(name1,'/');
  2994.   name2 = strrchr(name2,'/');
  2995.  
  2996.   if (!name1 || !name2) return(False);
  2997.   
  2998.   fstrcpy(root1,name1);
  2999.   fstrcpy(root2,name2);
  3000.   p = strrchr(root1,'.');
  3001.   if (p) {
  3002.     *p = 0;
  3003.     fstrcpy(ext1,p+1);
  3004.   } else {
  3005.     fstrcpy(ext1,"");    
  3006.   }
  3007.   p = strrchr(root2,'.');
  3008.   if (p) {
  3009.     *p = 0;
  3010.     fstrcpy(ext2,p+1);
  3011.   } else {
  3012.     fstrcpy(ext2,"");    
  3013.   }
  3014.  
  3015.   p = root1;
  3016.   p2 = root2;
  3017.   while (*p2) {
  3018.     if (*p2 == '?') {
  3019.       *p2 = *p;
  3020.       p2++;
  3021.     } else {
  3022.       p2++;
  3023.     }
  3024.     if (*p) p++;
  3025.   }
  3026.  
  3027.   p = ext1;
  3028.   p2 = ext2;
  3029.   while (*p2) {
  3030.     if (*p2 == '?') {
  3031.       *p2 = *p;
  3032.       p2++;
  3033.     } else {
  3034.       p2++;
  3035.     }
  3036.     if (*p) p++;
  3037.   }
  3038.  
  3039.   fstrcpy(name2,root2);
  3040.   if (ext2[0]) {
  3041.     fstrcat(name2,".");
  3042.     fstrcat(name2,ext2);
  3043.   }
  3044.  
  3045.   return(True);
  3046. }
  3047.  
  3048. /*******************************************************************
  3049. check if a user is allowed to rename a file
  3050. ********************************************************************/
  3051. static BOOL can_rename(char *fname,int cnum)
  3052. {
  3053.   struct stat sbuf;
  3054.  
  3055.   if (!CAN_WRITE(cnum)) return(False);
  3056.  
  3057.   if (sys_lstat(fname,&sbuf) != 0) return(False);
  3058.   if (!check_file_sharing(cnum,fname,True)) return(False);
  3059.  
  3060.   return(True);
  3061. }
  3062.  
  3063. /****************************************************************************
  3064.   reply to a mv
  3065. ****************************************************************************/
  3066. int reply_mv(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  3067. {
  3068.   int outsize = 0;
  3069.   pstring name;
  3070.   int cnum;
  3071.   pstring directory;
  3072.   pstring mask,newname;
  3073.   pstring newname_last_component;
  3074.   char *p;
  3075.   int count=0;
  3076.   int error = ERRnoaccess;
  3077.   BOOL has_wild;
  3078.   BOOL exists=False;
  3079.   BOOL bad_path1 = False;
  3080.   BOOL bad_path2 = False;
  3081.  
  3082.   *directory = *mask = 0;
  3083.  
  3084.   cnum = SVAL(inbuf,smb_tid);
  3085.   
  3086.   pstrcpy(name,smb_buf(inbuf) + 1);
  3087.   pstrcpy(newname,smb_buf(inbuf) + 3 + strlen(name));
  3088.    
  3089.   DEBUG(3,("reply_mv : %s -> %s\n",name,newname));
  3090.    
  3091.   unix_convert(name,cnum,0,&bad_path1);
  3092.   unix_convert(newname,cnum,newname_last_component,&bad_path2);
  3093.  
  3094.   /*
  3095.    * Split the old name into directory and last component
  3096.    * strings. Note that unix_convert may have stripped off a 
  3097.    * leading ./ from both name and newname if the rename is 
  3098.    * at the root of the share. We need to make sure either both
  3099.    * name and newname contain a / character or neither of them do
  3100.    * as this is checked in resolve_wildcards().
  3101.    */
  3102.  
  3103.   p = strrchr(name,'/');
  3104.   if (!p) {
  3105.     pstrcpy(directory,".");
  3106.     pstrcpy(mask,name);
  3107.   } else {
  3108.     *p = 0;
  3109.     pstrcpy(directory,name);
  3110.     pstrcpy(mask,p+1);
  3111.     *p = '/'; /* Replace needed for exceptional test below. */
  3112.   }
  3113.  
  3114.   if (is_mangled(mask))
  3115.     check_mangled_stack(mask);
  3116.  
  3117.   has_wild = strchr(mask,'*') || strchr(mask,'?');
  3118.  
  3119.   if (!has_wild) {
  3120.     BOOL is_short_name = is_8_3(name, True);
  3121.  
  3122.     /* Add a terminating '/' to the directory name. */
  3123.     pstrcat(directory,"/");
  3124.     pstrcat(directory,mask);
  3125.  
  3126.     /* Ensure newname contains a '/' also */
  3127.     if(strrchr(newname,'/') == 0) {
  3128.       pstring tmpstr;
  3129.  
  3130.       pstrcpy(tmpstr, "./");
  3131.       pstrcat(tmpstr, newname);
  3132.       pstrcpy(newname, tmpstr);
  3133.     }
  3134.   
  3135.     DEBUG(3,("reply_mv : case_sensitive = %d, case_preserve = %d, short case preserve = %d, directory = %s, newname = %s, newname_last_component = %s, is_8_3 = %d\n", 
  3136.             case_sensitive, case_preserve, short_case_preserve, directory, 
  3137.             newname, newname_last_component, is_short_name));
  3138.  
  3139.     /*
  3140.      * Check for special case with case preserving and not
  3141.      * case sensitive, if directory and newname are identical,
  3142.      * and the old last component differs from the original
  3143.      * last component only by case, then we should allow
  3144.      * the rename (user is trying to change the case of the
  3145.      * filename).
  3146.      */
  3147.     if((case_sensitive == False) && ( ((case_preserve == True) && (is_short_name == False)) || 
  3148.             ((short_case_preserve == True) && (is_short_name == True))) &&
  3149.        strcsequal(directory, newname)) {
  3150.       pstring newname_modified_last_component;
  3151.  
  3152.       /*
  3153.        * Get the last component of the modified name.
  3154.        * Note that we guarantee that newname contains a '/'
  3155.        * character above.
  3156.        */
  3157.       p = strrchr(newname,'/');
  3158.       pstrcpy(newname_modified_last_component,p+1);
  3159.  
  3160.       if(strcsequal(newname_modified_last_component, 
  3161.             newname_last_component) == False) {
  3162.     /*
  3163.      * Replace the modified last component with
  3164.      * the original.
  3165.      */
  3166.         pstrcpy(p+1, newname_last_component);
  3167.       }
  3168.     }
  3169.  
  3170.     if (resolve_wildcards(directory,newname) && 
  3171.     can_rename(directory,cnum) && 
  3172.     !file_exist(newname,NULL) &&
  3173.     !sys_rename(directory,newname)) count++;
  3174.  
  3175.     DEBUG(3,("reply_mv : %s doing rename on %s -> %s\n",(count != 0) ? "succeeded" : "failed",
  3176.                          directory,newname));
  3177.  
  3178.     if (!count) exists = file_exist(directory,NULL);
  3179.     if (!count && exists && file_exist(newname,NULL)) {
  3180.       exists = True;
  3181.       error = 183;
  3182.     }
  3183.   } else {
  3184.     void *dirptr = NULL;
  3185.     char *dname;
  3186.     pstring destname;
  3187.  
  3188.     if (check_name(directory,cnum))
  3189.       dirptr = OpenDir(cnum, directory, True);
  3190.  
  3191.     if (dirptr)
  3192.       {
  3193.     error = ERRbadfile;
  3194.  
  3195.     if (strequal(mask,"????????.???"))
  3196.       pstrcpy(mask,"*");
  3197.  
  3198.     while ((dname = ReadDirName(dirptr)))
  3199.       {
  3200.         pstring fname;
  3201.         pstrcpy(fname,dname);
  3202.         
  3203.         if(!mask_match(fname, mask, case_sensitive, False)) continue;
  3204.  
  3205.         error = ERRnoaccess;
  3206.         slprintf(fname,sizeof(fname)-1,"%s/%s",directory,dname);
  3207.         if (!can_rename(fname,cnum)) {
  3208.             DEBUG(6,("rename %s refused\n", fname));
  3209.             continue;
  3210.         }
  3211.         pstrcpy(destname,newname);
  3212.  
  3213.         if (!resolve_wildcards(fname,destname)) {
  3214.             DEBUG(6,("resolve_wildcards %s %s failed\n", 
  3215.                  fname, destname));
  3216.             continue;
  3217.         }
  3218.  
  3219.         if (file_exist(destname,NULL)) {
  3220.             DEBUG(6,("file_exist %s\n", 
  3221.                  destname));
  3222.             error = 183;
  3223.             continue;
  3224.         }
  3225.         if (!sys_rename(fname,destname)) count++;
  3226.         DEBUG(3,("reply_mv : doing rename on %s -> %s\n",fname,destname));
  3227.       }
  3228.     CloseDir(dirptr);
  3229.       }
  3230.   }
  3231.   
  3232.   if (count == 0) {
  3233.     if (exists)
  3234.       return(ERROR(ERRDOS,error));
  3235.     else
  3236.     {
  3237.       if((errno == ENOENT) && (bad_path1 || bad_path2))
  3238.       {
  3239.         unix_ERR_class = ERRDOS;
  3240.         unix_ERR_code = ERRbadpath;
  3241.       }
  3242.       return(UNIXERROR(ERRDOS,error));
  3243.     }
  3244.   }
  3245.   
  3246.   outsize = set_message(outbuf,0,0,True);
  3247.   
  3248.   return(outsize);
  3249. }
  3250.  
  3251. /*******************************************************************
  3252.   copy a file as part of a reply_copy
  3253.   ******************************************************************/
  3254. static BOOL copy_file(char *src,char *dest1,int cnum,int ofun,
  3255.               int count,BOOL target_is_directory)
  3256. {
  3257.   int Access,action;
  3258.   struct stat st;
  3259.   int ret=0;
  3260.   int fnum1,fnum2;
  3261.   pstring dest;
  3262.   
  3263.   pstrcpy(dest,dest1);
  3264.   if (target_is_directory) {
  3265.     char *p = strrchr(src,'/');
  3266.     if (p) 
  3267.       p++;
  3268.     else
  3269.       p = src;
  3270.     pstrcat(dest,"/");
  3271.     pstrcat(dest,p);
  3272.   }
  3273.  
  3274.   if (!file_exist(src,&st)) return(False);
  3275.  
  3276.   fnum1 = find_free_file();
  3277.   if (fnum1<0) return(False);
  3278.   open_file_shared(fnum1,cnum,src,(DENY_NONE<<4),
  3279.            1,0,0,&Access,&action);
  3280.  
  3281.   if (!Files[fnum1].open) {
  3282.       Files[fnum1].reserved = False;
  3283.       return(False);
  3284.   }
  3285.  
  3286.   if (!target_is_directory && count)
  3287.     ofun = 1;
  3288.  
  3289.   fnum2 = find_free_file();
  3290.   if (fnum2<0) {
  3291.     close_file(fnum1,False);
  3292.     return(False);
  3293.   }
  3294.   open_file_shared(fnum2,cnum,dest,(DENY_NONE<<4)|1,
  3295.            ofun,st.st_mode,0,&Access,&action);
  3296.  
  3297.   if (!Files[fnum2].open) {
  3298.     close_file(fnum1,False);
  3299.     Files[fnum2].reserved = False;
  3300.     return(False);
  3301.   }
  3302.  
  3303.   if ((ofun&3) == 1) {
  3304.     lseek(Files[fnum2].fd_ptr->fd,0,SEEK_END);
  3305.   }
  3306.   
  3307.   if (st.st_size)
  3308.     ret = transfer_file(Files[fnum1].fd_ptr->fd,Files[fnum2].fd_ptr->fd,st.st_size,NULL,0,0);
  3309.  
  3310.   close_file(fnum1,False);
  3311.   close_file(fnum2,False);
  3312.  
  3313.   return(ret == st.st_size);
  3314. }
  3315.  
  3316.  
  3317.  
  3318. /****************************************************************************
  3319.   reply to a file copy.
  3320.   ****************************************************************************/
  3321. int reply_copy(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  3322. {
  3323.   int outsize = 0;
  3324.   pstring name;
  3325.   int cnum;
  3326.   pstring directory;
  3327.   pstring mask,newname;
  3328.   char *p;
  3329.   int count=0;
  3330.   int error = ERRnoaccess;
  3331.   BOOL has_wild;
  3332.   BOOL exists=False;
  3333.   int tid2 = SVAL(inbuf,smb_vwv0);
  3334.   int ofun = SVAL(inbuf,smb_vwv1);
  3335.   int flags = SVAL(inbuf,smb_vwv2);
  3336.   BOOL target_is_directory=False;
  3337.   BOOL bad_path1 = False;
  3338.   BOOL bad_path2 = False;
  3339.  
  3340.   *directory = *mask = 0;
  3341.  
  3342.   cnum = SVAL(inbuf,smb_tid);
  3343.   
  3344.   pstrcpy(name,smb_buf(inbuf));
  3345.   pstrcpy(newname,smb_buf(inbuf) + 1 + strlen(name));
  3346.    
  3347.   DEBUG(3,("reply_copy : %s -> %s\n",name,newname));
  3348.    
  3349.   if (tid2 != cnum) {
  3350.     /* can't currently handle inter share copies XXXX */
  3351.     DEBUG(3,("Rejecting inter-share copy\n"));
  3352.     return(ERROR(ERRSRV,ERRinvdevice));
  3353.   }
  3354.  
  3355.   unix_convert(name,cnum,0,&bad_path1);
  3356.   unix_convert(newname,cnum,0,&bad_path2);
  3357.  
  3358.   target_is_directory = directory_exist(newname,NULL);
  3359.  
  3360.   if ((flags&1) && target_is_directory) {
  3361.     return(ERROR(ERRDOS,ERRbadfile));
  3362.   }
  3363.  
  3364.   if ((flags&2) && !target_is_directory) {
  3365.     return(ERROR(ERRDOS,ERRbadpath));
  3366.   }
  3367.  
  3368.   if ((flags&(1<<5)) && directory_exist(name,NULL)) {
  3369.     /* wants a tree copy! XXXX */
  3370.     DEBUG(3,("Rejecting tree copy\n"));
  3371.     return(ERROR(ERRSRV,ERRerror));    
  3372.   }
  3373.  
  3374.   p = strrchr(name,'/');
  3375.   if (!p) {
  3376.     pstrcpy(directory,"./");
  3377.     pstrcpy(mask,name);
  3378.   } else {
  3379.     *p = 0;
  3380.     pstrcpy(directory,name);
  3381.     pstrcpy(mask,p+1);
  3382.   }
  3383.  
  3384.   if (is_mangled(mask))
  3385.     check_mangled_stack(mask);
  3386.  
  3387.   has_wild = strchr(mask,'*') || strchr(mask,'?');
  3388.  
  3389.   if (!has_wild) {
  3390.     pstrcat(directory,"/");
  3391.     pstrcat(directory,mask);
  3392.     if (resolve_wildcards(directory,newname) && 
  3393.     copy_file(directory,newname,cnum,ofun,
  3394.           count,target_is_directory)) count++;
  3395.     if (!count) exists = file_exist(directory,NULL);
  3396.   } else {
  3397.     void *dirptr = NULL;
  3398.     char *dname;
  3399.     pstring destname;
  3400.  
  3401.     if (check_name(directory,cnum))
  3402.       dirptr = OpenDir(cnum, directory, True);
  3403.  
  3404.     if (dirptr)
  3405.       {
  3406.     error = ERRbadfile;
  3407.  
  3408.     if (strequal(mask,"????????.???"))
  3409.       pstrcpy(mask,"*");
  3410.  
  3411.     while ((dname = ReadDirName(dirptr)))
  3412.       {
  3413.         pstring fname;
  3414.         pstrcpy(fname,dname);
  3415.         
  3416.         if(!mask_match(fname, mask, case_sensitive, False)) continue;
  3417.  
  3418.         error = ERRnoaccess;
  3419.         slprintf(fname,sizeof(fname)-1, "%s/%s",directory,dname);
  3420.         pstrcpy(destname,newname);
  3421.         if (resolve_wildcards(fname,destname) && 
  3422.         copy_file(directory,newname,cnum,ofun,
  3423.               count,target_is_directory)) count++;
  3424.         DEBUG(3,("reply_copy : doing copy on %s -> %s\n",fname,destname));
  3425.       }
  3426.     CloseDir(dirptr);
  3427.       }
  3428.   }
  3429.   
  3430.   if (count == 0) {
  3431.     if (exists)
  3432.       return(ERROR(ERRDOS,error));
  3433.     else
  3434.     {
  3435.       if((errno == ENOENT) && (bad_path1 || bad_path2))
  3436.       {
  3437.         unix_ERR_class = ERRDOS;
  3438.         unix_ERR_code = ERRbadpath;
  3439.       }
  3440.       return(UNIXERROR(ERRDOS,error));
  3441.     }
  3442.   }
  3443.   
  3444.   outsize = set_message(outbuf,1,0,True);
  3445.   SSVAL(outbuf,smb_vwv0,count);
  3446.  
  3447.   return(outsize);
  3448. }
  3449.  
  3450.  
  3451.  
  3452. /****************************************************************************
  3453.   reply to a setdir
  3454. ****************************************************************************/
  3455. int reply_setdir(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  3456. {
  3457.   int cnum,snum;
  3458.   int outsize = 0;
  3459.   BOOL ok = False;
  3460.   pstring newdir;
  3461.   
  3462.   cnum = SVAL(inbuf,smb_tid);
  3463.   
  3464.   snum = Connections[cnum].service;
  3465.   if (!CAN_SETDIR(snum))
  3466.     return(ERROR(ERRDOS,ERRnoaccess));
  3467.   
  3468.   pstrcpy(newdir,smb_buf(inbuf) + 1);
  3469.   strlower(newdir);
  3470.   
  3471.   if (strlen(newdir) == 0)
  3472.     ok = True;
  3473.   else
  3474.     {
  3475.       ok = directory_exist(newdir,NULL);
  3476.       if (ok)
  3477.     string_set(&Connections[cnum].connectpath,newdir);
  3478.     }
  3479.   
  3480.   if (!ok)
  3481.     return(ERROR(ERRDOS,ERRbadpath));
  3482.   
  3483.   outsize = set_message(outbuf,0,0,True);
  3484.   CVAL(outbuf,smb_reh) = CVAL(inbuf,smb_reh);
  3485.   
  3486.   DEBUG(3,("%s setdir %s cnum=%d\n",timestring(),newdir,cnum));
  3487.   
  3488.   return(outsize);
  3489. }
  3490.  
  3491.  
  3492. /****************************************************************************
  3493.   reply to a lockingX request
  3494. ****************************************************************************/
  3495. int reply_lockingX(char *inbuf,char *outbuf,int length,int bufsize)
  3496. {
  3497.   int fnum = GETFNUM(inbuf,smb_vwv2);
  3498.   unsigned char locktype = CVAL(inbuf,smb_vwv3);
  3499. #if 0
  3500.   unsigned char oplocklevel = CVAL(inbuf,smb_vwv3+1);
  3501. #endif
  3502.   uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
  3503.   uint16 num_locks = SVAL(inbuf,smb_vwv7);
  3504.   uint32 count, offset;
  3505.  
  3506.   int cnum;
  3507.   int i;
  3508.   char *data;
  3509.   uint32 ecode=0, dummy2;
  3510.   int eclass=0, dummy1;
  3511.  
  3512.   cnum = SVAL(inbuf,smb_tid);
  3513.  
  3514.   CHECK_FNUM(fnum,cnum);
  3515.   CHECK_ERROR(fnum);
  3516.  
  3517.   data = smb_buf(inbuf);
  3518.  
  3519.   /* Check if this is an oplock break on a file
  3520.      we have granted an oplock on.
  3521.    */
  3522.   if ((locktype & LOCKING_ANDX_OPLOCK_RELEASE))
  3523.   {
  3524.     int token;
  3525.     files_struct *fsp = &Files[fnum];
  3526.     uint32 dev = fsp->fd_ptr->dev;
  3527.     uint32 inode = fsp->fd_ptr->inode;
  3528.  
  3529.     DEBUG(5,("reply_lockingX: oplock break reply from client for fnum = %d\n",
  3530.               fnum));
  3531.     /*
  3532.      * Make sure we have granted an oplock on this file.
  3533.      */
  3534.     if(!fsp->granted_oplock)
  3535.     {
  3536.       DEBUG(0,("reply_lockingX: Error : oplock break from client for fnum = %d and \
  3537. no oplock granted on this file.\n", fnum));
  3538.       return ERROR(ERRDOS,ERRlock);
  3539.     }
  3540.  
  3541.     /* Remove the oplock flag from the sharemode. */
  3542.     lock_share_entry(fsp->cnum, dev, inode, &token);
  3543.     if(remove_share_oplock( fnum, token)==False) {
  3544.         DEBUG(0,("reply_lockingX: failed to remove share oplock for fnum %d, \
  3545. dev = %x, inode = %x\n", 
  3546.              fnum, dev, inode));
  3547.         unlock_share_entry(fsp->cnum, dev, inode, token);
  3548.     } else {
  3549.         unlock_share_entry(fsp->cnum, dev, inode, token);
  3550.  
  3551.         /* Clear the granted flag and return. */
  3552.         fsp->granted_oplock = False;
  3553.     }
  3554.  
  3555.     /* if this is a pure oplock break request then don't send a reply */
  3556.     if (num_locks == 0 && num_ulocks == 0)
  3557.     {
  3558.       /* Sanity check - ensure a pure oplock break is not a
  3559.          chained request. */
  3560.       if(CVAL(inbuf,smb_vwv0) != 0xff)
  3561.         DEBUG(0,("reply_lockingX: Error : pure oplock break is a chained %d request !\n",
  3562.                  (unsigned int)CVAL(inbuf,smb_vwv0) ));
  3563.       return -1;
  3564.     }
  3565.   }
  3566.  
  3567.   /* Data now points at the beginning of the list
  3568.      of smb_unlkrng structs */
  3569.   for(i = 0; i < (int)num_ulocks; i++) {
  3570.     count = IVAL(data,SMB_LKLEN_OFFSET(i));
  3571.     offset = IVAL(data,SMB_LKOFF_OFFSET(i));
  3572.     if(!do_unlock(fnum,cnum,count,offset,&eclass, &ecode))
  3573.       return ERROR(eclass,ecode);
  3574.   }
  3575.  
  3576.   /* Now do any requested locks */
  3577.   data += 10*num_ulocks;
  3578.   /* Data now points at the beginning of the list
  3579.      of smb_lkrng structs */
  3580.   for(i = 0; i < (int)num_locks; i++) {
  3581.     count = IVAL(data,SMB_LKLEN_OFFSET(i)); 
  3582.     offset = IVAL(data,SMB_LKOFF_OFFSET(i)); 
  3583.     if(!do_lock(fnum,cnum,count,offset, &eclass, &ecode))
  3584.       break;
  3585.   }
  3586.  
  3587.   /* If any of the above locks failed, then we must unlock
  3588.      all of the previous locks (X/Open spec). */
  3589.   if(i != num_locks && num_locks != 0) {
  3590.     for(; i >= 0; i--) {
  3591.       count = IVAL(data,SMB_LKLEN_OFFSET(i));  
  3592.       offset = IVAL(data,SMB_LKOFF_OFFSET(i)); 
  3593.       do_unlock(fnum,cnum,count,offset,&dummy1,&dummy2);
  3594.     }
  3595.     return ERROR(eclass,ecode);
  3596.   }
  3597.  
  3598.   set_message(outbuf,2,0,True);
  3599.   
  3600.   DEBUG(3,("%s lockingX fnum=%d cnum=%d type=%d num_locks=%d num_ulocks=%d\n",
  3601.     timestring(),fnum,cnum,(unsigned int)locktype,num_locks,num_ulocks));
  3602.  
  3603.   chain_fnum = fnum;
  3604.  
  3605.   return chain_reply(inbuf,outbuf,length,bufsize);
  3606. }
  3607.  
  3608.  
  3609. /****************************************************************************
  3610.   reply to a SMBreadbmpx (read block multiplex) request
  3611. ****************************************************************************/
  3612. int reply_readbmpx(char *inbuf,char *outbuf,int length,int bufsize)
  3613. {
  3614.   int cnum,fnum;
  3615.   int nread = -1;
  3616.   int total_read;
  3617.   char *data;
  3618.   uint32 startpos;
  3619.   int outsize, mincount, maxcount;
  3620.   int max_per_packet;
  3621.   int tcount;
  3622.   int pad;
  3623.  
  3624.   /* this function doesn't seem to work - disable by default */
  3625.   if (!lp_readbmpx())
  3626.     return(ERROR(ERRSRV,ERRuseSTD));
  3627.  
  3628.   outsize = set_message(outbuf,8,0,True);
  3629.  
  3630.   cnum = SVAL(inbuf,smb_tid);
  3631.   fnum = GETFNUM(inbuf,smb_vwv0);
  3632.  
  3633.   CHECK_FNUM(fnum,cnum);
  3634.   CHECK_READ(fnum);
  3635.   CHECK_ERROR(fnum);
  3636.  
  3637.   startpos = IVAL(inbuf,smb_vwv1);
  3638.   maxcount = SVAL(inbuf,smb_vwv3);
  3639.   mincount = SVAL(inbuf,smb_vwv4);
  3640.  
  3641.   data = smb_buf(outbuf);
  3642.   pad = ((long)data)%4;
  3643.   if (pad) pad = 4 - pad;
  3644.   data += pad;
  3645.  
  3646.   max_per_packet = bufsize-(outsize+pad);
  3647.   tcount = maxcount;
  3648.   total_read = 0;
  3649.  
  3650.   if (is_locked(fnum,cnum,maxcount,startpos))
  3651.     return(ERROR(ERRDOS,ERRlock));
  3652.     
  3653.   do
  3654.     {
  3655.       int N = MIN(max_per_packet,tcount-total_read);
  3656.   
  3657.       nread = read_file(fnum,data,startpos,N);
  3658.  
  3659.       if (nread <= 0) nread = 0;
  3660.  
  3661.       if (nread < N)
  3662.     tcount = total_read + nread;
  3663.  
  3664.       set_message(outbuf,8,nread,False);
  3665.       SIVAL(outbuf,smb_vwv0,startpos);
  3666.       SSVAL(outbuf,smb_vwv2,tcount);
  3667.       SSVAL(outbuf,smb_vwv6,nread);
  3668.       SSVAL(outbuf,smb_vwv7,smb_offset(data,outbuf));
  3669.  
  3670.       send_smb(Client,outbuf);
  3671.  
  3672.       total_read += nread;
  3673.       startpos += nread;
  3674.     }
  3675.   while (total_read < tcount);
  3676.  
  3677.   return(-1);
  3678. }
  3679.  
  3680.  
  3681. /****************************************************************************
  3682.   reply to a SMBwritebmpx (write block multiplex primary) request
  3683. ****************************************************************************/
  3684. int reply_writebmpx(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  3685. {
  3686.   int cnum,numtowrite,fnum;
  3687.   int nwritten = -1;
  3688.   int outsize = 0;
  3689.   uint32 startpos;
  3690.   int tcount, write_through, smb_doff;
  3691.   char *data;
  3692.   
  3693.   cnum = SVAL(inbuf,smb_tid);
  3694.   fnum = GETFNUM(inbuf,smb_vwv0);
  3695.  
  3696.   CHECK_FNUM(fnum,cnum);
  3697.   CHECK_WRITE(fnum);
  3698.   CHECK_ERROR(fnum);
  3699.  
  3700.   tcount = SVAL(inbuf,smb_vwv1);
  3701.   startpos = IVAL(inbuf,smb_vwv3);
  3702.   write_through = BITSETW(inbuf+smb_vwv7,0);
  3703.   numtowrite = SVAL(inbuf,smb_vwv10);
  3704.   smb_doff = SVAL(inbuf,smb_vwv11);
  3705.  
  3706.   data = smb_base(inbuf) + smb_doff;
  3707.  
  3708.   /* If this fails we need to send an SMBwriteC response,
  3709.      not an SMBwritebmpx - set this up now so we don't forget */
  3710.   CVAL(outbuf,smb_com) = SMBwritec;
  3711.  
  3712.   if (is_locked(fnum,cnum,tcount,startpos))
  3713.     return(ERROR(ERRDOS,ERRlock));
  3714.  
  3715.   seek_file(fnum,startpos);
  3716.   nwritten = write_file(fnum,data,numtowrite);
  3717.  
  3718.   if(lp_syncalways(SNUM(cnum)) || write_through)
  3719.     sync_file(fnum);
  3720.   
  3721.   if(nwritten < numtowrite)
  3722.     return(UNIXERROR(ERRHRD,ERRdiskfull));
  3723.  
  3724.   /* If the maximum to be written to this file
  3725.      is greater than what we just wrote then set
  3726.      up a secondary struct to be attached to this
  3727.      fd, we will use this to cache error messages etc. */
  3728.   if(tcount > nwritten) 
  3729.     {
  3730.       write_bmpx_struct *wbms;
  3731.       if(Files[fnum].wbmpx_ptr != NULL)
  3732.     wbms = Files[fnum].wbmpx_ptr; /* Use an existing struct */
  3733.       else
  3734.     wbms = (write_bmpx_struct *)malloc(sizeof(write_bmpx_struct));
  3735.       if(!wbms)
  3736.     {
  3737.       DEBUG(0,("Out of memory in reply_readmpx\n"));
  3738.       return(ERROR(ERRSRV,ERRnoresource));
  3739.     }
  3740.       wbms->wr_mode = write_through;
  3741.       wbms->wr_discard = False; /* No errors yet */
  3742.       wbms->wr_total_written = nwritten;
  3743.       wbms->wr_errclass = 0;
  3744.       wbms->wr_error = 0;
  3745.       Files[fnum].wbmpx_ptr = wbms;
  3746.     }
  3747.  
  3748.   /* We are returning successfully, set the message type back to
  3749.      SMBwritebmpx */
  3750.   CVAL(outbuf,smb_com) = SMBwriteBmpx;
  3751.   
  3752.   outsize = set_message(outbuf,1,0,True);
  3753.   
  3754.   SSVALS(outbuf,smb_vwv0,-1); /* We don't support smb_remaining */
  3755.   
  3756.   DEBUG(3,("%s writebmpx fnum=%d cnum=%d num=%d wrote=%d\n",
  3757.     timestring(),fnum,cnum,numtowrite,nwritten));
  3758.   
  3759.   if (write_through && tcount==nwritten) {
  3760.     /* we need to send both a primary and a secondary response */
  3761.     smb_setlen(outbuf,outsize - 4);
  3762.     send_smb(Client,outbuf);
  3763.  
  3764.     /* now the secondary */
  3765.     outsize = set_message(outbuf,1,0,True);
  3766.     CVAL(outbuf,smb_com) = SMBwritec;
  3767.     SSVAL(outbuf,smb_vwv0,nwritten);
  3768.   }
  3769.  
  3770.   return(outsize);
  3771. }
  3772.  
  3773.  
  3774. /****************************************************************************
  3775.   reply to a SMBwritebs (write block multiplex secondary) request
  3776. ****************************************************************************/
  3777. int reply_writebs(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  3778. {
  3779.   int cnum,numtowrite,fnum;
  3780.   int nwritten = -1;
  3781.   int outsize = 0;
  3782.   int32 startpos;
  3783.   int tcount, write_through, smb_doff;
  3784.   char *data;
  3785.   write_bmpx_struct *wbms;
  3786.   BOOL send_response = False;
  3787.   
  3788.   cnum = SVAL(inbuf,smb_tid);
  3789.   fnum = GETFNUM(inbuf,smb_vwv0);
  3790.   CHECK_FNUM(fnum,cnum);
  3791.   CHECK_WRITE(fnum);
  3792.  
  3793.   tcount = SVAL(inbuf,smb_vwv1);
  3794.   startpos = IVAL(inbuf,smb_vwv2);
  3795.   numtowrite = SVAL(inbuf,smb_vwv6);
  3796.   smb_doff = SVAL(inbuf,smb_vwv7);
  3797.  
  3798.   data = smb_base(inbuf) + smb_doff;
  3799.  
  3800.   /* We need to send an SMBwriteC response, not an SMBwritebs */
  3801.   CVAL(outbuf,smb_com) = SMBwritec;
  3802.  
  3803.   /* This fd should have an auxiliary struct attached,
  3804.      check that it does */
  3805.   wbms = Files[fnum].wbmpx_ptr;
  3806.   if(!wbms) return(-1);
  3807.  
  3808.   /* If write through is set we can return errors, else we must
  3809.      cache them */
  3810.   write_through = wbms->wr_mode;
  3811.  
  3812.   /* Check for an earlier error */
  3813.   if(wbms->wr_discard)
  3814.     return -1; /* Just discard the packet */
  3815.  
  3816.   seek_file(fnum,startpos);
  3817.   nwritten = write_file(fnum,data,numtowrite);
  3818.  
  3819.   if(lp_syncalways(SNUM(cnum)) || write_through)
  3820.     sync_file(fnum);
  3821.   
  3822.   if (nwritten < numtowrite)
  3823.     {
  3824.       if(write_through)    {
  3825.     /* We are returning an error - we can delete the aux struct */
  3826.     if (wbms) free((char *)wbms);
  3827.     Files[fnum].wbmpx_ptr = NULL;
  3828.     return(ERROR(ERRHRD,ERRdiskfull));
  3829.       }
  3830.       return(CACHE_ERROR(wbms,ERRHRD,ERRdiskfull));
  3831.     }
  3832.  
  3833.   /* Increment the total written, if this matches tcount
  3834.      we can discard the auxiliary struct (hurrah !) and return a writeC */
  3835.   wbms->wr_total_written += nwritten;
  3836.   if(wbms->wr_total_written >= tcount)
  3837.     {
  3838.       if (write_through) {
  3839.     outsize = set_message(outbuf,1,0,True);
  3840.     SSVAL(outbuf,smb_vwv0,wbms->wr_total_written);    
  3841.     send_response = True;
  3842.       }
  3843.  
  3844.       free((char *)wbms);
  3845.       Files[fnum].wbmpx_ptr = NULL;
  3846.     }
  3847.  
  3848.   if(send_response)
  3849.     return(outsize);
  3850.  
  3851.   return(-1);
  3852. }
  3853.  
  3854.  
  3855. /****************************************************************************
  3856.   reply to a SMBsetattrE
  3857. ****************************************************************************/
  3858. int reply_setattrE(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  3859. {
  3860.   int cnum,fnum;
  3861.   struct utimbuf unix_times;
  3862.   int outsize = 0;
  3863.  
  3864.   outsize = set_message(outbuf,0,0,True);
  3865.  
  3866.   cnum = SVAL(inbuf,smb_tid);
  3867.   fnum = GETFNUM(inbuf,smb_vwv0);
  3868.  
  3869.   CHECK_FNUM(fnum,cnum);
  3870.   CHECK_ERROR(fnum);
  3871.  
  3872.   /* Convert the DOS times into unix times. Ignore create
  3873.      time as UNIX can't set this.
  3874.      */
  3875.   unix_times.actime = make_unix_date2(inbuf+smb_vwv3);
  3876.   unix_times.modtime = make_unix_date2(inbuf+smb_vwv5);
  3877.   
  3878.   /* 
  3879.    * Patch from Ray Frush <frush@engr.colostate.edu>
  3880.    * Sometimes times are sent as zero - ignore them.
  3881.    */
  3882.  
  3883.   if ((unix_times.actime == 0) && (unix_times.modtime == 0)) 
  3884.   {
  3885.     /* Ignore request */
  3886.     DEBUG(3,("%s reply_setattrE fnum=%d cnum=%d ignoring zero request - \
  3887. not setting timestamps of 0\n",
  3888.           timestring(), fnum,cnum,unix_times.actime,unix_times.modtime));
  3889.     return(outsize);
  3890.   }
  3891.   else if ((unix_times.actime != 0) && (unix_times.modtime == 0)) 
  3892.   {
  3893.     /* set modify time = to access time if modify time was 0 */
  3894.     unix_times.modtime = unix_times.actime;
  3895.   }
  3896.  
  3897.   /* Set the date on this file */
  3898.   if(file_utime(cnum, Files[fnum].name, &unix_times))
  3899.     return(ERROR(ERRDOS,ERRnoaccess));
  3900.   
  3901.   DEBUG(3,("%s reply_setattrE fnum=%d cnum=%d actime=%d modtime=%d\n",
  3902.     timestring(), fnum,cnum,unix_times.actime,unix_times.modtime));
  3903.  
  3904.   return(outsize);
  3905. }
  3906.  
  3907.  
  3908. /****************************************************************************
  3909.   reply to a SMBgetattrE
  3910. ****************************************************************************/
  3911. int reply_getattrE(char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
  3912. {
  3913.   int cnum,fnum;
  3914.   struct stat sbuf;
  3915.   int outsize = 0;
  3916.   int mode;
  3917.  
  3918.   outsize = set_message(outbuf,11,0,True);
  3919.  
  3920.   cnum = SVAL(inbuf,smb_tid);
  3921.   fnum = GETFNUM(inbuf,smb_vwv0);
  3922.  
  3923.   CHECK_FNUM(fnum,cnum);
  3924.   CHECK_ERROR(fnum);
  3925.  
  3926.   /* Do an fstat on this file */
  3927.   if(fstat(Files[fnum].fd_ptr->fd, &sbuf))
  3928.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  3929.   
  3930.   mode = dos_mode(cnum,Files[fnum].name,&sbuf);
  3931.   
  3932.   /* Convert the times into dos times. Set create
  3933.      date to be last modify date as UNIX doesn't save
  3934.      this */
  3935.   put_dos_date2(outbuf,smb_vwv0,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(cnum))));
  3936.   put_dos_date2(outbuf,smb_vwv2,sbuf.st_atime);
  3937.   put_dos_date2(outbuf,smb_vwv4,sbuf.st_mtime);
  3938.   if (mode & aDIR)
  3939.     {
  3940.       SIVAL(outbuf,smb_vwv6,0);
  3941.       SIVAL(outbuf,smb_vwv8,0);
  3942.     }
  3943.   else
  3944.     {
  3945.       SIVAL(outbuf,smb_vwv6,sbuf.st_size);
  3946.       SIVAL(outbuf,smb_vwv8,ROUNDUP(sbuf.st_size,1024));
  3947.     }
  3948.   SSVAL(outbuf,smb_vwv10, mode);
  3949.   
  3950.   DEBUG(3,("%s reply_getattrE fnum=%d cnum=%d\n",timestring(),fnum,cnum));
  3951.   
  3952.   return(outsize);
  3953. }
  3954.