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"
2012-04-13 02:36:00 +00:00
"errors"
2011-01-18 02:28:38 +00:00
"flag"
2011-09-26 00:40:01 +00:00
"fmt"
2011-01-18 02:28:38 +00:00
"log"
2011-12-07 22:41:19 +00:00
"os"
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
"os/exec"
2011-04-02 05:26:33 +00:00
2013-08-04 02:54:30 +00:00
"camlistore.org/pkg/blob"
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 {
2013-12-10 16:52:52 +00:00
newKey bool
gpgkey string
2013-09-20 13:42:05 +00:00
noconfig bool
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 )
2013-06-21 14:10:40 +00:00
flags . BoolVar ( & cmd . newKey , "newkey" , false , "Automatically generate a new identity in a new secret ring." )
2011-09-27 03:02:35 +00:00
flags . StringVar ( & cmd . gpgkey , "gpgkey" , "" , "GPG key 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." )
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 ( ) {
fmt . Fprintf ( cmdmain . Stderr , "Usage: camput init [opts]" )
2011-09-26 00:40:01 +00:00
}
2011-09-27 03:02:35 +00:00
func ( c * initCmd ) Examples ( ) [ ] string {
return [ ] string {
"" ,
"--gpgkey=XXXXX" ,
2013-06-21 14:10:40 +00:00
"--newkey Creates a new identity" ,
2011-09-26 00:40:01 +00:00
}
2011-09-27 03:02:35 +00:00
}
2013-06-21 14:10:40 +00:00
// keyId returns the current keyId. It checks, in this order,
// the --gpgkey flag, the GPGKEY env var, and the default
// identity secret ring.
func ( c * initCmd ) keyId ( secRing string ) ( string , error ) {
2012-04-13 02:36:00 +00:00
if k := c . gpgkey ; k != "" {
return k , nil
}
if k := os . Getenv ( "GPGKEY" ) ; k != "" {
return k , nil
}
2013-06-21 14:10:40 +00:00
k , err := jsonsign . KeyIdFromRing ( secRing )
if err != nil {
log . Printf ( "No suitable gpg key was found in %v: %v" , secRing , err )
} else {
if k != "" {
log . Printf ( "Re-using identity with keyId %q found in file %s" , k , secRing )
return k , nil
}
}
2012-04-13 02:36:00 +00:00
// TODO: run and parse gpg --list-secret-keys and see if there's just one and suggest that? Or show
// a list of them?
2013-06-21 14:10:40 +00:00
return "" , errors . New ( "Initialization requires your public GPG key.\nYou can set --gpgkey=<pubid> or set $GPGKEY in your environment. Run gpg --list-secret-keys to find their key IDs.\nOr you can create a new secret ring and key with 'camput init --newkey'." )
2012-04-13 02:36:00 +00:00
}
func ( c * initCmd ) getPublicKeyArmoredFromFile ( secretRingFileName , keyId string ) ( b [ ] byte , err error ) {
entity , err := jsonsign . EntityFromSecring ( keyId , secretRingFileName )
if err == nil {
pubArmor , err := jsonsign . ArmoredPublicKey ( entity )
if err == nil {
return [ ] byte ( pubArmor ) , nil
}
}
b , err = exec . Command ( "gpg" , "--export" , "--armor" , keyId ) . Output ( )
if err != nil {
return nil , fmt . Errorf ( "Error running gpg to export public key %q: %v" , keyId , err )
}
if len ( b ) == 0 {
return nil , fmt . Errorf ( "gpg export of public key %q was empty." , keyId )
}
return b , nil
}
func ( c * initCmd ) getPublicKeyArmored ( keyId string ) ( b [ ] byte , err error ) {
2013-12-10 16:52:52 +00:00
file := osutil . IdentitySecretRing ( )
b , err = c . getPublicKeyArmoredFromFile ( file , keyId )
if err != nil {
return nil , fmt . Errorf ( "failed to export armored public key ID %q from %v: %v" , keyId , file , err )
2012-04-13 02:36:00 +00:00
}
2013-12-10 16:52:52 +00:00
return b , nil
2012-04-13 02:36:00 +00:00
}
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
2013-06-21 14:10:40 +00:00
if c . newKey && c . gpgkey != "" {
log . Fatal ( "--newkey and --gpgkey are mutually exclusive" )
}
var keyId string
var err error
secRing := osutil . IdentitySecretRing ( )
if c . newKey {
keyId , err = jsonsign . GenerateNewSecRing ( secRing )
if err != nil {
return err
}
} else {
keyId , err = c . keyId ( secRing )
if err != nil {
return err
}
2011-01-18 02:28:38 +00:00
}
2012-04-13 02:36:00 +00:00
pubArmor , err := c . getPublicKeyArmored ( keyId )
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
}
2013-06-21 14:10:40 +00:00
configFilePath := osutil . UserClientConfigPath ( )
_ , err = os . Stat ( configFilePath )
2011-01-18 02:28:38 +00:00
if err == nil {
2013-06-21 14:10:40 +00:00
log . Fatalf ( "Config file %q already exists; quitting without touching it." , configFilePath )
2011-01-18 02:28:38 +00:00
}
2013-06-21 14:10:40 +00:00
if f , err := os . OpenFile ( configFilePath , os . O_CREATE | os . O_EXCL | os . O_WRONLY , 0600 ) ; err == nil {
2011-01-18 02:28:38 +00:00
defer f . Close ( )
2014-01-05 02:43:58 +00:00
m := & clientconfig . Config {
Servers : map [ string ] * clientconfig . Server {
"localhost" : {
Server : "http://localhost:3179" ,
IsDefault : true ,
Auth : "localhost" ,
} ,
} ,
Identity : keyId ,
IgnoredFiles : [ ] string { ".DS_Store" } ,
}
2011-01-18 18:29:38 +00:00
2011-01-18 02:28:38 +00:00
jsonBytes , err := json . MarshalIndent ( m , "" , " " )
if err != nil {
2011-02-02 20:27:30 +00:00
log . Fatalf ( "JSON serialization error: %v" , err )
2011-01-18 02:28:38 +00:00
}
_ , err = f . Write ( jsonBytes )
if err != nil {
2013-06-21 14:10:40 +00:00
log . Fatalf ( "Error writing to %q: %v" , configFilePath , err )
2011-01-18 02:28:38 +00:00
}
2013-06-21 14:10:40 +00:00
log . Printf ( "Wrote %q; modify as necessary." , configFilePath )
2011-01-18 02:28:38 +00:00
}
2011-09-26 00:40:01 +00:00
return nil
2011-02-02 20:27:30 +00:00
}