Changeset: 4c2084c2a824 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=4c2084c2a824
Modified Files:
        clients/src/mapiclient/dump.c
Branch: Jun2010
Log Message:

Implemented dumping of GRANTs to columns/tables.

This fixes bug 2574.


diffs (173 lines):

diff -r 5d02f4983b31 -r 4c2084c2a824 clients/src/mapiclient/dump.c
--- a/clients/src/mapiclient/dump.c     Mon Jul 05 16:08:26 2010 +0200
+++ b/clients/src/mapiclient/dump.c     Mon Jul 05 17:34:13 2010 +0200
@@ -992,6 +992,41 @@
                "WHERE \"a1\".\"id\" = \"ur\".\"login_id\" AND "
                      "\"a2\".\"id\" = \"ur\".\"role_id\" "
                "ORDER BY \"a1\".\"name\", \"a2\".\"name\"";
+       const char *table_grants = "SELECT \"s\".\"name\", \"t\".\"name\", "
+                    "\"a\".\"name\", "
+                    "CASE WHEN \"p\".\"privileges\" = 1 THEN 'SELECT' "
+                         "WHEN \"p\".\"privileges\" = 2 THEN 'UPDATE' "
+                         "WHEN \"p\".\"privileges\" = 4 THEN 'INSERT' "
+                         "WHEN \"p\".\"privileges\" = 8 THEN 'DELETE' "
+                         "WHEN \"p\".\"privileges\" = 16 THEN 'EXECUTE' "
+                         "WHEN \"p\".\"privileges\" = 32 THEN 'GRANT' END, "
+                    "\"g\".\"name\", \"p\".\"grantable\" "
+               "FROM \"sys\".\"schemas\" \"s\", \"sys\".\"tables\" \"t\", "
+                    "\"sys\".\"auths\" \"a\", \"sys\".\"privileges\" \"p\", "
+                    "\"sys\".\"auths\" \"g\" "
+               "WHERE \"p\".\"obj_id\" = \"t\".\"id\" AND "
+                     "\"p\".\"auth_id\" = \"a\".\"id\" AND "
+                     "\"t\".\"schema_id\" = \"s\".\"id\" AND "
+                     "\"t\".\"system\" = FALSE AND "
+                     "\"p\".\"grantor\" = \"g\".\"id\"";
+       const char *column_grants = "SELECT \"s\".\"name\", \"t\".\"name\", "
+                    "\"c\".\"name\", \"a\".\"name\", "
+                    "CASE WHEN \"p\".\"privileges\" = 1 THEN 'SELECT' "
+                         "WHEN \"p\".\"privileges\" = 2 THEN 'UPDATE' "
+                         "WHEN \"p\".\"privileges\" = 4 THEN 'INSERT' "
+                         "WHEN \"p\".\"privileges\" = 8 THEN 'DELETE' "
+                         "WHEN \"p\".\"privileges\" = 16 THEN 'EXECUTE' "
+                         "WHEN \"p\".\"privileges\" = 32 THEN 'GRANT' END, "
+                    "\"g\".\"name\", \"p\".\"grantable\" "
+               "FROM \"sys\".\"schemas\" \"s\", \"sys\".\"tables\" \"t\", "
+                    "\"sys\".\"columns\" \"c\", \"sys\".\"auths\" \"a\", "
+                    "\"sys\".\"privileges\" \"p\", \"sys\".\"auths\" \"g\" "
+               "WHERE \"p\".\"obj_id\" = \"c\".\"id\" AND "
+                     "\"c\".\"table_id\" = \"t\".\"id\" AND "
+                     "\"p\".\"auth_id\" = \"a\".\"id\" AND "
+                     "\"t\".\"schema_id\" = \"s\".\"id\" AND "
+                     "\"t\".\"system\" = FALSE AND "
+                     "\"p\".\"grantor\" = \"g\".\"id\"";
        const char *schemas = "SELECT \"s\".\"name\", \"a\".\"name\" "
                "FROM \"sys\".\"schemas\" \"s\", "
                     "\"sys\".\"auths\" \"a\" "
@@ -1252,6 +1287,9 @@
                        quoted_print(toConsole, rname);
                        stream_printf(toConsole, " TO ");
                        quoted_print(toConsole, uname);
