Fix talloc msg registration
[ira/wip.git] / source3 / smbd / process.c
index 9f23c7d3be99016b79884506db61572f237cc43a..af081d5059a1683bdafa74f6cefd463f0b26b2e2 100644 (file)
@@ -21,8 +21,6 @@
 
 #include "includes.h"
 
-extern int DEBUGLEVEL;
-
 struct timeval smb_last_time;
 
 static char *InBuffer = NULL;
@@ -44,9 +42,6 @@ int max_recv = BUFFER_SIZE;
 extern int last_message;
 extern int global_oplock_break;
 extern userdom_struct current_user_info;
-extern char *last_inbuf;
-extern char *InBuffer;
-extern char *OutBuffer;
 extern int smb_read_error;
 extern VOLATILE sig_atomic_t reload_after_sighup;
 extern BOOL global_machine_password_needs_changing;
@@ -87,7 +82,7 @@ static BOOL push_message(ubi_slList *list_head, char *buf, int msg_len)
   if(msg->msg_buf == NULL)
   {
     DEBUG(0,("push_message: malloc fail (2)\n"));
-    free((char *)msg);
+    SAFE_FREE(msg);
     return False;
   }
 
@@ -125,7 +120,7 @@ static void async_processing(fd_set *fds, char *buffer, int buffer_len)
 
        /* check for sighup processing */
        if (reload_after_sighup) {
-               unbecome_user();
+               change_to_root_user();
                DEBUG(1,("Reloading services after SIGHUP\n"));
                reload_services(False);
                reload_after_sighup = False;
@@ -180,8 +175,8 @@ static BOOL receive_message_or_smb(char *buffer, int buffer_len, int timeout)
                memcpy(buffer, msg->msg_buf, MIN(buffer_len, msg->msg_len));
   
                /* Free the message we just copied. */
-               free((char *)msg->msg_buf);
-               free((char *)msg);
+               SAFE_FREE(msg->msg_buf);
+               SAFE_FREE(msg);
                
                DEBUG(5,("receive_message_or_smb: returning queued smb message.\n"));
                return True;
@@ -207,6 +202,11 @@ static BOOL receive_message_or_smb(char *buffer, int buffer_len, int timeout)
           signals */
        if (selrtn == -1 && errno == EINTR) {
                async_processing(&fds, buffer, buffer_len);
+               /*
+                * After async processing we must go and do the select again, as
+                * the state of the flag in fds for the server file descriptor is
+                * indeterminate - we may have done I/O on it in the oplock processing. JRA.
+                */
                goto again;
        }
 
@@ -231,7 +231,12 @@ static BOOL receive_message_or_smb(char *buffer, int buffer_len, int timeout)
 
        if (oplock_message_waiting(&fds)) {
                async_processing(&fds, buffer, buffer_len);
-               if (!FD_ISSET(smbd_server_fd(),&fds)) goto again;
+               /*
+                * After async processing we must go and do the select again, as
+                * the state of the flag in fds for the server file descriptor is
+                * indeterminate - we may have done I/O on it in the oplock processing. JRA.
+                */
+               goto again;
        }
        
        return receive_smb(smbd_server_fd(), buffer, 0);
@@ -249,7 +254,7 @@ BOOL receive_next_smb(char *inbuf, int bufsize, int timeout)
        do {
                ret = receive_message_or_smb(inbuf,bufsize,timeout);
                
-               got_keepalive = (ret && (CVAL(inbuf,0) == 0x85));
+               got_keepalive = (ret && (CVAL(inbuf,0) == SMBkeepalive));
        } while (ret && got_keepalive);
 
        return ret;
@@ -320,7 +325,7 @@ force write permissions on print services.
    functions. Any message that has a NULL function is unimplemented -
    please feel free to contribute implementations!
 */
-struct smb_message_struct
+static struct smb_message_struct
 {
   char *name;
   int (*fn)(connection_struct *conn, char *, char *, int, int);
@@ -356,11 +361,11 @@ struct smb_message_struct
 /* 0x19 */ { NULL, NULL, 0 },
 /* 0x1a */ { "SMBreadbraw",reply_readbraw,AS_USER},
 /* 0x1b */ { "SMBreadBmpx",reply_readbmpx,AS_USER},
-/* 0x1c */ { "SMBreadBs",NULL,AS_USER},
+/* 0x1c */ { "SMBreadBs",NULL,},
 /* 0x1d */ { "SMBwritebraw",reply_writebraw,AS_USER},
-/* 0x1e */ { NULL,NULL,0},
-/* 0x1f */ { NULL,NULL,0},
-/* 0x20 */ { NULL,NULL,0},
+/* 0x1e */ { "SMBwriteBmpx",reply_writebmpx,AS_USER},
+/* 0x1f */ { "SMBwriteBs",reply_writebs,AS_USER},
+/* 0x20 */ { "SMBwritec",NULL,0},
 /* 0x21 */ { NULL, NULL, 0 },
 /* 0x22 */ { "SMBsetattrE",reply_setattrE,AS_USER | NEED_WRITE },
 /* 0x23 */ { "SMBgetattrE",reply_getattrE,AS_USER },
@@ -697,7 +702,7 @@ static int switch_message(int type,char *inbuf,char *outbuf,int size,int bufsize
 
     /* does this protocol need to be run as root? */
     if (!(flags & AS_USER))
-      unbecome_user();
+      change_to_root_user();
 
     /* does this protocol need a valid tree connection? */
     if ((flags & AS_USER) && !conn) {
@@ -706,7 +711,7 @@ static int switch_message(int type,char *inbuf,char *outbuf,int size,int bufsize
 
 
     /* does this protocol need to be run as the connected user? */
-    if ((flags & AS_USER) && !become_user(conn,session_tag)) {
+    if ((flags & AS_USER) && !change_to_user(conn,session_tag)) {
       if (flags & AS_GUEST) 
         flags &= ~AS_USER;
       else
@@ -729,13 +734,13 @@ static int switch_message(int type,char *inbuf,char *outbuf,int size,int bufsize
     }
 
     /* load service specific parameters */
-    if (conn && !become_service(conn,(flags & AS_USER)?True:False)) {
+    if (conn && !set_current_service(conn,(flags & AS_USER)?True:False)) {
       return(ERROR_DOS(ERRSRV,ERRaccess));
     }
 
     /* does this protocol need to be run as guest? */
     if ((flags & AS_GUEST) && 
-                (!become_guest() || 
+                (!change_to_guest() || 
                !check_access(smbd_server_fd(), lp_hostsallow(-1), lp_hostsdeny(-1)))) {
       return(ERROR_DOS(ERRSRV,ERRaccess));
     }
@@ -796,6 +801,12 @@ static BOOL smbd_process_limit(void)
 
                total_smbds = 1; /* In case we need to create the entry. */
 
+               if (!conn_tdb_ctx()) {
+                       DEBUG(0,("smbd_process_limit: max smbd processes parameter set with status parameter not \
+set. Ignoring max smbd restriction.\n"));
+                       return False;
+               }
+
                if (tdb_change_int_atomic(conn_tdb_ctx(), "INFO/total_smbds", &total_smbds, 1) == -1)
                        return True;
 
@@ -856,7 +867,7 @@ void process_smb(char *inbuf, char *outbuf)
 
   if (msg_type == 0)
     show_msg(inbuf);
-  else if(msg_type == 0x85)
+  else if(msg_type == SMBkeepalive)
     return; /* Keepalive packet. */
 
   nread = construct_reply(inbuf,outbuf,nread,max_send);
@@ -873,7 +884,7 @@ void process_smb(char *inbuf, char *outbuf)
     }
     else
       if (!send_smb(smbd_server_fd(),outbuf))
-        exit_server("process_smb: send_smb failed.\n");
+        exit_server("process_smb: send_smb failed.");
   }
   trans_num++;
 }
@@ -900,25 +911,24 @@ char *smb_fn_name(int type)
 
 void construct_reply_common(char *inbuf,char *outbuf)
 {
-  memset(outbuf,'\0',smb_size);
-
-  set_message(outbuf,0,0,True);
-  CVAL(outbuf,smb_com) = CVAL(inbuf,smb_com);
-
-  memcpy(outbuf+4,inbuf+4,4);
-  CVAL(outbuf,smb_rcls) = SMB_SUCCESS;
-  CVAL(outbuf,smb_reh) = 0;
-  SCVAL(outbuf,smb_flg, FLAG_REPLY | (CVAL(inbuf,smb_flg) & FLAG_CASELESS_PATHNAMES)); /* bit 7 set
-                                 means a reply */
-  SSVAL(outbuf,smb_flg2,
-       (SVAL(inbuf,smb_flg2)&FLAGS2_UNICODE_STRINGS) | FLAGS2_LONG_PATH_COMPONENTS);
-       /* say we support long filenames */
-
-  SSVAL(outbuf,smb_err,SMB_SUCCESS);
-  SSVAL(outbuf,smb_tid,SVAL(inbuf,smb_tid));
-  SSVAL(outbuf,smb_pid,SVAL(inbuf,smb_pid));
-  SSVAL(outbuf,smb_uid,SVAL(inbuf,smb_uid));
-  SSVAL(outbuf,smb_mid,SVAL(inbuf,smb_mid));
+       memset(outbuf,'\0',smb_size);
+
+       set_message(outbuf,0,0,True);
+       CVAL(outbuf,smb_com) = CVAL(inbuf,smb_com);
+       
+       memcpy(outbuf+4,inbuf+4,4);
+       CVAL(outbuf,smb_rcls) = SMB_SUCCESS;
+       CVAL(outbuf,smb_reh) = 0;
+       SCVAL(outbuf,smb_flg, FLAG_REPLY | (CVAL(inbuf,smb_flg) & FLAG_CASELESS_PATHNAMES)); 
+       SSVAL(outbuf,smb_flg2,
+             FLAGS2_UNICODE_STRINGS | FLAGS2_LONG_PATH_COMPONENTS |
+             FLAGS2_32_BIT_ERROR_CODES | FLAGS2_EXTENDED_SECURITY);
+
+       SSVAL(outbuf,smb_err,SMB_SUCCESS);
+       SSVAL(outbuf,smb_tid,SVAL(inbuf,smb_tid));
+       SSVAL(outbuf,smb_pid,SVAL(inbuf,smb_pid));
+       SSVAL(outbuf,smb_uid,SVAL(inbuf,smb_uid));
+       SSVAL(outbuf,smb_mid,SVAL(inbuf,smb_mid));
 }
 
 /****************************************************************************
@@ -1086,7 +1096,7 @@ static BOOL timeout_processing(int deadtime, int *select_timeout, time_t *last_t
     last_idle_closed_check = t;
 
   /* become root again if waiting */
-  unbecome_user();
+  change_to_root_user();
 
   /* check if we need to reload services */
   check_reload(t);
@@ -1102,19 +1112,23 @@ static BOOL timeout_processing(int deadtime, int *select_timeout, time_t *last_t
 
   if (keepalive && (t - last_keepalive_sent_time)>keepalive) 
   {
-    struct cli_state *cli = server_client();
-    if (!send_keepalive(smbd_server_fd())) {
-      DEBUG( 2, ( "Keepalive failed - exiting.\n" ) );
-      return False;
-    }      
-    /* also send a keepalive to the password server if its still
-       connected */
-    if (cli && cli->initialised)
-      if (!send_keepalive(cli->fd)) {
-        DEBUG( 2, ( "password server keepalive failed.\n"));
-        cli_shutdown(cli);
-      }
-    last_keepalive_sent_time = t;
+         extern auth_authsupplied_info *negprot_global_auth_info;
+         if (!send_keepalive(smbd_server_fd())) {
+                 DEBUG( 2, ( "Keepalive failed - exiting.\n" ) );
+                 return False;
+         }
+         
+         /* send a keepalive for a password server or the like.
+            This is attached to the auth_info created in the
+            negprot */
+         if (negprot_global_auth_info 
+             && negprot_global_auth_info->challenge_set_method 
+             && negprot_global_auth_info->challenge_set_method->send_keepalive) {
+                 negprot_global_auth_info->challenge_set_method->send_keepalive
+                         (&negprot_global_auth_info->challenge_set_method->private_data);
+         }
+
+         last_keepalive_sent_time = t;
   }
 
   /* check for connection timeouts */
@@ -1125,7 +1139,10 @@ static BOOL timeout_processing(int deadtime, int *select_timeout, time_t *last_t
     return False;
   }
 
-  if(global_machine_password_needs_changing)
+  if(global_machine_password_needs_changing && 
+     /* for ADS we need to do a regular ADS password change, not a domain
+        password change */
+     lp_security() == SEC_DOMAIN)
   {
     unsigned char trust_passwd_hash[16];
     time_t lct;
@@ -1213,6 +1230,7 @@ void smbd_process(void)
 
        /* register our message handlers */
        message_register(MSG_SMB_FORCE_TDIS, msg_force_tdis);
+       talloc_init_named("dummy!");
 
        while (True) {
                int deadtime = lp_deadtime()*60;