smb-agent improvements. added -D (daemon) option. smb agent is
authorLuke Leighton <lkcl@samba.org>
Fri, 3 Dec 1999 19:55:34 +0000 (19:55 +0000)
committerLuke Leighton <lkcl@samba.org>
Fri, 3 Dec 1999 19:55:34 +0000 (19:55 +0000)
restricted to connections from the current user (socket is created
with current user uid).
(This used to be commit 5af076e4b7ee13eebe0b89748e3f5a1ef21f8c73)

source3/libsmb/clientgen.c
source3/utils/smb-agent.c

index 218ab67758f0bf8ce3a70dcbe4c877d1974fc4e4..176be9948be12faeb79d9a9a54297f8d3701d077 100644 (file)
@@ -2938,6 +2938,7 @@ static int cli_init_redirect(struct cli_state *cli,
        struct sockaddr_un sa;
        fstring ip_name;
        struct cli_state cli_redir;
+       fstring path;
 
        pstring data;
        uint32 len;
@@ -2945,6 +2946,8 @@ static int cli_init_redirect(struct cli_state *cli,
        char *in = cli->inbuf;
        char *out = cli->outbuf;
 
+       slprintf(path, sizeof(path)-1, "/tmp/smb-agent/smb.%d", getuid());
+
        if (strequal(srv_name, "*SMBSERVER"))
        {
                fstrcpy(ip_name, "\\\\");
@@ -2962,8 +2965,7 @@ static int cli_init_redirect(struct cli_state *cli,
 
        ZERO_STRUCT(sa);
        sa.sun_family = AF_UNIX;
-       safe_strcpy(sa.sun_path, "/tmp/smb-agent/smb.sock",
-                   sizeof(sa.sun_path)-1);
+       safe_strcpy(sa.sun_path, path, sizeof(sa.sun_path)-1);
 
        DEBUG(10, ("socket open succeeded.  file name: %s\n", sa.sun_path));
 
index 52feda2bea5353a9c89cab8245ddd99ea2a66f89..8c039dfc86d1d346becb24810a98acdb8d9e1d9e 100644 (file)
@@ -218,11 +218,15 @@ static void agent_child(int c)
                                        DEBUG(0,("client closed connection\n"));
                                        exit(0);
                                }
-                               if (!send_smb(s->fd, packet))
+                               /* ignore keep-alives */
+                               if (CVAL(packet, 0) != 0x85)
                                {
-                                       DEBUG(0,("server is dead\n"));
-                                       exit(1);
-                               }                       
+                                       if (!send_smb(s->fd, packet))
+                                       {
+                                               DEBUG(0,("server is dead\n"));
+                                               exit(1);
+                                       }                       
+                               }
                        }
                }
                if (s != NULL && FD_ISSET(s->fd, &fds))
@@ -258,12 +262,14 @@ static void start_agent(void)
 {
        int s, c;
        struct sockaddr_un sa;
+       fstring path;
+       slprintf(path, sizeof(path)-1, "/tmp/smb-agent/smb.%d", getuid());
 
        CatchChild();
 
        /* start listening on unix socket */
-       
-       mkdir("/tmp/smb-agent", 700);
+       mkdir("/tmp/smb-agent", 777);
+
        s = socket(AF_UNIX, SOCK_STREAM, 0);
 
        if (s < 0)
@@ -274,27 +280,35 @@ static void start_agent(void)
 
        ZERO_STRUCT(sa);
        sa.sun_family = AF_UNIX;
-       safe_strcpy(sa.sun_path, "/tmp/smb-agent/smb.sock",
-                   sizeof(sa.sun_path)-1);
+       safe_strcpy(sa.sun_path, path, sizeof(sa.sun_path)-1);
 
        if (bind(s, (struct sockaddr*) &sa, sizeof(sa)) < 0)
        {
                fprintf(stderr, "socket bind to %s failed\n", sa.sun_path);
                close(s);
-               remove("/tmp/smb-agent/smb.sock");
+               remove(path);
+               exit(1);
+       }
+
+       if (chmod(path, S_IRUSR|S_IWUSR|S_ISVTX) < 0)
+       {
+               fprintf(stderr, "chmod on %s failed\n", sa.sun_path);
+               close(s);
+               remove(path);
                exit(1);
        }
 
-       if (s == -1) {
+       if (s == -1)
+       {
                DEBUG(0,("bind failed\n"));
-               remove("/tmp/smb-agent/smb.sock");
+               remove(path);
                exit(1);
        }
 
        if (listen(s, 5) == -1)
        {
                DEBUG(0,("listen failed\n"));
-               remove("/tmp/smb-agent/smb.sock");
+               remove(path);
        }
 
        while (1)
@@ -325,23 +339,65 @@ static void start_agent(void)
        }
 }
 
+/****************************************************************************
+usage on the program
+****************************************************************************/
+static void usage(char *pname)
+{
+  printf("Usage: %s [-D]", pname);
+
+  printf("\nVersion %s\n",VERSION);
+  printf("\t-D                 run as a daemon\n");
+  printf("\t-h                 usage\n");
+  printf("\n");
+}
 
 int main(int argc, char *argv[])
 {
        pstring configfile;
+       BOOL is_daemon = False;
+       int opt;
+       extern pstring debugf;
 
        TimeInit();
 
-       setup_logging(argv[0],True);
+       pstrcpy(configfile,CONFIGFILE);
+       while ((opt = getopt(argc, argv, "Dh")) != EOF)
+       {
+               switch (opt)
+               {
+                       case 'D':
+                       {
+                               is_daemon = True;
+                               break;
+                       }
+                       case 'h':
+                       default:
+                       {
+                               usage(argv[0]);
+                               break;
+                       }
+               }
+       }
+
+       slprintf(debugf, sizeof(debugf)-1, "log.%s", argv[0]);
+       setup_logging(argv[0], !is_daemon);
   
        charset_initialise();
 
-       pstrcpy(configfile,CONFIGFILE);
-       if (!lp_load(configfile,True,False,False)) {
+       if (!lp_load(configfile,True,False,False))
+       {
                DEBUG(0,("Unable to load config file\n"));
        }
 
+       if (is_daemon)
+       {
+               DEBUG(0,("%s: becoming daemon\n", argv[0]));
+               become_daemon();
+       }
+
        start_agent();
+
        return 0;
 }