+                       /* optional WITH ADMIN OPTION and FROM
+                          (CURRENT_USER|CURRENT_ROLE) are ignored by
+                          server, so we can't dump them */
                        stream_printf(toConsole, ";\n");
                }
                if (mapi_error(mid)) {
@@ -1340,13 +1378,13 @@
                        stream_printf(toConsole, "%s\n", func);
        }
        if (curschema) {
-               if (sname == NULL || strcmp(sname, curschema) != 0) {
+               if (strcmp(sname ? sname : "sys", curschema) != 0) {
                        stream_printf(toConsole, "SET SCHEMA ");
                        quoted_print(toConsole, sname ? sname : "sys");
                        stream_printf(toConsole, ";\n");
                }
                free(curschema);
-               curschema = NULL;
+               curschema = strdup(sname ? sname : "sys");
        }
        if (mapi_error(mid)) {
                mapi_explain_query(hdl, stderr);
@@ -1408,6 +1446,101 @@
                mapi_close_handle(hdl);
        }
 
+       if ((hdl = mapi_query(mid, table_grants)) == NULL || mapi_error(mid)) {
+               if (hdl) {
+                       mapi_explain_query(hdl, stderr);
+                       mapi_close_handle(hdl);
+               } else
+                       mapi_explain(mid, stderr);
+               return 1;
+       }
+
+       while (mapi_fetch_row(hdl) != 0) {
+               char *schema = mapi_fetch_field(hdl, 0);
+               char *tname = mapi_fetch_field(hdl, 1);
+               char *aname = mapi_fetch_field(hdl, 2);
+               char *priv = mapi_fetch_field(hdl, 3);
+               char *grantable = mapi_fetch_field(hdl, 5);
+
+               if (sname != NULL && strcmp(schema, sname) != 0)
+                       continue;
+               if (curschema == NULL || strcmp(schema, curschema) != 0) {
+                       if (curschema)
+                               free(curschema);
+                       curschema = strdup(schema);
+                       stream_printf(toConsole, "SET SCHEMA ");
+                       quoted_print(toConsole, curschema);
+                       stream_printf(toConsole, ";\n");
+               }
+               stream_printf(toConsole, "GRANT %s ON ", priv);
+               quoted_print(toConsole, tname);
+               stream_printf(toConsole, " TO ");
+               quoted_print(toConsole, aname);
+               if (strcmp(grantable, "1") == 0)
+                       stream_printf(toConsole, " WITH GRANT OPTION");
+               stream_printf(toConsole, ";\n");
+       }
+       if (mapi_error(mid)) {
+               mapi_explain_query(hdl, stderr);
+               mapi_close_handle(hdl);
+               return 1;
+       }
+       mapi_close_handle(hdl);
+
+       if ((hdl = mapi_query(mid, column_grants)) == NULL || mapi_error(mid)) {
+               if (hdl) {
+                       mapi_explain_query(hdl, stderr);
+                       mapi_close_handle(hdl);
+               } else
+                       mapi_explain(mid, stderr);
+               return 1;
+       }
+
+       while (mapi_fetch_row(hdl) != 0) {
+               char *schema = mapi_fetch_field(hdl, 0);
+               char *tname = mapi_fetch_field(hdl, 1);
+               char *cname = mapi_fetch_field(hdl, 2);
+               char *aname = mapi_fetch_field(hdl, 3);
+               char *priv = mapi_fetch_field(hdl, 4);
+               char *grantable = mapi_fetch_field(hdl, 6);
+
+               if (sname != NULL && strcmp(schema, sname) != 0)
+                       continue;
+               if (curschema == NULL || strcmp(schema, curschema) != 0) {
+                       if (curschema)
+                               free(curschema);
+                       curschema = strdup(schema);
+                       stream_printf(toConsole, "SET SCHEMA ");
+                       quoted_print(toConsole, curschema);
+                       stream_printf(toConsole, ";\n");
+               }
+               stream_printf(toConsole, "GRANT %s(", priv);
+               quoted_print(toConsole, cname);
+               stream_printf(toConsole, ") ON ");
+               quoted_print(toConsole, tname);
+               stream_printf(toConsole, " TO ");
+               quoted_print(toConsole, aname);
+               if (strcmp(grantable, "1") == 0)
+                       stream_printf(toConsole, " WITH GRANT OPTION");
+               stream_printf(toConsole, ";\n");
+       }
+       if (mapi_error(mid)) {
+               mapi_explain_query(hdl, stderr);
+               mapi_close_handle(hdl);
+               return 1;
+       }
+       mapi_close_handle(hdl);
+
+       if (curschema) {
+               if (strcmp(sname ? sname : "sys", curschema) != 0) {
+                       stream_printf(toConsole, "SET SCHEMA ");
+                       quoted_print(toConsole, sname ? sname : "sys");
+                       stream_printf(toConsole, ";\n");
+               }
+               free(curschema);
+               curschema = NULL;
+       }
+
        if ((hdl = mapi_query(mid, end)) == NULL || mapi_error(mid)) {
                if (hdl) {
                        mapi_explain_query(hdl, stderr);
_______________________________________________
Checkin-list mailing list
Checkin-list@monetdb.org
http://mail.monetdb.org/mailman/listinfo/checkin-list

Reply via email to