Skip to contentSkip to conversation

slirp: smb fixes and cmd: target for guestfwd

GIT PULL New Jan Kiszka 2 participants 6 messages Jul 9, 2012 Networking

lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

The following changes since commit 84988cf910a6881f2180fdcec516b60f8f0dc8c4:

bitops.h: Add functions to extract and deposit bitfields (2012-07-07 09:07:01 +0000)

are available in the git repository at:
git://git.kiszka.org/qemu.git queues/slirp

Alexander Graf (1):
slirp: add 'cmd:' target for guestfwd

Dunrong Huang (1):
slirp: Ensure smbd and shared directory exist when enable smb

Jan Kiszka (2):
slirp: Enforce host-side user of smb share
slirp: Improve error reporting of inaccessible smb directories

 net/slirp.c     |   70 ++++++++++++++++++++++++++++++++++++++++---------------
 qemu-options.hx |   22 ++++++++++++++++-
 2 files changed, 72 insertions(+), 20 deletions(-)
CC Alexander Graf <[email protected]>
CC Dunrong Huang <[email protected]>

Alexander Graf (1):
slirp: add 'cmd:' target for guestfwd

Dunrong Huang (1):
slirp: Ensure smbd and shared directory exist when enable smb

Jan Kiszka (2):
slirp: Enforce host-side user of smb share
slirp: Improve error reporting of inaccessible smb directories

 net/slirp.c     |   70 ++++++++++++++++++++++++++++++++++++++++---------------
 qemu-options.hx |   22 ++++++++++++++++-
 2 files changed, 72 insertions(+), 20 deletions(-)
Signature · 1 line
1.7.3.4
author
#1 lore raw

PATCH 1/4 slirp: Enforce host-side user of smb share

Windows 7 (and possibly other versions) cannot connect to the samba share if the exported host directory is not world-readable. This can be resolved by forcing the username used for access checks to the one under which QEMU and smbd are running.

---
 net/slirp.c |   14 ++++++++++++--
 1 files changed, 12 insertions(+), 2 deletions(-)
net/slirp.c +12 -2
index 37b6ccf..a43b576 100644
@@ -26,6 +26,7 @@
 #include "config-host.h"
 
 #ifndef _WIN32
+#include <pwd.h>
 #include <sys/wait.h>
 #endif
 #include "net.h"
