2011-03-30 00:42:49 +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 .
* /
2010-06-12 21:45:58 +00:00
2013-07-08 04:12:18 +00:00
// The camlistored binary is the Camlistore server.
2010-06-12 21:45:58 +00:00
package main
2010-07-11 04:18:16 +00:00
import (
2011-11-10 15:20:22 +00:00
"crypto/rand"
"crypto/rsa"
"crypto/x509"
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
"crypto/x509/pkix"
2011-11-10 15:20:22 +00:00
"encoding/pem"
2011-04-02 05:14:23 +00:00
"flag"
"fmt"
2013-08-27 02:07:28 +00:00
"io"
2012-03-03 22:27:17 +00:00
"io/ioutil"
2011-04-02 05:14:23 +00:00
"log"
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
"math/big"
2012-03-15 12:31:06 +00:00
"net"
2014-08-02 23:53:58 +00:00
"net/http"
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"
2012-05-15 08:44:03 +00:00
"os/signal"
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
"path/filepath"
2013-11-28 16:55:02 +00:00
"runtime"
2013-07-30 19:57:50 +00:00
"strconv"
2011-04-02 05:14:23 +00:00
"strings"
2012-05-15 08:44:03 +00:00
"syscall"
2011-11-10 15:20:22 +00:00
"time"
2011-04-02 05:14:23 +00:00
2013-02-02 18:59:39 +00:00
"camlistore.org/pkg/buildinfo"
2014-08-16 00:15:09 +00:00
"camlistore.org/pkg/httputil"
2014-07-03 20:03:31 +00:00
"camlistore.org/pkg/legal/legalprint"
2013-04-08 13:50:50 +00:00
"camlistore.org/pkg/misc"
2014-07-29 18:23:14 +00:00
"camlistore.org/pkg/netutil"
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/osutil"
2014-01-23 16:18:22 +00:00
"camlistore.org/pkg/serverinit"
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/webserver"
2014-08-05 04:27:07 +00:00
"camlistore.org/pkg/wkfs"
2014-08-07 19:33:52 +00:00
// VM environments:
_ "camlistore.org/pkg/osutil/gce"
2014-08-16 06:17:14 +00:00
"camlistore.org/third_party/github.com/bradfitz/gce"
2011-04-02 05:14:23 +00:00
// Storage options:
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/blobserver/cond"
2013-09-02 00:35:17 +00:00
_ "camlistore.org/pkg/blobserver/diskpacked"
2013-06-12 11:35:50 +00:00
_ "camlistore.org/pkg/blobserver/encrypt"
2013-08-11 15:07:18 +00:00
_ "camlistore.org/pkg/blobserver/google/cloudstorage"
_ "camlistore.org/pkg/blobserver/google/drive"
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/blobserver/localdisk"
2014-02-21 14:59:16 +00:00
_ "camlistore.org/pkg/blobserver/mongo"
2014-05-15 19:12:21 +00:00
_ "camlistore.org/pkg/blobserver/proxycache"
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/blobserver/remote"
_ "camlistore.org/pkg/blobserver/replica"
_ "camlistore.org/pkg/blobserver/s3"
_ "camlistore.org/pkg/blobserver/shard"
2012-03-26 20:57:53 +00:00
// Indexers: (also present themselves as storage targets)
2014-04-01 16:03:20 +00:00
"camlistore.org/pkg/index"
2013-12-13 21:14:14 +00:00
// KeyValue implementations:
_ "camlistore.org/pkg/sorted/kvfile"
_ "camlistore.org/pkg/sorted/mongo"
2013-12-18 18:01:13 +00:00
_ "camlistore.org/pkg/sorted/mysql"
2013-12-23 15:30:28 +00:00
_ "camlistore.org/pkg/sorted/postgres"
2014-04-01 16:03:20 +00:00
"camlistore.org/pkg/sorted/sqlite" // for sqlite.CompiledIn()
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
2011-05-30 05:52:31 +00:00
// Handlers:
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/search"
_ "camlistore.org/pkg/server" // UI, publish, etc
2013-10-19 22:48:05 +00:00
// Importers:
2014-07-21 20:58:06 +00:00
_ "camlistore.org/pkg/importer/allimporters"
2010-07-11 04:18:16 +00:00
)
2010-07-07 04:57:53 +00:00
2012-02-29 16:01:49 +00:00
var (
2013-02-02 18:59:39 +00:00
flagVersion = flag . Bool ( "version" , false , "show version" )
2012-03-03 22:27:17 +00:00
flagConfigFile = flag . String ( "configfile" , "" ,
2014-08-02 23:53:58 +00:00
"Config file to use, relative to the Camlistore configuration directory root. " +
"If blank, the default is used or auto-generated. " +
"If it starts with 'http:' or 'https:', it is fetched from the network." )
2014-08-02 21:31:02 +00:00
flagListen = flag . String ( "listen" , "" , "host:port to listen on, or :0 to auto-select. If blank, the value in the config will be used instead." )
2013-08-23 09:28:22 +00:00
flagOpenBrowser = flag . Bool ( "openbrowser" , true , "Launches the UI on startup" )
2013-12-24 03:07:17 +00:00
flagReindex = flag . Bool ( "reindex" , false , "Reindex all blobs on startup" )
2013-08-23 09:28:22 +00:00
flagPollParent bool
2012-02-29 16:01:49 +00:00
)
2011-03-05 23:09:36 +00:00
2013-07-30 19:57:50 +00:00
func init ( ) {
if debug , _ := strconv . ParseBool ( os . Getenv ( "CAMLI_DEBUG" ) ) ; debug {
flag . BoolVar ( & flagPollParent , "pollparent" , false , "Camlistored regularly polls its parent process to detect if it has been orphaned, and terminates in that case. Mainly useful for tests." )
}
}
2012-02-29 16:01:49 +00:00
func exitf ( pattern string , args ... interface { } ) {
2011-02-04 22:31:23 +00:00
if ! strings . HasSuffix ( pattern , "\n" ) {
pattern = pattern + "\n"
}
fmt . Fprintf ( os . Stderr , pattern , args ... )
2013-11-24 23:03:34 +00:00
osExit ( 1 )
2010-06-12 21:45:58 +00:00
}
2013-04-08 13:50:50 +00:00
// 1) We do not want to force the user to buy a cert.
// 2) We still want our client (camput) to be able to
// verify the cert's authenticity.
// 3) We want to avoid MITM attacks and warnings in
// the browser.
// Using a simple self-signed won't do because of 3),
// as Chrome offers no way to set a self-signed as
// trusted when importing it. (same on android).
// We could have created a self-signed CA (that we
// would import in the browsers) and create another
// cert (signed by that CA) which would be the one
// used in camlistore.
// We're doing even simpler: create a self-signed
// CA and directly use it as a self-signed cert
// (and install it as a CA in the browsers).
// 2) is satisfied by doing our own checks,
// See pkg/client
2012-03-15 12:31:06 +00:00
func genSelfTLS ( listen string ) error {
2014-08-16 05:05:08 +00:00
priv , err := rsa . GenerateKey ( rand . Reader , 2048 )
2011-11-10 15:20:22 +00:00
if err != nil {
return fmt . Errorf ( "failed to generate private key: %s" , err )
}
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
now := time . Now ( )
2011-11-10 15:20:22 +00:00
2012-03-15 12:31:06 +00:00
hostname , _ , err := net . SplitHostPort ( listen )
if err != nil {
return fmt . Errorf ( "splitting listen failed: %q" , err )
2011-11-10 15:20:22 +00:00
}
2013-04-08 13:50:50 +00:00
// TODO(mpl): if no host is specified in the listening address
// (e.g ":3179") we'll end up in this case, and the self-signed
// will have "localhost" as a CommonName. But I don't think
// there's anything we can do about it. Maybe warn...
if hostname == "" {
hostname = "localhost"
}
2011-11-10 15:20:22 +00:00
template := x509 . Certificate {
SerialNumber : new ( big . Int ) . SetInt64 ( 0 ) ,
Subject : pkix . Name {
CommonName : hostname ,
Organization : [ ] string { hostname } ,
} ,
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
NotBefore : now . Add ( - 5 * time . Minute ) . UTC ( ) ,
NotAfter : now . AddDate ( 1 , 0 , 0 ) . UTC ( ) ,
2011-11-10 15:20:22 +00:00
SubjectKeyId : [ ] byte { 1 , 2 , 3 , 4 } ,
2013-04-08 13:50:50 +00:00
KeyUsage : x509 . KeyUsageKeyEncipherment | x509 . KeyUsageDigitalSignature | x509 . KeyUsageCertSign ,
IsCA : true ,
BasicConstraintsValid : true ,
2011-11-10 15:20:22 +00:00
}
derBytes , err := x509 . CreateCertificate ( rand . Reader , & template , & template , & priv . PublicKey , priv )
if err != nil {
return fmt . Errorf ( "Failed to create certificate: %s" , err )
}
2013-12-09 22:23:38 +00:00
defCert := osutil . DefaultTLSCert ( )
defKey := osutil . DefaultTLSKey ( )
2014-08-05 04:27:07 +00:00
certOut , err := wkfs . Create ( defCert )
2011-11-10 15:20:22 +00:00
if err != nil {
return fmt . Errorf ( "failed to open %s for writing: %s" , defCert , err )
}
2014-08-08 17:53:44 +00:00
if err := pem . Encode ( certOut , & pem . Block { Type : "CERTIFICATE" , Bytes : derBytes } ) ; err != nil {
return err
}
if err := certOut . Close ( ) ; err != nil {
return fmt . Errorf ( "Writing writing self-signed HTTPS cert: %v" , err )
}
log . Printf ( "Wrote %s" , defCert )
2013-04-08 13:50:50 +00:00
cert , err := x509 . ParseCertificate ( derBytes )
if err != nil {
return fmt . Errorf ( "Failed to parse certificate: %v" , err )
}
2014-01-05 00:37:33 +00:00
sig := misc . SHA256Prefix ( cert . Raw )
2013-04-08 13:50:50 +00:00
hint := "You must add this certificate's fingerprint to your client's trusted certs list to use it. Like so:\n" +
` "trustedCerts": [" ` + sig + ` "], `
log . Printf ( hint )
2011-11-10 15:20:22 +00:00
2014-08-05 04:27:07 +00:00
keyOut , err := wkfs . OpenFile ( defKey , os . O_WRONLY | os . O_CREATE | os . O_TRUNC , 0600 )
2011-11-10 15:20:22 +00:00
if err != nil {
2014-08-08 17:53:44 +00:00
return fmt . Errorf ( "failed to open %s for writing: %v" , defKey , err )
}
if err := pem . Encode ( keyOut , & pem . Block { Type : "RSA PRIVATE KEY" , Bytes : x509 . MarshalPKCS1PrivateKey ( priv ) } ) ; err != nil {
return fmt . Errorf ( "Error writing self-signed HTTPS private key: %v" , err )
}
if err := keyOut . Close ( ) ; err != nil {
return fmt . Errorf ( "Error writing self-signed HTTPS private key: %v" , err )
2011-11-10 15:20:22 +00:00
}
2014-08-08 17:53:44 +00:00
log . Printf ( "Wrote %s" , defKey )
2011-11-10 15:20:22 +00:00
return nil
}
2014-08-02 23:53:58 +00:00
func slurpURL ( urls string , limit int64 ) ( [ ] byte , error ) {
res , err := http . Get ( urls )
if err != nil {
return nil , err
}
defer res . Body . Close ( )
return ioutil . ReadAll ( io . LimitReader ( res . Body , limit ) )
}
// loadConfig returns the server's parsed config file, locating it using the provided arg.
//
// The arg may be of the form:
// - empty, to mean automatic (will write a default high-level config if
// no cloud config is available)
// - a filepath absolute or relative to the user's configuration directory,
// - a URL
func loadConfig ( arg string ) ( conf * serverinit . Config , isNewConfig bool , err error ) {
if strings . HasPrefix ( arg , "http://" ) || strings . HasPrefix ( arg , "https://" ) {
contents , err := slurpURL ( arg , 256 << 10 )
if err != nil {
return nil , false , err
}
conf , err = serverinit . Load ( contents )
return conf , false , err
}
var absPath string
2012-03-19 20:09:00 +00:00
switch {
2014-08-02 23:53:58 +00:00
case arg == "" :
2014-08-07 19:33:52 +00:00
absPath = osutil . UserServerConfigPath ( )
2014-08-05 04:27:07 +00:00
_ , err = wkfs . Stat ( absPath )
2014-08-02 23:53:58 +00:00
if err != nil {
if ! os . IsNotExist ( err ) {
return
}
2014-08-16 03:50:59 +00:00
conf , err = serverinit . DefaultEnvConfig ( )
2014-08-16 05:05:33 +00:00
if err != nil || conf != nil {
2014-08-16 03:50:59 +00:00
return
}
2014-08-05 04:27:07 +00:00
err = wkfs . MkdirAll ( osutil . CamliConfigDir ( ) , 0700 )
2012-05-13 15:37:46 +00:00
if err != nil {
return
}
2012-03-19 20:09:00 +00:00
log . Printf ( "Generating template config file %s" , absPath )
2014-04-01 16:03:20 +00:00
if err = serverinit . WriteDefaultConfigFile ( absPath , sqlite . CompiledIn ( ) ) ; err == nil {
2013-08-23 09:28:22 +00:00
isNewConfig = true
}
2012-03-03 22:27:17 +00:00
}
2014-08-02 23:53:58 +00:00
case filepath . IsAbs ( arg ) :
absPath = arg
2012-03-19 20:09:00 +00:00
default :
2014-08-02 23:53:58 +00:00
absPath = filepath . Join ( osutil . CamliConfigDir ( ) , arg )
2012-03-03 22:27:17 +00:00
}
2014-08-02 23:53:58 +00:00
conf , err = serverinit . LoadFile ( absPath )
2012-03-19 20:09:00 +00:00
return
2012-03-03 22:27:17 +00:00
}
2014-01-23 16:18:22 +00:00
func setupTLS ( ws * webserver . Server , config * serverinit . Config , listen string ) {
2014-01-23 22:40:12 +00:00
cert , key := config . OptionalString ( "httpsCert" , "" ) , config . OptionalString ( "httpsKey" , "" )
2012-03-05 15:17:58 +00:00
if ! config . OptionalBool ( "https" , true ) {
return
}
if ( cert != "" ) != ( key != "" ) {
2014-01-23 22:40:12 +00:00
exitf ( "httpsCert and httpsKey must both be either present or absent" )
2012-03-05 15:17:58 +00:00
}
2013-12-09 22:23:38 +00:00
defCert := osutil . DefaultTLSCert ( )
defKey := osutil . DefaultTLSKey ( )
2012-03-05 15:17:58 +00:00
if cert == defCert && key == defKey {
2014-08-05 04:27:07 +00:00
_ , err1 := wkfs . Stat ( cert )
_ , err2 := wkfs . Stat ( key )
2012-03-05 15:17:58 +00:00
if err1 != nil || err2 != nil {
if os . IsNotExist ( err1 ) || os . IsNotExist ( err2 ) {
2012-03-15 12:31:06 +00:00
if err := genSelfTLS ( listen ) ; err != nil {
2012-03-05 15:17:58 +00:00
exitf ( "Could not generate self-signed TLS cert: %q" , err )
}
} else {
exitf ( "Could not stat cert or key: %q, %q" , err1 , err2 )
}
}
}
if cert == "" && key == "" {
2012-03-15 12:31:06 +00:00
err := genSelfTLS ( listen )
2012-03-05 15:17:58 +00:00
if err != nil {
exitf ( "Could not generate self signed creds: %q" , err )
}
cert = defCert
key = defKey
}
2014-08-08 17:55:25 +00:00
data , err := wkfs . ReadFile ( cert )
2013-04-08 13:50:50 +00:00
if err != nil {
exitf ( "Failed to read pem certificate: %s" , err )
}
block , _ := pem . Decode ( data )
if block == nil {
exitf ( "Failed to decode pem certificate" )
}
certif , err := x509 . ParseCertificate ( block . Bytes )
if err != nil {
exitf ( "Failed to parse certificate: %v" , err )
}
2014-01-05 00:37:33 +00:00
sig := misc . SHA256Prefix ( certif . Raw )
log . Printf ( "TLS enabled, with SHA-256 certificate fingerprint: %v" , sig )
2012-03-05 15:17:58 +00:00
ws . SetTLS ( cert , key )
}
2013-11-24 23:03:34 +00:00
var osExit = os . Exit // testing hook
2013-08-27 02:07:28 +00:00
func handleSignals ( shutdownc <- chan io . Closer ) {
2012-05-15 08:44:03 +00:00
c := make ( chan os . Signal , 1 )
signal . Notify ( c , syscall . SIGHUP )
2013-08-27 02:07:28 +00:00
signal . Notify ( c , syscall . SIGINT )
2012-05-15 08:44:03 +00:00
for {
sig := <- c
sysSig , ok := sig . ( syscall . Signal )
if ! ok {
log . Fatal ( "Not a unix signal" )
}
switch sysSig {
case syscall . SIGHUP :
log . Print ( "SIGHUP: restarting camli" )
err := osutil . RestartProcess ( )
if err != nil {
log . Fatal ( "Failed to restart: " + err . Error ( ) )
}
2013-08-27 02:07:28 +00:00
case syscall . SIGINT :
2013-12-06 18:24:32 +00:00
log . Print ( "Got SIGINT: shutting down" )
2013-08-27 02:07:28 +00:00
donec := make ( chan bool )
go func ( ) {
cl := <- shutdownc
if err := cl . Close ( ) ; err != nil {
exitf ( "Error shutting down: %v" , err )
}
donec <- true
} ( )
select {
case <- donec :
log . Printf ( "Shut down." )
2013-11-24 23:03:34 +00:00
osExit ( 0 )
2013-08-27 02:07:28 +00:00
case <- time . After ( 2 * time . Second ) :
exitf ( "Timeout shutting down. Exiting uncleanly." )
}
2012-05-15 08:44:03 +00:00
default :
log . Fatal ( "Received another signal, should not happen." )
}
}
}
2012-09-16 22:20:49 +00:00
// listenAndBaseURL finds the configured, default, or inferred listen address
// and base URL from the command-line flags and provided config.
2014-01-23 16:18:22 +00:00
func listenAndBaseURL ( config * serverinit . Config ) ( listen , baseURL string ) {
2012-09-16 22:20:49 +00:00
baseURL = config . OptionalString ( "baseURL" , "" )
2014-08-02 21:31:02 +00:00
listen = * flagListen
2012-09-16 22:20:49 +00:00
listenConfig := config . OptionalString ( "listen" , "" )
// command-line takes priority over config
if listen == "" {
listen = listenConfig
if listen == "" {
exitf ( "\"listen\" needs to be specified either in the config or on the command line" )
}
}
return
}
2014-08-16 06:17:14 +00:00
func redirectFromHTTP ( base string ) {
http . ListenAndServe ( ":80" , http . HandlerFunc ( func ( w http . ResponseWriter , r * http . Request ) {
http . Redirect ( w , r , base , http . StatusFound )
} ) )
}
2013-11-24 23:03:34 +00:00
// main wraps Main so tests (which generate their own func main) can still run Main.
2010-06-12 21:45:58 +00:00
func main ( ) {
2013-11-24 23:03:34 +00:00
Main ( nil , nil )
}
// Main sends on up when it's running, and shuts down when it receives from down.
func Main ( up chan <- struct { } , down <- chan struct { } ) {
2010-06-12 21:45:58 +00:00
flag . Parse ( )
2013-02-02 18:59:39 +00:00
if * flagVersion {
2013-11-28 16:55:02 +00:00
fmt . Fprintf ( os . Stderr , "camlistored version: %s\nGo version: %s (%s/%s)\n" ,
buildinfo . Version ( ) , runtime . Version ( ) , runtime . GOOS , runtime . GOARCH )
2013-02-02 18:59:39 +00:00
return
}
2014-07-03 20:03:31 +00:00
if legalprint . MaybePrint ( os . Stderr ) {
return
}
2013-12-25 17:49:51 +00:00
if * flagReindex {
index . SetImpendingReindex ( )
}
2013-02-02 18:59:39 +00:00
2013-11-28 16:55:02 +00:00
log . Printf ( "Starting camlistored version %s; Go %s (%s/%s)" , buildinfo . Version ( ) , runtime . Version ( ) ,
runtime . GOOS , runtime . GOARCH )
2013-08-27 02:07:28 +00:00
shutdownc := make ( chan io . Closer , 1 ) // receives io.Closer to cleanly shut down
go handleSignals ( shutdownc )
2014-08-16 00:15:09 +00:00
// In case we're running in a Docker container with no
// filesytem from which to load the root CAs, this
// conditionally installs a static set if necessary. We do
// this before we load the config file, which might come from
// an https URL.
httputil . InstallCerts ( )
2014-08-02 23:53:58 +00:00
config , isNewConfig , err := loadConfig ( * flagConfigFile )
2012-03-15 12:31:06 +00:00
if err != nil {
2014-08-05 05:50:56 +00:00
exitf ( "Error loading config file: %v" , err )
2012-03-15 12:31:06 +00:00
}
2011-09-30 05:07:04 +00:00
2011-04-04 02:58:20 +00:00
ws := webserver . New ( )
2012-09-16 22:20:49 +00:00
listen , baseURL := listenAndBaseURL ( config )
2011-04-04 02:58:20 +00:00
2012-03-15 12:31:06 +00:00
setupTLS ( ws , config , listen )
2011-09-30 04:18:12 +00:00
2012-09-16 22:20:49 +00:00
err = ws . Listen ( listen )
if err != nil {
exitf ( "Listen: %v" , err )
}
2011-09-30 04:18:12 +00:00
2013-11-17 08:39:06 +00:00
if baseURL == "" {
baseURL = ws . ListenURL ( )
}
2013-12-24 03:07:17 +00:00
shutdownCloser , err := config . InstallHandlers ( ws , baseURL , * flagReindex , nil )
2013-11-17 08:39:06 +00:00
if err != nil {
exitf ( "Error parsing config: %v" , err )
2011-09-30 04:18:12 +00:00
}
2013-11-17 08:39:06 +00:00
shutdownc <- shutdownCloser
urlToOpen := baseURL
2013-08-23 09:28:22 +00:00
if ! isNewConfig {
// user may like to configure the server at the initial startup,
// open UI if this is not the first run with a new config file.
urlToOpen += config . UIPath
}
if * flagOpenBrowser {
go osutil . OpenURL ( urlToOpen )
2012-04-16 14:08:36 +00:00
}
2012-11-07 16:52:09 +00:00
2012-05-15 08:44:03 +00:00
go ws . Serve ( )
2013-07-30 19:57:50 +00:00
if flagPollParent {
osutil . DieOnParentDeath ( )
}
2013-11-24 23:03:34 +00:00
2014-05-08 14:07:29 +00:00
if err := config . StartApps ( ) ; err != nil {
exitf ( "StartApps: %v" , err )
}
2014-07-29 18:23:14 +00:00
for appName , appURL := range config . AppURL ( ) {
addr , err := netutil . HostPort ( appURL )
if err != nil {
log . Printf ( "Could not get app %v address: %v" , appName , err )
continue
}
if err := netutil . AwaitReachable ( addr , 5 * time . Second ) ; err != nil {
log . Printf ( "Could not reach app %v: %v" , appName , err )
}
}
2014-07-28 23:11:17 +00:00
log . Printf ( "Available on %s" , urlToOpen )
2014-08-16 06:17:14 +00:00
if gce . OnGCE ( ) && strings . HasPrefix ( baseURL , "https://" ) {
go redirectFromHTTP ( baseURL )
}
2013-11-24 23:03:34 +00:00
// Block forever, except during tests.
up <- struct { } { }
<- down
osExit ( 0 )
2011-09-30 04:18:12 +00:00
}