r293: Handle incorrect user- or groupnames correctly
[nbd.git] / nbd-server.c
index d7109c6..f9691df 100644 (file)
@@ -147,7 +147,8 @@ gchar* rungroup=NULL;
 #define F_COPYONWRITE 4          /**< flag to tell us a file is exported using
                            copyonwrite */
 #define F_AUTOREADONLY 8  /**< flag to tell us a file is set to autoreadonly */
-#define F_SPARSE 16
+#define F_SPARSE 16      /**< flag to tell us copyronwrite should use a sparse file */
+#define F_SDP 32         /**< flag to tell us the export should be done using the Socket Direct Protocol for RDMA */
 GHashTable *children;
 char pidfname[256]; /**< name of our PID file */
 char pidftemplate[256]; /**< template to be used for the filename of the PID file */
@@ -407,6 +408,7 @@ SERVER* cmdline(int argc, char *argv[]) {
        }
        serve=g_new0(SERVER, 1);
        serve->authname = g_strdup(default_authname);
+       serve->virtstyle=VIRT_IPLIT;
        while((c=getopt_long(argc, argv, "-a:C:cl:mo:rp:", long_options, &i))>=0) {
                switch (c) {
                case 1:
@@ -497,6 +499,7 @@ typedef enum {
        CFILE_MISSING_GENERIC,  /**< The (required) group "generic" is missing */
        CFILE_KEY_MISSING,      /**< A (required) key is missing */
        CFILE_VALUE_INVALID,    /**< A value is syntactically invalid */
+       CFILE_VALUE_UNSUPPORTED,/**< A value is not supported in this build */
        CFILE_PROGERR           /**< Programmer error */
 } CFILE_ERRORS;
 
@@ -542,8 +545,9 @@ GArray* parse_cfile(gchar* f, GError** e) {
                { "copyonwrite", FALSE, PARAM_BOOL,     NULL, F_COPYONWRITE },
                { "autoreadonly", FALSE, PARAM_BOOL,    NULL, F_AUTOREADONLY },
                { "sparse_cow", FALSE,  PARAM_BOOL,     NULL, F_SPARSE },
+               { "sdp",        FALSE,  PARAM_BOOL,     NULL, F_SDP },
        };
-       const int lp_size=11;
+       const int lp_size=14;
        PARAM gp[] = {
                { "user",       FALSE, PARAM_STRING,    &runuser,       0 },
                { "group",      FALSE, PARAM_STRING,    &rungroup,      0 },
@@ -586,8 +590,9 @@ GArray* parse_cfile(gchar* f, GError** e) {
                lp[6].target=&(s.prerun);
                lp[7].target=&(s.postrun);
                lp[8].target=lp[9].target=lp[10].target=
-                               lp[11].target=lp[12].target=&(s.flags);
-               
+                               lp[11].target=lp[12].target=
+                               lp[13].target=&(s.flags);
+
                /* After the [generic] group, start parsing exports */
                if(i==1) {
                        p=lp;
@@ -674,6 +679,14 @@ GArray* parse_cfile(gchar* f, GError** e) {
                if(i>0) {
                        g_array_append_val(retval, s);
                }
+#ifndef WITH_SDP
+               if(s.flags & F_SDP) {
+                       g_set_error(e, errdomain, CFILE_VALUE_UNSUPPORTED, "This nbd-server was built without support for SDP, yet group %s uses it", groups[i]);
+                       g_array_free(retval, TRUE);
+                       g_key_file_free(cfile);
+                       return NULL;
+               }
+#endif
        }
        return retval;
 }
@@ -1276,6 +1289,9 @@ int do_run(gchar* command, gchar* file) {
  * @param client a connected client
  **/
 void serveconnection(CLIENT *client) {
+       if(do_run(client->server->prerun, client->exportname)) {
+               exit(EXIT_FAILURE);
+       }
        setupexport(client);
 
        if (client->server->flags & F_COPYONWRITE) {
@@ -1284,9 +1300,7 @@ void serveconnection(CLIENT *client) {
 
        setmysockopt(client->net);
 
-       if(!do_run(client->server->prerun, client->exportname)) {
-               mainloop(client);
-       }
+       mainloop(client);
        do_run(client->server->postrun, client->exportname);
 }
 
@@ -1398,12 +1412,20 @@ void setup_serve(SERVER *serve) {
        struct sigaction sa;
        int addrinlen = sizeof(addrin);
        int sock_flags;
+       int af;
 #ifndef sun
        int yes=1;
 #else
        char yes='1';
 #endif /* sun */
-       if ((serve->socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
+
+       af = AF_INET;
+#ifdef WITH_SDP
+       if ((serve->flags) && F_SDP) {
+               af = AF_INET_SDP;
+       }
+#endif
+       if ((serve->socket = socket(af, SOCK_STREAM, IPPROTO_TCP)) < 0)
                err("socket: %m");
 
        /* lose the pesky "Address already in use" error message */
@@ -1424,6 +1446,11 @@ void setup_serve(SERVER *serve) {
 
        DEBUG("Waiting for connections... bind, ");
        addrin.sin_family = AF_INET;
+#ifdef WITH_SDP
+       if(serve->flags & F_SDP) {
+               addrin.sin_family = AF_INET_SDP;
+       }
+#endif
        addrin.sin_port = htons(serve->port);
        addrin.sin_addr.s_addr = 0;
        if (bind(serve->socket, (struct sockaddr *) &addrin, addrinlen) < 0)
@@ -1524,7 +1551,8 @@ int serveloop(GArray* servers) {
                                        }
                                        /* child */
                                        g_hash_table_destroy(children);
-                                       for(i=0;i<servers->len,serve=(g_array_index(servers, SERVER*, i));i++) {
+                                       for(i=0;i<servers->len;i++) {
+                                               serve=g_array_index(servers, SERVER*, i);
                                                close(serve->socket);
                                        }
                                        /* FALSE does not free the
@@ -1552,11 +1580,19 @@ void dousers(void) {
        struct group *gr;
        if(rungroup) {
                gr=getgrnam(rungroup);
+               if(!gr) {
+                       g_message("Invalid group name: %s", rungroup);
+                       exit(EXIT_FAILURE);
+               }
                if(setgid(gr->gr_gid)<0)
                        msg3(LOG_DEBUG, "Could not set GID: %s", strerror(errno));
        }
        if(runuser) {
                pw=getpwnam(runuser);
+               if(!pw) {
+                       g_message("Invalid user name: %s", runuser);
+                       exit(EXIT_FAILURE);
+               }
                if(setuid(pw->pw_uid)<0)
                        msg3(LOG_DEBUG, "Could not set UID: %s", strerror(errno));
        }