Artifact 8499f278199c6c7f13c15d1299d9ac6d5db50ae5:
File
src/admin.c
part of check-in
[1f1d96529c]
- Users unconditionally inherit capabilities of "anonymous". New capability
"v" means to inherit capabilities of user "developer". Login is prohibited
if the password is empty.
by
drh on
2008-08-03 16:47:43.
Also file
src/admin.c
part of check-in
[5fb14b9a0f]
- Include non-sym- tags in tagview web page. Also merge mainline into tagview branch.
by
eric on
2008-08-21 20:59:01.
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 76 6f 69 turn rc;.}...voi
0620: 64 20 61 64 6d 69 6e 5f 70 72 65 70 61 72 65 5f d admin_prepare_
0630: 73 75 62 6d 65 6e 75 28 29 7b 0a 20 20 69 66 28 submenu(){. if(
0640: 20 67 2e 6f 6b 41 64 6d 69 6e 20 29 7b 0a 20 20 g.okAdmin ){.
0650: 20 20 73 74 79 6c 65 5f 73 75 62 6d 65 6e 75 5f style_submenu_
0660: 65 6c 65 6d 65 6e 74 28 22 4d 61 69 6e 22 2c 20 element("Main",
0670: 22 4d 61 69 6e 20 61 64 6d 69 6e 20 70 61 67 65 "Main admin page
0680: 22 2c 20 22 25 73 2f 61 64 6d 69 6e 22 2c 20 67 ", "%s/admin", g
0690: 2e 7a 54 6f 70 20 29 3b 0a 20 20 20 20 73 74 79 .zTop );. sty
06a0: 6c 65 5f 73 75 62 6d 65 6e 75 5f 65 6c 65 6d 65 le_submenu_eleme
06b0: 6e 74 28 22 53 51 4c 22 2c 20 22 53 51 4c 20 70 nt("SQL", "SQL p
06c0: 61 67 65 22 2c 20 22 25 73 2f 61 64 6d 69 6e 2f age", "%s/admin/
06d0: 73 71 6c 22 2c 20 67 2e 7a 54 6f 70 20 29 3b 0a sql", g.zTop );.
06e0: 20 20 20 20 73 74 79 6c 65 5f 73 75 62 6d 65 6e style_submen
06f0: 75 5f 65 6c 65 6d 65 6e 74 28 22 53 65 74 75 70 u_element("Setup
0700: 22 2c 20 22 53 65 74 75 70 20 70 61 67 65 22 2c ", "Setup page",
0710: 20 22 25 73 2f 73 65 74 75 70 22 2c 20 67 2e 7a "%s/setup", g.z
0720: 54 6f 70 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f Top );. }.}.../
0730: 2a 0a 2a 2a 20 57 45 42 50 41 47 45 3a 20 2f 61 *.** WEBPAGE: /a
0740: 64 6d 69 6e 2f 73 71 6c 0a 2a 2f 0a 76 6f 69 64 dmin/sql.*/.void
0750: 20 61 64 6d 69 6e 5f 73 71 6c 5f 70 61 67 65 28 admin_sql_page(
0760: 76 6f 69 64 29 7b 0a 20 20 63 6f 6e 73 74 20 63 void){. const c
0770: 68 61 72 20 2a 7a 53 71 6c 20 3d 20 50 44 28 22 har *zSql = PD("
0780: 73 71 6c 22 2c 22 22 29 3b 0a 20 20 6c 6f 67 69 sql","");. logi
0790: 6e 5f 63 68 65 63 6b 5f 63 72 65 64 65 6e 74 69 n_check_credenti
07a0: 61 6c 73 28 29 3b 0a 20 20 69 66 28 20 21 67 2e als();. if( !g.
07b0: 6f 6b 41 64 6d 69 6e 20 29 7b 0a 20 20 20 20 6c okAdmin ){. l
07c0: 6f 67 69 6e 5f 6e 65 65 64 65 64 28 29 3b 0a 20 ogin_needed();.
07d0: 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 return;. }.
07e0: 20 61 64 6d 69 6e 5f 70 72 65 70 61 72 65 5f 73 admin_prepare_s
07f0: 75 62 6d 65 6e 75 28 29 3b 0a 20 20 73 74 79 6c ubmenu();. styl
0800: 65 5f 68 65 61 64 65 72 28 22 41 64 6d 69 6e 20 e_header("Admin
0810: 53 51 4c 22 29 3b 0a 20 20 40 20 3c 68 32 3e 53 SQL");. @ <h2>S
0820: 51 4c 3a 3c 2f 68 32 3e 0a 20 20 40 20 59 6f 75 QL:</h2>. @ You
0830: 20 63 61 6e 20 65 6e 74 65 72 20 6f 6e 6c 79 20 can enter only
0840: 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
0850: 73 20 68 65 72 65 2c 20 61 6e 64 20 73 6f 6d 65 s here, and some
0860: 20 53 51 4c 2d 73 69 64 65 20 66 75 6e 63 74 69 SQL-side functi
0870: 6f 6e 73 0a 20 20 40 20 61 72 65 20 61 6c 73 6f ons. @ are also
0880: 20 72 65 73 74 72 69 63 74 65 64 2e 3c 62 72 2f restricted.<br/
0890: 3e 0a 20 20 40 20 3c 66 6f 72 6d 20 61 63 74 69 >. @ <form acti
08a0: 6f 6e 3d 27 27 20 6d 65 74 68 6f 64 3d 27 70 6f on='' method='po
08b0: 73 74 27 3e 0a 20 20 40 20 3c 74 65 78 74 61 72 st'>. @ <textar
08c0: 65 61 20 73 74 79 6c 65 3d 27 62 6f 72 64 65 72 ea style='border
08d0: 3a 32 70 78 20 73 6f 6c 69 64 20 62 6c 61 63 6b :2px solid black
08e0: 27 20 6e 61 6d 65 3d 27 73 71 6c 27 0a 20 20 40 ' name='sql'. @
08f0: 20 20 63 6f 6c 73 3d 27 38 30 27 20 72 6f 77 73 cols='80' rows
0900: 3d 27 35 27 3e 25 68 28 7a 53 71 6c 29 3c 2f 74 ='5'>%h(zSql)</t
0910: 65 78 74 61 72 65 61 3e 0a 20 20 40 20 3c 62 72 extarea>. @ <br
0920: 2f 3e 3c 69 6e 70 75 74 20 74 79 70 65 3d 27 73 /><input type='s
0930: 75 62 6d 69 74 27 20 6e 61 6d 65 3d 27 73 71 6c ubmit' name='sql
0940: 5f 73 75 62 6d 69 74 27 2f 3e 20 3c 69 6e 70 75 _submit'/> <inpu
0950: 74 20 74 79 70 65 3d 27 72 65 73 65 74 27 2f 3e t type='reset'/>
0960: 0a 20 20 40 20 3c 2f 66 6f 72 6d 3e 0a 20 20 69 . @ </form>. i
0970: 66 28 20 7a 53 71 6c 5b 30 5d 20 29 7b 0a 20 20 f( zSql[0] ){.
0980: 20 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 sqlite3_set_au
0990: 74 68 6f 72 69 7a 65 72 28 67 2e 64 62 2c 20 73 thorizer(g.db, s
09a0: 65 6c 65 63 74 4f 6e 6c 79 2c 20 30 29 3b 0a 20 electOnly, 0);.
09b0: 20 20 20 64 62 5f 67 65 6e 65 72 69 63 5f 71 75 db_generic_qu
09c0: 65 72 79 5f 76 69 65 77 28 7a 53 71 6c 2c 20 30 ery_view(zSql, 0
09d0: 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 );. sqlite3_s
09e0: 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 67 2e et_authorizer(g.
09f0: 64 62 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 db, 0, 0);. }.
0a00: 20 73 74 79 6c 65 5f 66 6f 6f 74 65 72 28 29 3b style_footer();
0a10: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 45 42 50 41 47 .}../*.** WEBPAG
0a20: 45 3a 20 2f 61 64 6d 69 6e 0a 2a 2f 0a 76 6f 69 E: /admin.*/.voi
0a30: 64 20 61 64 6d 69 6e 5f 70 61 67 65 28 76 6f 69 d admin_page(voi
0a40: 64 29 7b 0a 20 20 6c 6f 67 69 6e 5f 63 68 65 63 d){. login_chec
0a50: 6b 5f 63 72 65 64 65 6e 74 69 61 6c 73 28 29 3b k_credentials();
0a60: 0a 20 20 69 66 28 20 21 67 2e 6f 6b 41 64 6d 69 . if( !g.okAdmi
0a70: 6e 20 29 7b 0a 20 20 20 20 6c 6f 67 69 6e 5f 6e n ){. login_n
0a80: 65 65 64 65 64 28 29 3b 0a 20 20 20 20 72 65 74 eeded();. ret
0a90: 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 urn;. }. if( g
0aa0: 2e 7a 45 78 74 72 61 20 26 26 20 67 2e 7a 45 78 .zExtra && g.zEx
0ab0: 74 72 61 5b 30 5d 20 29 7b 0a 20 20 20 20 69 66 tra[0] ){. if
0ac0: 28 67 2e 7a 45 78 74 72 61 20 3d 3d 20 73 74 72 (g.zExtra == str
0ad0: 73 74 72 28 67 2e 7a 45 78 74 72 61 2c 22 73 71 str(g.zExtra,"sq
0ae0: 6c 22 29 29 20 61 64 6d 69 6e 5f 73 71 6c 5f 70 l")) admin_sql_p
0af0: 61 67 65 28 29 3b 0a 20 20 20 20 2f 2a 20 46 49 age();. /* FI
0b00: 58 4d 45 3a 20 5e 5e 5e 20 74 68 69 73 20 5e 5e XME: ^^^ this ^^
0b10: 5e 20 69 73 20 61 6e 20 61 77 66 75 6c 20 6c 6f ^ is an awful lo
0b20: 74 20 6f 66 20 77 6f 72 6b 2c 20 65 73 70 65 63 t of work, espec
0b30: 69 61 6c 6c 79 20 6f 6e 63 65 0a 20 20 20 20 2a ially once. *
0b40: 2a 20 74 68 65 20 70 61 74 68 73 20 64 65 65 70 * the paths deep
0b50: 65 6e 2e 20 46 69 67 75 72 65 20 6f 75 74 20 61 en. Figure out a
0b60: 20 77 61 79 20 74 6f 20 73 69 6d 70 6c 69 66 79 way to simplify
0b70: 20 64 69 73 70 61 74 63 68 69 6e 67 2e 0a 20 20 dispatching..
0b80: 20 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 3b */. return;
0b90: 0a 20 20 7d 0a 20 20 61 64 6d 69 6e 5f 70 72 65 . }. admin_pre
0ba0: 70 61 72 65 5f 73 75 62 6d 65 6e 75 28 29 3b 0a pare_submenu();.
0bb0: 20 20 73 74 79 6c 65 5f 68 65 61 64 65 72 28 22 style_header("
0bc0: 41 64 6d 69 6e 22 29 3b 0a 20 20 40 20 3c 68 32 Admin");. @ <h2
0bd0: 3e 4c 69 6e 6b 73 3a 3c 2f 68 32 3e 0a 20 20 40 >Links:</h2>. @
0be0: 20 3c 75 6c 3e 0a 20 20 40 20 3c 6c 69 3e 3c 61 <ul>. @ <li><a
0bf0: 20 68 72 65 66 3d 27 25 73 28 67 2e 7a 42 61 73 href='%s(g.zBas
0c00: 65 55 52 4c 29 2f 73 65 74 75 70 27 3e 46 6f 73 eURL)/setup'>Fos
0c10: 73 69 6c 20 57 57 57 20 53 65 74 75 70 3c 2f 61 sil WWW Setup</a
0c20: 3e 3c 2f 6c 69 3e 0a 20 20 40 20 3c 6c 69 3e 3c ></li>. @ <li><
0c30: 61 20 68 72 65 66 3d 27 25 73 28 67 2e 7a 42 61 a href='%s(g.zBa
0c40: 73 65 55 52 4c 29 2f 61 64 6d 69 6e 2f 73 71 6c seURL)/admin/sql
0c50: 27 3e 52 75 6e 20 53 51 4c 20 71 75 65 72 69 65 '>Run SQL querie
0c60: 73 3c 2f 61 3e 3c 2f 6c 69 3e 0a 20 20 40 20 3c s</a></li>. @ <
0c70: 2f 75 6c 3e 0a 20 20 73 74 79 6c 65 5f 66 6f 6f /ul>. style_foo
0c80: 74 65 72 28 29 3b 0a 7d 0a ter();.}.