Artifact 04901037544f9bf95cc7f64efa11573eed130c57:
File
src/admin.c
part of check-in
[ef432c2014]
- Cleanup of the "admin_sql" web page (formerly "admin/sql").
by
drh on
2009-08-16 21:22:49.
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 20 7b 0a 20 20 20 20 20 20 69 66 28 EAD: {. if(
05d0: 20 73 74 72 63 6d 70 28 7a 41 72 67 32 2c 22 70 strcmp(zArg2,"p
05e0: 77 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 w")==0 ){.
05f0: 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 47 rc = SQLITE_IG
0600: 4e 4f 52 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73 NORE;. }els
0610: 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 e{. rc =
0620: 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 SQLITE_OK;.
0630: 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a }. break;.
0640: 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 }. case S
0650: 51 4c 49 54 45 5f 46 55 4e 43 54 49 4f 4e 3a 0a QLITE_FUNCTION:.
0660: 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
0670: 53 45 4c 45 43 54 3a 20 7b 0a 20 20 20 20 20 20 SELECT: {.
0680: 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
0690: 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
06a0: 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
06b0: 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 45 42 rc;.}../*.** WEB
06c0: 50 41 47 45 3a 20 61 64 6d 69 6e 5f 73 71 6c 0a PAGE: admin_sql.
06d0: 2a 2f 0a 76 6f 69 64 20 61 64 6d 69 6e 5f 73 71 */.void admin_sq
06e0: 6c 5f 70 61 67 65 28 76 6f 69 64 29 7b 0a 20 20 l_page(void){.
06f0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c const char *zSql
0700: 20 3d 20 50 44 28 22 73 71 6c 22 2c 22 22 29 3b = PD("sql","");
0710: 0a 20 20 6c 6f 67 69 6e 5f 63 68 65 63 6b 5f 63 . login_check_c
0720: 72 65 64 65 6e 74 69 61 6c 73 28 29 3b 0a 20 20 redentials();.
0730: 69 66 28 20 21 67 2e 6f 6b 41 64 6d 69 6e 20 29 if( !g.okAdmin )
0740: 7b 0a 20 20 20 20 6c 6f 67 69 6e 5f 6e 65 65 64 {. login_need
0750: 65 64 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e ed();. return
0760: 3b 0a 20 20 7d 0a 20 20 73 74 79 6c 65 5f 68 65 ;. }. style_he
0770: 61 64 65 72 28 22 41 64 6d 69 6e 20 53 51 4c 22 ader("Admin SQL"
0780: 29 3b 0a 20 20 40 20 3c 68 32 3e 53 51 4c 3a 3c );. @ <h2>SQL:<
0790: 2f 68 32 3e 0a 20 20 40 20 59 6f 75 20 63 61 6e /h2>. @ You can
07a0: 20 65 6e 74 65 72 20 6f 6e 6c 79 20 53 45 4c 45 enter only SELE
07b0: 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 68 65 CT statements he
07c0: 72 65 2c 20 61 6e 64 20 73 6f 6d 65 20 53 51 4c re, and some SQL
07d0: 2d 73 69 64 65 20 66 75 6e 63 74 69 6f 6e 73 0a -side functions.
07e0: 20 20 40 20 61 72 65 20 61 6c 73 6f 20 72 65 73 @ are also res
07f0: 74 72 69 63 74 65 64 2e 3c 62 72 2f 3e 0a 20 20 tricted.<br/>.
0800: 40 20 3c 66 6f 72 6d 20 61 63 74 69 6f 6e 3d 27 @ <form action='
0810: 27 20 6d 65 74 68 6f 64 3d 27 70 6f 73 74 27 3e ' method='post'>
0820: 0a 20 20 6c 6f 67 69 6e 5f 69 6e 73 65 72 74 5f . login_insert_
0830: 63 73 72 66 5f 73 65 63 72 65 74 28 29 3b 0a 20 csrf_secret();.
0840: 20 40 20 3c 74 65 78 74 61 72 65 61 20 73 74 79 @ <textarea sty
0850: 6c 65 3d 27 62 6f 72 64 65 72 3a 32 70 78 20 73 le='border:2px s
0860: 6f 6c 69 64 20 62 6c 61 63 6b 27 20 6e 61 6d 65 olid black' name
0870: 3d 27 73 71 6c 27 0a 20 20 40 20 20 63 6f 6c 73 ='sql'. @ cols
0880: 3d 27 38 30 27 20 72 6f 77 73 3d 27 35 27 3e 25 ='80' rows='5'>%
0890: 68 28 7a 53 71 6c 29 3c 2f 74 65 78 74 61 72 65 h(zSql)</textare
08a0: 61 3e 0a 20 20 40 20 3c 62 72 2f 3e 3c 69 6e 70 a>. @ <br/><inp
08b0: 75 74 20 74 79 70 65 3d 27 73 75 62 6d 69 74 27 ut type='submit'
08c0: 20 6e 61 6d 65 3d 27 73 71 6c 5f 73 75 62 6d 69 name='sql_submi
08d0: 74 27 2f 3e 20 3c 69 6e 70 75 74 20 74 79 70 65 t'/> <input type
08e0: 3d 27 72 65 73 65 74 27 2f 3e 0a 20 20 40 20 3c ='reset'/>. @ <
08f0: 2f 66 6f 72 6d 3e 0a 20 20 69 66 28 20 7a 53 71 /form>. if( zSq
0900: 6c 5b 30 5d 20 29 7b 0a 20 20 20 20 6c 6f 67 69 l[0] ){. logi
0910: 6e 5f 76 65 72 69 66 79 5f 63 73 72 66 5f 73 65 n_verify_csrf_se
0920: 63 72 65 74 28 29 3b 0a 20 20 20 20 73 71 6c 69 cret();. sqli
0930: 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a te3_set_authoriz
0940: 65 72 28 67 2e 64 62 2c 20 73 65 6c 65 63 74 4f er(g.db, selectO
0950: 6e 6c 79 2c 20 30 29 3b 0a 20 20 20 20 64 62 5f nly, 0);. db_
0960: 67 65 6e 65 72 69 63 5f 71 75 65 72 79 5f 76 69 generic_query_vi
0970: 65 77 28 7a 53 71 6c 2c 20 30 29 3b 0a 20 20 20 ew(zSql, 0);.
0980: 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 sqlite3_set_aut
0990: 68 6f 72 69 7a 65 72 28 67 2e 64 62 2c 20 30 2c horizer(g.db, 0,
09a0: 20 30 29 3b 0a 20 20 7d 0a 20 20 73 74 79 6c 65 0);. }. style
09b0: 5f 66 6f 6f 74 65 72 28 29 3b 0a 7d 0a _footer();.}.