prs_give_memory in wrong place, also poss. was losing mem.
[samba.git] / source3 / smbd / process.c
1 #define OLD_NTDOMAIN 1
2 /* 
3    Unix SMB/Netbios implementation.
4    Version 1.9.
5    process incoming packets - main loop
6    Copyright (C) Andrew Tridgell 1992-1998
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 extern int DEBUGLEVEL;
26
27 struct timeval smb_last_time;
28
29 static char *InBuffer = NULL;
30 char *OutBuffer = NULL;
31 char *last_inbuf = NULL;
32
33 /* 
34  * Size of data we can send to client. Set
35  *  by the client for all protocols above CORE.
36  *  Set by us for CORE protocol.
37  */
38 int max_send = BUFFER_SIZE;
39 /*
40  * Size of the data we can receive. Set by us.
41  * Can be modified by the max xmit parameter.
42  */
43 int max_recv = BUFFER_SIZE;
44
45 extern int last_message;
46 extern int global_oplock_break;
47 extern pstring sesssetup_user;
48 extern char *last_inbuf;
49 extern char *InBuffer;
50 extern char *OutBuffer;
51 extern int smb_read_error;
52 extern VOLATILE SIG_ATOMIC_T reload_after_sighup;
53 extern BOOL global_machine_password_needs_changing;
54 extern fstring global_myworkgroup;
55 extern pstring global_myname;
56 extern int max_send;
57
58 /****************************************************************************
59  structure to hold a linked list of queued messages.
60  for processing.
61 ****************************************************************************/
62
63 typedef struct {
64    ubi_slNode msg_next;
65    char *msg_buf;
66    int msg_len;
67 } pending_message_list;
68
69 static ubi_slList smb_oplock_queue = { NULL, (ubi_slNodePtr)&smb_oplock_queue, 0};
70
71 /****************************************************************************
72  Function to push a message onto the tail of a linked list of smb messages ready
73  for processing.
74 ****************************************************************************/
75
76 static BOOL push_message(ubi_slList *list_head, char *buf, int msg_len)
77 {
78   pending_message_list *msg = (pending_message_list *)
79                                malloc(sizeof(pending_message_list));
80
81   if(msg == NULL)
82   {
83     DEBUG(0,("push_message: malloc fail (1)\n"));
84     return False;
85   }
86
87   msg->msg_buf = (char *)malloc(msg_len);
88   if(msg->msg_buf == NULL)
89   {
90     DEBUG(0,("push_message: malloc fail (2)\n"));
91     free((char *)msg);
92     return False;
93   }
94
95   memcpy(msg->msg_buf, buf, msg_len);
96   msg->msg_len = msg_len;
97
98   ubi_slAddTail( list_head, msg);
99
100   return True;
101 }
102
103 /****************************************************************************
104  Function to push a smb message onto a linked list of local smb messages ready
105  for processing.
106 ****************************************************************************/
107
108 BOOL push_oplock_pending_smb_message(char *buf, int msg_len)
109 {
110   return push_message(&smb_oplock_queue, buf, msg_len);
111 }
112
113 /****************************************************************************
114   Do a select on an two fd's - with timeout. 
115
116   If a local udp message has been pushed onto the
117   queue (this can only happen during oplock break
118   processing) return this first.
119
120   If a pending smb message has been pushed onto the
121   queue (this can only happen during oplock break
122   processing) return this next.
123
124   If the first smbfd is ready then read an smb from it.
125   if the second (loopback UDP) fd is ready then read a message
126   from it and setup the buffer header to identify the length
127   and from address.
128   Returns False on timeout or error.
129   Else returns True.
130
131 The timeout is in milli seconds
132 ****************************************************************************/
133
134 static BOOL receive_message_or_smb(char *buffer, int buffer_len, 
135                                    int timeout, BOOL *got_smb)
136 {
137   fd_set fds;
138   int selrtn;
139   struct timeval to;
140   int maxfd;
141
142   smb_read_error = 0;
143
144   *got_smb = False;
145
146   /*
147    * Check to see if we already have a message on the smb queue.
148    * If so - copy and return it.
149    */
150   
151   if(ubi_slCount(&smb_oplock_queue) != 0)
152   {
153     pending_message_list *msg = (pending_message_list *)ubi_slRemHead(&smb_oplock_queue);
154     memcpy(buffer, msg->msg_buf, MIN(buffer_len, msg->msg_len));
155   
156     /* Free the message we just copied. */
157     free((char *)msg->msg_buf);
158     free((char *)msg);
159     *got_smb = True;
160
161     DEBUG(5,("receive_message_or_smb: returning queued smb message.\n"));
162     return True;
163   }
164
165   /*
166    * Setup the select read fd set.
167    */
168
169   FD_ZERO(&fds);
170   FD_SET(smbd_server_fd(),&fds);
171   maxfd = setup_oplock_select_set(&fds);
172
173   to.tv_sec = timeout / 1000;
174   to.tv_usec = (timeout % 1000) * 1000;
175
176   selrtn = sys_select(MAX(maxfd,smbd_server_fd())+1,&fds,timeout>0?&to:NULL);
177
178   /* Check if error */
179   if(selrtn == -1) {
180     /* something is wrong. Maybe the socket is dead? */
181     smb_read_error = READ_ERROR;
182     return False;
183   } 
184     
185   /* Did we timeout ? */
186   if (selrtn == 0) {
187     smb_read_error = READ_TIMEOUT;
188     return False;
189   }
190
191   if (FD_ISSET(smbd_server_fd(),&fds))
192   {
193     *got_smb = True;
194     return receive_smb(smbd_server_fd(), buffer, 0);
195   }
196   else
197   {
198     return receive_local_message(&fds, buffer, buffer_len, 0);
199   }
200 }
201
202 /****************************************************************************
203 Get the next SMB packet, doing the local message processing automatically.
204 ****************************************************************************/
205
206 BOOL receive_next_smb(char *inbuf, int bufsize, int timeout)
207 {
208   BOOL got_smb = False;
209   BOOL ret;
210
211   do
212   {
213     ret = receive_message_or_smb(inbuf,bufsize,timeout,&got_smb);
214
215     if(ret && !got_smb)
216     {
217       /* Deal with oplock break requests from other smbd's. */
218       process_local_message(inbuf, bufsize);
219       continue;
220     }
221
222     if(ret && (CVAL(inbuf,0) == 0x85))
223     {
224       /* Keepalive packet. */
225       got_smb = False;
226     }
227
228   }
229   while(ret && !got_smb);
230
231   return ret;
232 }
233
234 /****************************************************************************
235  We're terminating and have closed all our files/connections etc.
236  If there are any pending local messages we need to respond to them
237  before termination so that other smbds don't think we just died whilst
238  holding oplocks.
239 ****************************************************************************/
240
241 void respond_to_all_remaining_local_messages(void)
242 {
243   char buffer[1024];
244   fd_set fds;
245
246   /*
247    * Assert we have no exclusive open oplocks.
248    */
249
250   if(get_number_of_exclusive_open_oplocks()) {
251     DEBUG(0,("respond_to_all_remaining_local_messages: PANIC : we have %d exclusive oplocks.\n",
252           get_number_of_exclusive_open_oplocks() ));
253     return;
254   }
255
256   /*
257    * Setup the select read fd set.
258    */
259
260   FD_ZERO(&fds);
261   if(!setup_oplock_select_set(&fds))
262     return;
263
264   /*
265    * Keep doing receive_local_message with a 1 ms timeout until
266    * we have no more messages.
267    */
268
269   while(receive_local_message(&fds, buffer, sizeof(buffer), 1)) {
270     /* Deal with oplock break requests from other smbd's. */
271     process_local_message(buffer, sizeof(buffer));
272
273     FD_ZERO(&fds);
274     (void)setup_oplock_select_set(&fds);
275   }
276
277   return;
278 }
279
280
281 /*
282 These flags determine some of the permissions required to do an operation 
283
284 Note that I don't set NEED_WRITE on some write operations because they
285 are used by some brain-dead clients when printing, and I don't want to
286 force write permissions on print services.
287 */
288 #define AS_USER (1<<0)
289 #define NEED_WRITE (1<<1)
290 #define TIME_INIT (1<<2)
291 #define CAN_IPC (1<<3)
292 #define AS_GUEST (1<<5)
293 #define QUEUE_IN_OPLOCK (1<<6)
294
295 /* 
296    define a list of possible SMB messages and their corresponding
297    functions. Any message that has a NULL function is unimplemented -
298    please feel free to contribute implementations!
299 */
300 struct smb_message_struct
301 {
302   int code;
303   char *name;
304   int (*fn)(connection_struct *conn, char *, char *, int, int);
305   int flags;
306 }
307  smb_messages[] = {
308
309     /* CORE PROTOCOL */
310
311    {SMBnegprot,"SMBnegprot",reply_negprot,0},
312    {SMBtcon,"SMBtcon",reply_tcon,0},
313    {SMBtdis,"SMBtdis",reply_tdis,0},
314    {SMBexit,"SMBexit",reply_exit,0},
315    {SMBioctl,"SMBioctl",reply_ioctl,0},
316    {SMBecho,"SMBecho",reply_echo,0},
317    {SMBsesssetupX,"SMBsesssetupX",reply_sesssetup_and_X,0},
318    {SMBtconX,"SMBtconX",reply_tcon_and_X,0},
319    {SMBulogoffX, "SMBulogoffX", reply_ulogoffX, 0}, /* ulogoff doesn't give a valid TID */
320    {SMBgetatr,"SMBgetatr",reply_getatr,AS_USER},
321    {SMBsetatr,"SMBsetatr",reply_setatr,AS_USER | NEED_WRITE},
322    {SMBchkpth,"SMBchkpth",reply_chkpth,AS_USER},
323    {SMBsearch,"SMBsearch",reply_search,AS_USER},
324    {SMBopen,"SMBopen",reply_open,AS_USER | QUEUE_IN_OPLOCK },
325
326    /* note that SMBmknew and SMBcreate are deliberately overloaded */   
327    {SMBcreate,"SMBcreate",reply_mknew,AS_USER},
328    {SMBmknew,"SMBmknew",reply_mknew,AS_USER}, 
329
330    {SMBunlink,"SMBunlink",reply_unlink,AS_USER | NEED_WRITE | QUEUE_IN_OPLOCK},
331    {SMBread,"SMBread",reply_read,AS_USER},
332    {SMBwrite,"SMBwrite",reply_write,AS_USER | CAN_IPC },
333    {SMBclose,"SMBclose",reply_close,AS_USER | CAN_IPC },
334    {SMBmkdir,"SMBmkdir",reply_mkdir,AS_USER | NEED_WRITE},
335    {SMBrmdir,"SMBrmdir",reply_rmdir,AS_USER | NEED_WRITE},
336    {SMBdskattr,"SMBdskattr",reply_dskattr,AS_USER},
337    {SMBmv,"SMBmv",reply_mv,AS_USER | NEED_WRITE | QUEUE_IN_OPLOCK},
338
339    /* this is a Pathworks specific call, allowing the 
340       changing of the root path */
341    {pSETDIR,"pSETDIR",reply_setdir,AS_USER}, 
342
343    {SMBlseek,"SMBlseek",reply_lseek,AS_USER},
344    {SMBflush,"SMBflush",reply_flush,AS_USER},
345    {SMBctemp,"SMBctemp",reply_ctemp,AS_USER | QUEUE_IN_OPLOCK },
346    {SMBsplopen,"SMBsplopen",reply_printopen,AS_USER | QUEUE_IN_OPLOCK },
347    {SMBsplclose,"SMBsplclose",reply_printclose,AS_USER},
348    {SMBsplretq,"SMBsplretq",reply_printqueue,AS_USER},
349    {SMBsplwr,"SMBsplwr",reply_printwrite,AS_USER},
350    {SMBlock,"SMBlock",reply_lock,AS_USER},
351    {SMBunlock,"SMBunlock",reply_unlock,AS_USER},
352    
353    /* CORE+ PROTOCOL FOLLOWS */
354    
355    {SMBreadbraw,"SMBreadbraw",reply_readbraw,AS_USER},
356    {SMBwritebraw,"SMBwritebraw",reply_writebraw,AS_USER},
357    {SMBwriteclose,"SMBwriteclose",reply_writeclose,AS_USER},
358    {SMBlockread,"SMBlockread",reply_lockread,AS_USER},
359    {SMBwriteunlock,"SMBwriteunlock",reply_writeunlock,AS_USER},
360    
361    /* LANMAN1.0 PROTOCOL FOLLOWS */
362    
363    {SMBreadBmpx,"SMBreadBmpx",reply_readbmpx,AS_USER},
364    {SMBreadBs,"SMBreadBs",NULL,AS_USER},
365    {SMBwriteBmpx,"SMBwriteBmpx",reply_writebmpx,AS_USER},
366    {SMBwriteBs,"SMBwriteBs",reply_writebs,AS_USER},
367    {SMBwritec,"SMBwritec",NULL,AS_USER},
368    {SMBsetattrE,"SMBsetattrE",reply_setattrE,AS_USER | NEED_WRITE },
369    {SMBgetattrE,"SMBgetattrE",reply_getattrE,AS_USER },
370    {SMBtrans,"SMBtrans",reply_trans,AS_USER | CAN_IPC | QUEUE_IN_OPLOCK},
371    {SMBtranss,"SMBtranss",NULL,AS_USER | CAN_IPC},
372    {SMBioctls,"SMBioctls",NULL,AS_USER},
373    {SMBcopy,"SMBcopy",reply_copy,AS_USER | NEED_WRITE | QUEUE_IN_OPLOCK },
374    {SMBmove,"SMBmove",NULL,AS_USER | NEED_WRITE | QUEUE_IN_OPLOCK },
375    
376    {SMBopenX,"SMBopenX",reply_open_and_X,AS_USER | CAN_IPC | QUEUE_IN_OPLOCK },
377    {SMBreadX,"SMBreadX",reply_read_and_X,AS_USER | CAN_IPC },
378    {SMBwriteX,"SMBwriteX",reply_write_and_X,AS_USER | CAN_IPC },
379    {SMBlockingX,"SMBlockingX",reply_lockingX,AS_USER },
380    
381    {SMBffirst,"SMBffirst",reply_search,AS_USER},
382    {SMBfunique,"SMBfunique",reply_search,AS_USER},
383    {SMBfclose,"SMBfclose",reply_fclose,AS_USER},
384
385    /* LANMAN2.0 PROTOCOL FOLLOWS */
386    {SMBfindnclose, "SMBfindnclose", reply_findnclose, AS_USER},
387    {SMBfindclose, "SMBfindclose", reply_findclose,AS_USER},
388    {SMBtrans2, "SMBtrans2", reply_trans2, AS_USER | QUEUE_IN_OPLOCK | CAN_IPC },
389    {SMBtranss2, "SMBtranss2", reply_transs2, AS_USER},
390
391    /* NT PROTOCOL FOLLOWS */
392    {SMBntcreateX, "SMBntcreateX", reply_ntcreate_and_X, AS_USER | CAN_IPC | QUEUE_IN_OPLOCK },
393    {SMBnttrans, "SMBnttrans", reply_nttrans, AS_USER | CAN_IPC | QUEUE_IN_OPLOCK},
394    {SMBnttranss, "SMBnttranss", reply_nttranss, AS_USER | CAN_IPC },
395    {SMBntcancel, "SMBntcancel", reply_ntcancel, 0 },
396
397    /* messaging routines */
398    {SMBsends,"SMBsends",reply_sends,AS_GUEST},
399    {SMBsendstrt,"SMBsendstrt",reply_sendstrt,AS_GUEST},
400    {SMBsendend,"SMBsendend",reply_sendend,AS_GUEST},
401    {SMBsendtxt,"SMBsendtxt",reply_sendtxt,AS_GUEST},
402
403    /* NON-IMPLEMENTED PARTS OF THE CORE PROTOCOL */
404    
405    {SMBsendb,"SMBsendb",NULL,AS_GUEST},
406    {SMBfwdname,"SMBfwdname",NULL,AS_GUEST},
407    {SMBcancelf,"SMBcancelf",NULL,AS_GUEST},
408    {SMBgetmac,"SMBgetmac",NULL,AS_GUEST}
409  };
410
411 /*******************************************************************
412 dump a prs to a file
413  ********************************************************************/
414 static void smb_dump(char *name, int type, char *data, ssize_t len)
415 {
416         int fd, i;
417         pstring fname;
418         if (DEBUGLEVEL < 50) return;
419
420         if (len < 4) len = smb_buflen(data);
421         for (i=1;i<100;i++) {
422                 slprintf(fname,sizeof(fname), "/tmp/%s.%d.%s", name, i,
423                                 type ? "req" : "resp");
424                 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0644);
425                 if (fd != -1 || errno != EEXIST) break;
426         }
427         if (fd != -1) {
428                 write(fd, data, len);
429                 close(fd);
430                 DEBUG(0,("created %s len %d\n", fname, len));
431         }
432 }
433
434
435 /****************************************************************************
436 do a switch on the message type, and return the response size
437 ****************************************************************************/
438 static int switch_message(int type,char *inbuf,char *outbuf,int size,int bufsize)
439 {
440   static pid_t pid= (pid_t)-1;
441   int outsize = 0;
442   static int num_smb_messages = 
443     sizeof(smb_messages) / sizeof(struct smb_message_struct);
444   int match;
445   extern int global_smbpid;
446
447   if (pid == (pid_t)-1)
448     pid = sys_getpid();
449
450   errno = 0;
451   last_message = type;
452
453   /* make sure this is an SMB packet */
454   if (strncmp(smb_base(inbuf),"\377SMB",4) != 0)
455   {
456     DEBUG(2,("Non-SMB packet of length %d\n",smb_len(inbuf)));
457     return(-1);
458   }
459
460   for (match=0;match<num_smb_messages;match++)
461     if (smb_messages[match].code == type)
462       break;
463
464   /* yuck! this is an interim measure before we get rid of our
465      current inbuf/outbuf system */
466   global_smbpid = SVAL(inbuf,smb_pid);
467
468   if (match == num_smb_messages)
469   {
470     DEBUG(0,("Unknown message type %d!\n",type));
471     smb_dump("Unknown", 1, inbuf, size);
472     outsize = reply_unknown(inbuf,outbuf);
473   }
474   else
475   {
476     DEBUG(3,("switch message %s (pid %d)\n",smb_messages[match].name,(int)pid));
477
478     smb_dump(smb_messages[match].name, 1, inbuf, size);
479     if(global_oplock_break)
480     {
481       int flags = smb_messages[match].flags;
482
483       if(flags & QUEUE_IN_OPLOCK)
484       {
485         /* 
486          * Queue this message as we are the process of an oplock break.
487          */
488
489         DEBUG( 2, ( "switch_message: queueing message due to being in " ) );
490         DEBUGADD( 2, ( "oplock break state.\n" ) );
491
492         push_oplock_pending_smb_message( inbuf, size );
493         return -1;
494       }          
495     }
496     if (smb_messages[match].fn)
497     {
498       int flags = smb_messages[match].flags;
499       static uint16 last_session_tag = UID_FIELD_INVALID;
500       /* In share mode security we must ignore the vuid. */
501       uint16 session_tag = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID : SVAL(inbuf,smb_uid);
502       connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
503
504
505       /* Ensure this value is replaced in the incoming packet. */
506       SSVAL(inbuf,smb_uid,session_tag);
507
508       /*
509        * Ensure the correct username is in sesssetup_user.
510        * This is a really ugly bugfix for problems with
511        * multiple session_setup_and_X's being done and
512        * allowing %U and %G substitutions to work correctly.
513        * There is a reason this code is done here, don't
514        * move it unless you know what you're doing... :-).
515        * JRA.
516        */
517       if (session_tag != last_session_tag) {
518         user_struct *vuser = NULL;
519
520         last_session_tag = session_tag;
521         if(session_tag != UID_FIELD_INVALID)
522           vuser = get_valid_user_struct(session_tag);           
523         if(vuser != NULL)
524           pstrcpy( sesssetup_user, vuser->user.smb_name);
525       }
526
527       /* does this protocol need to be run as root? */
528       if (!(flags & AS_USER))
529         unbecome_user();
530
531       /* does this protocol need to be run as the connected user? */
532       if ((flags & AS_USER) && !become_user(conn,session_tag)) {
533         if (flags & AS_GUEST) 
534           flags &= ~AS_USER;
535         else
536           return(ERROR(ERRSRV,ERRaccess));
537       }
538       /* this code is to work around a bug is MS client 3 without
539          introducing a security hole - it needs to be able to do
540          print queue checks as guest if it isn't logged in properly */
541       if (flags & AS_USER)
542         flags &= ~AS_GUEST;
543
544       /* does it need write permission? */
545       if ((flags & NEED_WRITE) && !CAN_WRITE(conn))
546         return(ERROR(ERRSRV,ERRaccess));
547
548       /* ipc services are limited */
549       if (IS_IPC(conn) && (flags & AS_USER) && !(flags & CAN_IPC)) {
550         return(ERROR(ERRSRV,ERRaccess));            
551       }
552
553       /* load service specific parameters */
554       if (conn && 
555           !become_service(conn,(flags & AS_USER)?True:False)) {
556         return(ERROR(ERRSRV,ERRaccess));
557       }
558
559       /* does this protocol need to be run as guest? */
560       if ((flags & AS_GUEST) && 
561           (!become_guest() || 
562            !check_access(smbd_server_fd(), lp_hostsallow(-1), lp_hostsdeny(-1)))) {
563         return(ERROR(ERRSRV,ERRaccess));
564       }
565
566       last_inbuf = inbuf;
567
568       outsize = smb_messages[match].fn(conn, inbuf,outbuf,size,bufsize);
569     }
570     else
571     {
572       outsize = reply_unknown(inbuf,outbuf);
573     }
574   }
575
576   smb_dump(smb_messages[match].name, 0, outbuf, outsize);
577
578   return(outsize);
579 }
580
581
582 /****************************************************************************
583   construct a reply to the incoming packet
584 ****************************************************************************/
585 static int construct_reply(char *inbuf,char *outbuf,int size,int bufsize)
586 {
587   int type = CVAL(inbuf,smb_com);
588   int outsize = 0;
589   int msg_type = CVAL(inbuf,0);
590
591   GetTimeOfDay(&smb_last_time);
592
593   chain_size = 0;
594   file_chain_reset();
595   reset_chain_p();
596
597   if (msg_type != 0)
598     return(reply_special(inbuf,outbuf));  
599
600   construct_reply_common(inbuf, outbuf);
601
602   outsize = switch_message(type,inbuf,outbuf,size,bufsize);
603
604   outsize += chain_size;
605
606   if(outsize > 4)
607     smb_setlen(outbuf,outsize - 4);
608   return(outsize);
609 }
610
611
612 /****************************************************************************
613   process an smb from the client - split out from the process() code so
614   it can be used by the oplock break code.
615 ****************************************************************************/
616 void process_smb(char *inbuf, char *outbuf)
617 {
618 #ifdef WITH_SSL
619   extern BOOL sslEnabled;     /* don't use function for performance reasons */
620   static int sslConnected = 0;
621 #endif /* WITH_SSL */
622   static int trans_num;
623   int msg_type = CVAL(inbuf,0);
624   int32 len = smb_len(inbuf);
625   int nread = len + 4;
626
627 #ifdef WITH_PROFILE
628   profile_p->smb_count++;
629 #endif
630
631   if (trans_num == 0) {
632           /* on the first packet, check the global hosts allow/ hosts
633              deny parameters before doing any parsing of the packet
634              passed to us by the client.  This prevents attacks on our
635              parsing code from hosts not in the hosts allow list */
636           if (!check_access(smbd_server_fd(), lp_hostsallow(-1), lp_hostsdeny(-1))) {
637                   /* send a negative session response "not listining on calling
638                    name" */
639                   static unsigned char buf[5] = {0x83, 0, 0, 1, 0x81};
640                   DEBUG( 1, ( "Connection denied from %s\n",
641                               client_addr() ) );
642                   send_smb(smbd_server_fd(),(char *)buf);
643                   exit_server("connection denied");
644           }
645   }
646
647   DEBUG( 6, ( "got message type 0x%x of len 0x%x\n", msg_type, len ) );
648   DEBUG( 3, ( "Transaction %d of length %d\n", trans_num, nread ) );
649
650 #ifdef WITH_SSL
651     if(sslEnabled && !sslConnected){
652         sslConnected = sslutil_negotiate_ssl(smbd_server_fd(), msg_type);
653         if(sslConnected < 0){   /* an error occured */
654             exit_server("SSL negotiation failed");
655         }else if(sslConnected){
656             trans_num++;
657             return;
658         }
659     }
660 #endif  /* WITH_SSL */
661
662   if (msg_type == 0)
663     show_msg(inbuf);
664   else if(msg_type == 0x85)
665     return; /* Keepalive packet. */
666
667   nread = construct_reply(inbuf,outbuf,nread,max_send);
668       
669   if(nread > 0) 
670   {
671     if (CVAL(outbuf,0) == 0)
672       show_msg(outbuf);
673         
674     if (nread != smb_len(outbuf) + 4) 
675     {
676       DEBUG(0,("ERROR: Invalid message response size! %d %d\n",
677                  nread, smb_len(outbuf)));
678     }
679     else
680       send_smb(smbd_server_fd(),outbuf);
681   }
682   trans_num++;
683 }
684
685
686
687 /****************************************************************************
688 return a string containing the function name of a SMB command
689 ****************************************************************************/
690 char *smb_fn_name(int type)
691 {
692         static char *unknown_name = "SMBunknown";
693         static int num_smb_messages = 
694                 sizeof(smb_messages) / sizeof(struct smb_message_struct);
695         int match;
696
697         for (match=0;match<num_smb_messages;match++)
698                 if (smb_messages[match].code == type)
699                         break;
700
701         if (match == num_smb_messages)
702                 return(unknown_name);
703
704         return(smb_messages[match].name);
705 }
706
707
708 /****************************************************************************
709  Helper function for contruct_reply.
710 ****************************************************************************/
711
712 void construct_reply_common(char *inbuf,char *outbuf)
713 {
714   memset(outbuf,'\0',smb_size);
715
716   set_message(outbuf,0,0,True);
717   CVAL(outbuf,smb_com) = CVAL(inbuf,smb_com);
718
719   memcpy(outbuf+4,inbuf+4,4);
720   CVAL(outbuf,smb_rcls) = SMB_SUCCESS;
721   CVAL(outbuf,smb_reh) = 0;
722   SCVAL(outbuf,smb_flg, FLAG_REPLY | (CVAL(inbuf,smb_flg) & FLAG_CASELESS_PATHNAMES)); /* bit 7 set
723                                  means a reply */
724   SSVAL(outbuf,smb_flg2,FLAGS2_LONG_PATH_COMPONENTS);
725         /* say we support long filenames */
726
727   SSVAL(outbuf,smb_err,SMB_SUCCESS);
728   SSVAL(outbuf,smb_tid,SVAL(inbuf,smb_tid));
729   SSVAL(outbuf,smb_pid,SVAL(inbuf,smb_pid));
730   SSVAL(outbuf,smb_uid,SVAL(inbuf,smb_uid));
731   SSVAL(outbuf,smb_mid,SVAL(inbuf,smb_mid));
732 }
733
734 /****************************************************************************
735   construct a chained reply and add it to the already made reply
736   **************************************************************************/
737 int chain_reply(char *inbuf,char *outbuf,int size,int bufsize)
738 {
739   static char *orig_inbuf;
740   static char *orig_outbuf;
741   int smb_com1, smb_com2 = CVAL(inbuf,smb_vwv0);
742   unsigned smb_off2 = SVAL(inbuf,smb_vwv1);
743   char *inbuf2, *outbuf2;
744   int outsize2;
745   char inbuf_saved[smb_wct];
746   char outbuf_saved[smb_wct];
747   int wct = CVAL(outbuf,smb_wct);
748   int outsize = smb_size + 2*wct + SVAL(outbuf,smb_vwv0+2*wct);
749
750   /* maybe its not chained */
751   if (smb_com2 == 0xFF) {
752     CVAL(outbuf,smb_vwv0) = 0xFF;
753     return outsize;
754   }
755
756   if (chain_size == 0) {
757     /* this is the first part of the chain */
758     orig_inbuf = inbuf;
759     orig_outbuf = outbuf;
760   }
761
762   /*
763    * The original Win95 redirector dies on a reply to
764    * a lockingX and read chain unless the chain reply is
765    * 4 byte aligned. JRA.
766    */
767
768   outsize = (outsize + 3) & ~3;
769
770   /* we need to tell the client where the next part of the reply will be */
771   SSVAL(outbuf,smb_vwv1,smb_offset(outbuf+outsize,outbuf));
772   CVAL(outbuf,smb_vwv0) = smb_com2;
773
774   /* remember how much the caller added to the chain, only counting stuff
775      after the parameter words */
776   chain_size += outsize - smb_wct;
777
778   /* work out pointers into the original packets. The
779      headers on these need to be filled in */
780   inbuf2 = orig_inbuf + smb_off2 + 4 - smb_wct;
781   outbuf2 = orig_outbuf + SVAL(outbuf,smb_vwv1) + 4 - smb_wct;
782
783   /* remember the original command type */
784   smb_com1 = CVAL(orig_inbuf,smb_com);
785
786   /* save the data which will be overwritten by the new headers */
787   memcpy(inbuf_saved,inbuf2,smb_wct);
788   memcpy(outbuf_saved,outbuf2,smb_wct);
789
790   /* give the new packet the same header as the last part of the SMB */
791   memmove(inbuf2,inbuf,smb_wct);
792
793   /* create the in buffer */
794   CVAL(inbuf2,smb_com) = smb_com2;
795
796   /* create the out buffer */
797   construct_reply_common(inbuf2, outbuf2);
798
799   DEBUG(3,("Chained message\n"));
800   show_msg(inbuf2);
801
802   /* process the request */
803   outsize2 = switch_message(smb_com2,inbuf2,outbuf2,size-chain_size,
804                             bufsize-chain_size);
805
806   /* copy the new reply and request headers over the old ones, but
807      preserve the smb_com field */
808   memmove(orig_outbuf,outbuf2,smb_wct);
809   CVAL(orig_outbuf,smb_com) = smb_com1;
810
811   /* restore the saved data, being careful not to overwrite any
812    data from the reply header */
813   memcpy(inbuf2,inbuf_saved,smb_wct);
814   {
815     int ofs = smb_wct - PTR_DIFF(outbuf2,orig_outbuf);
816     if (ofs < 0) ofs = 0;
817     memmove(outbuf2+ofs,outbuf_saved+ofs,smb_wct-ofs);
818   }
819
820   return outsize2;
821 }
822
823 /****************************************************************************
824  Setup the needed select timeout.
825 ****************************************************************************/
826
827 static int setup_select_timeout(void)
828 {
829   int change_notify_timeout = lp_change_notify_timeout() * 1000;
830   int select_timeout;
831
832   /*
833    * Increase the select timeout back to SMBD_SELECT_TIMEOUT if we
834    * have removed any blocking locks. JRA.
835    */
836
837   select_timeout = blocking_locks_pending() ? SMBD_SELECT_TIMEOUT_WITH_PENDING_LOCKS*1000 :
838                                               SMBD_SELECT_TIMEOUT*1000;
839
840   if (change_notifies_pending())
841     select_timeout = MIN(select_timeout, change_notify_timeout);
842
843   return select_timeout;
844 }
845
846 /****************************************************************************
847  Check if services need reloading.
848 ****************************************************************************/
849
850 void check_reload(int t)
851 {
852   static time_t last_smb_conf_reload_time = 0;
853
854   if(last_smb_conf_reload_time == 0)
855     last_smb_conf_reload_time = t;
856
857   if (reload_after_sighup || (t >= last_smb_conf_reload_time+SMBD_RELOAD_CHECK))
858   {
859     reload_services(True);
860     reload_after_sighup = False;
861     last_smb_conf_reload_time = t;
862   }
863 }
864
865 /****************************************************************************
866  Process any timeout housekeeping. Return False if the caller should exit.
867 ****************************************************************************/
868
869 static BOOL timeout_processing(int deadtime, int *select_timeout, time_t *last_timeout_processing_time)
870 {
871   static time_t last_keepalive_sent_time = 0;
872   static time_t last_idle_closed_check = 0;
873   time_t t;
874   BOOL allidle = True;
875   extern int keepalive;
876
877   if (smb_read_error == READ_EOF) 
878   {
879     DEBUG(3,("end of file from client\n"));
880     return False;
881   }
882
883   if (smb_read_error == READ_ERROR) 
884   {
885     DEBUG(3,("receive_smb error (%s) exiting\n",
886               strerror(errno)));
887     return False;
888   }
889
890   *last_timeout_processing_time = t = time(NULL);
891
892   if(last_keepalive_sent_time == 0)
893     last_keepalive_sent_time = t;
894
895   if(last_idle_closed_check == 0)
896     last_idle_closed_check = t;
897
898   /* become root again if waiting */
899   unbecome_user();
900
901   /* check if we need to reload services */
902   check_reload(t);
903
904   /* automatic timeout if all connections are closed */      
905   if (conn_num_open()==0 && (t - last_idle_closed_check) >= IDLE_CLOSED_TIMEOUT) 
906   {
907     DEBUG( 2, ( "Closing idle connection\n" ) );
908     return False;
909   }
910   else
911     last_idle_closed_check = t;
912
913   if (keepalive && (t - last_keepalive_sent_time)>keepalive) 
914   {
915     struct cli_state *cli = server_client();
916     if (!send_keepalive(smbd_server_fd())) {
917       DEBUG( 2, ( "Keepalive failed - exiting.\n" ) );
918       return False;
919     }       
920     /* also send a keepalive to the password server if its still
921        connected */
922     if (cli && cli->initialised)
923       send_keepalive(cli->fd);
924     last_keepalive_sent_time = t;
925   }
926
927   /* check for connection timeouts */
928   allidle = conn_idle_all(t, deadtime);
929
930   if (allidle && conn_num_open()>0) {
931     DEBUG(2,("Closing idle connection 2.\n"));
932     return False;
933   }
934
935   if(global_machine_password_needs_changing)
936   {
937     unsigned char trust_passwd_hash[16];
938     time_t lct;
939     pstring remote_machine_list;
940
941     /*
942      * We're in domain level security, and the code that
943      * read the machine password flagged that the machine
944      * password needs changing.
945      */
946
947     /*
948      * First, open the machine password file with an exclusive lock.
949      */
950
951     if(!get_trust_account_password(global_myworkgroup, trust_passwd_hash, &lct)) {
952       DEBUG(0,("process: unable to read the machine account password for \
953 machine %s in domain %s.\n", global_myname, global_myworkgroup ));
954       return True;
955     }
956
957     /*
958      * Make sure someone else hasn't already done this.
959      */
960
961     if(t < lct + lp_machine_password_timeout()) {
962       global_machine_password_needs_changing = False;
963       return True;
964     }
965
966     pstrcpy(remote_machine_list, lp_passwordserver());
967
968     change_trust_account_password( global_myworkgroup, remote_machine_list);
969     global_machine_password_needs_changing = False;
970   }
971
972   /*
973    * Check to see if we have any blocking locks
974    * outstanding on the queue.
975    */
976   process_blocking_lock_queue(t);
977
978   /*
979    * Check to see if we have any change notifies 
980    * outstanding on the queue.
981    */
982   process_pending_change_notify_queue(t);
983
984   /*
985    * Now we are root, check if the log files need pruning.
986    */
987   if(need_to_check_log_size())
988       check_log_size();
989
990   /*
991    * Modify the select timeout depending upon
992    * what we have remaining in our queues.
993    */
994
995   *select_timeout = setup_select_timeout();
996
997   return True;
998 }
999
1000 /****************************************************************************
1001   process commands from the client
1002 ****************************************************************************/
1003
1004 void smbd_process(void)
1005 {
1006   extern int smb_echo_count;
1007   time_t last_timeout_processing_time = time(NULL);
1008   unsigned int num_smbs = 0;
1009
1010   InBuffer = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
1011   OutBuffer = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
1012   if ((InBuffer == NULL) || (OutBuffer == NULL)) 
1013     return;
1014
1015   InBuffer += SMB_ALIGNMENT;
1016   OutBuffer += SMB_ALIGNMENT;
1017
1018   max_recv = MIN(lp_maxxmit(),BUFFER_SIZE);
1019
1020   /* re-initialise the timezone */
1021   TimeInit();
1022
1023   while (True)
1024   {
1025     int deadtime = lp_deadtime()*60;
1026     BOOL got_smb = False;
1027     int select_timeout = setup_select_timeout();
1028
1029     if (deadtime <= 0)
1030       deadtime = DEFAULT_SMBD_TIMEOUT;
1031
1032     errno = 0;      
1033
1034     /* free up temporary memory */
1035     lp_talloc_free();
1036
1037     /*
1038      * If reload_after_sighup == True then we got a SIGHUP
1039      * and are being asked to reload. Fix from <branko.cibej@hermes.si>
1040      */
1041     if (reload_after_sighup) {
1042             /* become root */
1043             unbecome_user();
1044             DEBUG(1,("Reloading services after SIGHUP\n"));
1045             reload_services(False);
1046             reload_after_sighup = False;
1047     }
1048
1049     while(!receive_message_or_smb(InBuffer,BUFFER_SIZE,select_timeout,&got_smb))
1050     {
1051       if(!timeout_processing( deadtime, &select_timeout, &last_timeout_processing_time))
1052         return;
1053       num_smbs = 0; /* Reset smb counter. */
1054     }
1055
1056     if(got_smb) {
1057       /*
1058        * Ensure we do timeout processing if the SMB we just got was
1059        * only an echo request. This allows us to set the select
1060        * timeout in 'receive_message_or_smb()' to any value we like
1061        * without worrying that the client will send echo requests
1062        * faster than the select timeout, thus starving out the
1063        * essential processing (change notify, blocking locks) that
1064        * the timeout code does. JRA.
1065        */ 
1066       int num_echos = smb_echo_count;
1067
1068       process_smb(InBuffer, OutBuffer);
1069
1070       if(smb_echo_count != num_echos) {
1071         if(!timeout_processing( deadtime, &select_timeout, &last_timeout_processing_time))
1072           return;
1073         num_smbs = 0; /* Reset smb counter. */
1074       }
1075
1076       num_smbs++;
1077
1078       /*
1079        * If we are getting smb requests in a constant stream
1080        * with no echos, make sure we attempt timeout processing
1081        * every select_timeout milliseconds - but only check for this
1082        * every 200 smb requests.
1083        */
1084
1085       if((num_smbs % 200) == 0) {
1086         time_t new_check_time = time(NULL);
1087         if(last_timeout_processing_time - new_check_time >= (select_timeout/1000)) {
1088           if(!timeout_processing( deadtime, &select_timeout, &last_timeout_processing_time))
1089             return;
1090           num_smbs = 0; /* Reset smb counter. */
1091           last_timeout_processing_time = new_check_time; /* Reset time. */
1092         }
1093       }
1094     }
1095     else
1096       process_local_message(InBuffer, BUFFER_SIZE);
1097   }
1098 }
1099
1100 #undef OLD_NTDOMAIN