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 20 0a 2a 2a 20 59 6f 75 20 73 68 6f 75 6c ** .** You shoul
01f0: 64 20 68 61 76 65 20 72 65 63 65 69 76 65 64 20 d have received
0200: 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 47 4e a copy of the GN
0210: 55 20 47 65 6e 65 72 61 6c 20 50 75 62 6c 69 63 U General Public
0220: 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 61 6c 6f 6e .** License alon
0230: 67 20 77 69 74 68 20 74 68 69 73 20 6c 69 62 72 g with this libr
0240: 61 72 79 3b 20 69 66 20 6e 6f 74 2c 20 77 72 69 ary; if not, wri
0250: 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 46 72 65 te to the.** Fre
0260: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64 e Software Found
0270: 61 74 69 6f 6e 2c 20 49 6e 63 2e 2c 20 35 39 20 ation, Inc., 59
0280: 54 65 6d 70 6c 65 20 50 6c 61 63 65 20 2d 20 53 Temple Place - S
0290: 75 69 74 65 20 33 33 30 2c 0a 2a 2a 20 42 6f 73 uite 330,.** Bos
02a0: 74 6f 6e 2c 20 4d 41 20 20 30 32 31 31 31 2d 31 ton, MA 02111-1
02b0: 33 30 37 2c 20 55 53 41 2e 0a 2a 2a 0a 2a 2a 20 307, USA..**.**
02c0: 41 75 74 68 6f 72 20 63 6f 6e 74 61 63 74 20 69 Author contact i
02d0: 6e 66 6f 72 6d 61 74 69 6f 6e 3a 0a 2a 2a 20 20 nformation:.**
02e0: 20 64 72 68 40 68 77 61 63 69 2e 63 6f 6d 0a 2a drh@hwaci.com.*
02f0: 2a 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 68 * http://www.h
0300: 77 61 63 69 2e 63 6f 6d 2f 64 72 68 2f 0a 2a 2a waci.com/drh/.**
0310: 0a 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 2a ****************
0360: 0a 2a 2a 0a 2a 2a 20 43 6f 6d 6d 61 6e 64 73 20 .**.** Commands
0370: 61 6e 64 20 70 72 6f 63 65 64 75 72 65 73 20 75 and procedures u
0380: 73 65 64 20 66 6f 72 20 63 72 65 61 74 69 6e 67 sed for creating
0390: 2c 20 70 72 6f 63 65 73 73 69 6e 67 2c 20 65 64 , processing, ed
03a0: 69 74 69 6e 67 2c 20 61 6e 64 0a 2a 2a 20 71 75 iting, and.** qu
03b0: 65 72 79 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 erying informati
03c0: 6f 6e 20 61 62 6f 75 74 20 75 73 65 72 73 2e 0a on about users..
03d0: 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 22 63 6f 6e */.#include "con
03e0: 66 69 67 2e 68 22 0a 23 69 6e 63 6c 75 64 65 20 fig.h".#include
03f0: 22 75 73 65 72 2e 68 22 0a 0a 0a 2f 2a 0a 2a 2a "user.h".../*.**
0400: 20 53 74 72 69 70 20 6c 65 61 64 69 6e 67 20 61 Strip leading a
0410: 6e 64 20 74 72 61 69 6c 69 6e 67 20 73 70 61 63 nd trailing spac
0420: 65 20 66 72 6f 6d 20 61 20 73 74 72 69 6e 67 20 e from a string
0430: 61 6e 64 20 61 64 64 20 74 68 65 20 73 74 72 69 and add the stri
0440: 6e 67 0a 2a 2a 20 6f 6e 74 6f 20 74 68 65 20 65 ng.** onto the e
0450: 6e 64 20 6f 66 20 61 20 62 6c 6f 62 2e 0a 2a 2f nd of a blob..*/
0460: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 74 72 .static void str
0470: 69 70 5f 73 74 72 69 6e 67 28 42 6c 6f 62 20 2a ip_string(Blob *
0480: 70 42 6c 6f 62 2c 20 63 68 61 72 20 2a 7a 29 7b pBlob, char *z){
0490: 0a 20 20 69 6e 74 20 69 3b 0a 20 20 62 6c 6f 62 . int i;. blob
04a0: 5f 72 65 73 65 74 28 70 42 6c 6f 62 29 3b 0a 20 _reset(pBlob);.
04b0: 20 77 68 69 6c 65 28 20 69 73 73 70 61 63 65 28 while( isspace(
04c0: 2a 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 *z) ){ z++; }.
04d0: 66 6f 72 28 69 3d 30 3b 20 7a 5b 69 5d 3b 20 69 for(i=0; z[i]; i
04e0: 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 5b 69 ++){. if( z[i
04f0: 5d 3d 3d 27 5c 72 27 20 7c 7c 20 7a 5b 69 5d 3d ]=='\r' || z[i]=
0500: 3d 27 5c 6e 27 20 29 7b 0a 20 20 20 20 20 20 20 ='\n' ){.
0510: 77 68 69 6c 65 28 20 69 3e 30 20 26 26 20 69 73 while( i>0 && is
0520: 73 70 61 63 65 28 7a 5b 69 2d 31 5d 29 20 29 7b space(z[i-1]) ){
0530: 20 69 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 20 7a i--; }. z
0540: 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 [i] = 0;.
0550: 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
0560: 20 69 66 28 20 7a 5b 69 5d 3c 27 20 27 20 29 20 if( z[i]<' ' )
0570: 7a 5b 69 5d 20 3d 20 27 20 27 3b 0a 20 20 7d 0a z[i] = ' ';. }.
0580: 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 28 70 42 blob_append(pB
0590: 6c 6f 62 2c 20 7a 2c 20 2d 31 29 3b 0a 7d 0a 0a lob, z, -1);.}..
05a0: 2f 2a 0a 2a 2a 20 44 6f 20 61 20 73 69 6e 67 6c /*.** Do a singl
05b0: 65 20 70 72 6f 6d 70 74 20 66 6f 72 20 61 20 70 e prompt for a p
05c0: 61 73 73 70 68 72 61 73 65 2e 20 20 53 74 6f 72 assphrase. Stor
05d0: 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 69 6e e the results in
05e0: 20 74 68 65 20 62 6c 6f 62 2e 0a 2a 2f 0a 73 74 the blob..*/.st
05f0: 61 74 69 63 20 76 6f 69 64 20 70 72 6f 6d 70 74 atic void prompt
0600: 5f 66 6f 72 5f 70 61 73 73 70 68 72 61 73 65 28 _for_passphrase(
0610: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 72 6f const char *zPro
0620: 6d 70 74 2c 20 42 6c 6f 62 20 2a 70 50 61 73 73 mpt, Blob *pPass
0630: 70 68 72 61 73 65 29 7b 0a 20 20 63 68 61 72 20 phrase){. char
0640: 2a 7a 20 3d 20 67 65 74 70 61 73 73 28 7a 50 72 *z = getpass(zPr
0650: 6f 6d 70 74 29 3b 0a 20 20 73 74 72 69 70 5f 73 ompt);. strip_s
0660: 74 72 69 6e 67 28 70 50 61 73 73 70 68 72 61 73 tring(pPassphras
0670: 65 2c 20 7a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 e, z);.}../*.**
0680: 50 72 6f 6d 70 74 20 74 68 65 20 75 73 65 72 20 Prompt the user
0690: 66 6f 72 20 61 20 70 61 73 73 70 68 72 61 73 65 for a passphrase
06a0: 20 75 73 65 64 20 74 6f 20 65 6e 63 72 79 70 74 used to encrypt
06b0: 20 74 68 65 20 70 72 69 76 61 74 65 20 6b 65 79 the private key
06c0: 0a 2a 2a 20 70 6f 72 74 69 6f 6e 20 6f 66 20 61 .** portion of a
06d0: 6e 20 52 53 41 20 6b 65 79 20 70 61 69 72 2e 0a n RSA key pair..
06e0: 2a 2a 0a 2a 2a 20 42 65 68 61 76 69 6f 72 20 69 **.** Behavior i
06f0: 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 s controlled by
0700: 74 68 65 20 76 65 72 69 66 79 20 70 61 72 61 6d the verify param
0710: 65 74 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 eter:.**.**
0720: 30 20 20 20 20 20 4a 75 73 74 20 61 73 6b 20 6f 0 Just ask o
0730: 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 31 nce..**.** 1
0740: 20 20 20 20 20 49 66 20 74 68 65 20 66 69 72 73 If the firs
0750: 74 20 61 6e 73 77 65 72 20 69 73 20 61 20 6e 6f t answer is a no
0760: 6e 2d 65 6d 70 74 79 20 73 74 72 69 6e 67 2c 20 n-empty string,
0770: 61 73 6b 20 66 6f 72 0a 2a 2a 20 20 20 20 20 20 ask for.**
0780: 20 20 20 20 20 76 65 72 69 66 69 63 61 74 69 6f verificatio
0790: 6e 2e 20 20 52 65 70 65 61 74 20 69 66 20 74 68 n. Repeat if th
07a0: 65 20 74 77 6f 20 73 74 72 69 6e 67 73 20 64 6f e two strings do
07b0: 20 6e 6f 74 20 6d 61 74 63 68 2e 0a 2a 2a 0a 2a not match..**.*
07c0: 2a 20 20 20 20 20 32 20 20 20 20 20 41 73 6b 20 * 2 Ask
07d0: 74 77 69 63 65 2c 20 72 65 70 65 61 74 20 69 66 twice, repeat if
07e0: 20 74 68 65 20 73 74 72 69 6e 67 73 20 64 6f 20 the strings do
07f0: 6e 6f 74 20 6d 61 74 63 68 2e 0a 2a 2f 0a 73 74 not match..*/.st
0800: 61 74 69 63 20 76 6f 69 64 20 67 65 74 5f 70 61 atic void get_pa
0810: 73 73 70 68 72 61 73 65 28 63 6f 6e 73 74 20 63 ssphrase(const c
0820: 68 61 72 20 2a 7a 50 72 6f 6d 70 74 2c 20 42 6c har *zPrompt, Bl
0830: 6f 62 20 2a 70 50 61 73 73 70 68 72 61 73 65 2c ob *pPassphrase,
0840: 20 69 6e 74 20 76 65 72 69 66 79 29 7b 0a 20 20 int verify){.
0850: 42 6c 6f 62 20 73 65 63 6f 6e 64 54 72 79 3b 0a Blob secondTry;.
0860: 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 70 50 61 73 blob_zero(pPas
0870: 73 70 68 72 61 73 65 29 3b 0a 20 20 62 6c 6f 62 sphrase);. blob
0880: 5f 7a 65 72 6f 28 26 73 65 63 6f 6e 64 54 72 79 _zero(&secondTry
0890: 29 3b 0a 20 20 77 68 69 6c 65 28 31 29 7b 0a 20 );. while(1){.
08a0: 20 20 20 70 72 6f 6d 70 74 5f 66 6f 72 5f 70 61 prompt_for_pa
08b0: 73 73 70 68 72 61 73 65 28 7a 50 72 6f 6d 70 74 ssphrase(zPrompt
08c0: 2c 20 70 50 61 73 73 70 68 72 61 73 65 29 3b 0a , pPassphrase);.
08d0: 20 20 20 20 69 66 28 20 76 65 72 69 66 79 3d 3d if( verify==
08e0: 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 69 0 ) break;. i
08f0: 66 28 20 76 65 72 69 66 79 3d 3d 31 20 26 26 20 f( verify==1 &&
0900: 62 6c 6f 62 5f 73 69 7a 65 28 70 50 61 73 73 70 blob_size(pPassp
0910: 68 72 61 73 65 29 3d 3d 30 20 29 20 62 72 65 61 hrase)==0 ) brea
0920: 6b 3b 0a 20 20 20 20 70 72 6f 6d 70 74 5f 66 6f k;. prompt_fo
0930: 72 5f 70 61 73 73 70 68 72 61 73 65 28 22 41 67 r_passphrase("Ag
0940: 61 69 6e 3a 20 22 2c 20 26 73 65 63 6f 6e 64 54 ain: ", &secondT
0950: 72 79 29 3b 0a 20 20 20 20 69 66 28 20 62 6c 6f ry);. if( blo
0960: 62 5f 63 6f 6d 70 61 72 65 28 70 50 61 73 73 70 b_compare(pPassp
0970: 68 72 61 73 65 2c 20 26 73 65 63 6f 6e 64 54 72 hrase, &secondTr
0980: 79 29 20 29 7b 0a 20 20 20 20 20 20 70 72 69 6e y) ){. prin
0990: 74 66 28 22 50 61 73 73 70 68 72 61 73 65 73 20 tf("Passphrases
09a0: 64 6f 20 6e 6f 74 20 6d 61 74 63 68 2e 20 20 54 do not match. T
09b0: 72 79 20 61 67 61 69 6e 2e 2e 2e 5c 6e 22 29 3b ry again...\n");
09c0: 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
09d0: 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
09e0: 20 7d 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 }. blob_reset(
09f0: 26 73 65 63 6f 6e 64 54 72 79 29 3b 0a 7d 0a 0a &secondTry);.}..
0a00: 2f 2a 0a 2a 2a 20 50 72 6f 6d 70 74 20 74 68 65 /*.** Prompt the
0a10: 20 75 73 65 72 20 74 6f 20 65 6e 74 65 72 20 61 user to enter a
0a20: 20 73 69 6e 67 6c 65 20 6c 69 6e 65 20 6f 66 20 single line of
0a30: 74 65 78 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 text..*/.static
0a40: 76 6f 69 64 20 70 72 6f 6d 70 74 5f 75 73 65 72 void prompt_user
0a50: 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 72 (const char *zPr
0a60: 6f 6d 70 74 2c 20 42 6c 6f 62 20 2a 70 49 6e 29 ompt, Blob *pIn)
0a70: 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 63 {. char *z;. c
0a80: 68 61 72 20 7a 4c 69 6e 65 5b 31 30 30 30 5d 3b har zLine[1000];
0a90: 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 70 49 6e . blob_zero(pIn
0aa0: 29 3b 0a 20 20 70 72 69 6e 74 66 28 22 25 73 22 );. printf("%s"
0ab0: 2c 20 7a 50 72 6f 6d 70 74 29 3b 0a 20 20 66 66 , zPrompt);. ff
0ac0: 6c 75 73 68 28 73 74 64 6f 75 74 29 3b 0a 20 20 lush(stdout);.
0ad0: 7a 20 3d 20 66 67 65 74 73 28 7a 4c 69 6e 65 2c z = fgets(zLine,
0ae0: 20 73 69 7a 65 6f 66 28 7a 4c 69 6e 65 29 2c 20 sizeof(zLine),
0af0: 73 74 64 69 6e 29 3b 0a 20 20 69 66 28 20 7a 20 stdin);. if( z
0b00: 29 7b 0a 20 20 20 20 73 74 72 69 70 5f 73 74 72 ){. strip_str
0b10: 69 6e 67 28 70 49 6e 2c 20 7a 29 3b 0a 20 20 7d ing(pIn, z);. }
0b20: 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 .}.../*.** COMMA
0b30: 4e 44 3a 20 20 75 73 65 72 0a 2a 2a 0a 2a 2a 20 ND: user.**.**
0b40: 44 69 73 70 61 74 63 68 65 72 20 66 6f 72 20 76 Dispatcher for v
0b50: 61 72 69 6f 75 73 20 75 73 65 72 20 73 75 62 63 arious user subc
0b60: 6f 6d 6d 61 6e 64 73 2e 0a 2a 2f 0a 76 6f 69 64 ommands..*/.void
0b70: 20 75 73 65 72 5f 63 6d 64 28 76 6f 69 64 29 7b user_cmd(void){
0b80: 0a 20 20 64 62 5f 66 69 6e 64 5f 61 6e 64 5f 6f . db_find_and_o
0b90: 70 65 6e 5f 72 65 70 6f 73 69 74 6f 72 79 28 29 pen_repository()
0ba0: 3b 0a 20 20 69 66 28 20 67 2e 61 72 67 63 3c 33 ;. if( g.argc<3
0bb0: 20 29 7b 0a 20 20 20 20 75 73 61 67 65 28 22 63 ){. usage("c
0bc0: 72 65 61 74 65 7c 64 65 66 61 75 6c 74 7c 6c 69 reate|default|li
0bd0: 73 74 7c 70 61 73 73 77 6f 72 64 20 2e 2e 2e 22 st|password ..."
0be0: 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 74 72 );. }. if( str
0bf0: 63 6d 70 28 67 2e 61 72 67 76 5b 32 5d 2c 22 63 cmp(g.argv[2],"c
0c00: 72 65 61 74 65 22 29 3d 3d 30 20 29 7b 0a 20 20 reate")==0 ){.
0c10: 20 20 42 6c 6f 62 20 70 61 73 73 77 64 2c 20 6c Blob passwd, l
0c20: 6f 67 69 6e 2c 20 63 6f 6e 74 61 63 74 3b 0a 0a ogin, contact;..
0c30: 20 20 20 20 70 72 6f 6d 70 74 5f 75 73 65 72 28 prompt_user(
0c40: 22 6c 6f 67 69 6e 3a 20 22 2c 20 26 6c 6f 67 69 "login: ", &logi
0c50: 6e 29 3b 0a 20 20 20 20 70 72 6f 6d 70 74 5f 75 n);. prompt_u
0c60: 73 65 72 28 22 63 6f 6e 74 61 63 74 2d 69 6e 66 ser("contact-inf
0c70: 6f 3a 20 22 2c 20 26 63 6f 6e 74 61 63 74 29 3b o: ", &contact);
0c80: 0a 20 20 20 20 67 65 74 5f 70 61 73 73 70 68 72 . get_passphr
0c90: 61 73 65 28 22 70 61 73 73 77 6f 72 64 3a 20 22 ase("password: "
0ca0: 2c 20 26 70 61 73 73 77 64 2c 20 31 29 3b 0a 20 , &passwd, 1);.
0cb0: 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 db_multi_exec
0cc0: 28 0a 20 20 20 20 20 20 22 49 4e 53 45 52 54 20 (. "INSERT
0cd0: 49 4e 54 4f 20 75 73 65 72 28 6c 6f 67 69 6e 2c INTO user(login,
0ce0: 70 77 2c 63 61 70 2c 69 6e 66 6f 29 22 0a 20 20 pw,cap,info)".
0cf0: 20 20 20 20 22 56 41 4c 55 45 53 28 25 42 2c 25 "VALUES(%B,%
0d00: 42 2c 27 6a 6e 6f 72 27 2c 25 42 29 22 2c 0a 20 B,'jnor',%B)",.
0d10: 20 20 20 20 20 26 6c 6f 67 69 6e 2c 20 26 70 61 &login, &pa
0d20: 73 73 77 64 2c 20 26 63 6f 6e 74 61 63 74 0a 20 sswd, &contact.
0d30: 20 20 20 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 );. }else if
0d40: 28 20 73 74 72 63 6d 70 28 67 2e 61 72 67 76 5b ( strcmp(g.argv[
0d50: 32 5d 2c 22 64 65 66 61 75 6c 74 22 29 3d 3d 30 2],"default")==0
0d60: 20 29 7b 0a 20 20 20 20 75 73 65 72 5f 73 65 6c ){. user_sel
0d70: 65 63 74 28 29 3b 0a 20 20 20 20 69 66 28 20 67 ect();. if( g
0d80: 2e 61 72 67 63 3d 3d 33 20 29 7b 0a 20 20 20 20 .argc==3 ){.
0d90: 20 20 70 72 69 6e 74 66 28 22 25 73 5c 6e 22 2c printf("%s\n",
0da0: 20 67 2e 7a 4c 6f 67 69 6e 29 3b 0a 20 20 20 20 g.zLogin);.
0db0: 7d 65 6c 73 65 20 69 66 28 20 67 2e 6c 6f 63 61 }else if( g.loca
0dc0: 6c 4f 70 65 6e 20 29 7b 0a 20 20 20 20 20 20 64 lOpen ){. d
0dd0: 62 5f 6c 73 65 74 28 22 64 65 66 61 75 6c 74 2d b_lset("default-
0de0: 75 73 65 72 22 2c 20 67 2e 7a 4c 6f 67 69 6e 29 user", g.zLogin)
0df0: 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
0e00: 20 20 20 64 62 5f 73 65 74 28 22 64 65 66 61 75 db_set("defau
0e10: 6c 74 2d 75 73 65 72 22 2c 20 67 2e 7a 4c 6f 67 lt-user", g.zLog
0e20: 69 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c in);. }. }el
0e30: 73 65 20 69 66 28 20 73 74 72 63 6d 70 28 67 2e se if( strcmp(g.
0e40: 61 72 67 76 5b 32 5d 2c 22 6c 69 73 74 22 29 3d argv[2],"list")=
0e50: 3d 30 20 29 7b 0a 20 20 20 20 53 74 6d 74 20 71 =0 ){. Stmt q
0e60: 3b 0a 20 20 20 20 64 62 5f 70 72 65 70 61 72 65 ;. db_prepare
0e70: 28 26 71 2c 20 22 53 45 4c 45 43 54 20 6c 6f 67 (&q, "SELECT log
0e80: 69 6e 2c 20 69 6e 66 6f 20 46 52 4f 4d 20 75 73 in, info FROM us
0e90: 65 72 20 4f 52 44 45 52 20 42 59 20 6c 6f 67 69 er ORDER BY logi
0ea0: 6e 22 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 n");. while(
0eb0: 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53 51 4c db_step(&q)==SQL
0ec0: 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 ITE_ROW ){.
0ed0: 20 70 72 69 6e 74 66 28 22 25 2d 31 32 73 20 25 printf("%-12s %
0ee0: 73 5c 6e 22 2c 20 64 62 5f 63 6f 6c 75 6d 6e 5f s\n", db_column_
0ef0: 74 65 78 74 28 26 71 2c 20 30 29 2c 20 64 62 5f text(&q, 0), db_
0f00: 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 26 71 2c 20 column_text(&q,
0f10: 31 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 1));. }. d
0f20: 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a b_finalize(&q);.
0f30: 20 20 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63 }else if( strc
0f40: 6d 70 28 67 2e 61 72 67 76 5b 32 5d 2c 22 70 61 mp(g.argv[2],"pa
0f50: 73 73 77 6f 72 64 22 29 3d 3d 30 20 29 7b 0a 20 ssword")==0 ){.
0f60: 20 20 20 63 68 61 72 20 2a 7a 50 72 6f 6d 70 74 char *zPrompt
0f70: 3b 0a 20 20 20 20 69 6e 74 20 75 69 64 3b 0a 20 ;. int uid;.
0f80: 20 20 20 42 6c 6f 62 20 70 77 3b 0a 20 20 20 20 Blob pw;.
0f90: 69 66 28 20 67 2e 61 72 67 63 21 3d 34 20 29 20 if( g.argc!=4 )
0fa0: 75 73 61 67 65 28 22 75 73 65 72 20 70 61 73 73 usage("user pass
0fb0: 77 6f 72 64 20 55 53 45 52 4e 41 4d 45 22 29 3b word USERNAME");
0fc0: 0a 20 20 20 20 75 69 64 20 3d 20 64 62 5f 69 6e . uid = db_in
0fd0: 74 28 30 2c 20 22 53 45 4c 45 43 54 20 75 69 64 t(0, "SELECT uid
0fe0: 20 46 52 4f 4d 20 75 73 65 72 20 57 48 45 52 45 FROM user WHERE
0ff0: 20 6c 6f 67 69 6e 3d 25 51 22 2c 20 67 2e 61 72 login=%Q", g.ar
1000: 67 76 5b 33 5d 29 3b 0a 20 20 20 20 69 66 28 20 gv[3]);. if(
1010: 75 69 64 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 uid==0 ){.
1020: 66 6f 73 73 69 6c 5f 66 61 74 61 6c 28 22 6e 6f fossil_fatal("no
1030: 20 73 75 63 68 20 75 73 65 72 3a 20 25 73 22 2c such user: %s",
1040: 20 67 2e 61 72 67 76 5b 33 5d 29 3b 0a 20 20 20 g.argv[3]);.
1050: 20 7d 0a 20 20 20 20 7a 50 72 6f 6d 70 74 20 3d }. zPrompt =
1060: 20 6d 70 72 69 6e 74 66 28 22 6e 65 77 20 70 61 mprintf("new pa
1070: 73 73 77 64 20 66 6f 72 20 25 73 3a 20 22 2c 20 sswd for %s: ",
1080: 67 2e 61 72 67 76 5b 33 5d 29 3b 0a 20 20 20 20 g.argv[3]);.
1090: 67 65 74 5f 70 61 73 73 70 68 72 61 73 65 28 7a get_passphrase(z
10a0: 50 72 6f 6d 70 74 2c 20 26 70 77 2c 20 31 29 3b Prompt, &pw, 1);
10b0: 0a 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 73 69 . if( blob_si
10c0: 7a 65 28 26 70 77 29 3d 3d 30 20 29 7b 0a 20 20 ze(&pw)==0 ){.
10d0: 20 20 20 20 70 72 69 6e 74 66 28 22 70 61 73 73 printf("pass
10e0: 77 6f 72 64 20 75 6e 63 68 61 6e 67 65 64 5c 6e word unchanged\n
10f0: 22 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ");. }else{.
1100: 20 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 db_multi_ex
1110: 65 63 28 22 55 50 44 41 54 45 20 75 73 65 72 20 ec("UPDATE user
1120: 53 45 54 20 70 77 3d 25 42 20 57 48 45 52 45 20 SET pw=%B WHERE
1130: 75 69 64 3d 25 64 22 2c 20 26 70 77 2c 20 75 69 uid=%d", &pw, ui
1140: 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 d);. }. }els
1150: 65 7b 0a 20 20 20 20 66 6f 73 73 69 6c 5f 70 61 e{. fossil_pa
1160: 6e 69 63 28 22 75 73 65 72 20 73 75 62 63 6f 6d nic("user subcom
1170: 6d 61 6e 64 20 73 68 6f 75 6c 64 20 62 65 20 6f mand should be o
1180: 6e 65 20 6f 66 3a 20 22 0a 20 20 20 20 20 20 20 ne of: ".
1190: 20 20 20 20 20 20 20 20 20 20 22 63 72 65 61 74 "creat
11a0: 65 20 64 65 66 61 75 6c 74 20 6c 69 73 74 20 70 e default list p
11b0: 61 73 73 77 6f 72 64 22 29 3b 0a 20 20 7d 0a 7d assword");. }.}
11c0: 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 ../*.** Attempt
11d0: 74 6f 20 73 65 74 20 74 68 65 20 75 73 65 72 20 to set the user
11e0: 74 6f 20 7a 4c 6f 67 69 6e 0a 2a 2f 0a 73 74 61 to zLogin.*/.sta
11f0: 74 69 63 20 69 6e 74 20 61 74 74 65 6d 70 74 5f tic int attempt_
1200: 75 73 65 72 28 63 6f 6e 73 74 20 63 68 61 72 20 user(const char
1210: 2a 7a 4c 6f 67 69 6e 29 7b 0a 20 20 69 6e 74 20 *zLogin){. int
1220: 75 69 64 3b 0a 0a 20 20 69 66 28 20 7a 4c 6f 67 uid;.. if( zLog
1230: 69 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 in==0 ){. ret
1240: 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 75 69 64 urn 0;. }. uid
1250: 20 3d 20 64 62 5f 69 6e 74 28 30 2c 20 22 53 45 = db_int(0, "SE
1260: 4c 45 43 54 20 75 69 64 20 46 52 4f 4d 20 75 73 LECT uid FROM us
1270: 65 72 20 57 48 45 52 45 20 6c 6f 67 69 6e 3d 25 er WHERE login=%
1280: 51 22 2c 20 7a 4c 6f 67 69 6e 29 3b 0a 20 20 69 Q", zLogin);. i
1290: 66 28 20 75 69 64 20 29 7b 0a 20 20 20 20 67 2e f( uid ){. g.
12a0: 75 73 65 72 55 69 64 20 3d 20 75 69 64 3b 0a 20 userUid = uid;.
12b0: 20 20 20 67 2e 7a 4c 6f 67 69 6e 20 3d 20 6d 70 g.zLogin = mp
12c0: 72 69 6e 74 66 28 22 25 73 22 2c 20 7a 4c 6f 67 rintf("%s", zLog
12d0: 69 6e 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 in);. return
12e0: 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 1;. }. return
12f0: 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 67 75 0;.}../*.** Figu
1300: 72 65 20 6f 75 74 20 77 68 61 74 20 75 73 65 72 re out what user
1310: 20 69 73 20 61 74 20 74 68 65 20 63 6f 6e 74 72 is at the contr
1320: 6f 6c 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 31 29 ols..**.** (1)
1330: 20 20 55 73 65 20 74 68 65 20 2d 2d 75 73 65 72 Use the --user
1340: 20 61 6e 64 20 2d 55 20 63 6f 6d 6d 61 6e 64 2d and -U command-
1350: 6c 69 6e 65 20 6f 70 74 69 6f 6e 73 2e 0a 2a 2a line options..**
1360: 0a 2a 2a 20 20 20 28 32 29 20 20 49 66 20 74 68 .** (2) If th
1370: 65 20 6c 6f 63 61 6c 20 64 61 74 61 62 61 73 65 e local database
1380: 20 69 73 20 6f 70 65 6e 2c 20 63 68 65 63 6b 20 is open, check
1390: 69 6e 20 56 56 41 52 2e 0a 2a 2a 0a 2a 2a 20 20 in VVAR..**.**
13a0: 20 28 33 29 20 20 43 68 65 63 6b 20 74 68 65 20 (3) Check the
13b0: 64 65 66 61 75 6c 74 20 75 73 65 72 20 69 6e 20 default user in
13c0: 74 68 65 20 72 65 70 6f 73 69 74 6f 72 79 0a 2a the repository.*
13d0: 2a 0a 2a 2a 20 20 20 28 34 29 20 20 54 72 79 20 *.** (4) Try
13e0: 74 68 65 20 55 53 45 52 20 65 6e 76 69 72 6f 6e the USER environ
13f0: 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 2e 0a 2a ment variable..*
1400: 2a 0a 2a 2a 20 20 20 28 35 29 20 20 55 73 65 20 *.** (5) Use
1410: 74 68 65 20 66 69 72 73 74 20 75 73 65 72 20 69 the first user i
1420: 6e 20 74 68 65 20 55 53 45 52 20 74 61 62 6c 65 n the USER table
1430: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 75 73 65 72 ..**.** The user
1440: 20 6e 61 6d 65 20 69 73 20 73 74 6f 72 65 64 20 name is stored
1450: 69 6e 20 67 2e 7a 4c 6f 67 69 6e 2e 20 20 54 68 in g.zLogin. Th
1460: 65 20 75 69 64 20 69 73 20 69 6e 20 67 2e 75 73 e uid is in g.us
1470: 65 72 55 69 64 2e 0a 2a 2f 0a 76 6f 69 64 20 75 erUid..*/.void u
1480: 73 65 72 5f 73 65 6c 65 63 74 28 76 6f 69 64 29 ser_select(void)
1490: 7b 0a 20 20 53 74 6d 74 20 73 3b 0a 0a 20 20 69 {. Stmt s;.. i
14a0: 66 28 20 67 2e 75 73 65 72 55 69 64 20 29 20 72 f( g.userUid ) r
14b0: 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 61 74 74 eturn;. if( att
14c0: 65 6d 70 74 5f 75 73 65 72 28 67 2e 7a 4c 6f 67 empt_user(g.zLog
14d0: 69 6e 29 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 in) ) return;..
14e0: 20 69 66 28 20 67 2e 6c 6f 63 61 6c 4f 70 65 6e if( g.localOpen
14f0: 20 26 26 20 61 74 74 65 6d 70 74 5f 75 73 65 72 && attempt_user
1500: 28 64 62 5f 6c 67 65 74 28 22 64 65 66 61 75 6c (db_lget("defaul
1510: 74 2d 75 73 65 72 22 2c 30 29 29 20 29 20 72 65 t-user",0)) ) re
1520: 74 75 72 6e 3b 0a 0a 20 20 69 66 28 20 61 74 74 turn;.. if( att
1530: 65 6d 70 74 5f 75 73 65 72 28 64 62 5f 67 65 74 empt_user(db_get
1540: 28 22 64 65 66 61 75 6c 74 2d 75 73 65 72 22 2c ("default-user",
1550: 20 30 29 29 20 29 20 72 65 74 75 72 6e 3b 0a 0a 0)) ) return;..
1560: 20 20 69 66 28 20 61 74 74 65 6d 70 74 5f 75 73 if( attempt_us
1570: 65 72 28 67 65 74 65 6e 76 28 22 55 53 45 52 22 er(getenv("USER"
1580: 29 29 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 )) ) return;..
1590: 64 62 5f 70 72 65 70 61 72 65 28 26 73 2c 20 22 db_prepare(&s, "
15a0: 53 45 4c 45 43 54 20 75 69 64 2c 20 6c 6f 67 69 SELECT uid, logi
15b0: 6e 20 46 52 4f 4d 20 75 73 65 72 20 57 48 45 52 n FROM user WHER
15c0: 45 20 6c 6f 67 69 6e 3c 3e 27 61 6e 6f 6e 79 6d E login<>'anonym
15d0: 6f 75 73 27 22 29 3b 0a 20 20 69 66 28 20 64 62 ous'");. if( db
15e0: 5f 73 74 65 70 28 26 73 29 3d 3d 53 51 4c 49 54 _step(&s)==SQLIT
15f0: 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 67 2e 75 E_ROW ){. g.u
1600: 73 65 72 55 69 64 20 3d 20 64 62 5f 63 6f 6c 75 serUid = db_colu
1610: 6d 6e 5f 69 6e 74 28 26 73 2c 20 30 29 3b 0a 20 mn_int(&s, 0);.
1620: 20 20 20 67 2e 7a 4c 6f 67 69 6e 20 3d 20 6d 70 g.zLogin = mp
1630: 72 69 6e 74 66 28 22 25 73 22 2c 20 64 62 5f 63 rintf("%s", db_c
1640: 6f 6c 75 6d 6e 5f 74 65 78 74 28 26 73 2c 20 31 olumn_text(&s, 1
1650: 29 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 66 69 6e ));. }. db_fin
1660: 61 6c 69 7a 65 28 26 73 29 3b 0a 0a 20 20 69 66 alize(&s);.. if
1670: 28 20 67 2e 75 73 65 72 55 69 64 3d 3d 30 20 29 ( g.userUid==0 )
1680: 7b 0a 20 20 20 20 64 62 5f 70 72 65 70 61 72 65 {. db_prepare
1690: 28 26 73 2c 20 22 53 45 4c 45 43 54 20 75 69 64 (&s, "SELECT uid
16a0: 2c 20 6c 6f 67 69 6e 20 46 52 4f 4d 20 75 73 65 , login FROM use
16b0: 72 22 29 3b 0a 20 20 20 20 69 66 28 20 64 62 5f r");. if( db_
16c0: 73 74 65 70 28 26 73 29 3d 3d 53 51 4c 49 54 45 step(&s)==SQLITE
16d0: 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 67 2e _ROW ){. g.
16e0: 75 73 65 72 55 69 64 20 3d 20 64 62 5f 63 6f 6c userUid = db_col
16f0: 75 6d 6e 5f 69 6e 74 28 26 73 2c 20 30 29 3b 0a umn_int(&s, 0);.
1700: 20 20 20 20 20 20 67 2e 7a 4c 6f 67 69 6e 20 3d g.zLogin =
1710: 20 6d 70 72 69 6e 74 66 28 22 25 73 22 2c 20 64 mprintf("%s", d
1720: 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 26 73 b_column_text(&s
1730: 2c 20 31 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 , 1));. }.
1740: 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73 29 db_finalize(&s)
1750: 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 67 2e 75 ;. }.. if( g.u
1760: 73 65 72 55 69 64 3d 3d 30 20 29 7b 0a 20 20 20 serUid==0 ){.
1770: 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a db_multi_exec(.
1780: 20 20 20 20 20 20 22 49 4e 53 45 52 54 20 49 4e "INSERT IN
1790: 54 4f 20 75 73 65 72 28 6c 6f 67 69 6e 2c 20 70 TO user(login, p
17a0: 77 2c 20 63 61 70 2c 20 69 6e 66 6f 29 22 0a 20 w, cap, info)".
17b0: 20 20 20 20 20 22 56 41 4c 55 45 53 28 27 61 6e "VALUES('an
17c0: 6f 6e 79 6d 6f 75 73 27 2c 20 27 27 2c 20 27 27 onymous', '', ''
17d0: 2c 20 27 27 29 22 0a 20 20 20 20 29 3b 0a 20 20 , '')". );.
17e0: 20 20 67 2e 75 73 65 72 55 69 64 20 3d 20 64 62 g.userUid = db
17f0: 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 _last_insert_row
1800: 69 64 28 29 3b 0a 20 20 20 20 67 2e 7a 4c 6f 67 id();. g.zLog
1810: 69 6e 20 3d 20 22 61 6e 6f 6e 79 6d 6f 75 73 22 in = "anonymous"
1820: 3b 0a 20 20 7d 0a 7d 0a ;. }.}.