@@ -487,8 +488,15 @@ static int slirp_smb(SlirpState* s, const char *exported_dir,
     static int instance;
     char smb_conf[128];
     char smb_cmdline[128];
+    struct passwd *passwd;
     FILE *f;
 
+    passwd = getpwuid(geteuid());
+    if (!passwd) {
+        error_report("failed to retrieve user name");
+        return -1;
+    }
+
     snprintf(s->smb_dir, sizeof(s->smb_dir), "/tmp/qemu-smb.%ld-%d",
              (long)getpid(), instance++);
     if (mkdir(s->smb_dir, 0700) < 0) {
@@ -517,14 +525,16 @@ static int slirp_smb(SlirpState* s, const char *exported_dir,
             "[qemu]\n"
             "path=%s\n"
             "read only=no\n"
-            "guest ok=yes\n",
+            "guest ok=yes\n"
+            "force user=%s\n",
             s->smb_dir,
             s->smb_dir,
             s->smb_dir,
             s->smb_dir,
             s->smb_dir,
             s->smb_dir,
-            exported_dir
+            exported_dir,
+            passwd->pw_name
             );
     fclose(f);
 
Signature · 1 line
1.7.3.4
author
#2 lore raw

PATCH 2/4 slirp: add 'cmd:' target for guestfwd

In reply to Jan Kiszka

From: Alexander Graf <[email protected]>

When using guestfwd=, Qemu only connects the virtual server's TCP port to a single chardev. This is useless in most cases, as we usually want to have more than a single connection from the guest to the outside world.

This patch adds a new cmd: target to guestfwd= that allows for execution of a command on every TCP connection. This leverages the same code as the -smb parameter, just that here the command is user defined.

---
 net/slirp.c     |   44 +++++++++++++++++++++++++++-----------------
 qemu-options.hx |   22 +++++++++++++++++++++-
 2 files changed, 48 insertions(+), 18 deletions(-)
net/slirp.c +27 -17
index a43b576..180147e 100644
@@ -626,25 +626,35 @@ static int slirp_guestfwd(SlirpState *s, const char *config_str,
 
     fwd = g_malloc(sizeof(struct GuestFwd));
     snprintf(buf, sizeof(buf), "guestfwd.tcp.%d", port);
-    fwd->hd = qemu_chr_new(buf, p, NULL);
-    if (!fwd->hd) {
-        error_report("could not open guest forwarding device '%s'", buf);
-        g_free(fwd);
-        return -1;
-    }
 
-    if (slirp_add_exec(s->slirp, 3, fwd->hd, &server, port) < 0) {
-        error_report("conflicting/invalid host:port in guest forwarding "
-                     "rule '%s'", config_str);
-        g_free(fwd);
-        return -1;
-    }
-    fwd->server = server;
-    fwd->port = port;
-    fwd->slirp = s->slirp;
+    if ((strlen(p) > 4) && !strncmp(p, "cmd:", 4)) {
+        if (slirp_add_exec(s->slirp, 0, &p[4], &server, port) < 0) {
+            error_report("conflicting/invalid host:port in guest forwarding "
+                         "rule '%s'", config_str);
+            g_free(fwd);
+            return -1;
+        }
+    } else {
+        fwd->hd = qemu_chr_new(buf, p, NULL);
+        if (!fwd->hd) {
+            error_report("could not open guest forwarding device '%s'", buf);
+            g_free(fwd);
+            return -1;
+        }
+
+        if (slirp_add_exec(s->slirp, 3, fwd->hd, &server, port) < 0) {
+            error_report("conflicting/invalid host:port in guest forwarding "
+                         "rule '%s'", config_str);
+            g_free(fwd);
+            return -1;
+        }
+        fwd->server = server;
+        fwd->port = port;
+        fwd->slirp = s->slirp;
 
-    qemu_chr_add_handlers(fwd->hd, guestfwd_can_read, guestfwd_read,
-                          NULL, fwd);
+        qemu_chr_add_handlers(fwd->hd, guestfwd_can_read, guestfwd_read,
+                              NULL, fwd);
+    }
     return 0;
 
  fail_syntax:
qemu-options.hx +21 -1
index 8b66264..ecf7ca1 100644
@@ -1421,8 +1421,28 @@ Then when you use on the host @code{telnet localhost 5555}, you
 connect to the guest telnet server.
 
 @item guestfwd=[tcp]:@var{server}:@var{port}-@var{dev}
+@item guestfwd=[tcp]:@var{server}:@var{port}-@var{cmd:command}
 Forward guest TCP connections to the IP address @var{server} on port @var{port}
-to the character device @var{dev}. This option can be given multiple times.
+to the character device @var{dev} or to a program executed by @var{cmd:command}
+which gets spawned for each connection. This option can be given multiple times.
+
+You can either use a chardev directly and have that one used throughout Qemu's
+lifetime, like in the following example:
+
+@example
+# open 10.10.1.1:4321 on bootup, connect 10.0.2.100:1234 to it whenever
+# the guest accesses it
+qemu -net user,guestfwd=tcp:10.0.2.100:1234-tcp:10.10.1.1:4321 [...]
+@end example
+
+Or you can execute a command on every TCP connection established by the guest,
+so that Qemu behaves similar to an inetd process for that virtual server:
+
+@example
+# call "netcat 10.10.1.1 4321" on every TCP connection to 10.0.2.100:1234
+# and connect the TCP stream to its stdin/stdout
+qemu -net 'user,guestfwd=tcp:10.0.2.100:1234-cmd:netcat 10.10.1.1 4321'
+@end example
 
 @end table
 
Signature · 1 line
1.7.3.4
author
#3 lore raw

PATCH 3/4 slirp: Ensure smbd and shared directory exist when enable smb

In reply to Jan Kiszka

From: Dunrong Huang <[email protected]>

Users may pass the following parameters to qemu:

    $ qemu-kvm -net nic -net user,smb= ...
    $ qemu-kvm -net nic -net user,smb ...
    $ qemu-kvm -net nic -net user,smb=bad_directory ...

In these cases, qemu started successfully while samba server failed to start. Users will confuse since samba server failed silently without any indication of what it did wrong.

To avoid it, we check whether the shared directory exist and if users have permission to access this directory when QEMU's "built-in" SMB server is enabled.

---
 net/slirp.c |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)
net/slirp.c +12 -0
index 180147e..eb80889 100644
@@ -497,6 +497,18 @@ static int slirp_smb(SlirpState* s, const char *exported_dir,
         return -1;
     }
 
+    if (access(CONFIG_SMBD_COMMAND, F_OK)) {
+        error_report("could not find '%s', please install it",
+                     CONFIG_SMBD_COMMAND);
+        return -1;
+    }
+
+    if (access(exported_dir, R_OK | X_OK)) {
+        error_report("no such directory '%s', or you do not have permission "
+                     "to access it, please check it", exported_dir);
+        return -1;
+    }
+
     snprintf(s->smb_dir, sizeof(s->smb_dir), "/tmp/qemu-smb.%ld-%d",
              (long)getpid(), instance++);
     if (mkdir(s->smb_dir, 0700) < 0) {
Signature · 1 line
1.7.3.4
author
#4 lore raw

PATCH 4/4 slirp: Improve error reporting of inaccessible smb directories

In reply to Jan Kiszka

Instead of guessing, print the error code returned by access.

---
 net/slirp.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
net/slirp.c +2 -2
index eb80889..b82eab0 100644
@@ -504,8 +504,8 @@ static int slirp_smb(SlirpState* s, const char *exported_dir,
     }
 
     if (access(exported_dir, R_OK | X_OK)) {
-        error_report("no such directory '%s', or you do not have permission "
-                     "to access it, please check it", exported_dir);
+        error_report("error accessing shared directory '%s': %s",
+                     exported_dir, strerror(errno));
         return -1;
     }
 
Signature · 1 line
1.7.3.4
#5 lore raw
In reply to Jan Kiszka
Quoted 6 lines · Jan Kiszka

The following changes since commit 84988cf910a6881f2180fdcec516b60f8f0dc8c4:

bitops.h: Add functions to extract and deposit bitfields (2012-07-07 09:07:01 +0000)

are available in the git repository at:
git://git.kiszka.org/qemu.git queues/slirp

Pulled. Thanks.

Regards,

Anthony Liguori

Quoted 33 lines

Alexander Graf (1):
slirp: add 'cmd:' target for guestfwd

Dunrong Huang (1):
slirp: Ensure smbd and shared directory exist when enable smb

Jan Kiszka (2):
slirp: Enforce host-side user of smb share
slirp: Improve error reporting of inaccessible smb directories

  net/slirp.c     |   70 ++++++++++++++++++++++++++++++++++++++++---------------
  qemu-options.hx |   22 ++++++++++++++++-
  2 files changed, 72 insertions(+), 20 deletions(-)
CC Alexander Graf<[email protected]>
CC Dunrong Huang<[email protected]>

Alexander Graf (1):
slirp: add 'cmd:' target for guestfwd

Dunrong Huang (1):
slirp: Ensure smbd and shared directory exist when enable smb

Jan Kiszka (2):
slirp: Enforce host-side user of smb share
slirp: Improve error reporting of inaccessible smb directories

  net/slirp.c     |   70 ++++++++++++++++++++++++++++++++++++++++---------------
  qemu-options.hx |   22 ++++++++++++++++-
  2 files changed, 72 insertions(+), 20 deletions(-)