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
|
|
|
|
|
|
|
package main
|
|
|
|
|
2010-07-11 04:18:16 +00:00
|
|
|
import (
|
2011-11-10 15:20:22 +00:00
|
|
|
"big"
|
|
|
|
"crypto/x509/pkix"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
2011-04-02 05:14:23 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2011-07-18 00:30:40 +00:00
|
|
|
"runtime"
|
2011-04-02 05:14:23 +00:00
|
|
|
"strings"
|
2011-11-10 15:20:22 +00:00
|
|
|
"time"
|
2011-04-02 05:14:23 +00:00
|
|
|
"os"
|
2011-10-04 23:22:12 +00:00
|
|
|
"path/filepath"
|
2011-04-02 05:14:23 +00:00
|
|
|
|
2011-04-03 15:07:40 +00:00
|
|
|
"camli/osutil"
|
2011-09-30 05:07:04 +00:00
|
|
|
"camli/serverconfig"
|
2011-04-16 05:25:45 +00:00
|
|
|
"camli/webserver"
|
2011-04-02 05:14:23 +00:00
|
|
|
|
|
|
|
// Storage options:
|
2011-06-04 04:52:56 +00:00
|
|
|
_ "camli/blobserver/cond"
|
2011-05-09 19:07:56 +00:00
|
|
|
_ "camli/blobserver/localdisk"
|
2011-05-21 20:40:17 +00:00
|
|
|
_ "camli/blobserver/remote"
|
2011-05-23 04:22:21 +00:00
|
|
|
_ "camli/blobserver/replica"
|
2011-04-02 03:45:40 +00:00
|
|
|
_ "camli/blobserver/s3"
|
2011-05-21 16:26:20 +00:00
|
|
|
_ "camli/blobserver/shard"
|
2011-05-01 23:10:53 +00:00
|
|
|
_ "camli/mysqlindexer" // indexer, but uses storage interface
|
2011-05-30 05:52:31 +00:00
|
|
|
// Handlers:
|
|
|
|
_ "camli/search"
|
2011-10-18 18:12:01 +00:00
|
|
|
_ "camli/server" // UI, publish, etc
|
2010-07-11 04:18:16 +00:00
|
|
|
)
|
2010-07-07 04:57:53 +00:00
|
|
|
|
2011-11-10 15:20:22 +00:00
|
|
|
const defCert = "config/selfgen_cert.pem"
|
|
|
|
const defKey = "config/selfgen_key.pem"
|
|
|
|
|
2011-05-01 23:10:53 +00:00
|
|
|
var flagConfigFile = flag.String("configfile", "serverconfig",
|
|
|
|
"Config file to use, relative to camli config dir root, or blank to not use config files.")
|
2011-03-05 23:09:36 +00:00
|
|
|
|
2011-02-04 22:31:23 +00:00
|
|
|
func exitFailure(pattern string, args ...interface{}) {
|
|
|
|
if !strings.HasSuffix(pattern, "\n") {
|
|
|
|
pattern = pattern + "\n"
|
|
|
|
}
|
|
|
|
fmt.Fprintf(os.Stderr, pattern, args...)
|
|
|
|
os.Exit(1)
|
2010-06-12 21:45:58 +00:00
|
|
|
}
|
|
|
|
|
2011-11-10 15:20:22 +00:00
|
|
|
// Mostly copied from $GOROOT/src/pkg/crypto/tls/generate_cert.go
|
|
|
|
func genSelfTLS() os.Error {
|
|
|
|
priv, err := rsa.GenerateKey(rand.Reader, 1024)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to generate private key: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Seconds()
|
|
|
|
|
|
|
|
baseurl := os.Getenv("CAMLI_BASEURL")
|
|
|
|
if baseurl == "" {
|
|
|
|
return fmt.Errorf("CAMLI_BASEURL is not set")
|
|
|
|
}
|
|
|
|
split := strings.Split(baseurl, ":")
|
|
|
|
hostname := split[1]
|
|
|
|
hostname = hostname[2:len(hostname)]
|
|
|
|
|
|
|
|
template := x509.Certificate{
|
|
|
|
SerialNumber: new(big.Int).SetInt64(0),
|
|
|
|
Subject: pkix.Name{
|
|
|
|
CommonName: hostname,
|
|
|
|
Organization: []string{hostname},
|
|
|
|
},
|
|
|
|
NotBefore: time.SecondsToUTC(now - 300),
|
|
|
|
NotAfter: time.SecondsToUTC(now + 60*60*24*365), // valid for 1 year.
|
|
|
|
|
|
|
|
SubjectKeyId: []byte{1, 2, 3, 4},
|
|
|
|
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
|
|
|
|
}
|
|
|
|
|
|
|
|
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to create certificate: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
certOut, err := os.Create(defCert)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to open %s for writing: %s", defCert, err)
|
|
|
|
}
|
|
|
|
pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
|
|
|
|
certOut.Close()
|
|
|
|
log.Printf("written %s\n", defCert)
|
|
|
|
|
|
|
|
keyOut, err := os.OpenFile(defKey, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to open %s for writing:", defKey, err)
|
|
|
|
}
|
|
|
|
pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)})
|
|
|
|
keyOut.Close()
|
|
|
|
log.Printf("written %s\n", defKey)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2011-11-16 10:41:38 +00:00
|
|
|
func notExist(err os.Error) bool {
|
|
|
|
pe, ok := err.(*os.PathError)
|
|
|
|
return ok && pe.Error == os.ENOENT
|
|
|
|
}
|
|
|
|
|
2010-06-12 21:45:58 +00:00
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
2011-10-04 23:22:12 +00:00
|
|
|
file := *flagConfigFile
|
|
|
|
if !filepath.IsAbs(file) {
|
|
|
|
file = filepath.Join(osutil.CamliConfigDir(), file)
|
|
|
|
}
|
|
|
|
config, err := serverconfig.Load(file)
|
2011-09-30 05:07:04 +00:00
|
|
|
if err != nil {
|
|
|
|
exitFailure("Could not load server config: %v", err)
|
|
|
|
}
|
|
|
|
|
2011-04-04 02:58:20 +00:00
|
|
|
ws := webserver.New()
|
|
|
|
baseURL := ws.BaseURL()
|
|
|
|
|
2011-05-09 18:49:02 +00:00
|
|
|
{
|
2011-11-16 10:41:38 +00:00
|
|
|
cert, key := config.OptionalString("TLSCertFile", ""), config.OptionalString("TLSKeyFile", "")
|
2011-11-28 17:45:08 +00:00
|
|
|
secure := config.OptionalBool("https", true)
|
2011-11-10 15:20:22 +00:00
|
|
|
if secure {
|
|
|
|
if (cert != "") != (key != "") {
|
|
|
|
exitFailure("TLSCertFile and TLSKeyFile must both be either present or absent")
|
|
|
|
}
|
|
|
|
|
2011-11-16 10:41:38 +00:00
|
|
|
if cert == defCert && key == defKey {
|
|
|
|
_, err1 := os.Stat(cert)
|
|
|
|
_, err2 := os.Stat(key)
|
|
|
|
if err1 != nil || err2 != nil {
|
|
|
|
if notExist(err1) || notExist(err2) {
|
|
|
|
err = genSelfTLS()
|
|
|
|
if err != nil {
|
|
|
|
exitFailure("Could not generate self signed creds: %q", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
exitFailure("Could not stat cert or key: %q, %q", err1, err2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-11-10 15:20:22 +00:00
|
|
|
if cert == "" && key == "" {
|
|
|
|
err = genSelfTLS()
|
|
|
|
if err != nil {
|
2011-11-16 10:41:38 +00:00
|
|
|
exitFailure("Could not generate self signed creds: %q", err)
|
2011-11-10 15:20:22 +00:00
|
|
|
}
|
|
|
|
cert = defCert
|
|
|
|
key = defKey
|
|
|
|
}
|
2011-05-09 18:49:02 +00:00
|
|
|
ws.SetTLS(cert, key)
|
|
|
|
}
|
2011-04-04 02:38:22 +00:00
|
|
|
}
|
|
|
|
|
2011-10-26 02:40:50 +00:00
|
|
|
err = config.InstallHandlers(ws, baseURL, nil)
|
2011-09-30 04:18:12 +00:00
|
|
|
if err != nil {
|
2011-09-30 05:07:04 +00:00
|
|
|
exitFailure("Error parsing config: %v", err)
|
2011-09-30 04:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ws.Listen()
|
|
|
|
|
2011-09-30 05:07:04 +00:00
|
|
|
if config.UIPath != "" {
|
|
|
|
uiURL := ws.BaseURL() + config.UIPath
|
2011-09-30 04:18:12 +00:00
|
|
|
log.Printf("UI available at %s", uiURL)
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// Might be double-clicking an icon with no shell window?
|
|
|
|
// Just open the URL for them.
|
|
|
|
osutil.OpenURL(uiURL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ws.Serve()
|
|
|
|
}
|