+ * Error codes for config file parsing
+ **/
+typedef enum {
+ CFILE_NOTFOUND, /**< The configuration file is not found */
+ 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_NO_EXPORTS, /**< A config file was specified that does not
+ define any exports */
+ CFILE_INCORRECT_PORT, /**< The reserved port was specified for an
+ old-style export. */
+} CFILE_ERRORS;
+
+/**
+ * Remove a SERVER from memory. Used from the hash table
+ **/
+void remove_server(gpointer s) {
+ SERVER *server;
+
+ server=(SERVER*)s;
+ g_free(server->exportname);
+ if(server->authname)
+ g_free(server->authname);
+ if(server->listenaddr)
+ g_free(server->listenaddr);
+ if(server->prerun)
+ g_free(server->prerun);
+ if(server->postrun)
+ g_free(server->postrun);
+ g_free(server);
+}
+
+/**
+ * duplicate server
+ * @param s the old server we want to duplicate
+ * @return new duplicated server
+ **/
+SERVER* dup_serve(SERVER *s) {
+ SERVER *serve = NULL;
+
+ serve=g_new0(SERVER, 1);
+ if (serve == NULL)
+ return NULL;
+
+ if (s->exportname)
+ serve->exportname = g_strdup(s->exportname);
+
+ serve->expected_size = s->expected_size;
+
+ if (s->listenaddr)
+ serve->listenaddr = g_strdup(s->listenaddr);
+
+ serve->port = s->port;
+
+ if (s->authname)
+ serve->authname = strdup(s->authname);
+
+ serve->flags = s->flags;
+ serve->socket = serve->socket;
+ serve->socket_family = serve->socket_family;
+ serve->cidrlen = s->cidrlen;
+
+ if (s->prerun)
+ serve->prerun = g_strdup(s->prerun);
+
+ if (s->postrun)
+ serve->postrun = g_strdup(s->postrun);
+
+ return serve;
+}
+
+/**
+ * append new server to array
+ * @param s server
+ * @param a server array
+ * @return 0 success, -1 error
+ */
+int append_serve(SERVER *s, GArray *a) {
+ SERVER *ns = NULL;
+ struct addrinfo hints;
+ struct addrinfo *ai = NULL;
+ struct addrinfo *rp = NULL;
+ char host[NI_MAXHOST];
+ gchar *port = NULL;
+ int e;
+ int ret;
+
+ if(!s) {
+ err("Invalid parsing server");
+ return -1;
+ }
+
+ port = g_strdup_printf("%d", s->port);
+
+ memset(&hints,'\0',sizeof(hints));
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;
+ hints.ai_protocol = IPPROTO_TCP;
+
+ e = getaddrinfo(s->listenaddr, port, &hints, &ai);
+
+ if (port)
+ g_free(port);
+
+ if(e == 0) {
+ for (rp = ai; rp != NULL; rp = rp->ai_next) {
+ e = getnameinfo(rp->ai_addr, rp->ai_addrlen, host, sizeof(host), NULL, 0, NI_NUMERICHOST);
+
+ if (e != 0) { // error
+ fprintf(stderr, "getnameinfo: %s\n", gai_strerror(e));
+ continue;
+ }
+
+ // duplicate server and set listenaddr to resolved IP address
+ ns = dup_serve (s);
+ if (ns) {
+ ns->listenaddr = g_strdup(host);
+ ns->socket_family = rp->ai_family;
+ g_array_append_val(a, *ns);
+ free(ns);
+ ns = NULL;
+ }
+ }
+
+ ret = 0;
+ } else {
+ fprintf(stderr, "getaddrinfo failed on listen host/address: %s (%s)\n", s->listenaddr ? s->listenaddr : "any", gai_strerror(e));
+ ret = -1;
+ }
+
+ if (ai)
+ freeaddrinfo(ai);
+
+ return ret;
+}
+
+/**
+ * Parse the config file.
+ *
+ * @param f the name of the config file
+ * @param e a GError. @see CFILE_ERRORS for what error values this function can
+ * return.
+ * @return a Array of SERVER* pointers, If the config file is empty or does not
+ * exist, returns an empty GHashTable; if the config file contains an
+ * error, returns NULL, and e is set appropriately
+ **/
+GArray* parse_cfile(gchar* f, GError** e) {
+ const char* DEFAULT_ERROR = "Could not parse %s in group %s: %s";
+ const char* MISSING_REQUIRED_ERROR = "Could not find required value %s in group %s: %s";
+ SERVER s;
+ gchar *virtstyle=NULL;
+ PARAM lp[] = {
+ { "exportname", TRUE, PARAM_STRING, NULL, 0 },
+ { "port", TRUE, PARAM_INT, NULL, 0 },
+ { "authfile", FALSE, PARAM_STRING, NULL, 0 },
+ { "filesize", FALSE, PARAM_INT, NULL, 0 },
+ { "virtstyle", FALSE, PARAM_STRING, NULL, 0 },
+ { "prerun", FALSE, PARAM_STRING, NULL, 0 },
+ { "postrun", FALSE, PARAM_STRING, NULL, 0 },
+ { "readonly", FALSE, PARAM_BOOL, NULL, F_READONLY },
+ { "multifile", FALSE, PARAM_BOOL, NULL, F_MULTIFILE },
+ { "copyonwrite", FALSE, PARAM_BOOL, NULL, F_COPYONWRITE },
+ { "sparse_cow", FALSE, PARAM_BOOL, NULL, F_SPARSE },
+ { "sdp", FALSE, PARAM_BOOL, NULL, F_SDP },
+ { "sync", FALSE, PARAM_BOOL, NULL, F_SYNC },
+ { "listenaddr", FALSE, PARAM_STRING, NULL, 0 },
+ };
+ const int lp_size=sizeof(lp)/sizeof(PARAM);
+ int do_oldstyle;
+ PARAM gp[] = {
+ { "user", FALSE, PARAM_STRING, &runuser, 0 },
+ { "group", FALSE, PARAM_STRING, &rungroup, 0 },
+ { "oldstyle", FALSE, PARAM_BOOL, &do_oldstyle, 1 },
+ { "listenaddr", FALSE, PARAM_STRING, &modern_listen, 0 },
+ };
+ PARAM* p=gp;
+ int p_size=sizeof(gp)/sizeof(PARAM);
+ GKeyFile *cfile;
+ GError *err = NULL;
+ const char *err_msg=NULL;
+ GQuark errdomain;
+ GArray *retval=NULL;
+ gchar **groups;
+ gboolean value;
+ gchar* startgroup;
+ gint i;
+ gint j;
+
+ errdomain = g_quark_from_string("parse_cfile");
+ cfile = g_key_file_new();
+ retval = g_array_new(FALSE, TRUE, sizeof(SERVER));
+ if(!g_key_file_load_from_file(cfile, f, G_KEY_FILE_KEEP_COMMENTS |
+ G_KEY_FILE_KEEP_TRANSLATIONS, &err)) {
+ g_set_error(e, errdomain, CFILE_NOTFOUND, "Could not open config file %s.", f);
+ g_key_file_free(cfile);
+ return retval;
+ }
+ startgroup = g_key_file_get_start_group(cfile);
+ if(!startgroup || strcmp(startgroup, "generic")) {
+ g_set_error(e, errdomain, CFILE_MISSING_GENERIC, "Config file does not contain the [generic] group!");
+ g_key_file_free(cfile);
+ return NULL;
+ }
+ groups = g_key_file_get_groups(cfile, NULL);
+ for(i=0;groups[i];i++) {
+ memset(&s, '\0', sizeof(SERVER));
+ lp[0].target=&(s.exportname);
+ lp[1].target=&(s.port);
+ lp[2].target=&(s.authname);
+ lp[3].target=&(s.expected_size);
+ lp[4].target=&(virtstyle);
+ lp[5].target=&(s.prerun);
+ lp[6].target=&(s.postrun);
+ lp[7].target=lp[8].target=lp[9].target=
+ lp[10].target=lp[11].target=
+ lp[12].target=&(s.flags);
+ lp[13].target=&(s.listenaddr);
+
+ /* After the [generic] group, start parsing exports */
+ if(i==1) {
+ p=lp;
+ p_size=lp_size;
+ }
+ for(j=0;j<p_size;j++) {
+ g_assert(p[j].target != NULL);
+ g_assert(p[j].ptype==PARAM_INT||p[j].ptype==PARAM_STRING||p[j].ptype==PARAM_BOOL);
+ switch(p[j].ptype) {
+ case PARAM_INT:
+ *((gint*)p[j].target) =
+ g_key_file_get_integer(cfile,
+ groups[i],
+ p[j].paramname,
+ &err);
+ break;
+ case PARAM_STRING:
+ *((gchar**)p[j].target) =
+ g_key_file_get_string(cfile,
+ groups[i],
+ p[j].paramname,
+ &err);
+ break;
+ case PARAM_BOOL:
+ value = g_key_file_get_boolean(cfile,
+ groups[i],
+ p[j].paramname, &err);
+ if(!err) {
+ if(value) {
+ *((gint*)p[j].target) |= p[j].flagval;
+ } else {
+ *((gint*)p[j].target) &= ~(p[j].flagval);
+ }
+ }
+ break;
+ }
+ if(!strcmp(p[j].paramname, "port") && !strcmp(p[j].target, NBD_DEFAULT_PORT)) {
+ g_set_error(e, errdomain, CFILE_INCORRECT_PORT, "Config file specifies default port for oldstyle export");
+ g_key_file_free(cfile);
+ return NULL;
+ }
+ if(err) {
+ if(err->code == G_KEY_FILE_ERROR_KEY_NOT_FOUND) {
+ if(!p[j].required) {
+ /* Ignore not-found error for optional values */
+ g_clear_error(&err);
+ continue;
+ } else {
+ err_msg = MISSING_REQUIRED_ERROR;
+ }
+ } else {
+ err_msg = DEFAULT_ERROR;
+ }
+ g_set_error(e, errdomain, CFILE_VALUE_INVALID, err_msg, p[j].paramname, groups[i], err->message);
+ g_array_free(retval, TRUE);
+ g_error_free(err);
+ g_key_file_free(cfile);
+ return NULL;
+ }
+ }
+ if(virtstyle) {
+ if(!strncmp(virtstyle, "none", 4)) {
+ s.virtstyle=VIRT_NONE;
+ } else if(!strncmp(virtstyle, "ipliteral", 9)) {
+ s.virtstyle=VIRT_IPLIT;
+ } else if(!strncmp(virtstyle, "iphash", 6)) {
+ s.virtstyle=VIRT_IPHASH;
+ } else if(!strncmp(virtstyle, "cidrhash", 8)) {
+ s.virtstyle=VIRT_CIDR;
+ if(strlen(virtstyle)<10) {
+ g_set_error(e, errdomain, CFILE_VALUE_INVALID, "Invalid value %s for parameter virtstyle in group %s: missing length", virtstyle, groups[i]);
+ g_array_free(retval, TRUE);
+ g_key_file_free(cfile);
+ return NULL;
+ }
+ s.cidrlen=strtol(virtstyle+8, NULL, 0);
+ } else {
+ g_set_error(e, errdomain, CFILE_VALUE_INVALID, "Invalid value %s for parameter virtstyle in group %s", virtstyle, groups[i]);
+ g_array_free(retval, TRUE);
+ g_key_file_free(cfile);
+ return NULL;
+ }
+ } else {
+ s.virtstyle=VIRT_IPLIT;
+ }
+ /* Don't need to free this, it's not our string */
+ virtstyle=NULL;
+ /* Don't append values for the [generic] group */
+ if(i>0) {
+ s.socket_family = AF_UNSPEC;
+ s.servename = groups[i];
+
+ append_serve(&s, retval);
+ } else {
+ if(!do_oldstyle) {
+ lp[1].required = 0;
+ }
+ }
+#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
+ }
+ if(i==1) {
+ g_set_error(e, errdomain, CFILE_NO_EXPORTS, "The config file does not specify any exports");
+ }
+ g_key_file_free(cfile);
+ return retval;
+}
+
+/**