Suharsh Sivakumar | 628a8ee | 2015-01-14 11:38:56 -0800 | [diff] [blame] | 1 | package rt |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "os/user" |
| 7 | "strconv" |
Ryan Brown | a08a221 | 2015-01-15 15:40:10 -0800 | [diff] [blame] | 8 | "syscall" |
Suharsh Sivakumar | 628a8ee | 2015-01-14 11:38:56 -0800 | [diff] [blame] | 9 | |
| 10 | "v.io/core/veyron2/context" |
Matt Rosencrantz | 99cc06e | 2015-01-16 10:25:11 -0800 | [diff] [blame] | 11 | "v.io/core/veyron2/ipc" |
Suharsh Sivakumar | 628a8ee | 2015-01-14 11:38:56 -0800 | [diff] [blame] | 12 | "v.io/core/veyron2/mgmt" |
| 13 | "v.io/core/veyron2/security" |
| 14 | |
| 15 | "v.io/core/veyron/lib/exec" |
| 16 | "v.io/core/veyron/lib/stats" |
| 17 | vsecurity "v.io/core/veyron/security" |
| 18 | "v.io/core/veyron/security/agent" |
| 19 | ) |
| 20 | |
Matt Rosencrantz | 3199588 | 2015-01-27 20:00:48 -0800 | [diff] [blame^] | 21 | func initSecurity(ctx *context.T, credentials string, client ipc.Client) (security.Principal, error) { |
| 22 | principal, err := setupPrincipal(ctx, credentials, client) |
Suharsh Sivakumar | 628a8ee | 2015-01-14 11:38:56 -0800 | [diff] [blame] | 23 | if err != nil { |
| 24 | return nil, err |
| 25 | } |
| 26 | |
| 27 | // TODO(suharshs,mattr): Move this code to SetNewPrincipal and determine what their string should be. |
| 28 | stats.NewString("security/principal/key").Set(principal.PublicKey().String()) |
| 29 | stats.NewStringFunc("security/principal/blessingstore", principal.BlessingStore().DebugString) |
| 30 | stats.NewStringFunc("security/principal/blessingroots", principal.Roots().DebugString) |
| 31 | return principal, nil |
| 32 | } |
| 33 | |
Matt Rosencrantz | 3199588 | 2015-01-27 20:00:48 -0800 | [diff] [blame^] | 34 | func setupPrincipal(ctx *context.T, credentials string, client ipc.Client) (security.Principal, error) { |
Suharsh Sivakumar | 628a8ee | 2015-01-14 11:38:56 -0800 | [diff] [blame] | 35 | var err error |
| 36 | var principal security.Principal |
| 37 | if principal, _ = ctx.Value(principalKey).(security.Principal); principal != nil { |
| 38 | return principal, nil |
| 39 | } |
Matt Rosencrantz | 3199588 | 2015-01-27 20:00:48 -0800 | [diff] [blame^] | 40 | if fd, err := agentFD(); err != nil { |
Suharsh Sivakumar | 628a8ee | 2015-01-14 11:38:56 -0800 | [diff] [blame] | 41 | return nil, err |
| 42 | } else if fd >= 0 { |
Matt Rosencrantz | e5dc980 | 2015-01-21 09:50:42 -0800 | [diff] [blame] | 43 | return connectToAgent(ctx, fd, client) |
Suharsh Sivakumar | 628a8ee | 2015-01-14 11:38:56 -0800 | [diff] [blame] | 44 | } |
| 45 | if len(credentials) > 0 { |
| 46 | // TODO(ataly, ashankar): If multiple runtimes are getting |
| 47 | // initialized at the same time from the same VEYRON_CREDENTIALS |
| 48 | // we will need some kind of locking for the credential files. |
| 49 | if principal, err = vsecurity.LoadPersistentPrincipal(credentials, nil); err != nil { |
| 50 | if os.IsNotExist(err) { |
| 51 | if principal, err = vsecurity.CreatePersistentPrincipal(credentials, nil); err != nil { |
| 52 | return principal, err |
| 53 | } |
| 54 | return principal, vsecurity.InitDefaultBlessings(principal, defaultBlessingName()) |
| 55 | } |
| 56 | return nil, err |
| 57 | } |
| 58 | return principal, nil |
| 59 | } |
| 60 | if principal, err = vsecurity.NewPrincipal(); err != nil { |
| 61 | return principal, err |
| 62 | } |
| 63 | return principal, vsecurity.InitDefaultBlessings(principal, defaultBlessingName()) |
| 64 | } |
| 65 | |
| 66 | // agentFD returns a non-negative file descriptor to be used to communicate with |
| 67 | // the security agent if the current process has been configured to use the |
| 68 | // agent. |
Matt Rosencrantz | 3199588 | 2015-01-27 20:00:48 -0800 | [diff] [blame^] | 69 | func agentFD() (int, error) { |
| 70 | handle, err := exec.GetChildHandle() |
| 71 | if err != nil && err != exec.ErrNoVersion { |
| 72 | return -1, err |
| 73 | } |
Suharsh Sivakumar | 628a8ee | 2015-01-14 11:38:56 -0800 | [diff] [blame] | 74 | var fd string |
| 75 | if handle != nil { |
| 76 | // We were started by a parent (presumably, device manager). |
| 77 | fd, _ = handle.Config.Get(mgmt.SecurityAgentFDConfigKey) |
| 78 | } else { |
| 79 | fd = os.Getenv(agent.FdVarName) |
| 80 | } |
| 81 | if fd == "" { |
| 82 | return -1, nil |
| 83 | } |
Ryan Brown | a08a221 | 2015-01-15 15:40:10 -0800 | [diff] [blame] | 84 | ifd, err := strconv.Atoi(fd) |
| 85 | if err == nil && handle != nil { |
| 86 | // If we're using a handle, children can't inherit the agent. |
| 87 | syscall.CloseOnExec(ifd) |
| 88 | } |
| 89 | return ifd, err |
Suharsh Sivakumar | 628a8ee | 2015-01-14 11:38:56 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | func defaultBlessingName() string { |
| 93 | var name string |
| 94 | if user, _ := user.Current(); user != nil && len(user.Username) > 0 { |
| 95 | name = user.Username |
| 96 | } else { |
| 97 | name = "anonymous" |
| 98 | } |
| 99 | if host, _ := os.Hostname(); len(host) > 0 { |
| 100 | name = name + "@" + host |
| 101 | } |
| 102 | return fmt.Sprintf("%s-%d", name, os.Getpid()) |
| 103 | } |
Matt Rosencrantz | e5dc980 | 2015-01-21 09:50:42 -0800 | [diff] [blame] | 104 | |
| 105 | func connectToAgent(ctx *context.T, fd int, client ipc.Client) (security.Principal, error) { |
| 106 | // Dup the fd, so we can create multiple runtimes. |
| 107 | syscall.ForkLock.Lock() |
| 108 | newfd, err := syscall.Dup(fd) |
| 109 | if err == nil { |
| 110 | syscall.CloseOnExec(newfd) |
| 111 | } |
| 112 | syscall.ForkLock.Unlock() |
| 113 | if err != nil { |
| 114 | return nil, err |
| 115 | } |
| 116 | return agent.NewAgentPrincipal(ctx, newfd, client) |
| 117 | } |