drh | e6d01c3 | 2003-01-12 18:07:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2003 January 11 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This file contains code used to implement the sqlite_set_authorizer() |
| 13 | ** API. This facility is an optional feature of the library. Embedded |
| 14 | ** systems that do not need this facility may omit it by recompiling |
| 15 | ** the library with -DSQLITE_OMIT_AUTHORIZATION=1 |
| 16 | ** |
drh | 1962bda | 2003-01-12 19:33:52 +0000 | [diff] [blame^] | 17 | ** $Id: auth.c,v 1.2 2003/01/12 19:33:53 drh Exp $ |
drh | e6d01c3 | 2003-01-12 18:07:48 +0000 | [diff] [blame] | 18 | */ |
| 19 | #include "sqliteInt.h" |
| 20 | |
| 21 | /* |
| 22 | ** All of the code in this file may be omitted by defining a single |
| 23 | ** macro. |
| 24 | */ |
| 25 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 26 | |
| 27 | /* |
| 28 | ** Set or clear the access authorization function. |
| 29 | ** |
| 30 | ** The access authorization function is be called during the compilation |
| 31 | ** phase to verify that the user has read and/or write access permission |
| 32 | ** various fields of the database. The first argument to the auth function |
| 33 | ** is a copy of the 3rd argument to this routine. The second argument |
| 34 | ** to the auth function is one of these constants: |
| 35 | ** |
| 36 | ** SQLITE_READ_COLUMN |
| 37 | ** SQLITE_WRITE_COLUMN |
| 38 | ** SQLITE_DELETE_ROW |
| 39 | ** SQLITE_INSERT_ROW |
| 40 | ** |
| 41 | ** The third and fourth arguments to the auth function are the name of |
| 42 | ** the table and the column that are being accessed. The auth function |
| 43 | ** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE. If |
| 44 | ** SQLITE_OK is returned, it means that access is allowed. SQLITE_DENY |
| 45 | ** means that the SQL statement will never-run - the sqlite_exec() call |
| 46 | ** will return with an error. SQLITE_IGNORE means that the SQL statement |
| 47 | ** should run but attempts to read the specified column will return NULL |
| 48 | ** and attempts to write the column will be ignored. |
| 49 | ** |
| 50 | ** Setting the auth function to NULL disables this hook. The default |
| 51 | ** setting of the auth function is NULL. |
| 52 | */ |
| 53 | int sqlite_set_authorizer( |
| 54 | sqlite *db, |
| 55 | int (*xAuth)(void*,int,const char*,const char*), |
| 56 | void *pArg |
| 57 | ){ |
| 58 | db->xAuth = xAuth; |
| 59 | db->pAuthArg = pArg; |
| 60 | return SQLITE_OK; |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | ** Write an error message into pParse->zErrMsg that explains that the |
| 65 | ** user-supplied authorization function returned an illegal value. |
| 66 | */ |
| 67 | static void sqliteAuthBadReturnCode(Parse *pParse, int rc){ |
| 68 | char zBuf[20]; |
| 69 | sprintf(zBuf, "(%d)", rc); |
| 70 | sqliteSetString(&pParse->zErrMsg, "illegal return value ", zBuf, |
drh | 1962bda | 2003-01-12 19:33:52 +0000 | [diff] [blame^] | 71 | " from the authorization function - should be SQLITE_OK, " |
drh | e6d01c3 | 2003-01-12 18:07:48 +0000 | [diff] [blame] | 72 | "SQLITE_IGNORE, or SQLITE_DENY", 0); |
| 73 | pParse->nErr++; |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | ** The pExpr should be a TK_COLUMN expression. The table referred to |
| 78 | ** is in pTabList with an offset of base. Check to see if it is OK to read |
| 79 | ** this particular column. |
| 80 | ** |
| 81 | ** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN |
| 82 | ** instruction into a TK_NULL. If the auth function returns SQLITE_DENY, |
| 83 | ** then generate an error. |
| 84 | */ |
| 85 | void sqliteAuthRead( |
| 86 | Parse *pParse, /* The parser context */ |
| 87 | Expr *pExpr, /* The expression to check authorization on */ |
| 88 | SrcList *pTabList, /* All table that pExpr might refer to */ |
| 89 | int base /* Offset of pTabList relative to pExpr */ |
| 90 | ){ |
| 91 | sqlite *db = pParse->db; |
| 92 | int rc; |
| 93 | Table *pTab; |
| 94 | const char *zCol; |
| 95 | if( db->xAuth==0 ) return; |
| 96 | assert( pExpr->op==TK_COLUMN ); |
| 97 | assert( pExpr->iTable>=base && pExpr->iTable<base+pTabList->nSrc ); |
| 98 | pTab = pTabList->a[pExpr->iTable-base].pTab; |
| 99 | if( pTab==0 ) return; |
| 100 | if( pExpr->iColumn>=0 ){ |
| 101 | assert( pExpr->iColumn<pTab->nCol ); |
| 102 | zCol = pTab->aCol[pExpr->iColumn].zName; |
| 103 | }else if( pTab->iPKey>=0 ){ |
| 104 | assert( pTab->iPKey<pTab->nCol ); |
| 105 | zCol = pTab->aCol[pTab->iPKey].zName; |
| 106 | }else{ |
| 107 | zCol = "ROWID"; |
| 108 | } |
| 109 | rc = db->xAuth(db->pAuthArg, SQLITE_READ_COLUMN, pTab->zName, zCol); |
| 110 | if( rc==SQLITE_IGNORE ){ |
| 111 | pExpr->op = TK_NULL; |
| 112 | }else if( rc==SQLITE_DENY ){ |
| 113 | sqliteSetString(&pParse->zErrMsg,"access to ", |
| 114 | pTab->zName, ".", zCol, " is prohibited", 0); |
| 115 | pParse->nErr++; |
| 116 | }else if( rc!=SQLITE_OK ){ |
| 117 | sqliteAuthBadReturnCode(pParse, rc); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | ** Check the user-supplied authorization function to see if it is ok to |
| 123 | ** delete rows from the table pTab. Return SQLITE_OK if it is. Return |
| 124 | ** SQLITE_IGNORE if deletions should be silently omitted. Return SQLITE_DENY |
| 125 | ** if an error is to be reported. In the last case, write the text of |
| 126 | ** the error into pParse->zErrMsg. |
| 127 | */ |
| 128 | int sqliteAuthDelete(Parse *pParse, const char *zName, int forceError){ |
| 129 | sqlite *db = pParse->db; |
| 130 | int rc; |
| 131 | if( db->xAuth==0 ){ |
| 132 | return SQLITE_OK; |
| 133 | } |
| 134 | rc = db->xAuth(db->pAuthArg, SQLITE_DELETE_ROW, zName, ""); |
| 135 | if( rc==SQLITE_DENY || (rc==SQLITE_IGNORE && forceError) ){ |
drh | 1962bda | 2003-01-12 19:33:52 +0000 | [diff] [blame^] | 136 | sqliteSetString(&pParse->zErrMsg,"deletion from table ", |
drh | e6d01c3 | 2003-01-12 18:07:48 +0000 | [diff] [blame] | 137 | zName, " is prohibited", 0); |
| 138 | pParse->nErr++; |
| 139 | }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){ |
| 140 | rc = SQLITE_DENY; |
| 141 | sqliteAuthBadReturnCode(pParse, rc); |
| 142 | } |
| 143 | return rc; |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | ** Check the user-supplied authorization function to see if it is ok to |
| 148 | ** insert rows from the table pTab. Return SQLITE_OK if it is. Return |
| 149 | ** SQLITE_IGNORE if deletions should be silently omitted. Return SQLITE_DENY |
| 150 | ** if an error is to be reported. In the last case, write the text of |
| 151 | ** the error into pParse->zErrMsg. |
| 152 | */ |
| 153 | int sqliteAuthInsert(Parse *pParse, const char *zName, int forceError){ |
| 154 | sqlite *db = pParse->db; |
| 155 | int rc; |
| 156 | if( db->xAuth==0 ){ |
| 157 | return SQLITE_OK; |
| 158 | } |
| 159 | rc = db->xAuth(db->pAuthArg, SQLITE_INSERT_ROW, zName, ""); |
| 160 | if( rc==SQLITE_DENY || (rc==SQLITE_IGNORE && forceError) ){ |
drh | 1962bda | 2003-01-12 19:33:52 +0000 | [diff] [blame^] | 161 | sqliteSetString(&pParse->zErrMsg,"insertion into table ", |
drh | e6d01c3 | 2003-01-12 18:07:48 +0000 | [diff] [blame] | 162 | zName, " is prohibited", 0); |
| 163 | pParse->nErr++; |
| 164 | }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){ |
| 165 | rc = SQLITE_DENY; |
| 166 | sqliteAuthBadReturnCode(pParse, rc); |
| 167 | } |
| 168 | return rc; |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | ** Check to see if it is ok to modify column "j" of table pTab. |
| 173 | ** Return SQLITE_OK, SQLITE_IGNORE, or SQLITE_DENY. |
| 174 | */ |
| 175 | int sqliteAuthWrite(Parse *pParse, Table *pTab, int j){ |
| 176 | sqlite *db = pParse->db; |
| 177 | int rc; |
| 178 | if( db->xAuth==0 ) return SQLITE_OK; |
| 179 | rc = db->xAuth(db->pAuthArg, SQLITE_WRITE_COLUMN, |
| 180 | pTab->zName, pTab->aCol[j].zName); |
| 181 | if( rc==SQLITE_DENY ){ |
| 182 | sqliteSetString(&pParse->zErrMsg, "changes to ", pTab->zName, |
| 183 | ".", pTab->aCol[j].zName, " are prohibited", 0); |
| 184 | pParse->nErr++; |
| 185 | }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){ |
| 186 | sqliteAuthBadReturnCode(pParse, rc); |
| 187 | } |
| 188 | return rc; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | ** Check to see if it is ok to execute a special command such as |
| 193 | ** COPY or VACUUM or ROLLBACK. |
| 194 | */ |
| 195 | int sqliteAuthCommand(Parse *pParse, const char *zCmd, const char *zArg1){ |
| 196 | sqlite *db = pParse->db; |
| 197 | int rc; |
| 198 | if( db->xAuth==0 ) return SQLITE_OK; |
| 199 | rc = db->xAuth(db->pAuthArg, SQLITE_COMMAND, zCmd, zArg1); |
| 200 | if( rc==SQLITE_DENY ){ |
| 201 | if( zArg1 && zArg1[0] ){ |
| 202 | sqliteSetString(&pParse->zErrMsg, "execution of the ", zCmd, " ", zArg1, |
| 203 | " command is prohibited", 0); |
| 204 | }else{ |
| 205 | sqliteSetString(&pParse->zErrMsg, "execution of the ", zCmd, |
| 206 | " command is prohibited", 0); |
| 207 | } |
| 208 | pParse->nErr++; |
| 209 | }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){ |
| 210 | sqliteAuthBadReturnCode(pParse, rc); |
| 211 | } |
| 212 | return rc; |
| 213 | } |
| 214 | |
| 215 | #endif /* SQLITE_OMIT_AUTHORIZATION */ |