LCOV - code coverage report
Current view: top level - clients/odbc/driver - SQLColumnPrivileges.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 87 0.0 %
Date: 2021-10-13 02:24:04 Functions: 0 1 0.0 %

          Line data    Source code
       1             : /*
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       5             :  *
       6             :  * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
       7             :  */
       8             : 
       9             : /*
      10             :  * This code was created by Peter Harvey (mostly during Christmas 98/99).
      11             :  * This code is LGPL. Please ensure that this message remains in future
      12             :  * distributions and uses of this code (thats about all I get out of it).
      13             :  * - Peter Harvey pharvey@codebydesign.com
      14             :  *
      15             :  * This file has been modified for the MonetDB project.  See the file
      16             :  * Copyright in this directory for more information.
      17             :  */
      18             : 
      19             : /**********************************************************************
      20             :  * SQLColumnPrivileges()
      21             :  * CLI Compliance: ODBC (Microsoft)
      22             :  *
      23             :  * Note: this function is not implemented (it only sets an error),
      24             :  * because monetDB SQL frontend does not support column based authorization.
      25             :  **********************************************************************/
      26             : 
      27             : #include "ODBCGlobal.h"
      28             : #include "ODBCUtil.h"
      29             : #include "ODBCStmt.h"
      30             : 
      31             : 
      32             : static SQLRETURN
      33           0 : MNDBColumnPrivileges(ODBCStmt *stmt,
      34             :                      const SQLCHAR *CatalogName,
      35             :                      SQLSMALLINT NameLength1,
      36             :                      const SQLCHAR *SchemaName,
      37             :                      SQLSMALLINT NameLength2,
      38             :                      const SQLCHAR *TableName,
      39             :                      SQLSMALLINT NameLength3,
      40             :                      const SQLCHAR *ColumnName,
      41             :                      SQLSMALLINT NameLength4)
      42             : {
      43             :         RETCODE rc;
      44             :         char *query = NULL;
      45             :         size_t querylen;
      46             :         size_t pos = 0;
      47             :         char *sch = NULL, *tab = NULL, *col = NULL;
      48             : 
      49           0 :         fixODBCstring(CatalogName, NameLength1, SQLSMALLINT,
      50             :                       addStmtError, stmt, return SQL_ERROR);
      51           0 :         fixODBCstring(SchemaName, NameLength2, SQLSMALLINT,
      52             :                       addStmtError, stmt, return SQL_ERROR);
      53           0 :         fixODBCstring(TableName, NameLength3, SQLSMALLINT,
      54             :                       addStmtError, stmt, return SQL_ERROR);
      55           0 :         fixODBCstring(ColumnName, NameLength4, SQLSMALLINT,
      56             :                       addStmtError, stmt, return SQL_ERROR);
      57             : 
      58             : #ifdef ODBCDEBUG
      59           0 :         ODBCLOG(" \"%.*s\" \"%.*s\" \"%.*s\" \"%.*s\"\n",
      60             :                 (int) NameLength1, CatalogName ? (char *) CatalogName : "",
      61             :                 (int) NameLength2, SchemaName ? (char *) SchemaName : "",
      62             :                 (int) NameLength3, TableName ? (char *) TableName : "",
      63             :                 (int) NameLength4, ColumnName ? (char *) ColumnName : "");
      64             : #endif
      65             : 
      66           0 :         if (stmt->Dbc->sql_attr_metadata_id == SQL_FALSE) {
      67           0 :                 if (NameLength2 > 0) {
      68           0 :                         sch = ODBCParseOA("s", "name",
      69             :                                           (const char *) SchemaName,
      70             :                                           (size_t) NameLength2);
      71           0 :                         if (sch == NULL)
      72           0 :                                 goto nomem;
      73             :                 }
      74           0 :                 if (NameLength3 > 0) {
      75           0 :                         tab = ODBCParseOA("t", "name",
      76             :                                           (const char *) TableName,
      77             :                                           (size_t) NameLength3);
      78           0 :                         if (tab == NULL)
      79           0 :                                 goto nomem;
      80             :                 }
      81           0 :                 if (NameLength4 > 0) {
      82           0 :                         col = ODBCParsePV("c", "name",
      83             :                                           (const char *) ColumnName,
      84             :                                           (size_t) NameLength4);
      85           0 :                         if (col == NULL)
      86           0 :                                 goto nomem;
      87             :                 }
      88             :         } else {
      89           0 :                 if (NameLength2 > 0) {
      90           0 :                         sch = ODBCParseID("s", "name",
      91             :                                           (const char *) SchemaName,
      92             :                                           (size_t) NameLength2);
      93           0 :                         if (sch == NULL)
      94           0 :                                 goto nomem;
      95             :                 }
      96           0 :                 if (NameLength3 > 0) {
      97           0 :                         tab = ODBCParseID("t", "name",
      98             :                                           (const char *) TableName,
      99             :                                           (size_t) NameLength3);
     100           0 :                         if (tab == NULL)
     101           0 :                                 goto nomem;
     102             :                 }
     103           0 :                 if (NameLength4 > 0) {
     104           0 :                         col = ODBCParseID("c", "name",
     105             :                                           (const char *) ColumnName,
     106             :                                           (size_t) NameLength4);
     107           0 :                         if (col == NULL)
     108           0 :                                 goto nomem;
     109             :                 }
     110             :         }
     111             : 
     112             :         /* construct the query now */
     113           0 :         querylen = 1200 + strlen(stmt->Dbc->dbname) + (sch ? strlen(sch) : 0) +
     114           0 :                 (tab ? strlen(tab) : 0) + (col ? strlen(col) : 0);
     115           0 :         query = malloc(querylen);
     116           0 :         if (query == NULL)
     117           0 :                 goto nomem;
     118             : 
     119             :         /* SQLColumnPrivileges returns a table with the following columns:
     120             :            table_cat    VARCHAR
     121             :            table_schem  VARCHAR
     122             :            table_name   VARCHAR NOT NULL
     123             :            column_name  VARCHAR NOT NULL
     124             :            grantor      VARCHAR
     125             :            grantee      VARCHAR NOT NULL
     126             :            privilege    VARCHAR NOT NULL
     127             :            is_grantable VARCHAR
     128             :          */
     129             : 
     130           0 :         pos += snprintf(query + pos, querylen - pos,
     131             :                 "select '%s' as table_cat, "
     132             :                        "s.name as table_schem, "
     133             :                        "t.name as table_name, "
     134             :                        "c.name as column_name, "
     135             :                        "case a.id "
     136             :                             "when s.owner "
     137             :                             "then '_SYSTEM' "
     138             :                             "else g.name "
     139             :                             "end as grantor, "
     140             :                        "case a.name "
     141             :                             "when 'public' then 'PUBLIC' "
     142             :                             "else a.name "
     143             :                             "end as grantee, "
     144             :                        "pc.privilege_code_name as privilege, "
     145             :                        "case p.grantable "
     146             :                             "when 1 then 'YES' "
     147             :                             "when 0 then 'NO' "
     148             :                             "end as is_grantable "
     149             :                 "from sys.schemas as s, "
     150             :                      "sys._tables as t, "
     151             :                      "sys._columns as c, "
     152             :                      "sys.auths as a, "
     153             :                      "sys.privileges as p, "
     154             :                      "sys.auths as g, "
     155             :                      "%s "
     156             :                 "where p.obj_id = c.id and "
     157             :                       "c.table_id = t.id and "
     158             :                       "p.auth_id = a.id and "
     159             :                       "t.schema_id = s.id and "
     160             :                       "not t.system and "
     161             :                       "p.grantor = g.id and "
     162             :                       "p.privileges = pc.privilege_code_id",
     163             :                 stmt->Dbc->dbname,
     164             :                 /* a server that supports sys.comments also supports
     165             :                  * sys.privilege_codes */
     166           0 :                 stmt->Dbc->has_comment ? "sys.privilege_codes as pc" :
     167             :                      "(values (1, 'SELECT'), "
     168             :                              "(2, 'UPDATE'), "
     169             :                              "(4, 'INSERT'), "
     170             :                              "(8, 'DELETE'), "
     171             :                              "(16, 'EXECUTE'), "
     172             :                              "(32, 'GRANT')) as pc(privilege_code_id, privilege_code_name)");
     173           0 :         assert(pos < querylen);
     174             : 
     175             :         /* Construct the selection condition query part */
     176           0 :         if (NameLength1 > 0 && CatalogName != NULL) {
     177             :                 /* filtering requested on catalog name */
     178           0 :                 if (strcmp((char *) CatalogName, stmt->Dbc->dbname) != 0) {
     179             :                         /* catalog name does not match the database name, so return no rows */
     180           0 :                         pos += snprintf(query + pos, querylen - pos, " and 1=2");
     181             :                 }
     182             :         }
     183           0 :         if (sch) {
     184             :                 /* filtering requested on schema name */
     185           0 :                 pos += snprintf(query + pos, querylen - pos, " and %s", sch);
     186           0 :                 free(sch);
     187             :         }
     188           0 :         if (tab) {
     189             :                 /* filtering requested on table name */
     190           0 :                 pos += snprintf(query + pos, querylen - pos, " and %s", tab);
     191           0 :                 free(tab);
     192             :         }
     193           0 :         if (col) {
     194             :                 /* filtering requested on column name */
     195           0 :                 pos += snprintf(query + pos, querylen - pos, " and %s", col);
     196           0 :                 free(col);
     197             :         }
     198             : 
     199             :         /* add the ordering (exclude table_cat as it is the same for all rows) */
     200           0 :         pos += strcpy_len(query + pos, " order by table_schem, table_name, column_name, privilege", querylen - pos);
     201           0 :         assert(pos <= querylen);
     202             : 
     203             :         /* query the MonetDB data dictionary tables */
     204           0 :         rc = MNDBExecDirect(stmt, (SQLCHAR *) query, (SQLINTEGER) pos);
     205             : 
     206           0 :         free(query);
     207             : 
     208           0 :         return rc;
     209             : 
     210           0 :   nomem:
     211             :         /* note that query must be NULL when we get here */
     212           0 :         if (sch)
     213           0 :                 free(sch);
     214           0 :         if (tab)
     215           0 :                 free(tab);
     216           0 :         if (col)
     217           0 :                 free(col);
     218             :         /* Memory allocation error */
     219           0 :         addStmtError(stmt, "HY001", NULL, 0);
     220           0 :         return SQL_ERROR;
     221             : }
     222             : 
     223             : SQLRETURN SQL_API
     224             : SQLColumnPrivileges(SQLHSTMT StatementHandle,
     225             :                     SQLCHAR *CatalogName,
     226             :                     SQLSMALLINT NameLength1,
     227             :                     SQLCHAR *SchemaName,
     228             :                     SQLSMALLINT NameLength2,
     229             :                     SQLCHAR *TableName,
     230             :                     SQLSMALLINT NameLength3,
     231             :                     SQLCHAR *ColumnName,
     232             :                     SQLSMALLINT NameLength4)
     233             : {
     234             :         ODBCStmt *stmt = (ODBCStmt *) StatementHandle;
     235             : 
     236             : #ifdef ODBCDEBUG
     237           0 :         ODBCLOG("SQLColumnPrivileges %p", StatementHandle);
     238             : #endif
     239             : 
     240           0 :         if (!isValidStmt(stmt))
     241             :                  return SQL_INVALID_HANDLE;
     242             : 
     243           0 :         clearStmtErrors(stmt);
     244             : 
     245           0 :         return MNDBColumnPrivileges(stmt,
     246             :                                     CatalogName, NameLength1,
     247             :                                     SchemaName, NameLength2,
     248             :                                     TableName, NameLength3,
     249             :                                     ColumnName, NameLength4);
     250             : }
     251             : 
     252             : SQLRETURN SQL_API
     253             : SQLColumnPrivilegesA(SQLHSTMT StatementHandle,
     254             :                      SQLCHAR *CatalogName,
     255             :                      SQLSMALLINT NameLength1,
     256             :                      SQLCHAR *SchemaName,
     257             :                      SQLSMALLINT NameLength2,
     258             :                      SQLCHAR *TableName,
     259             :                      SQLSMALLINT NameLength3,
     260             :                      SQLCHAR *ColumnName,
     261             :                      SQLSMALLINT NameLength4)
     262             : {
     263           0 :         return SQLColumnPrivileges(StatementHandle,
     264             :                                    CatalogName, NameLength1,
     265             :                                    SchemaName, NameLength2,
     266             :                                    TableName, NameLength3,
     267             :                                    ColumnName, NameLength4);
     268             : }
     269             : 
     270             : SQLRETURN SQL_API
     271             : SQLColumnPrivilegesW(SQLHSTMT StatementHandle,
     272             :                      SQLWCHAR *CatalogName,
     273             :                      SQLSMALLINT NameLength1,
     274             :                      SQLWCHAR *SchemaName,
     275             :                      SQLSMALLINT NameLength2,
     276             :                      SQLWCHAR *TableName,
     277             :                      SQLSMALLINT NameLength3,
     278             :                      SQLWCHAR *ColumnName,
     279             :                      SQLSMALLINT NameLength4)
     280             : {
     281             :         ODBCStmt *stmt = (ODBCStmt *) StatementHandle;
     282             :         SQLCHAR *catalog = NULL, *schema = NULL, *table = NULL, *column = NULL;
     283             :         SQLRETURN rc = SQL_ERROR;
     284             : 
     285             : #ifdef ODBCDEBUG
     286           0 :         ODBCLOG("SQLColumnPrivilegesW %p", StatementHandle);
     287             : #endif
     288             : 
     289           0 :         if (!isValidStmt(stmt))
     290             :                  return SQL_INVALID_HANDLE;
     291             : 
     292           0 :         clearStmtErrors(stmt);
     293             : 
     294           0 :         fixWcharIn(CatalogName, NameLength1, SQLCHAR, catalog,
     295             :                    addStmtError, stmt, goto bailout);
     296           0 :         fixWcharIn(SchemaName, NameLength2, SQLCHAR, schema,
     297             :                    addStmtError, stmt, goto bailout);
     298           0 :         fixWcharIn(TableName, NameLength3, SQLCHAR, table,
     299             :                    addStmtError, stmt, goto bailout);
     300           0 :         fixWcharIn(ColumnName, NameLength4, SQLCHAR, column,
     301             :                    addStmtError, stmt, goto bailout);
     302             : 
     303           0 :         rc = MNDBColumnPrivileges(stmt,
     304             :                                   catalog, SQL_NTS,
     305             :                                   schema, SQL_NTS,
     306             :                                   table, SQL_NTS,
     307             :                                   column, SQL_NTS);
     308             : 
     309           0 :       bailout:
     310           0 :         if (catalog)
     311           0 :                 free(catalog);
     312           0 :         if (schema)
     313           0 :                 free(schema);
     314           0 :         if (table)
     315           0 :                 free(table);
     316           0 :         if (column)
     317           0 :                 free(column);
     318             :         return rc;
     319             : }

Generated by: LCOV version 1.14