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 36 20 44 2e 20 52 69 63 68 (c) 2006 D. Rich
0020: 61 72 64 20 48 69 70 70 0a 2a 2a 0a 2a 2a 20 54 ard Hipp.**.** T
0030: 68 69 73 20 70 72 6f 67 72 61 6d 20 69 73 20 66 his program is f
0040: 72 65 65 20 73 6f 66 74 77 61 72 65 3b 20 79 6f ree software; yo
0050: 75 20 63 61 6e 20 72 65 64 69 73 74 72 69 62 75 u can redistribu
0060: 74 65 20 69 74 20 61 6e 64 2f 6f 72 0a 2a 2a 20 te it and/or.**
0070: 6d 6f 64 69 66 79 20 69 74 20 75 6e 64 65 72 20 modify it under
0080: 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65 the terms of the
0090: 20 47 4e 55 20 47 65 6e 65 72 61 6c 20 50 75 62 GNU General Pub
00a0: 6c 69 63 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 76 lic.** License v
00b0: 65 72 73 69 6f 6e 20 32 20 61 73 20 70 75 62 6c ersion 2 as publ
00c0: 69 73 68 65 64 20 62 79 20 74 68 65 20 46 72 65 ished by the Fre
00d0: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64 e Software Found
00e0: 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 ation..**.** Thi
00f0: 73 20 70 72 6f 67 72 61 6d 20 69 73 20 64 69 73 s program is dis
0100: 74 72 69 62 75 74 65 64 20 69 6e 20 74 68 65 20 tributed in the
0110: 68 6f 70 65 20 74 68 61 74 20 69 74 20 77 69 6c hope that it wil
0120: 6c 20 62 65 20 75 73 65 66 75 6c 2c 0a 2a 2a 20 l be useful,.**
0130: 62 75 74 20 57 49 54 48 4f 55 54 20 41 4e 59 20 but WITHOUT ANY
0140: 57 41 52 52 41 4e 54 59 3b 20 77 69 74 68 6f 75 WARRANTY; withou
0150: 74 20 65 76 65 6e 20 74 68 65 20 69 6d 70 6c 69 t even the impli
0160: 65 64 20 77 61 72 72 61 6e 74 79 20 6f 66 0a 2a ed warranty of.*
0170: 2a 20 4d 45 52 43 48 41 4e 54 41 42 49 4c 49 54 * MERCHANTABILIT
0180: 59 20 6f 72 20 46 49 54 4e 45 53 53 20 46 4f 52 Y or FITNESS FOR
0190: 20 41 20 50 41 52 54 49 43 55 4c 41 52 20 50 55 A PARTICULAR PU
01a0: 52 50 4f 53 45 2e 20 20 53 65 65 20 74 68 65 20 RPOSE. See the
01b0: 47 4e 55 0a 2a 2a 20 47 65 6e 65 72 61 6c 20 50 GNU.** General P
01c0: 75 62 6c 69 63 20 4c 69 63 65 6e 73 65 20 66 6f ublic License fo
01d0: 72 20 6d 6f 72 65 20 64 65 74 61 69 6c 73 2e 0a r more details..
01e0: 2a 2a 0a 2a 2a 20 59 6f 75 20 73 68 6f 75 6c 64 **.** You should
01f0: 20 68 61 76 65 20 72 65 63 65 69 76 65 64 20 61 have received a
0200: 20 63 6f 70 79 20 6f 66 20 74 68 65 20 47 4e 55 copy of the GNU
0210: 20 47 65 6e 65 72 61 6c 20 50 75 62 6c 69 63 0a General Public.
0220: 2a 2a 20 4c 69 63 65 6e 73 65 20 61 6c 6f 6e 67 ** License along
0230: 20 77 69 74 68 20 74 68 69 73 20 6c 69 62 72 61 with this libra
0240: 72 79 3b 20 69 66 20 6e 6f 74 2c 20 77 72 69 74 ry; if not, writ
0250: 65 20 74 6f 20 74 68 65 0a 2a 2a 20 46 72 65 65 e to the.** Free
0260: 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64 61 Software Founda
0270: 74 69 6f 6e 2c 20 49 6e 63 2e 2c 20 35 39 20 54 tion, Inc., 59 T
0280: 65 6d 70 6c 65 20 50 6c 61 63 65 20 2d 20 53 75 emple Place - Su
0290: 69 74 65 20 33 33 30 2c 0a 2a 2a 20 42 6f 73 74 ite 330,.** Bost
02a0: 6f 6e 2c 20 4d 41 20 20 30 32 31 31 31 2d 31 33 on, MA 02111-13
02b0: 30 37 2c 20 55 53 41 2e 0a 2a 2a 0a 2a 2a 20 41 07, USA..**.** A
02c0: 75 74 68 6f 72 20 63 6f 6e 74 61 63 74 20 69 6e uthor contact in
02d0: 66 6f 72 6d 61 74 69 6f 6e 3a 0a 2a 2a 20 20 20 formation:.**
02e0: 64 72 68 40 68 77 61 63 69 2e 63 6f 6d 0a 2a 2a drh@hwaci.com.**
02f0: 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 68 77 http://www.hw
0300: 61 63 69 2e 63 6f 6d 2f 64 72 68 2f 0a 2a 2a 0a aci.com/drh/.**.
0310: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0320: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0330: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0340: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0350: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
0360: 2a 2a 0a 2a 2a 20 43 6f 6d 6d 61 6e 64 73 20 61 **.** Commands a
0370: 6e 64 20 70 72 6f 63 65 64 75 72 65 73 20 75 73 nd procedures us
0380: 65 64 20 66 6f 72 20 63 72 65 61 74 69 6e 67 2c ed for creating,
0390: 20 70 72 6f 63 65 73 73 69 6e 67 2c 20 65 64 69 processing, edi
03a0: 74 69 6e 67 2c 20 61 6e 64 0a 2a 2a 20 71 75 65 ting, and.** que
03b0: 72 79 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f rying informatio
03c0: 6e 20 61 62 6f 75 74 20 75 73 65 72 73 2e 0a 2a n about users..*
03d0: 2f 0a 23 69 6e 63 6c 75 64 65 20 22 63 6f 6e 66 /.#include "conf
03e0: 69 67 2e 68 22 0a 23 69 6e 63 6c 75 64 65 20 22 ig.h".#include "
03f0: 75 73 65 72 2e 68 22 0a 0a 0a 2f 2a 0a 2a 2a 20 user.h".../*.**
0400: 53 74 72 69 70 20 6c 65 61 64 69 6e 67 20 61 6e Strip leading an
0410: 64 20 74 72 61 69 6c 69 6e 67 20 73 70 61 63 65 d trailing space
0420: 20 66 72 6f 6d 20 61 20 73 74 72 69 6e 67 20 61 from a string a
0430: 6e 64 20 61 64 64 20 74 68 65 20 73 74 72 69 6e nd add the strin
0440: 67 0a 2a 2a 20 6f 6e 74 6f 20 74 68 65 20 65 6e g.** onto the en
0450: 64 20 6f 66 20 61 20 62 6c 6f 62 2e 0a 2a 2f 0a d of a blob..*/.
0460: 73 74 61 74 69 63 20 76 6f 69 64 20 73 74 72 69 static void stri
0470: 70 5f 73 74 72 69 6e 67 28 42 6c 6f 62 20 2a 70 p_string(Blob *p
0480: 42 6c 6f 62 2c 20 63 68 61 72 20 2a 7a 29 7b 0a Blob, char *z){.
0490: 20 20 69 6e 74 20 69 3b 0a 20 20 62 6c 6f 62 5f int i;. blob_
04a0: 72 65 73 65 74 28 70 42 6c 6f 62 29 3b 0a 20 20 reset(pBlob);.
04b0: 77 68 69 6c 65 28 20 69 73 73 70 61 63 65 28 2a while( isspace(*
04c0: 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 66 z) ){ z++; }. f
04d0: 6f 72 28 69 3d 30 3b 20 7a 5b 69 5d 3b 20 69 2b or(i=0; z[i]; i+
04e0: 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 5b 69 5d +){. if( z[i]
04f0: 3d 3d 27 5c 72 27 20 7c 7c 20 7a 5b 69 5d 3d 3d =='\r' || z[i]==
0500: 27 5c 6e 27 20 29 7b 0a 20 20 20 20 20 20 20 77 '\n' ){. w
0510: 68 69 6c 65 28 20 69 3e 30 20 26 26 20 69 73 73 hile( i>0 && iss
0520: 70 61 63 65 28 7a 5b 69 2d 31 5d 29 20 29 7b 20 pace(z[i-1]) ){
0530: 69 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 20 7a 5b i--; }. z[
0540: 69 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 62 i] = 0;. b
0550: 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
0560: 69 66 28 20 7a 5b 69 5d 3c 27 20 27 20 29 20 7a if( z[i]<' ' ) z
0570: 5b 69 5d 20 3d 20 27 20 27 3b 0a 20 20 7d 0a 20 [i] = ' ';. }.
0580: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 28 70 42 6c blob_append(pBl
0590: 6f 62 2c 20 7a 2c 20 2d 31 29 3b 0a 7d 0a 0a 23 ob, z, -1);.}..#
05a0: 69 66 64 65 66 20 5f 5f 4d 49 4e 47 57 33 32 5f ifdef __MINGW32_
05b0: 5f 0a 2f 2a 0a 2a 2a 20 67 65 74 70 61 73 73 20 _./*.** getpass
05c0: 66 6f 72 20 57 69 6e 64 6f 77 73 0a 2a 2f 0a 73 for Windows.*/.s
05d0: 74 61 74 69 63 20 63 68 61 72 20 2a 67 65 74 70 tatic char *getp
05e0: 61 73 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a ass(const char *
05f0: 70 72 6f 6d 70 74 29 7b 0a 20 20 73 74 61 74 69 prompt){. stati
0600: 63 20 63 68 61 72 20 70 77 64 5b 36 34 5d 3b 0a c char pwd[64];.
0610: 20 20 73 69 7a 65 5f 74 20 69 3b 0a 0a 20 20 66 size_t i;.. f
0620: 70 75 74 73 28 70 72 6f 6d 70 74 2c 73 74 64 65 puts(prompt,stde
0630: 72 72 29 3b 0a 20 20 66 66 6c 75 73 68 28 73 74 rr);. fflush(st
0640: 64 65 72 72 29 3b 0a 20 20 66 6f 72 28 69 3d 30 derr);. for(i=0
0650: 3b 20 69 3c 73 69 7a 65 6f 66 28 70 77 64 29 2d ; i<sizeof(pwd)-
0660: 31 3b 20 2b 2b 69 29 7b 0a 20 20 20 20 70 77 64 1; ++i){. pwd
0670: 5b 69 5d 20 3d 20 5f 67 65 74 63 68 28 29 3b 0a [i] = _getch();.
0680: 20 20 20 20 69 66 28 70 77 64 5b 69 5d 3d 3d 27 if(pwd[i]=='
0690: 5c 72 27 20 7c 7c 20 70 77 64 5b 69 5d 3d 3d 27 \r' || pwd[i]=='
06a0: 5c 6e 27 29 7b 0a 20 20 20 20 20 20 62 72 65 61 \n'){. brea
06b0: 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 k;. }. /*
06c0: 42 53 20 6f 72 20 44 45 4c 20 2a 2f 0a 20 20 20 BS or DEL */.
06d0: 20 65 6c 73 65 20 69 66 28 69 3e 30 20 26 26 20 else if(i>0 &&
06e0: 28 70 77 64 5b 69 5d 3d 3d 38 20 7c 7c 20 70 77 (pwd[i]==8 || pw
06f0: 64 5b 69 5d 3d 3d 31 32 37 29 29 7b 0a 20 20 20 d[i]==127)){.
0700: 20 20 20 69 20 2d 3d 20 32 3b 0a 20 20 20 20 20 i -= 2;.
0710: 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d continue;. }
0720: 0a 20 20 20 20 2f 2a 20 43 54 52 4c 2d 43 20 2a . /* CTRL-C *
0730: 2f 0a 20 20 20 20 65 6c 73 65 20 69 66 28 70 77 /. else if(pw
0740: 64 5b 69 5d 3d 3d 33 29 20 7b 0a 20 20 20 20 20 d[i]==3) {.
0750: 20 69 3d 30 3b 0a 20 20 20 20 20 20 62 72 65 61 i=0;. brea
0760: 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 k;. }. /*
0770: 45 53 43 20 2a 2f 0a 20 20 20 20 65 6c 73 65 20 ESC */. else
0780: 69 66 28 70 77 64 5b 69 5d 3d 3d 32 37 29 7b 0a if(pwd[i]==27){.
0790: 20 20 20 20 20 20 69 3d 30 3b 0a 20 20 20 20 20 i=0;.
07a0: 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
07b0: 20 20 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 70 else{. fp
07c0: 75 74 63 28 27 2a 27 2c 73 74 64 65 72 72 29 3b utc('*',stderr);
07d0: 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 77 64 . }. }. pwd
07e0: 5b 69 5d 3d 27 5c 30 27 3b 0a 20 20 66 70 75 74 [i]='\0';. fput
07f0: 73 28 22 5c 6e 22 2c 20 73 74 64 65 72 72 29 3b s("\n", stderr);
0800: 0a 20 20 72 65 74 75 72 6e 20 70 77 64 3b 0a 7d . return pwd;.}
0810: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 .#endif../*.** D
0820: 6f 20 61 20 73 69 6e 67 6c 65 20 70 72 6f 6d 70 o a single promp
0830: 74 20 66 6f 72 20 61 20 70 61 73 73 70 68 72 61 t for a passphra
0840: 73 65 2e 20 20 53 74 6f 72 65 20 74 68 65 20 72 se. Store the r
0850: 65 73 75 6c 74 73 20 69 6e 20 74 68 65 20 62 6c esults in the bl
0860: 6f 62 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ob..*/.static vo
0870: 69 64 20 70 72 6f 6d 70 74 5f 66 6f 72 5f 70 61 id prompt_for_pa
0880: 73 73 70 68 72 61 73 65 28 63 6f 6e 73 74 20 63 ssphrase(const c
0890: 68 61 72 20 2a 7a 50 72 6f 6d 70 74 2c 20 42 6c har *zPrompt, Bl
08a0: 6f 62 20 2a 70 50 61 73 73 70 68 72 61 73 65 29 ob *pPassphrase)
08b0: 7b 0a 20 20 63 68 61 72 20 2a 7a 20 3d 20 67 65 {. char *z = ge
08c0: 74 70 61 73 73 28 7a 50 72 6f 6d 70 74 29 3b 0a tpass(zPrompt);.
08d0: 20 20 73 74 72 69 70 5f 73 74 72 69 6e 67 28 70 strip_string(p
08e0: 50 61 73 73 70 68 72 61 73 65 2c 20 7a 29 3b 0a Passphrase, z);.
08f0: 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 6d 70 74 20 }../*.** Prompt
0900: 74 68 65 20 75 73 65 72 20 66 6f 72 20 61 20 70 the user for a p
0910: 61 73 73 77 6f 72 64 2e 20 20 53 74 6f 72 65 20 assword. Store
0920: 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 74 68 the result in th
0930: 65 20 70 50 61 73 73 70 68 72 61 73 65 0a 2a 2a e pPassphrase.**
0940: 20 62 6c 6f 62 2e 0a 2a 2a 0a 2a 2a 20 42 65 68 blob..**.** Beh
0950: 61 76 69 6f 72 20 69 73 20 63 6f 6e 74 72 6f 6c avior is control
0960: 6c 65 64 20 62 79 20 74 68 65 20 76 65 72 69 66 led by the verif
0970: 79 20 70 61 72 61 6d 65 74 65 72 3a 0a 2a 2a 0a y parameter:.**.
0980: 2a 2a 20 20 20 20 20 30 20 20 20 20 20 4a 75 73 ** 0 Jus
0990: 74 20 61 73 6b 20 6f 6e 63 65 2e 0a 2a 2a 0a 2a t ask once..**.*
09a0: 2a 20 20 20 20 20 31 20 20 20 20 20 49 66 20 74 * 1 If t
09b0: 68 65 20 66 69 72 73 74 20 61 6e 73 77 65 72 20 he first answer
09c0: 69 73 20 61 20 6e 6f 6e 2d 65 6d 70 74 79 20 73 is a non-empty s
09d0: 74 72 69 6e 67 2c 20 61 73 6b 20 66 6f 72 0a 2a tring, ask for.*
09e0: 2a 20 20 20 20 20 20 20 20 20 20 20 76 65 72 69 * veri
09f0: 66 69 63 61 74 69 6f 6e 2e 20 20 52 65 70 65 61 fication. Repea
0a00: 74 20 69 66 20 74 68 65 20 74 77 6f 20 73 74 72 t if the two str
0a10: 69 6e 67 73 20 64 6f 20 6e 6f 74 20 6d 61 74 63 ings do not matc
0a20: 68 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 32 20 20 h..**.** 2
0a30: 20 20 20 41 73 6b 20 74 77 69 63 65 2c 20 72 65 Ask twice, re
0a40: 70 65 61 74 20 69 66 20 74 68 65 20 73 74 72 69 peat if the stri
0a50: 6e 67 73 20 64 6f 20 6e 6f 74 20 6d 61 74 63 68 ngs do not match
0a60: 2e 0a 2a 2f 0a 76 6f 69 64 20 70 72 6f 6d 70 74 ..*/.void prompt
0a70: 5f 66 6f 72 5f 70 61 73 73 77 6f 72 64 28 0a 20 _for_password(.
0a80: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 72 const char *zPr
0a90: 6f 6d 70 74 2c 0a 20 20 42 6c 6f 62 20 2a 70 50 ompt,. Blob *pP
0aa0: 61 73 73 70 68 72 61 73 65 2c 0a 20 20 69 6e 74 assphrase,. int
0ab0: 20 76 65 72 69 66 79 0a 29 7b 0a 20 20 42 6c 6f verify.){. Blo
0ac0: 62 20 73 65 63 6f 6e 64 54 72 79 3b 0a 20 20 62 b secondTry;. b
0ad0: 6c 6f 62 5f 7a 65 72 6f 28 70 50 61 73 73 70 68 lob_zero(pPassph
0ae0: 72 61 73 65 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 rase);. blob_ze
0af0: 72 6f 28 26 73 65 63 6f 6e 64 54 72 79 29 3b 0a ro(&secondTry);.
0b00: 20 20 77 68 69 6c 65 28 31 29 7b 0a 20 20 20 20 while(1){.
0b10: 70 72 6f 6d 70 74 5f 66 6f 72 5f 70 61 73 73 70 prompt_for_passp
0b20: 68 72 61 73 65 28 7a 50 72 6f 6d 70 74 2c 20 70 hrase(zPrompt, p
0b30: 50 61 73 73 70 68 72 61 73 65 29 3b 0a 20 20 20 Passphrase);.
0b40: 20 69 66 28 20 76 65 72 69 66 79 3d 3d 30 20 29 if( verify==0 )
0b50: 20 62 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 break;. if(
0b60: 76 65 72 69 66 79 3d 3d 31 20 26 26 20 62 6c 6f verify==1 && blo
0b70: 62 5f 73 69 7a 65 28 70 50 61 73 73 70 68 72 61 b_size(pPassphra
0b80: 73 65 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a se)==0 ) break;.
0b90: 20 20 20 20 70 72 6f 6d 70 74 5f 66 6f 72 5f 70 prompt_for_p
0ba0: 61 73 73 70 68 72 61 73 65 28 22 41 67 61 69 6e assphrase("Again
0bb0: 3a 20 22 2c 20 26 73 65 63 6f 6e 64 54 72 79 29 : ", &secondTry)
0bc0: 3b 0a 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 63 ;. if( blob_c
0bd0: 6f 6d 70 61 72 65 28 70 50 61 73 73 70 68 72 61 ompare(pPassphra
0be0: 73 65 2c 20 26 73 65 63 6f 6e 64 54 72 79 29 20 se, &secondTry)
0bf0: 29 7b 0a 20 20 20 20 20 20 70 72 69 6e 74 66 28 ){. printf(
0c00: 22 50 61 73 73 70 68 72 61 73 65 73 20 64 6f 20 "Passphrases do
0c10: 6e 6f 74 20 6d 61 74 63 68 2e 20 20 54 72 79 20 not match. Try
0c20: 61 67 61 69 6e 2e 2e 2e 5c 6e 22 29 3b 0a 20 20 again...\n");.
0c30: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 62 }else{. b
0c40: 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a reak;. }. }.
0c50: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 73 65 blob_reset(&se
0c60: 63 6f 6e 64 54 72 79 29 3b 0a 7d 0a 0a 2f 2a 0a condTry);.}../*.
0c70: 2a 2a 20 50 72 6f 6d 70 74 20 74 68 65 20 75 73 ** Prompt the us
0c80: 65 72 20 74 6f 20 65 6e 74 65 72 20 61 20 73 69 er to enter a si
0c90: 6e 67 6c 65 20 6c 69 6e 65 20 6f 66 20 74 65 78 ngle line of tex
0ca0: 74 2e 0a 2a 2f 0a 76 6f 69 64 20 70 72 6f 6d 70 t..*/.void promp
0cb0: 74 5f 75 73 65 72 28 63 6f 6e 73 74 20 63 68 61 t_user(const cha
0cc0: 72 20 2a 7a 50 72 6f 6d 70 74 2c 20 42 6c 6f 62 r *zPrompt, Blob
0cd0: 20 2a 70 49 6e 29 7b 0a 20 20 63 68 61 72 20 2a *pIn){. char *
0ce0: 7a 3b 0a 20 20 63 68 61 72 20 7a 4c 69 6e 65 5b z;. char zLine[
0cf0: 31 30 30 30 5d 3b 0a 20 20 62 6c 6f 62 5f 7a 65 1000];. blob_ze
0d00: 72 6f 28 70 49 6e 29 3b 0a 20 20 70 72 69 6e 74 ro(pIn);. print
0d10: 66 28 22 25 73 22 2c 20 7a 50 72 6f 6d 70 74 29 f("%s", zPrompt)
0d20: 3b 0a 20 20 66 66 6c 75 73 68 28 73 74 64 6f 75 ;. fflush(stdou
0d30: 74 29 3b 0a 20 20 7a 20 3d 20 66 67 65 74 73 28 t);. z = fgets(
0d40: 7a 4c 69 6e 65 2c 20 73 69 7a 65 6f 66 28 7a 4c zLine, sizeof(zL
0d50: 69 6e 65 29 2c 20 73 74 64 69 6e 29 3b 0a 20 20 ine), stdin);.
0d60: 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 73 74 72 if( z ){. str
0d70: 69 70 5f 73 74 72 69 6e 67 28 70 49 6e 2c 20 7a ip_string(pIn, z
0d80: 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a );. }.}.../*.**
0d90: 20 43 4f 4d 4d 41 4e 44 3a 20 20 75 73 65 72 0a COMMAND: user.
0da0: 2a 2a 0a 2a 2a 20 55 73 61 67 65 3a 20 25 66 6f **.** Usage: %fo
0db0: 73 73 69 6c 20 75 73 65 72 20 53 55 42 43 4f 4d ssil user SUBCOM
0dc0: 4d 41 4e 44 20 2e 2e 2e 20 20 3f 2d 52 7c 2d 2d MAND ... ?-R|--
0dd0: 72 65 70 6f 73 69 74 6f 72 79 20 46 49 4c 45 3f repository FILE?
0de0: 0a 2a 2a 0a 2a 2a 20 52 75 6e 20 76 61 72 69 6f .**.** Run vario
0df0: 75 73 20 73 75 62 63 6f 6d 6d 61 6e 64 73 20 6f us subcommands o
0e00: 6e 20 75 73 65 72 73 20 6f 66 20 74 68 65 20 6f n users of the o
0e10: 70 65 6e 20 72 65 70 6f 73 69 74 6f 72 79 20 6f pen repository o
0e20: 72 20 6f 66 0a 2a 2a 20 74 68 65 20 72 65 70 6f r of.** the repo
0e30: 73 69 74 6f 72 79 20 69 64 65 6e 74 69 66 69 65 sitory identifie
0e40: 64 20 62 79 20 74 68 65 20 2d 52 20 6f 72 20 2d d by the -R or -
0e50: 2d 72 65 70 6f 73 69 74 6f 72 79 20 6f 70 74 69 -repository opti
0e60: 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 25 66 6f on..**.** %fo
0e70: 73 73 69 6c 20 75 73 65 72 20 63 61 70 61 62 69 ssil user capabi
0e80: 6c 69 74 69 65 73 20 55 53 45 52 4e 41 4d 45 20 lities USERNAME
0e90: 3f 53 54 52 49 4e 47 3f 0a 2a 2a 0a 2a 2a 20 20 ?STRING?.**.**
0ea0: 20 20 20 20 20 20 51 75 65 72 79 20 6f 72 20 73 Query or s
0eb0: 65 74 20 74 68 65 20 63 61 70 61 62 69 6c 69 74 et the capabilit
0ec0: 69 65 73 20 66 6f 72 20 75 73 65 72 20 55 53 45 ies for user USE
0ed0: 52 4e 41 4d 45 0a 2a 2a 0a 2a 2a 20 20 20 20 25 RNAME.**.** %
0ee0: 66 6f 73 73 69 6c 20 75 73 65 72 20 64 65 66 61 fossil user defa
0ef0: 75 6c 74 20 3f 55 53 45 52 4e 41 4d 45 3f 0a 2a ult ?USERNAME?.*
0f00: 2a 0a 2a 2a 20 20 20 20 20 20 20 20 51 75 65 72 *.** Quer
0f10: 79 20 6f 72 20 73 65 74 20 74 68 65 20 64 65 66 y or set the def
0f20: 61 75 6c 74 20 75 73 65 72 2e 20 20 54 68 65 20 ault user. The
0f30: 64 65 66 61 75 6c 74 20 75 73 65 72 20 69 73 20 default user is
0f40: 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 75 73 the.** us
0f50: 65 72 20 66 6f 72 20 63 6f 6d 6d 61 6e 64 2d 6c er for command-l
0f60: 69 6e 65 20 69 6e 74 65 72 61 63 74 69 6f 6e 2e ine interaction.
0f70: 0a 2a 2a 0a 2a 2a 20 20 20 20 25 66 6f 73 73 69 .**.** %fossi
0f80: 6c 20 75 73 65 72 20 6c 69 73 74 0a 2a 2a 0a 2a l user list.**.*
0f90: 2a 20 20 20 20 20 20 20 20 4c 69 73 74 20 61 6c * List al
0fa0: 6c 20 75 73 65 72 73 20 6b 6e 6f 77 6e 20 74 6f l users known to
0fb0: 20 74 68 65 20 72 65 70 6f 73 69 74 6f 72 79 0a the repository.
0fc0: 2a 2a 0a 2a 2a 20 20 20 20 25 66 6f 73 73 69 6c **.** %fossil
0fd0: 20 75 73 65 72 20 6e 65 77 20 3f 55 53 45 52 4e user new ?USERN
0fe0: 41 4d 45 3f 20 3f 43 4f 4e 54 41 43 54 2d 49 4e AME? ?CONTACT-IN
0ff0: 46 4f 3f 20 3f 50 41 53 53 57 4f 52 44 3f 0a 2a FO? ?PASSWORD?.*
1000: 2a 0a 2a 2a 20 20 20 20 20 20 20 20 43 72 65 61 *.** Crea
1010: 74 65 20 61 20 6e 65 77 20 75 73 65 72 20 69 6e te a new user in
1020: 20 74 68 65 20 72 65 70 6f 73 69 74 6f 72 79 2e the repository.
1030: 20 20 55 73 65 72 73 20 63 61 6e 20 6e 65 76 65 Users can neve
1040: 72 20 62 65 0a 2a 2a 20 20 20 20 20 20 20 20 64 r be.** d
1050: 65 6c 65 74 65 64 2e 20 20 54 68 65 79 20 63 61 eleted. They ca
1060: 6e 20 62 65 20 64 65 6e 69 65 64 20 61 6c 6c 20 n be denied all
1070: 61 63 63 65 73 73 20 62 75 74 20 74 68 65 79 20 access but they
1080: 6d 75 73 74 20 63 6f 6e 74 69 6e 75 65 0a 2a 2a must continue.**
1090: 20 20 20 20 20 20 20 20 74 6f 20 65 78 69 73 74 to exist
10a0: 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
10b0: 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 25 66 6f 73 73 ..**.** %foss
10c0: 69 6c 20 75 73 65 72 20 70 61 73 73 77 6f 72 64 il user password
10d0: 20 55 53 45 52 4e 41 4d 45 20 3f 50 41 53 53 57 USERNAME ?PASSW
10e0: 4f 52 44 3f 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 ORD?.**.**
10f0: 20 20 43 68 61 6e 67 65 20 74 68 65 20 77 65 62 Change the web
1100: 20 61 63 63 65 73 73 20 70 61 73 73 77 6f 72 64 access password
1110: 20 66 6f 72 20 61 20 75 73 65 72 2e 0a 2a 2f 0a for a user..*/.
1120: 76 6f 69 64 20 75 73 65 72 5f 63 6d 64 28 76 6f void user_cmd(vo
1130: 69 64 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 id){. int n;.
1140: 64 62 5f 66 69 6e 64 5f 61 6e 64 5f 6f 70 65 6e db_find_and_open
1150: 5f 72 65 70 6f 73 69 74 6f 72 79 28 31 29 3b 0a _repository(1);.
1160: 20 20 69 66 28 20 67 2e 61 72 67 63 3c 33 20 29 if( g.argc<3 )
1170: 7b 0a 20 20 20 20 75 73 61 67 65 28 22 63 61 70 {. usage("cap
1180: 61 62 69 6c 69 74 69 65 73 7c 64 65 66 61 75 6c abilities|defaul
1190: 74 7c 6c 69 73 74 7c 6e 65 77 7c 70 61 73 73 77 t|list|new|passw
11a0: 6f 72 64 20 2e 2e 2e 22 29 3b 0a 20 20 7d 0a 20 ord ...");. }.
11b0: 20 6e 20 3d 20 73 74 72 6c 65 6e 28 67 2e 61 72 n = strlen(g.ar
11c0: 67 76 5b 32 5d 29 3b 0a 20 20 69 66 28 20 6e 3e gv[2]);. if( n>
11d0: 3d 32 20 26 26 20 73 74 72 6e 63 6d 70 28 67 2e =2 && strncmp(g.
11e0: 61 72 67 76 5b 32 5d 2c 22 6e 65 77 22 2c 6e 29 argv[2],"new",n)
11f0: 3d 3d 30 20 29 7b 0a 20 20 20 20 42 6c 6f 62 20 ==0 ){. Blob
1200: 70 61 73 73 77 64 2c 20 6c 6f 67 69 6e 2c 20 63 passwd, login, c
1210: 6f 6e 74 61 63 74 3b 0a 0a 20 20 20 20 69 66 28 ontact;.. if(
1220: 20 67 2e 61 72 67 63 3e 3d 34 20 29 7b 0a 20 20 g.argc>=4 ){.
1230: 20 20 20 20 62 6c 6f 62 5f 69 6e 69 74 28 26 6c blob_init(&l
1240: 6f 67 69 6e 2c 20 67 2e 61 72 67 76 5b 33 5d 2c ogin, g.argv[3],
1250: 20 2d 31 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b -1);. }else{
1260: 0a 20 20 20 20 20 20 70 72 6f 6d 70 74 5f 75 73 . prompt_us
1270: 65 72 28 22 6c 6f 67 69 6e 3a 20 22 2c 20 26 6c er("login: ", &l
1280: 6f 67 69 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 ogin);. }.
1290: 20 69 66 28 20 64 62 5f 65 78 69 73 74 73 28 22 if( db_exists("
12a0: 53 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 75 73 SELECT 1 FROM us
12b0: 65 72 20 57 48 45 52 45 20 6c 6f 67 69 6e 3d 25 er WHERE login=%
12c0: 42 22 2c 20 26 6c 6f 67 69 6e 29 20 29 7b 0a 20 B", &login) ){.
12d0: 20 20 20 20 20 66 6f 73 73 69 6c 5f 66 61 74 61 fossil_fata
12e0: 6c 28 22 75 73 65 72 20 25 62 20 61 6c 72 65 61 l("user %b alrea
12f0: 64 79 20 65 78 69 73 74 73 22 2c 20 26 6c 6f 67 dy exists", &log
1300: 69 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 in);. }. i
1310: 66 28 20 67 2e 61 72 67 63 3e 3d 35 20 29 7b 0a f( g.argc>=5 ){.
1320: 20 20 20 20 20 20 62 6c 6f 62 5f 69 6e 69 74 28 blob_init(
1330: 26 63 6f 6e 74 61 63 74 2c 20 67 2e 61 72 67 76 &contact, g.argv
1340: 5b 34 5d 2c 20 2d 31 29 3b 0a 20 20 20 20 7d 65 [4], -1);. }e
1350: 6c 73 65 7b 0a 20 20 20 20 20 20 70 72 6f 6d 70 lse{. promp
1360: 74 5f 75 73 65 72 28 22 63 6f 6e 74 61 63 74 2d t_user("contact-
1370: 69 6e 66 6f 3a 20 22 2c 20 26 63 6f 6e 74 61 63 info: ", &contac
1380: 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 t);. }. if
1390: 28 20 67 2e 61 72 67 63 3e 3d 36 20 29 7b 0a 20 ( g.argc>=6 ){.
13a0: 20 20 20 20 20 62 6c 6f 62 5f 69 6e 69 74 28 26 blob_init(&
13b0: 70 61 73 73 77 64 2c 20 67 2e 61 72 67 76 5b 35 passwd, g.argv[5
13c0: 5d 2c 20 2d 31 29 3b 0a 20 20 20 20 7d 65 6c 73 ], -1);. }els
13d0: 65 7b 0a 20 20 20 20 20 20 70 72 6f 6d 70 74 5f e{. prompt_
13e0: 66 6f 72 5f 70 61 73 73 77 6f 72 64 28 22 70 61 for_password("pa
13f0: 73 73 77 6f 72 64 3a 20 22 2c 20 26 70 61 73 73 ssword: ", &pass
1400: 77 64 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20 wd, 1);. }.
1410: 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 db_multi_exec(
1420: 0a 20 20 20 20 20 20 22 49 4e 53 45 52 54 20 49 . "INSERT I
1430: 4e 54 4f 20 75 73 65 72 28 6c 6f 67 69 6e 2c 70 NTO user(login,p
1440: 77 2c 63 61 70 2c 69 6e 66 6f 29 22 0a 20 20 20 w,cap,info)".
1450: 20 20 20 22 56 41 4c 55 45 53 28 25 42 2c 25 42 "VALUES(%B,%B
1460: 2c 27 76 27 2c 25 42 29 22 2c 0a 20 20 20 20 20 ,'v',%B)",.
1470: 20 26 6c 6f 67 69 6e 2c 20 26 70 61 73 73 77 64 &login, &passwd
1480: 2c 20 26 63 6f 6e 74 61 63 74 0a 20 20 20 20 29 , &contact. )
1490: 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3e ;. }else if( n>
14a0: 3d 32 20 26 26 20 73 74 72 6e 63 6d 70 28 67 2e =2 && strncmp(g.
14b0: 61 72 67 76 5b 32 5d 2c 22 64 65 66 61 75 6c 74 argv[2],"default
14c0: 22 2c 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 75 ",n)==0 ){. u
14d0: 73 65 72 5f 73 65 6c 65 63 74 28 29 3b 0a 20 20 ser_select();.
14e0: 20 20 69 66 28 20 67 2e 61 72 67 63 3d 3d 33 20 if( g.argc==3
14f0: 29 7b 0a 20 20 20 20 20 20 70 72 69 6e 74 66 28 ){. printf(
1500: 22 25 73 5c 6e 22 2c 20 67 2e 7a 4c 6f 67 69 6e "%s\n", g.zLogin
1510: 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
1520: 20 20 20 20 69 66 28 20 21 64 62 5f 65 78 69 73 if( !db_exis
1530: 74 73 28 22 53 45 4c 45 43 54 20 31 20 46 52 4f ts("SELECT 1 FRO
1540: 4d 20 75 73 65 72 20 57 48 45 52 45 20 6c 6f 67 M user WHERE log
1550: 69 6e 3d 25 51 22 2c 20 67 2e 61 72 67 76 5b 33 in=%Q", g.argv[3
1560: 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6f ]) ){. fo
1570: 73 73 69 6c 5f 66 61 74 61 6c 28 22 6e 6f 20 73 ssil_fatal("no s
1580: 75 63 68 20 75 73 65 72 3a 20 25 73 22 2c 20 67 uch user: %s", g
1590: 2e 61 72 67 76 5b 33 5d 29 3b 0a 20 20 20 20 20 .argv[3]);.
15a0: 20 7d 0a 20 20 20 20 20 20 69 66 28 20 67 2e 6c }. if( g.l
15b0: 6f 63 61 6c 4f 70 65 6e 20 29 7b 0a 20 20 20 20 ocalOpen ){.
15c0: 20 20 20 20 64 62 5f 6c 73 65 74 28 22 64 65 66 db_lset("def
15d0: 61 75 6c 74 2d 75 73 65 72 22 2c 20 67 2e 61 72 ault-user", g.ar
15e0: 67 76 5b 33 5d 29 3b 0a 20 20 20 20 20 20 7d 65 gv[3]);. }e
15f0: 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 64 62 5f lse{. db_
1600: 73 65 74 28 22 64 65 66 61 75 6c 74 2d 75 73 65 set("default-use
1610: 72 22 2c 20 67 2e 61 72 67 76 5b 33 5d 2c 20 30 r", g.argv[3], 0
1620: 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
1630: 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3e 3d . }else if( n>=
1640: 32 20 26 26 20 73 74 72 6e 63 6d 70 28 67 2e 61 2 && strncmp(g.a
1650: 72 67 76 5b 32 5d 2c 22 6c 69 73 74 22 2c 6e 29 rgv[2],"list",n)
1660: 3d 3d 30 20 29 7b 0a 20 20 20 20 53 74 6d 74 20 ==0 ){. Stmt
1670: 71 3b 0a 20 20 20 20 64 62 5f 70 72 65 70 61 72 q;. db_prepar
1680: 65 28 26 71 2c 20 22 53 45 4c 45 43 54 20 6c 6f e(&q, "SELECT lo
1690: 67 69 6e 2c 20 69 6e 66 6f 20 46 52 4f 4d 20 75 gin, info FROM u
16a0: 73 65 72 20 4f 52 44 45 52 20 42 59 20 6c 6f 67 ser ORDER BY log
16b0: 69 6e 22 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 in");. while(
16c0: 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53 51 db_step(&q)==SQ
16d0: 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 LITE_ROW ){.
16e0: 20 20 70 72 69 6e 74 66 28 22 25 2d 31 32 73 20 printf("%-12s
16f0: 25 73 5c 6e 22 2c 20 64 62 5f 63 6f 6c 75 6d 6e %s\n", db_column
1700: 5f 74 65 78 74 28 26 71 2c 20 30 29 2c 20 64 62 _text(&q, 0), db
1710: 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 26 71 2c _column_text(&q,
1720: 20 31 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 1));. }.
1730: 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b db_finalize(&q);
1740: 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3e 3d . }else if( n>=
1750: 32 20 26 26 20 73 74 72 6e 63 6d 70 28 67 2e 61 2 && strncmp(g.a
1760: 72 67 76 5b 32 5d 2c 22 70 61 73 73 77 6f 72 64 rgv[2],"password
1770: 22 2c 32 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63 ",2)==0 ){. c
1780: 68 61 72 20 2a 7a 50 72 6f 6d 70 74 3b 0a 20 20 har *zPrompt;.
1790: 20 20 69 6e 74 20 75 69 64 3b 0a 20 20 20 20 42 int uid;. B
17a0: 6c 6f 62 20 70 77 3b 0a 20 20 20 20 69 66 28 20 lob pw;. if(
17b0: 67 2e 61 72 67 63 21 3d 34 20 26 26 20 67 2e 61 g.argc!=4 && g.a
17c0: 72 67 63 21 3d 35 20 29 20 75 73 61 67 65 28 22 rgc!=5 ) usage("
17d0: 70 61 73 73 77 6f 72 64 20 55 53 45 52 4e 41 4d password USERNAM
17e0: 45 20 3f 4e 45 57 2d 50 41 53 53 57 4f 52 44 3f E ?NEW-PASSWORD?
17f0: 22 29 3b 0a 20 20 20 20 75 69 64 20 3d 20 64 62 ");. uid = db
1800: 5f 69 6e 74 28 30 2c 20 22 53 45 4c 45 43 54 20 _int(0, "SELECT
1810: 75 69 64 20 46 52 4f 4d 20 75 73 65 72 20 57 48 uid FROM user WH
1820: 45 52 45 20 6c 6f 67 69 6e 3d 25 51 22 2c 20 67 ERE login=%Q", g
1830: 2e 61 72 67 76 5b 33 5d 29 3b 0a 20 20 20 20 69 .argv[3]);. i
1840: 66 28 20 75 69 64 3d 3d 30 20 29 7b 0a 20 20 20 f( uid==0 ){.
1850: 20 20 20 66 6f 73 73 69 6c 5f 66 61 74 61 6c 28 fossil_fatal(
1860: 22 6e 6f 20 73 75 63 68 20 75 73 65 72 3a 20 25 "no such user: %
1870: 73 22 2c 20 67 2e 61 72 67 76 5b 33 5d 29 3b 0a s", g.argv[3]);.
1880: 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 67 2e }. if( g.
1890: 61 72 67 63 3d 3d 35 20 29 7b 0a 20 20 20 20 20 argc==5 ){.
18a0: 20 62 6c 6f 62 5f 69 6e 69 74 28 26 70 77 2c 20 blob_init(&pw,
18b0: 67 2e 61 72 67 76 5b 34 5d 2c 20 2d 31 29 3b 0a g.argv[4], -1);.
18c0: 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
18d0: 20 7a 50 72 6f 6d 70 74 20 3d 20 6d 70 72 69 6e zPrompt = mprin
18e0: 74 66 28 22 6e 65 77 20 70 61 73 73 77 64 20 66 tf("new passwd f
18f0: 6f 72 20 25 73 3a 20 22 2c 20 67 2e 61 72 67 76 or %s: ", g.argv
1900: 5b 33 5d 29 3b 0a 20 20 20 20 20 20 70 72 6f 6d [3]);. prom
1910: 70 74 5f 66 6f 72 5f 70 61 73 73 77 6f 72 64 28 pt_for_password(
1920: 7a 50 72 6f 6d 70 74 2c 20 26 70 77 2c 20 31 29 zPrompt, &pw, 1)
1930: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
1940: 62 6c 6f 62 5f 73 69 7a 65 28 26 70 77 29 3d 3d blob_size(&pw)==
1950: 30 20 29 7b 0a 20 20 20 20 20 20 70 72 69 6e 74 0 ){. print
1960: 66 28 22 70 61 73 73 77 6f 72 64 20 75 6e 63 68 f("password unch
1970: 61 6e 67 65 64 5c 6e 22 29 3b 0a 20 20 20 20 7d anged\n");. }
1980: 65 6c 73 65 7b 0a 20 20 20 20 20 20 64 62 5f 6d else{. db_m
1990: 75 6c 74 69 5f 65 78 65 63 28 22 55 50 44 41 54 ulti_exec("UPDAT
19a0: 45 20 75 73 65 72 20 53 45 54 20 70 77 3d 25 42 E user SET pw=%B
19b0: 20 57 48 45 52 45 20 75 69 64 3d 25 64 22 2c 20 WHERE uid=%d",
19c0: 26 70 77 2c 20 75 69 64 29 3b 0a 20 20 20 20 7d &pw, uid);. }
19d0: 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3e 3d . }else if( n>=
19e0: 32 20 26 26 20 73 74 72 6e 63 6d 70 28 67 2e 61 2 && strncmp(g.a
19f0: 72 67 76 5b 32 5d 2c 22 63 61 70 61 62 69 6c 69 rgv[2],"capabili
1a00: 74 69 65 73 22 2c 32 29 3d 3d 30 20 29 7b 0a 20 ties",2)==0 ){.
1a10: 20 20 20 69 6e 74 20 75 69 64 3b 0a 20 20 20 20 int uid;.
1a20: 69 66 28 20 67 2e 61 72 67 63 21 3d 34 20 26 26 if( g.argc!=4 &&
1a30: 20 67 2e 61 72 67 63 21 3d 35 20 29 7b 0a 20 20 g.argc!=5 ){.
1a40: 20 20 20 20 75 73 61 67 65 28 22 75 73 65 72 20 usage("user
1a50: 63 61 70 61 62 69 6c 69 74 69 65 73 20 55 53 45 capabilities USE
1a60: 52 4e 41 4d 45 20 3f 50 45 52 4d 49 53 53 49 4f RNAME ?PERMISSIO
1a70: 4e 53 3f 22 29 3b 0a 20 20 20 20 7d 0a 20 20 20 NS?");. }.
1a80: 20 75 69 64 20 3d 20 64 62 5f 69 6e 74 28 30 2c uid = db_int(0,
1a90: 20 22 53 45 4c 45 43 54 20 75 69 64 20 46 52 4f "SELECT uid FRO
1aa0: 4d 20 75 73 65 72 20 57 48 45 52 45 20 6c 6f 67 M user WHERE log
1ab0: 69 6e 3d 25 51 22 2c 20 67 2e 61 72 67 76 5b 33 in=%Q", g.argv[3
1ac0: 5d 29 3b 0a 20 20 20 20 69 66 28 20 75 69 64 3d ]);. if( uid=
1ad0: 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 6f 73 73 =0 ){. foss
1ae0: 69 6c 5f 66 61 74 61 6c 28 22 6e 6f 20 73 75 63 il_fatal("no suc
1af0: 68 20 75 73 65 72 3a 20 25 73 22 2c 20 67 2e 61 h user: %s", g.a
1b00: 72 67 76 5b 33 5d 29 3b 0a 20 20 20 20 7d 0a 20 rgv[3]);. }.
1b10: 20 20 20 69 66 28 20 67 2e 61 72 67 63 3d 3d 35 if( g.argc==5
1b20: 20 29 7b 0a 20 20 20 20 20 20 64 62 5f 6d 75 6c ){. db_mul
1b30: 74 69 5f 65 78 65 63 28 0a 20 20 20 20 20 20 20 ti_exec(.
1b40: 20 22 55 50 44 41 54 45 20 75 73 65 72 20 53 45 "UPDATE user SE
1b50: 54 20 63 61 70 3d 25 51 20 57 48 45 52 45 20 75 T cap=%Q WHERE u
1b60: 69 64 3d 25 64 22 2c 20 67 2e 61 72 67 76 5b 34 id=%d", g.argv[4
1b70: 5d 2c 0a 20 20 20 20 20 20 20 20 75 69 64 0a 20 ],. uid.
1b80: 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20 20 );. }.
1b90: 20 20 70 72 69 6e 74 66 28 22 25 73 5c 6e 22 2c printf("%s\n",
1ba0: 20 64 62 5f 74 65 78 74 28 30 2c 20 22 53 45 4c db_text(0, "SEL
1bb0: 45 43 54 20 63 61 70 20 46 52 4f 4d 20 75 73 65 ECT cap FROM use
1bc0: 72 20 57 48 45 52 45 20 75 69 64 3d 25 64 22 2c r WHERE uid=%d",
1bd0: 20 75 69 64 29 29 3b 0a 20 20 7d 65 6c 73 65 7b uid));. }else{
1be0: 0a 20 20 20 20 66 6f 73 73 69 6c 5f 70 61 6e 69 . fossil_pani
1bf0: 63 28 22 75 73 65 72 20 73 75 62 63 6f 6d 6d 61 c("user subcomma
1c00: 6e 64 20 73 68 6f 75 6c 64 20 62 65 20 6f 6e 65 nd should be one
1c10: 20 6f 66 3a 20 22 0a 20 20 20 20 20 20 20 20 20 of: ".
1c20: 20 20 20 20 20 20 20 20 22 63 61 70 61 62 69 6c "capabil
1c30: 69 74 69 65 73 20 64 65 66 61 75 6c 74 20 6c 69 ities default li
1c40: 73 74 20 6e 65 77 20 70 61 73 73 77 6f 72 64 22 st new password"
1c50: 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
1c60: 41 74 74 65 6d 70 74 20 74 6f 20 73 65 74 20 74 Attempt to set t
1c70: 68 65 20 75 73 65 72 20 74 6f 20 7a 4c 6f 67 69 he user to zLogi
1c80: 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 n.*/.static int
1c90: 61 74 74 65 6d 70 74 5f 75 73 65 72 28 63 6f 6e attempt_user(con
1ca0: 73 74 20 63 68 61 72 20 2a 7a 4c 6f 67 69 6e 29 st char *zLogin)
1cb0: 7b 0a 20 20 69 6e 74 20 75 69 64 3b 0a 0a 20 20 {. int uid;..
1cc0: 69 66 28 20 7a 4c 6f 67 69 6e 3d 3d 30 20 29 7b if( zLogin==0 ){
1cd0: 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
1ce0: 20 7d 0a 20 20 75 69 64 20 3d 20 64 62 5f 69 6e }. uid = db_in
1cf0: 74 28 30 2c 20 22 53 45 4c 45 43 54 20 75 69 64 t(0, "SELECT uid
1d00: 20 46 52 4f 4d 20 75 73 65 72 20 57 48 45 52 45 FROM user WHERE
1d10: 20 6c 6f 67 69 6e 3d 25 51 22 2c 20 7a 4c 6f 67 login=%Q", zLog
1d20: 69 6e 29 3b 0a 20 20 69 66 28 20 75 69 64 20 29 in);. if( uid )
1d30: 7b 0a 20 20 20 20 67 2e 75 73 65 72 55 69 64 20 {. g.userUid
1d40: 3d 20 75 69 64 3b 0a 20 20 20 20 67 2e 7a 4c 6f = uid;. g.zLo
1d50: 67 69 6e 20 3d 20 6d 70 72 69 6e 74 66 28 22 25 gin = mprintf("%
1d60: 73 22 2c 20 7a 4c 6f 67 69 6e 29 3b 0a 20 20 20 s", zLogin);.
1d70: 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 return 1;. }.
1d80: 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a return 0;.}../*
1d90: 0a 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 20 77 .** Figure out w
1da0: 68 61 74 20 75 73 65 72 20 69 73 20 61 74 20 74 hat user is at t
1db0: 68 65 20 63 6f 6e 74 72 6f 6c 73 2e 0a 2a 2a 0a he controls..**.
1dc0: 2a 2a 20 20 20 28 31 29 20 20 55 73 65 20 74 68 ** (1) Use th
1dd0: 65 20 2d 2d 75 73 65 72 20 61 6e 64 20 2d 55 20 e --user and -U
1de0: 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20 6f 70 74 command-line opt
1df0: 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 32 ions..**.** (2
1e00: 29 20 20 49 66 20 74 68 65 20 6c 6f 63 61 6c 20 ) If the local
1e10: 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e database is open
1e20: 2c 20 63 68 65 63 6b 20 69 6e 20 56 56 41 52 2e , check in VVAR.
1e30: 0a 2a 2a 0a 2a 2a 20 20 20 28 33 29 20 20 43 68 .**.** (3) Ch
1e40: 65 63 6b 20 74 68 65 20 64 65 66 61 75 6c 74 20 eck the default
1e50: 75 73 65 72 20 69 6e 20 74 68 65 20 72 65 70 6f user in the repo
1e60: 73 69 74 6f 72 79 0a 2a 2a 0a 2a 2a 20 20 20 28 sitory.**.** (
1e70: 34 29 20 20 54 72 79 20 74 68 65 20 55 53 45 52 4) Try the USER
1e80: 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 76 61 72 environment var
1e90: 69 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 iable..**.** (
1ea0: 35 29 20 20 55 73 65 20 74 68 65 20 66 69 72 73 5) Use the firs
1eb0: 74 20 75 73 65 72 20 69 6e 20 74 68 65 20 55 53 t user in the US
1ec0: 45 52 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 ER table..**.**
1ed0: 54 68 65 20 75 73 65 72 20 6e 61 6d 65 20 69 73 The user name is
1ee0: 20 73 74 6f 72 65 64 20 69 6e 20 67 2e 7a 4c 6f stored in g.zLo
1ef0: 67 69 6e 2e 20 20 54 68 65 20 75 69 64 20 69 73 gin. The uid is
1f00: 20 69 6e 20 67 2e 75 73 65 72 55 69 64 2e 0a 2a in g.userUid..*
1f10: 2f 0a 76 6f 69 64 20 75 73 65 72 5f 73 65 6c 65 /.void user_sele
1f20: 63 74 28 76 6f 69 64 29 7b 0a 20 20 53 74 6d 74 ct(void){. Stmt
1f30: 20 73 3b 0a 0a 20 20 69 66 28 20 67 2e 75 73 65 s;.. if( g.use
1f40: 72 55 69 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 rUid ) return;.
1f50: 20 69 66 28 20 61 74 74 65 6d 70 74 5f 75 73 65 if( attempt_use
1f60: 72 28 67 2e 7a 4c 6f 67 69 6e 29 20 29 20 72 65 r(g.zLogin) ) re
1f70: 74 75 72 6e 3b 0a 0a 20 20 69 66 28 20 67 2e 6c turn;.. if( g.l
1f80: 6f 63 61 6c 4f 70 65 6e 20 26 26 20 61 74 74 65 ocalOpen && atte
1f90: 6d 70 74 5f 75 73 65 72 28 64 62 5f 6c 67 65 74 mpt_user(db_lget
1fa0: 28 22 64 65 66 61 75 6c 74 2d 75 73 65 72 22 2c ("default-user",
1fb0: 30 29 29 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 0)) ) return;..
1fc0: 20 69 66 28 20 61 74 74 65 6d 70 74 5f 75 73 65 if( attempt_use
1fd0: 72 28 64 62 5f 67 65 74 28 22 64 65 66 61 75 6c r(db_get("defaul
1fe0: 74 2d 75 73 65 72 22 2c 20 30 29 29 20 29 20 72 t-user", 0)) ) r
1ff0: 65 74 75 72 6e 3b 0a 0a 20 20 69 66 28 20 61 74 eturn;.. if( at
2000: 74 65 6d 70 74 5f 75 73 65 72 28 67 65 74 65 6e tempt_user(geten
2010: 76 28 22 55 53 45 52 22 29 29 20 29 20 72 65 74 v("USER")) ) ret
2020: 75 72 6e 3b 0a 0a 20 20 64 62 5f 70 72 65 70 61 urn;.. db_prepa
2030: 72 65 28 26 73 2c 0a 20 20 20 20 22 53 45 4c 45 re(&s,. "SELE
2040: 43 54 20 75 69 64 2c 20 6c 6f 67 69 6e 20 46 52 CT uid, login FR
2050: 4f 4d 20 75 73 65 72 22 0a 20 20 20 20 22 20 57 OM user". " W
2060: 48 45 52 45 20 6c 6f 67 69 6e 20 4e 4f 54 20 49 HERE login NOT I
2070: 4e 20 28 27 61 6e 6f 6e 79 6d 6f 75 73 27 2c 27 N ('anonymous','
2080: 6e 6f 62 6f 64 79 27 2c 27 72 65 61 64 65 72 27 nobody','reader'
2090: 2c 27 64 65 76 65 6c 6f 70 65 72 27 29 22 0a 20 ,'developer')".
20a0: 20 29 3b 0a 20 20 69 66 28 20 64 62 5f 73 74 65 );. if( db_ste
20b0: 70 28 26 73 29 3d 3d 53 51 4c 49 54 45 5f 52 4f p(&s)==SQLITE_RO
20c0: 57 20 29 7b 0a 20 20 20 20 67 2e 75 73 65 72 55 W ){. g.userU
20d0: 69 64 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 id = db_column_i
20e0: 6e 74 28 26 73 2c 20 30 29 3b 0a 20 20 20 20 67 nt(&s, 0);. g
20f0: 2e 7a 4c 6f 67 69 6e 20 3d 20 6d 70 72 69 6e 74 .zLogin = mprint
2100: 66 28 22 25 73 22 2c 20 64 62 5f 63 6f 6c 75 6d f("%s", db_colum
2110: 6e 5f 74 65 78 74 28 26 73 2c 20 31 29 29 3b 0a n_text(&s, 1));.
2120: 20 20 7d 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a }. db_finaliz
2130: 65 28 26 73 29 3b 0a 0a 20 20 69 66 28 20 67 2e e(&s);.. if( g.
2140: 75 73 65 72 55 69 64 3d 3d 30 20 29 7b 0a 20 20 userUid==0 ){.
2150: 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 73 2c db_prepare(&s,
2160: 20 22 53 45 4c 45 43 54 20 75 69 64 2c 20 6c 6f "SELECT uid, lo
2170: 67 69 6e 20 46 52 4f 4d 20 75 73 65 72 22 29 3b gin FROM user");
2180: 0a 20 20 20 20 69 66 28 20 64 62 5f 73 74 65 70 . if( db_step
2190: 28 26 73 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 (&s)==SQLITE_ROW
21a0: 20 29 7b 0a 20 20 20 20 20 20 67 2e 75 73 65 72 ){. g.user
21b0: 55 69 64 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f Uid = db_column_
21c0: 69 6e 74 28 26 73 2c 20 30 29 3b 0a 20 20 20 20 int(&s, 0);.
21d0: 20 20 67 2e 7a 4c 6f 67 69 6e 20 3d 20 6d 70 72 g.zLogin = mpr
21e0: 69 6e 74 66 28 22 25 73 22 2c 20 64 62 5f 63 6f intf("%s", db_co
21f0: 6c 75 6d 6e 5f 74 65 78 74 28 26 73 2c 20 31 29 lumn_text(&s, 1)
2200: 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 5f );. }. db_
2210: 66 69 6e 61 6c 69 7a 65 28 26 73 29 3b 0a 20 20 finalize(&s);.
2220: 7d 0a 0a 20 20 69 66 28 20 67 2e 75 73 65 72 55 }.. if( g.userU
2230: 69 64 3d 3d 30 20 29 7b 0a 20 20 20 20 64 62 5f id==0 ){. db_
2240: 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 20 multi_exec(.
2250: 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 75 "INSERT INTO u
2260: 73 65 72 28 6c 6f 67 69 6e 2c 20 70 77 2c 20 63 ser(login, pw, c
2270: 61 70 2c 20 69 6e 66 6f 29 22 0a 20 20 20 20 20 ap, info)".
2280: 20 22 56 41 4c 55 45 53 28 27 61 6e 6f 6e 79 6d "VALUES('anonym
2290: 6f 75 73 27 2c 20 27 27 2c 20 27 63 66 67 68 6a ous', '', 'cfghj
22a0: 6b 6d 6e 6f 71 77 27 2c 20 27 27 29 22 0a 20 20 kmnoqw', '')".
22b0: 20 20 29 3b 0a 20 20 20 20 67 2e 75 73 65 72 55 );. g.userU
22c0: 69 64 20 3d 20 64 62 5f 6c 61 73 74 5f 69 6e 73 id = db_last_ins
22d0: 65 72 74 5f 72 6f 77 69 64 28 29 3b 0a 20 20 20 ert_rowid();.
22e0: 20 67 2e 7a 4c 6f 67 69 6e 20 3d 20 22 61 6e 6f g.zLogin = "ano
22f0: 6e 79 6d 6f 75 73 22 3b 0a 20 20 7d 0a 7d 0a nymous";. }.}.