LCOV - code coverage report
Current view: top level - clients/odbc/driver - SQLTablePrivileges.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 69 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             :  * SQLTablePrivileges()
      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 table based authorization.
      25             :  *
      26             :  * Author: Martin van Dinther, Sjoerd Mullender
      27             :  * Date  : 30 aug 2002
      28             :  *
      29             :  **********************************************************************/
      30             : 
      31             : #include "ODBCGlobal.h"
      32             : #include "ODBCStmt.h"
      33             : #include "ODBCUtil.h"
      34             : 
      35             : 
      36             : static SQLRETURN
      37           0 : MNDBTablePrivileges(ODBCStmt *stmt,
      38             :                     const SQLCHAR *CatalogName,
      39             :                     SQLSMALLINT NameLength1,
      40             :                     const SQLCHAR *SchemaName,
      41             :                     SQLSMALLINT NameLength2,
      42             :                     const SQLCHAR *TableName,
      43             :                     SQLSMALLINT NameLength3)
      44             : {
      45             :         RETCODE rc;
      46             :         char *query = NULL;
      47             :         size_t querylen;
      48             :         size_t pos = 0;
      49             :         char *sch = NULL, *tab = NULL;
      50             : 
      51           0 :         fixODBCstring(CatalogName, NameLength1, SQLSMALLINT,
      52             :                       addStmtError, stmt, return SQL_ERROR);
      53           0 :         fixODBCstring(SchemaName, NameLength2, SQLSMALLINT,
      54             :                       addStmtError, stmt, return SQL_ERROR);
      55           0 :         fixODBCstring(TableName, NameLength3, SQLSMALLINT,
      56             :                       addStmtError, stmt, return SQL_ERROR);
      57             : 
      58             : #ifdef ODBCDEBUG
      59           0 :         ODBCLOG("\"%.*s\" \"%.*s\" \"%.*s\"\n",
      60             :                 (int) NameLength1, CatalogName ? (char *) CatalogName : "",
      61             :                 (int) NameLength2, SchemaName ? (char *) SchemaName : "",
      62             :                 (int) NameLength3, TableName ? (char *) TableName : "");
      63             : #endif
      64             : 
      65           0 :         if (stmt->Dbc->sql_attr_metadata_id == SQL_FALSE) {
      66           0 :                 if (NameLength2 > 0) {
      67           0 :                         sch = ODBCParsePV("s", "name",
      68             :                                           (const char *) SchemaName,
      69             :                                           (size_t) NameLength2);
      70           0 :                         if (sch == NULL)
      71           0 :                                 goto nomem;
      72             :                 }
      73           0 :                 if (NameLength3 > 0) {
      74           0 :                         tab = ODBCParsePV("t", "name",
      75             :                                           (const char *) TableName,
      76             :                                           (size_t) NameLength3);
      77           0 :                         if (tab == NULL)
      78           0 :                                 goto nomem;
      79             :                 }
      80             :         } else {
      81           0 :                 if (NameLength2 > 0) {
      82           0 :                         sch = ODBCParseID("s", "name",
      83             :                                           (const char *) SchemaName,
      84             :                                           (size_t) NameLength2);
      85           0 :                         if (sch == NULL)
      86           0 :                                 goto nomem;
      87             :                 }
      88           0 :                 if (NameLength3 > 0) {
      89           0 :                         tab = ODBCParseID("t", "name",
      90             :                                           (const char *) TableName,
      91             :                                           (size_t) NameLength3);
      92           0 :                         if (tab == NULL)
      93           0 :                                 goto nomem;
      94             :                 }
      95             :         }
      96             : 
      97             :         /* construct the query now */
      98           0 :         querylen = 1200 + strlen(stmt->Dbc->dbname) +
      99           0 :                 (sch ? strlen(sch) : 0) + (tab ? strlen(tab) : 0);
     100           0 :         query = malloc(querylen);
     101           0 :         if (query == NULL)
     102           0 :                 goto nomem;
     103             : 
     104             :         /* SQLTablePrivileges returns a table with the following columns:
     105             :            table_cat    VARCHAR
     106             :            table_schem  VARCHAR
     107             :            table_name   VARCHAR NOT NULL
     108             :            grantor      VARCHAR
     109             :            grantee      VARCHAR NOT NULL
     110             :            privilege    VARCHAR NOT NULL
     111             :            is_grantable VARCHAR
     112             :          */
     113             : 
     114           0 :         pos += snprintf(query + pos, querylen - pos,
     115             :                 "select '%s' as table_cat, "
     116             :                         "s.name as table_schem, "
     117             :                         "t.name as table_name, "
     118             :                         "case a.id "
     119             :                              "when s.owner then '_SYSTEM' "
     120             :                              "else g.name "
     121             :                              "end as grantor, "
     122             :                         "case a.name "
     123             :                              "when 'public' then 'PUBLIC' "
     124             :                              "else a.name "
     125             :                              "end as grantee, "
     126             :                         "pc.privilege_code_name as privilege, "
     127             :                         "case p.grantable "
     128             :                              "when 1 then 'YES' "
     129             :                              "when 0 then 'NO' "
     130             :                              "end as is_grantable "
     131             :                 "from sys.schemas s, "
     132             :                       "sys._tables t, "
     133             :                       "sys.auths a, "
     134             :                       "sys.privileges p, "
     135             :                       "sys.auths g, "
     136             :                       "%s "
     137             :                 "where p.obj_id = t.id and "
     138             :                       "p.auth_id = a.id and "
     139             :                       "t.schema_id = s.id and "
     140             :                       "not t.system and "
     141             :                       "p.grantor = g.id and "
     142             :                       "p.privileges = pc.privilege_code_id",
     143             :                 stmt->Dbc->dbname,
     144             :                 /* a server that supports sys.comments also supports
     145             :                  * sys.privilege_codes */
     146           0 :                 stmt->Dbc->has_comment ? "sys.privilege_codes as pc" :
     147             :                      "(values (1, 'SELECT'), "
     148             :                              "(2, 'UPDATE'), "
     149             :                              "(4, 'INSERT'), "
     150             :                              "(8, 'DELETE'), "
     151             :                              "(16, 'EXECUTE'), "
     152             :                              "(32, 'GRANT')) as pc(privilege_code_id, privilege_code_name)");
     153           0 :         assert(pos < 1000);
     154             : 
     155             :         /* Construct the selection condition query part */
     156           0 :         if (NameLength1 > 0 && CatalogName != NULL) {
     157             :                 /* filtering requested on catalog name */
     158           0 :                 if (strcmp((char *) CatalogName, stmt->Dbc->dbname) != 0) {
     159             :                         /* catalog name does not match the database name, so return no rows */
     160           0 :                         pos += snprintf(query + pos, querylen - pos, " and 1=2");
     161             :                 }
     162             :         }
     163           0 :         if (sch) {
     164             :                 /* filtering requested on schema name */
     165           0 :                 pos += snprintf(query + pos, querylen - pos, " and %s", sch);
     166           0 :                 free(sch);
     167             :         }
     168           0 :         if (tab) {
     169             :                 /* filtering requested on table name */
     170           0 :                 pos += snprintf(query + pos, querylen - pos, " and %s", tab);
     171           0 :                 free(tab);
     172             :         }
     173             : 
     174             :         /* add the ordering (exclude table_cat as it is the same for all rows) */
     175           0 :         pos += strcpy_len(query + pos, " order by table_schem, table_name, privilege, grantee", querylen - pos);
     176             : 
     177             :         /* query the MonetDB data dictionary tables */
     178           0 :         rc = MNDBExecDirect(stmt, (SQLCHAR *) query, (SQLINTEGER) pos);
     179             : 
     180           0 :         free(query);
     181             : 
     182           0 :         return rc;
     183             : 
     184           0 :   nomem:
     185             :         /* note that query must be NULL when we get here */
     186           0 :         if (sch)
     187           0 :                 free(sch);
     188           0 :         if (tab)
     189           0 :                 free(tab);
     190             :         /* Memory allocation error */
     191           0 :         addStmtError(stmt, "HY001", NULL, 0);
     192           0 :         return SQL_ERROR;
     193             : }
     194             : 
     195             : SQLRETURN SQL_API
     196             : SQLTablePrivileges(SQLHSTMT StatementHandle,
     197             :                    SQLCHAR *CatalogName,
     198             :                    SQLSMALLINT NameLength1,
     199             :                    SQLCHAR *SchemaName,
     200             :                    SQLSMALLINT NameLength2,
     201             :                    SQLCHAR *TableName,
     202             :                    SQLSMALLINT NameLength3)
     203             : {
     204             :         ODBCStmt *stmt = (ODBCStmt *) StatementHandle;
     205             : 
     206             : #ifdef ODBCDEBUG
     207           0 :         ODBCLOG("SQLTablePrivileges %p ", StatementHandle);
     208             : #endif
     209             : 
     210           0 :         if (!isValidStmt(stmt))
     211             :                  return SQL_INVALID_HANDLE;
     212             : 
     213           0 :         clearStmtErrors(stmt);
     214             : 
     215           0 :         return MNDBTablePrivileges(stmt,
     216             :                                    CatalogName, NameLength1,
     217             :                                    SchemaName, NameLength2,
     218             :                                    TableName, NameLength3);
     219             : }
     220             : 
     221             : SQLRETURN SQL_API
     222             : SQLTablePrivilegesA(SQLHSTMT StatementHandle,
     223             :                     SQLCHAR *CatalogName,
     224             :                     SQLSMALLINT NameLength1,
     225             :                     SQLCHAR *SchemaName,
     226             :                     SQLSMALLINT NameLength2,
     227             :                     SQLCHAR *TableName,
     228             :                     SQLSMALLINT NameLength3)
     229             : {
     230           0 :         return SQLTablePrivileges(StatementHandle,
     231             :                                   CatalogName, NameLength1,
     232             :                                   SchemaName, NameLength2,
     233             :                                   TableName, NameLength3);
     234             : }
     235             : 
     236             : SQLRETURN SQL_API
     237             : SQLTablePrivilegesW(SQLHSTMT StatementHandle,
     238             :                     SQLWCHAR *CatalogName,
     239             :                     SQLSMALLINT NameLength1,
     240             :                     SQLWCHAR *SchemaName,
     241             :                     SQLSMALLINT NameLength2,
     242             :                     SQLWCHAR *TableName,
     243             :                     SQLSMALLINT NameLength3)
     244             : {
     245             :         ODBCStmt *stmt = (ODBCStmt *) StatementHandle;
     246             :         SQLRETURN rc = SQL_ERROR;
     247             :         SQLCHAR *catalog = NULL, *schema = NULL, *table = NULL;
     248             : 
     249             : #ifdef ODBCDEBUG
     250           0 :         ODBCLOG("SQLTablePrivilegesW %p ", StatementHandle);
     251             : #endif
     252             : 
     253           0 :         if (!isValidStmt(stmt))
     254             :                  return SQL_INVALID_HANDLE;
     255             : 
     256           0 :         clearStmtErrors(stmt);
     257             : 
     258           0 :         fixWcharIn(CatalogName, NameLength1, SQLCHAR, catalog,
     259             :                    addStmtError, stmt, goto bailout);
     260           0 :         fixWcharIn(SchemaName, NameLength2, SQLCHAR, schema,
     261             :                    addStmtError, stmt, goto bailout);
     262           0 :         fixWcharIn(TableName, NameLength3, SQLCHAR, table,
     263             :                    addStmtError, stmt, goto bailout);
     264             : 
     265           0 :         rc = MNDBTablePrivileges(stmt,
     266             :                                  catalog, SQL_NTS,
     267             :                                  schema, SQL_NTS,
     268             :                                  table, SQL_NTS);
     269             : 
     270           0 :       bailout:
     271           0 :         if (catalog)
     272           0 :                 free(catalog);
     273           0 :         if (schema)
     274           0 :                 free(schema);
     275           0 :         if (table)
     276           0 :                 free(table);
     277             : 
     278             :         return rc;
     279             : }

Generated by: LCOV version 1.14