summaryrefslogtreecommitdiff
path: root/source3/msrpc
diff options
context:
space:
mode:
Diffstat (limited to 'source3/msrpc')
-rw-r--r--source3/msrpc/msrpcd.c571
-rw-r--r--source3/msrpc/msrpcd_process.c501
2 files changed, 1072 insertions, 0 deletions
diff --git a/source3/msrpc/msrpcd.c b/source3/msrpc/msrpcd.c
new file mode 100644
index 0000000000..942d6f1258
--- /dev/null
+++ b/source3/msrpc/msrpcd.c
@@ -0,0 +1,571 @@
+/*
+ Unix SMB/Netbios implementation.
+ Version 1.9.
+ Main SMB server routines
+ Copyright (C) Andrew Tridgell 1992-1998
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+#include "includes.h"
+#include "trans2.h"
+
+extern pstring servicesf;
+extern pstring debugf;
+extern pstring global_myname;
+
+int am_parent = 1;
+
+/* the last message the was processed */
+int last_message = -1;
+
+/* a useful macro to debug the last message processed */
+#define LAST_MESSAGE() smb_fn_name(last_message)
+
+extern pstring scope;
+extern int DEBUGLEVEL;
+
+extern fstring remote_machine;
+extern pstring myhostname;
+extern pstring pipe_name;
+
+extern pstring OriginalDir;
+
+/****************************************************************************
+ when exiting, take the whole family
+****************************************************************************/
+static void *dflt_sig(void)
+{
+ exit_server("caught signal");
+ return NULL;
+}
+
+/****************************************************************************
+ Send a SIGTERM to our process group.
+*****************************************************************************/
+static void killkids(void)
+{
+ if(am_parent) kill(0,SIGTERM);
+}
+
+
+/****************************************************************************
+ open and listen to a socket
+****************************************************************************/
+static int open_server_socket(void)
+{
+ int s;
+ fstring dir;
+ fstring path;
+
+ slprintf(dir, sizeof(dir)-1, "/tmp/.msrpc");
+ slprintf(path, sizeof(path)-1, "%s/%s", dir, pipe_name);
+
+ s = create_pipe_socket(dir, 0777, path, 0777);
+
+ if (s == -1)
+ return -1;
+ /* ready to listen */
+ if (listen(s, 5) == -1) {
+ DEBUG(0,("listen: %s\n", strerror(errno)));
+ close(s);
+ return -1;
+ }
+ return s;
+}
+
+/****************************************************************************
+ open the socket communication
+****************************************************************************/
+static int open_sockets(BOOL is_daemon)
+{
+ int ClientMSRPC;
+ int num_interfaces = iface_count();
+ int fd_listenset;
+ fd_set listen_set;
+ int s;
+
+ memset(&fd_listenset, 0, sizeof(fd_listenset));
+
+#ifdef HAVE_ATEXIT
+ {
+ static int atexit_set;
+ if(atexit_set == 0) {
+ atexit_set=1;
+ atexit(killkids);
+ }
+ }
+#endif
+
+ /* Stop zombies */
+ CatchChild();
+
+
+ FD_ZERO(&listen_set);
+
+ /* Just bind to 0.0.0.0 - accept connections
+ from anywhere. */
+ num_interfaces = 1;
+
+ /* open an incoming socket */
+ s = open_server_socket();
+ if (s == -1)
+ return -1;
+ fd_listenset = s;
+ FD_SET(s,&listen_set);
+
+ /* now accept incoming connections - forking a new process
+ for each incoming connection */
+ DEBUG(2,("waiting for a connection\n"));
+ while (1)
+ {
+ struct sockaddr_un addr;
+ int in_addrlen = sizeof(addr);
+ fd_set lfds;
+ int num;
+
+ memcpy((char *)&lfds, (char *)&listen_set,
+ sizeof(listen_set));
+
+ num = sys_select(256,&lfds,NULL, NULL);
+
+ if (num == -1 && errno == EINTR)
+ continue;
+
+ /* Find the sockets that are read-ready -
+ accept on these. */
+
+ s = -1;
+ if(FD_ISSET(fd_listenset,&lfds))
+ {
+ s = fd_listenset;
+ }
+
+ /* Clear this so we don't look at it again. */
+ FD_CLR(s,&lfds);
+
+ ClientMSRPC = accept(s,(struct sockaddr*)&addr,&in_addrlen);
+
+ if (ClientMSRPC == -1 && errno == EINTR)
+ continue;
+
+ if (ClientMSRPC == -1)
+ {
+ DEBUG(0,("open_sockets: accept: %s\n",
+ strerror(errno)));
+ continue;
+ }
+
+ if (ClientMSRPC != -1 && fork()==0)
+ {
+ /* Child code ... */
+
+ /* close the listening socket(s) */
+ close(fd_listenset);
+
+ /* close our standard file
+ descriptors */
+ close_low_fds();
+ am_parent = 0;
+
+ /* Reset global variables in util.c so
+ that client substitutions will be
+ done correctly in the process. */
+ reset_globals_after_fork();
+
+ return ClientMSRPC;
+ }
+ /* The parent doesn't need this socket */
+ close(ClientMSRPC);
+
+ /* Force parent to check log size after
+ * spawning child. Fix from
+ * klausr@ITAP.Physik.Uni-Stuttgart.De. The
+ * parent daemon will log to logserver.smb. It
+ * writes only two messages for each child
+ * started/finished. But each child writes,
+ * say, 50 messages also in logserver.smb,
+ * begining with the debug_count of the
+ * parent, before the child opens its own log
+ * file logserver.client. In a worst case
+ * scenario the size of logserver.smb would be
+ * checked after about 50*50=2500 messages
+ * (ca. 100kb).
+ * */
+ force_check_log_size();
+
+ } /* end while 1 */
+
+/* NOTREACHED */
+}
+
+
+/****************************************************************************
+this prevents zombie child processes
+****************************************************************************/
+BOOL reload_after_sighup = False;
+
+static void sig_hup(int sig)
+{
+ BlockSignals(True,SIGHUP);
+ DEBUG(0,("Got SIGHUP\n"));
+
+ /*
+ * Fix from <branko.cibej@hermes.si> here.
+ * We used to reload in the signal handler - this
+ * is a *BIG* no-no.
+ */
+
+ reload_after_sighup = True;
+ BlockSignals(False,SIGHUP);
+}
+
+
+
+#if DUMP_CORE
+/*******************************************************************
+prepare to dump a core file - carefully!
+********************************************************************/
+static BOOL dump_core(void)
+{
+ char *p;
+ pstring dname;
+ pstrcpy(dname,debugf);
+ if ((p=strrchr(dname,'/'))) *p=0;
+ pstrcat(dname,"/corefiles");
+ mkdir(dname,0700);
+ sys_chown(dname,getuid(),getgid());
+ chmod(dname,0700);
+ if (chdir(dname)) return(False);
+ umask(~(0700));
+
+#ifdef HAVE_GETRLIMIT
+#ifdef RLIMIT_CORE
+ {
+ struct rlimit rlp;
+ getrlimit(RLIMIT_CORE, &rlp);
+ rlp.rlim_cur = MAX(4*1024*1024,rlp.rlim_cur);
+ setrlimit(RLIMIT_CORE, &rlp);
+ getrlimit(RLIMIT_CORE, &rlp);
+ DEBUG(3,("Core limits now %d %d\n",
+ (int)rlp.rlim_cur,(int)rlp.rlim_max));
+ }
+#endif
+#endif
+
+
+ DEBUG(0,("Dumping core in %s\n",dname));
+ abort();
+ return(True);
+}
+#endif
+
+
+/****************************************************************************
+exit the server
+****************************************************************************/
+void exit_server(char *reason)
+{
+ static int firsttime=1;
+ extern char *last_inbuf;
+
+
+ if (!firsttime) exit(0);
+ firsttime = 0;
+
+ unbecome_vuser();
+ DEBUG(2,("Closing connections\n"));
+
+#ifdef WITH_DFS
+ if (dcelogin_atmost_once) {
+ dfs_unlogin();
+ }
+#endif
+
+ if (!reason) {
+ int oldlevel = DEBUGLEVEL;
+ DEBUGLEVEL = 10;
+ if (last_inbuf)
+ show_msg(last_inbuf);
+ DEBUGLEVEL = oldlevel;
+ DEBUG(0,("===============================================================\n"));
+#if DUMP_CORE
+ if (dump_core()) return;
+#endif
+ }
+
+ locking_end();
+
+ DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
+#ifdef MEM_MAN
+ {
+ extern FILE *dbf;
+ smb_mem_write_verbose(dbf);
+ dbgflush();
+ }
+#endif
+ exit(0);
+}
+
+
+
+/****************************************************************************
+ initialise connect, service and file structs
+****************************************************************************/
+static void init_structs(void)
+{
+#if 0
+ conn_init();
+#endif
+ init_rpc_pipe_hnd(); /* for RPC pipes */
+ if (!init_policy_hnd(MAX_SERVER_POLICY_HANDLES))
+ {
+ exit_server("could not allocate policy handles\n");
+ }
+}
+
+/****************************************************************************
+usage on the program
+****************************************************************************/
+static void usage(char *pname)
+{
+ DEBUG(0,("Incorrect program usage - are you sure the command line is correct?\n"));
+
+ printf("Usage: %s [-D] [-p port] [-d debuglevel] ", pname);
+ printf("[-l log basename] [-s services file]\n" );
+ printf("Version %s\n",VERSION);
+ printf("\t-D become a daemon\n");
+ printf("\t-p port listen on the specified port\n");
+ printf("\t-d debuglevel set the debuglevel\n");
+ printf("\t-l log basename. Basename for log/debug files\n");
+ printf("\t-s services file. Filename of services file\n");
+ printf("\t-P passive only\n");
+ printf("\t-a append to log file (default)\n");
+ printf("\t-o overwrite log file, don't append\n");
+ printf("\t-i scope NetBIOS scope to use (default none)\n");
+ printf("\n");
+}
+
+
+/****************************************************************************
+ main program
+****************************************************************************/
+int msrpc_main(int argc,char *argv[])
+{
+ extern BOOL append_log;
+ /* shall I run as a daemon */
+ BOOL is_daemon = False;
+ int opt;
+ extern char *optarg;
+ int ClientMSRPC = -1;
+ pipes_struct static_pipe;
+
+ pstrcpy(remote_machine, pipe_name);
+
+ charset_initialise();
+
+ /* make absolutely sure we run as root - to handle cases where people
+ are crazy enough to have it setuid */
+#ifdef HAVE_SETRESUID
+ setresuid(0,0,0);
+#else
+ setuid(0);
+ seteuid(0);
+ setuid(0);
+ seteuid(0);
+#endif
+
+ fault_setup((void (*)(void *))exit_server);
+ CatchSignal(SIGTERM , SIGNAL_CAST dflt_sig);
+
+ /* we are never interested in SIGPIPE */
+ BlockSignals(True,SIGPIPE);
+
+ /* we want total control over the permissions on created files,
+ so set our umask to 0 */
+ umask(0);
+
+ dos_GetWd(OriginalDir);
+
+ init_uid();
+
+ /* this is for people who can't start the program correctly */
+ while (argc > 1 && (*argv[1] != '-')) {
+ argv++;
+ argc--;
+ }
+
+ while ( EOF != (opt = getopt(argc, argv, "i:l:s:d:Dh?Paof:")) )
+ switch (opt) {
+ case 'i':
+ pstrcpy(scope,optarg);
+ break;
+
+ case 'P':
+ {
+ extern BOOL passive;
+ passive = True;
+ }
+ break;
+
+ case 's':
+ pstrcpy(servicesf,optarg);
+ break;
+
+ case 'l':
+ pstrcpy(debugf,optarg);
+ break;
+
+ case 'a':
+ append_log = True;
+ break;
+
+ case 'o':
+ append_log = False;
+ break;
+
+ case 'D':
+ is_daemon = True;
+ break;
+
+ case 'd':
+ if (*optarg == 'A')
+ DEBUGLEVEL = 10000;
+ else
+ DEBUGLEVEL = atoi(optarg);
+ break;
+
+ case 'h':
+ case '?':
+ usage(argv[0]);
+ exit(0);
+ break;
+
+ default:
+ usage(argv[0]);
+ exit(1);
+ }
+
+ reopen_logs();
+
+ DEBUG(1,( "%s version %s started.\n", argv[0], VERSION));
+ DEBUGADD(1,( "Copyright Andrew Tridgell 1992-1999\n"));
+
+ DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
+ (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));
+
+ if (sizeof(uint16) < 2 || sizeof(uint32) < 4) {
+ DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
+ exit(1);
+ }
+
+ get_myname(myhostname,NULL);
+
+ if (!reload_services(False))
+ return(-1);
+
+ init_structs();
+
+#ifdef WITH_PROFILE
+ if (!profile_setup(False)) {
+ DEBUG(0,("ERROR: failed to setup profiling\n"));
+ return -1;
+ }
+#endif
+
+ /*
+ * Set the machine NETBIOS name if not already
+ * set from the config file.
+ */
+ if (!*global_myname)
+ {
+ fstrcpy(global_myname, dns_to_netbios_name(myhostname));
+ }
+ strupper(global_myname);
+
+ codepage_initialise(lp_client_code_page());
+
+ CatchSignal(SIGHUP,SIGNAL_CAST sig_hup);
+
+ /* Setup the signals that allow the debug log level
+ to by dynamically changed. */
+
+ /* If we are using the malloc debug code we can't use
+ SIGUSR1 and SIGUSR2 to do debug level changes. */
+
+#ifndef MEM_MAN
+#if defined(SIGUSR1)
+ CatchSignal( SIGUSR1, SIGNAL_CAST sig_usr1 );
+#endif /* SIGUSR1 */
+
+#if defined(SIGUSR2)
+ CatchSignal( SIGUSR2, SIGNAL_CAST sig_usr2 );
+#endif /* SIGUSR2 */
+#endif /* MEM_MAN */
+
+ DEBUG(3,( "loaded services\n"));
+
+ if (!is_daemon && !is_a_socket(0)) {
+ DEBUG(0,("standard input is not a socket, assuming -D option\n"));
+ is_daemon = True;
+ }
+
+ if (is_daemon) {
+ DEBUG( 3, ( "Becoming a daemon.\n" ) );
+ become_daemon();
+ }
+
+ if (!directory_exist(lp_lockdir(), NULL)) {
+ mkdir(lp_lockdir(), 0755);
+ }
+
+ if (is_daemon) {
+ pidfile_create(pipe_name);
+ }
+
+ ClientMSRPC = open_sockets(is_daemon);
+ if (ClientMSRPC == -1)
+ {
+ exit_server("open socket failed");
+ }
+
+ if (!locking_init(0))
+ exit(1);
+
+ /* possibly reload the services file. */
+ reload_services(True);
+
+ if (*lp_rootdir()) {
+ if (sys_chroot(lp_rootdir()) == 0)
+ DEBUG(2,("Changed root to %s\n", lp_rootdir()));
+ }
+
+ msrpc_service_init();
+
+ ZERO_STRUCT(static_pipe);
+ fstrcpy(static_pipe.name, pipe_name);
+ if (msrpcd_init(ClientMSRPC, &static_pipe))
+ {
+ reload_services(False);
+ msrpcd_process(ClientMSRPC, &static_pipe);
+ }
+ if (ClientMSRPC != -1)
+ {
+ close(ClientMSRPC);
+ }
+
+ exit_server("normal exit");
+ return(0);
+}
diff --git a/source3/msrpc/msrpcd_process.c b/source3/msrpc/msrpcd_process.c
new file mode 100644
index 0000000000..240087be14
--- /dev/null
+++ b/source3/msrpc/msrpcd_process.c
@@ -0,0 +1,501 @@
+/*
+ Unix SMB/Netbios implementation.
+ Version 1.9.
+ process incoming packets - main loop
+ Copyright (C) Andrew Tridgell 1992-1998
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+#include "includes.h"
+
+extern int DEBUGLEVEL;
+
+time_t smb_last_time=(time_t)0;
+
+char *InBuffer = NULL;
+char *OutBuffer = NULL;
+char *last_inbuf = NULL;
+
+/*
+ * Size of data we can send to client. Set
+ * by the client for all protocols above CORE.
+ * Set by us for CORE protocol.
+ */
+int max_send = BUFFER_SIZE;
+/*
+ * Size of the data we can receive. Set by us.
+ * Can be modified by the max xmit parameter.
+ */
+int max_recv = BUFFER_SIZE;
+
+extern int last_message;
+extern pstring sesssetup_user;
+extern char *last_inbuf;
+extern char *InBuffer;
+extern char *OutBuffer;
+extern int smb_read_error;
+extern BOOL reload_after_sighup;
+extern int max_send;
+
+
+/****************************************************************************
+ Do a select on an two fd's - with timeout.
+
+ If a local udp message has been pushed onto the
+ queue (this can only happen during oplock break
+ processing) return this first.
+
+ If a pending smb message has been pushed onto the
+ queue (this can only happen during oplock break
+ processing) return this next.
+
+ If the first smbfd is ready then read an smb from it.
+ if the second (loopback UDP) fd is ready then read a message
+ from it and setup the buffer header to identify the length
+ and from address.
+ Returns False on timeout or error.
+ Else returns True.
+
+The timeout is in milli seconds
+****************************************************************************/
+
+static BOOL receive_message_or_msrpc(int c, char *buffer, int buffer_len,
+ int timeout, BOOL *got_smb)
+{
+ fd_set fds;
+ int selrtn;
+ struct timeval to;
+ int maxfd;
+
+ smb_read_error = 0;
+
+ *got_smb = False;
+
+ /*
+ * Check to see if we already have a message on the smb queue.
+ * If so - copy and return it.
+ */
+
+ /*
+ * Setup the select read fd set.
+ */
+
+ FD_ZERO(&fds);
+ FD_SET(c,&fds);
+ maxfd = 0;
+
+ to.tv_sec = timeout / 1000;
+ to.tv_usec = (timeout % 1000) * 1000;
+
+ selrtn = sys_select(MAX(maxfd,c)+1,&fds,NULL, timeout>0?&to:NULL);
+
+ /* Check if error */
+ if(selrtn == -1) {
+ /* something is wrong. Maybe the socket is dead? */
+ smb_read_error = READ_ERROR;
+ return False;
+ }
+
+ /* Did we timeout ? */
+ if (selrtn == 0) {
+ smb_read_error = READ_TIMEOUT;
+ return False;
+ }
+
+ if (FD_ISSET(c,&fds))
+ {
+ *got_smb = True;
+ return receive_smb(c, buffer, 0);
+ }
+ return False;
+}
+
+
+/*
+These flags determine some of the permissions required to do an operation
+
+Note that I don't set NEED_WRITE on some write operations because they
+are used by some brain-dead clients when printing, and I don't want to
+force write permissions on print services.
+*/
+#define AS_USER (1<<0)
+#define NEED_WRITE (1<<1)
+#define TIME_INIT (1<<2)
+#define CAN_IPC (1<<3)
+#define AS_GUEST (1<<5)
+#define QUEUE_IN_OPLOCK (1<<6)
+
+/*
+ define a list of possible SMB messages and their corresponding
+ functions. Any message that has a NULL function is unimplemented -
+ please feel free to contribute implementations!
+*/
+
+/****************************************************************************
+do a switch on the message type, and return the response size
+****************************************************************************/
+static int do_message(pipes_struct *p,
+ char *inbuf,char *outbuf,int size,int bufsize)
+{
+ static int pid= -1;
+
+ prs_struct pd;
+ int outsize = -1;
+
+ /* make a static data parsing structure from the api_fd_reply data */
+ prs_init(&pd, 0, 4, 0, True);
+ mem_create(pd.data, smb_base(inbuf), 0, smb_len(inbuf), 0, False);
+
+ if (pid == -1)
+ pid = getpid();
+
+ /* dce/rpc command */
+ if (rpc_to_smb(p, smb_base(inbuf), smb_len(inbuf)))
+ {
+ char *copy_into = smb_base(outbuf);
+ outsize = mem_buf_len(p->rsmb_pdu.data);
+ if (!mem_buf_copy(copy_into, p->rsmb_pdu.data, 0, outsize))
+ {
+ return -1;
+ }
+ mem_free_data(p->rsmb_pdu.data);
+ }
+
+ return outsize;
+}
+
+
+/****************************************************************************
+ construct a reply to the incoming packet
+****************************************************************************/
+static int construct_reply(pipes_struct *p,
+ char *inbuf,char *outbuf,int size,int bufsize)
+{
+ int outsize = 0;
+ smb_last_time = time(NULL);
+
+ outsize = do_message(p, inbuf,outbuf,size,bufsize) + 4;
+
+ if(outsize > 4)
+ _smb_setlen(outbuf,outsize - 4);
+ return(outsize);
+}
+
+
+/****************************************************************************
+ process an smb from the client - split out from the process() code so
+ it can be used by the oplock break code.
+****************************************************************************/
+static void process_msrpc(pipes_struct *p, int c, char *inbuf, char *outbuf)
+{
+ static int trans_num;
+ int32 len = smb_len(inbuf);
+ int nread = len + 4;
+
+ if (trans_num == 0) {
+ /* on the first packet, check the global hosts allow/ hosts
+ deny parameters before doing any parsing of the packet
+ passed to us by the client. This prevents attacks on our
+ parsing code from hosts not in the hosts allow list */
+ if (!check_access(c, lp_hostsallow(-1), lp_hostsdeny(-1))) {
+ /* send a negative session response "not listining on calling
+ name" */
+ DEBUG( 1, ( "Connection denied from %s\n",
+ client_addr(c) ) );
+ exit_server("connection denied");
+ }
+ }
+
+ DEBUG( 6, ( "got message of len 0x%x\n", len ) );
+ DEBUG( 3, ( "Transaction %d of length %d\n", trans_num, nread ) );
+
+ dump_data(10, inbuf, len);
+
+#ifdef WITH_VTP
+ if(trans_num == 1 && VT_Check(inbuf))
+ {
+ VT_Process();
+ return;
+ }
+#endif
+
+ nread = construct_reply(p, inbuf,outbuf,nread,max_send);
+
+ if(nread > 0)
+ {
+ dump_data(10, outbuf, nread);
+
+ if (nread != smb_len(outbuf) + 4)
+ {
+ DEBUG(0,("ERROR: Invalid message response size! %d %d\n",
+ nread, smb_len(outbuf)));
+ }
+ else
+ send_smb(c,outbuf);
+ }
+ trans_num++;
+}
+
+/****************************************************************************
+ reads user credentials from the socket
+****************************************************************************/
+BOOL get_user_creds(int c, struct user_creds *usr)
+{
+ pstring buf;
+ int rl;
+ uint32 len;
+ BOOL new_con = False;
+ uint32 status;
+
+ CREDS_CMD cmd;
+ prs_struct ps;
+
+ ZERO_STRUCTP(usr);
+ ZERO_STRUCT(cmd);
+ cmd.cred = usr;
+
+ DEBUG(10,("get_user_creds: first request\n"));
+
+ rl = read(c, &buf, sizeof(len));
+
+ if (rl != sizeof(len))
+ {
+ DEBUG(0,("Unable to read length\n"));
+ dump_data(0, buf, sizeof(len));
+ return False;
+ }
+
+ len = IVAL(buf, 0);
+
+ if (len > sizeof(buf))
+ {
+ DEBUG(0,("length %d too long\n", len));
+ return False;
+ }
+
+ rl = read(c, buf, len);
+
+ if (rl < 0)
+ {
+ DEBUG(0,("Unable to read from connection\n"));
+ return False;
+ }
+
+#ifdef DEBUG_PASSWORD
+ dump_data(100, buf, rl);
+#endif
+
+ /* make a static data parsing structure from the api_fd_reply data */
+ prs_init(&ps, 0, 4, 0, True);
+ mem_create(ps.data, buf, 0, len, 0, False);
+
+ if (!creds_io_cmd("creds", &cmd, &ps, 0))
+ {
+ DEBUG(0,("Unable to parse credentials\n"));
+ mem_free_data(ps.data);
+ return False;
+ }
+
+ mem_free_data(ps.data);
+
+ if (ps.offset != rl)
+ {
+ DEBUG(0,("Buffer size %d %d!\n", ps.offset, rl));
+ return False;
+ }
+
+ switch (cmd.command)
+ {
+ case AGENT_CMD_CON:
+ case AGENT_CMD_CON_ANON:
+ {
+ new_con = True;
+ break;
+ }
+ case AGENT_CMD_CON_REUSE:
+ {
+ new_con = True;
+ break;
+ }
+ default:
+ {
+ DEBUG(0,("unknown command %d\n", cmd.command));
+ return False;
+ }
+ }
+
+ status = new_con ? 0x0 : 0x1;
+
+ if (write(c, &status, sizeof(status)) !=
+ sizeof(status))
+ {
+ return False;
+ }
+
+ return new_con;
+}
+
+/****************************************************************************
+ initialise from pipe
+****************************************************************************/
+BOOL msrpcd_init(int c, pipes_struct *p)
+{
+ struct user_creds usr;
+ gid_t *groups = NULL;
+
+ if (!get_user_creds(c, &usr))
+ {
+ DEBUG(0,("authentication failed\n"));
+ free_user_creds(&usr);
+ return False;
+ }
+
+ if (usr.uxs.num_grps != 0)
+ {
+ int i;
+ groups = malloc(usr.uxs.num_grps * sizeof(groups[0]));
+ if (groups == NULL)
+ {
+ return False;
+ }
+ for (i = 0; i < usr.uxs.num_grps; i++)
+ {
+ groups[i] = (gid_t)usr.uxs.grps[i];
+ }
+ }
+
+ p->vuid = create_vuid(usr.uxs.uid, usr.uxs.gid,
+ usr.uxs.num_grps, groups,
+ usr.uxc.user_name,
+ usr.uxc.requested_name,
+ usr.uxc.real_name,
+ usr.uxc.guest,
+ usr.ntc.pwd.sess_key);
+
+ if (p->vuid == UID_FIELD_INVALID)
+ {
+ return False;
+ }
+
+ free_user_creds(&usr);
+
+ if (!become_vuser(p->vuid))
+ {
+ return False;
+ }
+
+ p->l = malloc(sizeof(*p->l));
+ if (p->l == NULL)
+ {
+ return False;
+ }
+
+ ZERO_STRUCTP(p->l);
+
+ return True;
+}
+
+/****************************************************************************
+ process commands from the client
+****************************************************************************/
+void msrpcd_process(int c, pipes_struct *p)
+{
+ InBuffer = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
+ OutBuffer = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
+ if ((InBuffer == NULL) || (OutBuffer == NULL))
+ return;
+
+ InBuffer += SMB_ALIGNMENT;
+ OutBuffer += SMB_ALIGNMENT;
+
+ max_recv = MIN(lp_maxxmit(),BUFFER_SIZE);
+
+ /* re-initialise the timezone */
+ TimeInit();
+
+ while (True)
+ {
+ int counter;
+ int service_load_counter = 0;
+ BOOL got_smb = False;
+
+ errno = 0;
+
+ for (counter=SMBD_SELECT_LOOP;
+ !receive_message_or_msrpc(c, InBuffer,BUFFER_SIZE,
+ SMBD_SELECT_LOOP*1000,&got_smb);
+ counter += SMBD_SELECT_LOOP)
+ {
+ time_t t;
+
+ if (counter > 365 * 3600) /* big number of seconds. */
+ {
+ counter = 0;
+ service_load_counter = 0;
+ }
+
+ if (smb_read_error == READ_EOF)
+ {
+ DEBUG(3,("end of file from client\n"));
+ return;
+ }
+
+ if (smb_read_error == READ_ERROR)
+ {
+ DEBUG(3,("receive_smb error (%s) exiting\n",
+ strerror(errno)));
+ return;
+ }
+
+ t = time(NULL);
+
+ /* check for smb.conf reload */
+ if (counter >= service_load_counter + SMBD_RELOAD_CHECK)
+ {
+ service_load_counter = counter;
+
+ /* reload services, if files have changed. */
+ reload_services(True);
+ }
+
+ /*
+ * If reload_after_sighup == True then we got a SIGHUP
+ * and are being asked to reload. Fix from <branko.cibej@hermes.si>
+ */
+
+ if (reload_after_sighup)
+ {
+ DEBUG(0,("Reloading services after SIGHUP\n"));
+ reload_services(False);
+ reload_after_sighup = False;
+ /*
+ * Use this as an excuse to print some stats.
+ */
+ }
+
+ /* automatic timeout if all connections are closed */
+ if (counter >= IDLE_CLOSED_TIMEOUT)
+ {
+ DEBUG( 2, ( "Closing idle connection\n" ) );
+ return;
+ }
+
+ }
+
+ if(got_smb)
+ process_msrpc(p, c, InBuffer, OutBuffer);
+ }
+}