Artifact ca95dba4854feb07265164c4e7780d89418b58b7:
File
src/admin.c
part of check-in
[007d1ce44f]
- Rename admin_sql_page.c to admin.c. Refactor the strxform functions into
SQL functions. Refactor the db_generic_query_view() routine. Fix multiple
security vulnerabilities. Bring the code closer into compliance with
style guidelines.
by
drh on
2008-02-07 15:08:02.
0000: 2f 2a 0a 2a 2a 20 43 6f 70 79 72 69 67 68 74 20 /*.** Copyright
0010: 28 63 29 20 32 30 30 37 20 44 2e 20 52 69 63 68 (c) 2007 D. Rich
0020: 61 72 64 20 48 69 70 70 0a 2a 2a 20 43 6f 70 79 ard Hipp.** Copy
0030: 72 69 67 68 74 20 28 63 29 20 32 30 30 38 20 53 right (c) 2008 S
0040: 74 65 70 68 61 6e 20 42 65 61 6c 0a 2a 2a 0a 2a tephan Beal.**.*
0050: 2a 20 54 68 69 73 20 70 72 6f 67 72 61 6d 20 69 * This program i
0060: 73 20 66 72 65 65 20 73 6f 66 74 77 61 72 65 3b s free software;
0070: 20 79 6f 75 20 63 61 6e 20 72 65 64 69 73 74 72 you can redistr
0080: 69 62 75 74 65 20 69 74 20 61 6e 64 2f 6f 72 0a ibute it and/or.
0090: 2a 2a 20 6d 6f 64 69 66 79 20 69 74 20 75 6e 64 ** modify it und
00a0: 65 72 20 74 68 65 20 74 65 72 6d 73 20 6f 66 20 er the terms of
00b0: 74 68 65 20 47 4e 55 20 47 65 6e 65 72 61 6c 20 the GNU General
00c0: 50 75 62 6c 69 63 0a 2a 2a 20 4c 69 63 65 6e 73 Public.** Licens
00d0: 65 20 61 73 20 70 75 62 6c 69 73 68 65 64 20 62 e as published b
00e0: 79 20 74 68 65 20 46 72 65 65 20 53 6f 66 74 77 y the Free Softw
00f0: 61 72 65 20 46 6f 75 6e 64 61 74 69 6f 6e 3b 20 are Foundation;
0100: 65 69 74 68 65 72 0a 2a 2a 20 76 65 72 73 69 6f either.** versio
0110: 6e 20 32 20 6f 66 20 74 68 65 20 4c 69 63 65 6e n 2 of the Licen
0120: 73 65 2c 20 6f 72 20 28 61 74 20 79 6f 75 72 20 se, or (at your
0130: 6f 70 74 69 6f 6e 29 20 61 6e 79 20 6c 61 74 65 option) any late
0140: 72 20 76 65 72 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a r version..**.**
0150: 20 54 68 69 73 20 70 72 6f 67 72 61 6d 20 69 73 This program is
0160: 20 64 69 73 74 72 69 62 75 74 65 64 20 69 6e 20 distributed in
0170: 74 68 65 20 68 6f 70 65 20 74 68 61 74 20 69 74 the hope that it
0180: 20 77 69 6c 6c 20 62 65 20 75 73 65 66 75 6c 2c will be useful,
0190: 0a 2a 2a 20 62 75 74 20 57 49 54 48 4f 55 54 20 .** but WITHOUT
01a0: 41 4e 59 20 57 41 52 52 41 4e 54 59 3b 20 77 69 ANY WARRANTY; wi
01b0: 74 68 6f 75 74 20 65 76 65 6e 20 74 68 65 20 69 thout even the i
01c0: 6d 70 6c 69 65 64 20 77 61 72 72 61 6e 74 79 20 mplied warranty
01d0: 6f 66 0a 2a 2a 20 4d 45 52 43 48 41 4e 54 41 42 of.** MERCHANTAB
01e0: 49 4c 49 54 59 20 6f 72 20 46 49 54 4e 45 53 53 ILITY or FITNESS
01f0: 20 46 4f 52 20 41 20 50 41 52 54 49 43 55 4c 41 FOR A PARTICULA
0200: 52 20 50 55 52 50 4f 53 45 2e 20 20 53 65 65 20 R PURPOSE. See
0210: 74 68 65 20 47 4e 55 0a 2a 2a 20 47 65 6e 65 72 the GNU.** Gener
0220: 61 6c 20 50 75 62 6c 69 63 20 4c 69 63 65 6e 73 al Public Licens
0230: 65 20 66 6f 72 20 6d 6f 72 65 20 64 65 74 61 69 e for more detai
0240: 6c 73 2e 0a 2a 2a 0a 2a 2a 20 59 6f 75 20 73 68 ls..**.** You sh
0250: 6f 75 6c 64 20 68 61 76 65 20 72 65 63 65 69 76 ould have receiv
0260: 65 64 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 ed a copy of the
0270: 20 47 4e 55 20 47 65 6e 65 72 61 6c 20 50 75 62 GNU General Pub
0280: 6c 69 63 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 61 lic.** License a
0290: 6c 6f 6e 67 20 77 69 74 68 20 74 68 69 73 20 6c long with this l
02a0: 69 62 72 61 72 79 3b 20 69 66 20 6e 6f 74 2c 20 ibrary; if not,
02b0: 77 72 69 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 write to the.**
02c0: 46 72 65 65 20 53 6f 66 74 77 61 72 65 20 46 6f Free Software Fo
02d0: 75 6e 64 61 74 69 6f 6e 2c 20 49 6e 63 2e 2c 20 undation, Inc.,
02e0: 35 39 20 54 65 6d 70 6c 65 20 50 6c 61 63 65 20 59 Temple Place
02f0: 2d 20 53 75 69 74 65 20 33 33 30 2c 0a 2a 2a 20 - Suite 330,.**
0300: 42 6f 73 74 6f 6e 2c 20 4d 41 20 20 30 32 31 31 Boston, MA 0211
0310: 31 2d 31 33 30 37 2c 20 55 53 41 2e 0a 2a 2a 0a 1-1307, USA..**.
0320: 2a 2a 20 41 75 74 68 6f 72 20 63 6f 6e 74 61 63 ** Author contac
0330: 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 3a 0a 2a t information:.*
0340: 2a 20 20 20 64 72 68 40 68 77 61 63 69 2e 63 6f * drh@hwaci.co
0350: 6d 0a 2a 2a 20 20 20 68 74 74 70 3a 2f 2f 77 77 m.** http://ww
0360: 77 2e 68 77 61 63 69 2e 63 6f 6d 2f 64 72 68 2f w.hwaci.com/drh/
0370: 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
0380: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0390: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
03a0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
03b0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
03c0: 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d ***.**.** Implem
03d0: 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
03e0: 41 64 6d 69 6e 20 53 51 4c 0a 2a 2f 0a 23 69 6e Admin SQL.*/.#in
03f0: 63 6c 75 64 65 20 3c 61 73 73 65 72 74 2e 68 3e clude <assert.h>
0400: 0a 23 69 6e 63 6c 75 64 65 20 22 63 6f 6e 66 69 .#include "confi
0410: 67 2e 68 22 0a 23 69 6e 63 6c 75 64 65 20 22 61 g.h".#include "a
0420: 64 6d 69 6e 2e 68 22 0a 0a 2f 2a 0a 2a 2a 20 54 dmin.h"../*.** T
0430: 68 69 73 20 53 51 4c 69 74 65 20 61 75 74 68 6f his SQLite autho
0440: 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 70 rizer callback p
0450: 72 65 76 65 6e 74 73 20 61 6e 79 20 53 51 4c 20 revents any SQL
0460: 6f 74 68 65 72 20 74 68 61 6e 0a 2a 2a 20 53 45 other than.** SE
0470: 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 LECT statements
0480: 66 72 6f 6d 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2f from running..*/
0490: 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 6c 65 .static int sele
04a0: 63 74 4f 6e 6c 79 28 0a 20 20 76 6f 69 64 20 2a ctOnly(. void *
04b0: 4e 6f 74 55 73 65 64 2c 20 20 20 20 20 20 20 20 NotUsed,
04c0: 20 20 20 2f 2a 20 41 70 70 6c 69 63 61 74 69 6f /* Applicatio
04d0: 6e 20 64 61 74 61 20 2d 20 6e 6f 74 20 75 73 65 n data - not use
04e0: 64 20 2a 2f 0a 20 20 69 6e 74 20 74 79 70 65 2c d */. int type,
04f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0500: 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 74 79 70 /* Operation typ
0510: 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 e */. const cha
0520: 72 20 2a 7a 41 72 67 31 2c 20 20 20 20 20 20 20 r *zArg1,
0530: 2f 2a 20 41 72 67 75 6d 65 6e 74 73 2e 2e 2e 2e /* Arguments....
0540: 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
0550: 20 2a 7a 41 72 67 32 2c 0a 20 20 63 6f 6e 73 74 *zArg2,. const
0560: 20 63 68 61 72 20 2a 7a 41 72 67 33 2c 0a 20 20 char *zArg3,.
0570: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 72 67 const char *zArg
0580: 34 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 4.){. int rc =
0590: 53 51 4c 49 54 45 5f 44 45 4e 59 3b 0a 20 20 73 SQLITE_DENY;. s
05a0: 77 69 74 63 68 28 20 74 79 70 65 20 29 7b 0a 20 witch( type ){.
05b0: 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 52 case SQLITE_R
05c0: 45 41 44 3a 0a 20 20 20 20 63 61 73 65 20 53 51 EAD:. case SQ
05d0: 4c 49 54 45 5f 53 45 4c 45 43 54 3a 20 7b 0a 20 LITE_SELECT: {.
05e0: 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
05f0: 5f 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b _OK;. break
0600: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
0610: 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a turn rc;.}.../*.
0620: 2a 2a 20 57 45 42 50 41 47 45 3a 20 2f 61 64 6d ** WEBPAGE: /adm
0630: 69 6e 2f 73 71 6c 0a 2a 2f 0a 76 6f 69 64 20 61 in/sql.*/.void a
0640: 64 6d 69 6e 5f 73 71 6c 5f 70 61 67 65 28 76 6f dmin_sql_page(vo
0650: 69 64 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 id){. const cha
0660: 72 20 2a 7a 53 71 6c 20 3d 20 50 44 28 22 73 71 r *zSql = PD("sq
0670: 6c 22 2c 22 22 29 3b 0a 20 20 6c 6f 67 69 6e 5f l","");. login_
0680: 63 68 65 63 6b 5f 63 72 65 64 65 6e 74 69 61 6c check_credential
0690: 73 28 29 3b 0a 20 20 69 66 28 20 21 67 2e 6f 6b s();. if( !g.ok
06a0: 41 64 6d 69 6e 20 29 7b 0a 20 20 20 20 6c 6f 67 Admin ){. log
06b0: 69 6e 5f 6e 65 65 64 65 64 28 29 3b 0a 20 20 20 in_needed();.
06c0: 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 73 return;. }. s
06d0: 74 79 6c 65 5f 68 65 61 64 65 72 28 22 41 64 6d tyle_header("Adm
06e0: 69 6e 20 53 51 4c 22 29 3b 0a 20 20 40 20 3c 68 in SQL");. @ <h
06f0: 72 2f 3e 3c 68 32 3e 53 51 4c 3a 3c 2f 68 32 3e r/><h2>SQL:</h2>
0700: 0a 20 20 40 20 3c 73 70 61 6e 20 63 6c 61 73 73 . @ <span class
0710: 3d 27 61 63 68 74 75 6e 67 27 3e 59 6f 75 20 63 ='achtung'>You c
0720: 61 6e 20 65 6e 74 65 72 20 61 72 62 69 74 72 61 an enter arbitra
0730: 72 79 20 53 51 4c 20 68 65 72 65 2c 20 74 6f 20 ry SQL here, to
0740: 65 78 65 63 75 74 65 0a 20 20 40 20 61 67 61 69 execute. @ agai
0750: 6e 73 74 20 74 68 65 20 72 65 70 6f 20 64 61 74 nst the repo dat
0760: 61 62 61 73 65 2e 0a 20 20 40 20 57 69 74 68 20 abase.. @ With
0770: 67 72 65 61 74 20 70 6f 77 65 72 20 63 6f 6d 65 great power come
0780: 73 20 67 72 65 61 74 20 72 65 73 70 6f 6e 73 69 s great responsi
0790: 62 69 6c 69 74 79 2e 2e 2e 3c 2f 73 70 61 6e 3e bility...</span>
07a0: 3c 62 72 2f 3e 0a 20 20 40 20 3c 66 6f 72 6d 20 <br/>. @ <form
07b0: 61 63 74 69 6f 6e 3d 27 27 20 6d 65 74 68 6f 64 action='' method
07c0: 3d 27 70 6f 73 74 27 3e 0a 20 20 40 20 3c 74 65 ='post'>. @ <te
07d0: 78 74 61 72 65 61 20 73 74 79 6c 65 3d 27 62 6f xtarea style='bo
07e0: 72 64 65 72 3a 32 70 78 20 73 6f 6c 69 64 20 62 rder:2px solid b
07f0: 6c 61 63 6b 27 20 6e 61 6d 65 3d 27 73 71 6c 27 lack' name='sql'
0800: 0a 20 20 40 20 20 63 6f 6c 73 3d 27 38 30 27 20 . @ cols='80'
0810: 72 6f 77 73 3d 27 35 27 3e 25 68 28 7a 53 71 6c rows='5'>%h(zSql
0820: 29 3c 2f 74 65 78 74 61 72 65 61 3e 0a 20 20 40 )</textarea>. @
0830: 20 3c 62 72 2f 3e 3c 69 6e 70 75 74 20 74 79 70 <br/><input typ
0840: 65 3d 27 73 75 62 6d 69 74 27 20 6e 61 6d 65 3d e='submit' name=
0850: 27 73 71 6c 5f 73 75 62 6d 69 74 27 2f 3e 20 3c 'sql_submit'/> <
0860: 69 6e 70 75 74 20 74 79 70 65 3d 27 72 65 73 65 input type='rese
0870: 74 27 2f 3e 0a 20 20 40 20 3c 2f 66 6f 72 6d 3e t'/>. @ </form>
0880: 0a 20 20 69 66 28 20 7a 53 71 6c 5b 30 5d 20 29 . if( zSql[0] )
0890: 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 65 {. sqlite3_se
08a0: 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 67 2e 64 t_authorizer(g.d
08b0: 62 2c 20 73 65 6c 65 63 74 4f 6e 6c 79 2c 20 30 b, selectOnly, 0
08c0: 29 3b 0a 20 20 20 20 64 62 5f 67 65 6e 65 72 69 );. db_generi
08d0: 63 5f 71 75 65 72 79 5f 76 69 65 77 28 7a 53 71 c_query_view(zSq
08e0: 6c 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 l, 0);. sqlit
08f0: 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 e3_set_authorize
0900: 72 28 67 2e 64 62 2c 20 30 2c 20 30 29 3b 0a 20 r(g.db, 0, 0);.
0910: 20 7d 0a 20 20 73 74 79 6c 65 5f 66 6f 6f 74 65 }. style_foote
0920: 72 28 29 3b 0a 7d 0a r();.}.