Hex Artifact Content
Not logged in

Artifact 83d1c6c6c3f4b3f168251a12e56dc6492e57ce2a:

File src/admin.c part of check-in [138177c30e] - correction to the 'warning' text in admin_sql_page() by stephan on 2008-02-08 16:27:25. Also file src/admin.c part of check-in [373e42de1d] - Merge two branches that were created within one second of one another. There is a race condition in the anti-forking logic that allowed this fork to occur. by drh on 2008-02-08 17:27:16.

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 59 6f 75 20 63 61 6e 20 65 6e 74  .  @ You can ent
0710: 65 72 20 6f 6e 6c 79 20 53 45 4c 45 43 54 20 73  er only SELECT s
0720: 74 61 74 65 6d 65 6e 74 73 20 68 65 72 65 2c 20  tatements here, 
0730: 61 6e 64 20 73 6f 6d 65 20 53 51 4c 2d 73 69 64  and some SQL-sid
0740: 65 20 66 75 6e 63 74 69 6f 6e 73 0a 20 20 40 20  e functions.  @ 
0750: 61 72 65 20 61 6c 73 6f 20 72 65 73 74 72 69 63  are also restric
0760: 74 65 64 2e 3c 62 72 2f 3e 0a 20 20 40 20 3c 66  ted.<br/>.  @ <f
0770: 6f 72 6d 20 61 63 74 69 6f 6e 3d 27 27 20 6d 65  orm action='' me
0780: 74 68 6f 64 3d 27 70 6f 73 74 27 3e 0a 20 20 40  thod='post'>.  @
0790: 20 3c 74 65 78 74 61 72 65 61 20 73 74 79 6c 65   <textarea style
07a0: 3d 27 62 6f 72 64 65 72 3a 32 70 78 20 73 6f 6c  ='border:2px sol
07b0: 69 64 20 62 6c 61 63 6b 27 20 6e 61 6d 65 3d 27  id black' name='
07c0: 73 71 6c 27 0a 20 20 40 20 20 63 6f 6c 73 3d 27  sql'.  @  cols='
07d0: 38 30 27 20 72 6f 77 73 3d 27 35 27 3e 25 68 28  80' rows='5'>%h(
07e0: 7a 53 71 6c 29 3c 2f 74 65 78 74 61 72 65 61 3e  zSql)</textarea>
07f0: 0a 20 20 40 20 3c 62 72 2f 3e 3c 69 6e 70 75 74  .  @ <br/><input
0800: 20 74 79 70 65 3d 27 73 75 62 6d 69 74 27 20 6e   type='submit' n
0810: 61 6d 65 3d 27 73 71 6c 5f 73 75 62 6d 69 74 27  ame='sql_submit'
0820: 2f 3e 20 3c 69 6e 70 75 74 20 74 79 70 65 3d 27  /> <input type='
0830: 72 65 73 65 74 27 2f 3e 0a 20 20 40 20 3c 2f 66  reset'/>.  @ </f
0840: 6f 72 6d 3e 0a 20 20 69 66 28 20 7a 53 71 6c 5b  orm>.  if( zSql[
0850: 30 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  0] ){.    sqlite
0860: 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72  3_set_authorizer
0870: 28 67 2e 64 62 2c 20 73 65 6c 65 63 74 4f 6e 6c  (g.db, selectOnl
0880: 79 2c 20 30 29 3b 0a 20 20 20 20 64 62 5f 67 65  y, 0);.    db_ge
0890: 6e 65 72 69 63 5f 71 75 65 72 79 5f 76 69 65 77  neric_query_view
08a0: 28 7a 53 71 6c 2c 20 30 29 3b 0a 20 20 20 20 73  (zSql, 0);.    s
08b0: 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f  qlite3_set_autho
08c0: 72 69 7a 65 72 28 67 2e 64 62 2c 20 30 2c 20 30  rizer(g.db, 0, 0
08d0: 29 3b 0a 20 20 7d 0a 20 20 73 74 79 6c 65 5f 66  );.  }.  style_f
08e0: 6f 6f 74 65 72 28 29 3b 0a 7d 0a                 ooter();.}.