2011-01-28 07:07:18 +00:00
/ *
Copyright 2011 Google Inc .
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
2011-01-18 02:28:38 +00:00
package main
import (
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
"encoding/json"
2011-01-18 02:28:38 +00:00
"flag"
2011-09-26 00:40:01 +00:00
"fmt"
2015-09-28 14:25:40 +00:00
"io/ioutil"
2011-01-18 02:28:38 +00:00
"log"
2011-12-07 22:41:19 +00:00
"os"
2014-03-28 18:45:22 +00:00
"path/filepath"
2015-09-28 14:25:40 +00:00
"strings"
2011-04-02 05:26:33 +00:00
2015-09-28 14:25:40 +00:00
"camlistore.org/pkg/auth"
2013-08-04 02:54:30 +00:00
"camlistore.org/pkg/blob"
2015-09-28 14:25:40 +00:00
"camlistore.org/pkg/client"
2014-03-28 18:45:22 +00:00
"camlistore.org/pkg/client/android"
2013-02-18 23:35:43 +00:00
"camlistore.org/pkg/cmdmain"
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
"camlistore.org/pkg/jsonsign"
"camlistore.org/pkg/osutil"
2014-01-05 02:43:58 +00:00
"camlistore.org/pkg/types/clientconfig"
2011-01-18 02:28:38 +00:00
)
2011-09-26 00:40:01 +00:00
type initCmd struct {
2015-09-28 14:25:40 +00:00
newKey bool // whether to create a new GPG ring and key.
noconfig bool // whether to generate a client config file.
keyId string // GPG key ID to use.
secretRing string // GPG secret ring file to use.
userPass string // username and password to use when asking a server for the config.
insecureTLS bool // TLS certificate verification disabled
2011-09-26 00:40:01 +00:00
}
func init ( ) {
2013-02-18 23:35:43 +00:00
cmdmain . RegisterCommand ( "init" , func ( flags * flag . FlagSet ) cmdmain . CommandRunner {
2011-09-27 03:02:35 +00:00
cmd := new ( initCmd )
2014-03-28 18:45:22 +00:00
flags . BoolVar ( & cmd . newKey , "newkey" , false ,
2014-03-31 16:00:25 +00:00
"Automatically generate a new identity in a new secret ring at the default location (~/.config/camlistore/identity-secring.gpg on linux)." )
2014-03-28 18:45:22 +00:00
flags . StringVar ( & cmd . keyId , "gpgkey" , "" , "GPG key ID to use for signing (overrides $GPGKEY environment)" )
2013-09-20 13:42:05 +00:00
flags . BoolVar ( & cmd . noconfig , "noconfig" , false , "Stop after creating the public key blob, and do not try and create a config file." )
2015-09-28 14:25:40 +00:00
flags . StringVar ( & cmd . userPass , "userpass" , "" , "username:password to use when asking a server for a client configuration. Requires --server global option." )
flags . BoolVar ( & cmd . insecureTLS , "insecure" , false , "If set, when getting configuration from a server (with --server and --userpass) over TLS, the server's certificate verification is disabled. Needed when the server is using a self-signed certificate." )
2011-09-27 03:02:35 +00:00
return cmd
} )
2011-09-26 00:40:01 +00:00
}
2013-02-26 14:26:18 +00:00
func ( c * initCmd ) Describe ( ) string {
2013-06-21 14:10:40 +00:00
return "Initialize the camput configuration file. With no option, it tries to use the GPG key found in the default identity secret ring."
2013-02-26 14:26:18 +00:00
}
2011-09-26 00:40:01 +00:00
2013-02-26 14:26:18 +00:00
func ( c * initCmd ) Usage ( ) {
2015-09-28 14:25:40 +00:00
usage := "Usage: camput [--server host] init [opts]\n\nExamples:\n"
for _ , v := range c . usageExamples ( ) {
usage += v + "\n"
}
fmt . Fprintf ( cmdmain . Stderr , usage )
}
func ( c * initCmd ) usageExamples ( ) [ ] string {
var examples [ ] string
for _ , v := range c . Examples ( ) {
examples = append ( examples , "camput init " + v )
}
return append ( examples ,
"camput --server=https://localhost:3179 init --userpass=foo:bar --insecure=true" )
2011-09-26 00:40:01 +00:00
}
2011-09-27 03:02:35 +00:00
func ( c * initCmd ) Examples ( ) [ ] string {
2015-09-28 14:25:40 +00:00
// TODO(mpl): I can't add the correct -userpass example to that list, because
// it requires the global --server flag, which has to be passed before the
// "init" subcommand. We should have a way to override that.
// Or I could just add a -server flag to the init subcommand, but it sounds
// like a lame hack.
2011-09-27 03:02:35 +00:00
return [ ] string {
"" ,
"--gpgkey=XXXXX" ,
2015-09-28 14:25:40 +00:00
"--newkey #Creates a new identity" ,
2011-09-26 00:40:01 +00:00
}
2011-09-27 03:02:35 +00:00
}
2014-03-28 18:45:22 +00:00
// initSecretRing sets c.secretRing. It tries, in this order, the --secret-keyring flag,
// the CAMLI_SECRET_RING env var, then defaults to the operating system dependent location
// otherwise.
// It returns an error if the file does not exist.
func ( c * initCmd ) initSecretRing ( ) error {
if secretRing , ok := osutil . ExplicitSecretRingFile ( ) ; ok {
c . secretRing = secretRing
2013-06-21 14:10:40 +00:00
} else {
2014-03-28 18:45:22 +00:00
if android . OnAndroid ( ) {
panic ( "on android, so CAMLI_SECRET_RING should have been defined, or --secret-keyring used." )
2013-06-21 14:10:40 +00:00
}
2014-03-28 18:45:22 +00:00
c . secretRing = osutil . SecretRingFile ( )
2013-06-21 14:10:40 +00:00
}
2014-03-28 18:45:22 +00:00
if _ , err := os . Stat ( c . secretRing ) ; err != nil {
hint := "\nA GPG key is required, please use 'camput init --newkey'.\n\nOr if you know what you're doing, you can set the global camput flag --secret-keyring, or the CAMLI_SECRET_RING env var, to use your own GPG ring. And --gpgkey=<pubid> or GPGKEY to select which key ID to use."
return fmt . Errorf ( "Could not use secret ring file %v: %v.\n%v" , c . secretRing , err , hint )
}
return nil
2012-04-13 02:36:00 +00:00
}
2014-03-28 18:45:22 +00:00
// initKeyId sets c.keyId. It checks, in this order, the --gpgkey flag, the GPGKEY env var,
// and in the default identity secret ring.
func ( c * initCmd ) initKeyId ( ) error {
if k := c . keyId ; k != "" {
return nil
2012-04-13 02:36:00 +00:00
}
2014-03-28 18:45:22 +00:00
if k := os . Getenv ( "GPGKEY" ) ; k != "" {
c . keyId = k
return nil
2012-04-13 02:36:00 +00:00
}
2014-03-28 18:45:22 +00:00
k , err := jsonsign . KeyIdFromRing ( c . secretRing )
if err != nil {
hint := "You can set --gpgkey=<pubid> or the GPGKEY env var to select which key ID to use.\n"
return fmt . Errorf ( "No suitable gpg key was found in %v: %v.\n%v" , c . secretRing , err , hint )
2012-04-13 02:36:00 +00:00
}
2014-03-28 18:45:22 +00:00
c . keyId = k
log . Printf ( "Re-using identity with keyId %q found in file %s" , c . keyId , c . secretRing )
return nil
2012-04-13 02:36:00 +00:00
}
2014-03-28 18:45:22 +00:00
func ( c * initCmd ) getPublicKeyArmored ( ) ( [ ] byte , error ) {
entity , err := jsonsign . EntityFromSecring ( c . keyId , c . secretRing )
2013-12-10 16:52:52 +00:00
if err != nil {
2014-03-28 18:45:22 +00:00
return nil , fmt . Errorf ( "Could not find keyId %v in ring %v: %v" , c . keyId , c . secretRing , err )
2012-04-13 02:36:00 +00:00
}
2014-03-28 18:45:22 +00:00
pubArmor , err := jsonsign . ArmoredPublicKey ( entity )
if err != nil {
return nil , fmt . Errorf ( "failed to export armored public key ID %q from %v: %v" , c . keyId , c . secretRing , err )
}
return [ ] byte ( pubArmor ) , nil
2012-04-13 02:36:00 +00:00
}
2015-09-28 14:25:40 +00:00
func ( c * initCmd ) clientConfigFromServer ( ) ( * clientconfig . Config , error ) {
if c . noconfig {
log . Print ( "--userpass and --noconfig are mutually exclusive" )
return nil , cmdmain . ErrUsage
}
server := client . ExplicitServer ( )
if server == "" {
log . Print ( "--userpass requires --server" )
return nil , cmdmain . ErrUsage
}
fields := strings . Split ( c . userPass , ":" )
if len ( fields ) != 2 {
log . Printf ( "wrong userpass; wanted username:password, got %q" , c . userPass )
return nil , cmdmain . ErrUsage
}
2015-12-29 19:32:47 +00:00
cl := client . NewFromParams ( server ,
auth . NewBasicAuth ( fields [ 0 ] , fields [ 1 ] ) ,
client . OptionInsecure ( c . insecureTLS ) )
2015-09-28 14:25:40 +00:00
helpRoot , err := cl . HelpRoot ( )
if err != nil {
return nil , err
}
2015-12-29 23:41:12 +00:00
var cc clientconfig . Config
2015-09-28 14:25:40 +00:00
if err := cl . GetJSON ( helpRoot + "?clientConfig=true" , & cc ) ; err != nil {
return nil , err
}
return & cc , nil
}
func ( c * initCmd ) writeConfig ( cc * clientconfig . Config ) error {
configFilePath := osutil . UserClientConfigPath ( )
if _ , err := os . Stat ( configFilePath ) ; err == nil {
2017-12-10 09:13:00 +00:00
return fmt . Errorf ( "config file %q already exists; quitting without touching it" , configFilePath )
2015-09-28 14:25:40 +00:00
}
if err := os . MkdirAll ( filepath . Dir ( configFilePath ) , 0700 ) ; err != nil {
return err
}
jsonBytes , err := json . MarshalIndent ( cc , "" , " " )
if err != nil {
log . Fatalf ( "JSON serialization error: %v" , err )
}
if err := ioutil . WriteFile ( configFilePath , jsonBytes , 0600 ) ; err != nil {
return fmt . Errorf ( "could not write client config file %v: %v" , configFilePath , err )
}
log . Printf ( "Wrote %q; modify as necessary." , configFilePath )
return nil
}
2013-02-18 23:35:43 +00:00
func ( c * initCmd ) RunCommand ( args [ ] string ) error {
2011-09-27 03:02:35 +00:00
if len ( args ) > 0 {
2013-02-18 23:35:43 +00:00
return cmdmain . ErrUsage
2011-09-26 00:40:01 +00:00
}
2011-01-18 02:28:38 +00:00
2014-03-28 18:45:22 +00:00
if c . newKey && c . keyId != "" {
2013-06-21 14:10:40 +00:00
log . Fatal ( "--newkey and --gpgkey are mutually exclusive" )
}
2015-09-28 14:25:40 +00:00
if c . userPass != "" {
cc , err := c . clientConfigFromServer ( )
if err != nil {
return err
}
return c . writeConfig ( cc )
}
2013-06-21 14:10:40 +00:00
var err error
if c . newKey {
2014-03-28 18:45:22 +00:00
c . secretRing = osutil . DefaultSecretRingFile ( )
c . keyId , err = jsonsign . GenerateNewSecRing ( c . secretRing )
2013-06-21 14:10:40 +00:00
if err != nil {
return err
}
} else {
2014-03-28 18:45:22 +00:00
if err := c . initSecretRing ( ) ; err != nil {
return err
}
if err := c . initKeyId ( ) ; err != nil {
2013-06-21 14:10:40 +00:00
return err
}
2011-01-18 02:28:38 +00:00
}
2014-03-28 18:45:22 +00:00
pubArmor , err := c . getPublicKeyArmored ( )
2011-01-18 02:28:38 +00:00
if err != nil {
2012-04-13 02:36:00 +00:00
return err
2011-07-02 16:09:50 +00:00
}
2013-08-04 02:54:30 +00:00
bref := blob . SHA1FromString ( string ( pubArmor ) )
2011-07-02 16:09:50 +00:00
2011-01-18 02:28:38 +00:00
log . Printf ( "Your Camlistore identity (your GPG public key's blobref) is: %s" , bref . String ( ) )
2013-09-20 13:42:05 +00:00
if c . noconfig {
return nil
}
2015-09-28 14:25:40 +00:00
return c . writeConfig ( & clientconfig . Config {
Servers : map [ string ] * clientconfig . Server {
"localhost" : {
Server : "http://localhost:3179" ,
IsDefault : true ,
Auth : "localhost" ,
2014-01-05 02:43:58 +00:00
} ,
2015-09-28 14:25:40 +00:00
} ,
Identity : c . keyId ,
IgnoredFiles : [ ] string { ".DS_Store" } ,
} )
2011-02-02 20:27:30 +00:00
}