Debian Codegraph @

service_unix.go

raw symbols SCIP
1 // +build !windows
2
3 package service
4
5 import (
6 "fmt"
7 "gopkg.in/hlandau/easyconfig.v1/cflag"
8 "gopkg.in/hlandau/service.v2/daemon"
9 "gopkg.in/hlandau/service.v2/daemon/bansuid"
10 "gopkg.in/hlandau/svcutils.v1/caps"
11 "gopkg.in/hlandau/svcutils.v1/passwd"
12 "gopkg.in/hlandau/svcutils.v1/pidfile"
13 "gopkg.in/hlandau/svcutils.v1/systemd"
14 "os"
15 "strconv"
16 )
17
18 // This will always point to a path which the platform guarantees is an empty
19 // directory. You can use it as your default chroot path if your service doesn't
20 // access the filesystem after it's started.
21 //
22 // On Linux, the FHS provides that "/var/empty" is an empty directory, so it
23 // points to that.
24 var EmptyChrootPath = daemon.EmptyChrootPath
25
26 var (
27 uidFlag = cflag.String(fg, "uid", "", "UID to run as (default: don't drop privileges)")
28 gidFlag = cflag.String(fg, "gid", "", "GID to run as (default: don't drop privileges)")
29 daemonizeFlag = cflag.Bool(fg, "daemon", false, "Run as daemon? (doesn't fork)")
30 stderrFlag = cflag.Bool(fg, "stderr", false, "Keep stderr open when daemonizing")
31 chrootFlag = cflag.String(fg, "chroot", "", "Chroot to a directory (must set UID, GID) (\"/\" disables)")
32 pidfileFlag = cflag.String(fg, "pidfile", "", "Write PID to file with given filename and hold a write lock")
33 forkFlag = cflag.Bool(fg, "fork", false, "Fork? (implies -daemon)")
34 )
35
36 func systemdUpdateStatus(status string) error {
37 return systemd.NotifySend(status)
38 }
39
40 func (info *Info) serviceMain() error {
41 if forkFlag.Value() {
42 isParent, err := daemon.Fork()
43 if err != nil {
44 return err
45 }
46
47 if isParent {
48 os.Exit(0)
49 }
50
51 daemonizeFlag.SetValue(true)
52 }
53
54 err := daemon.Init()
55 if err != nil {
56 return err
57 }
58
59 err = systemdUpdateStatus("\n")
60 if err == nil {
61 info.systemd = true
62 }
63
64 // default: daemon=no, stderr=yes
65 // --daemon: daemon=yes, stderr=no
66 // systemd/--daemon --stderr: daemon=yes, stderr=yes
67 // systemd --daemon: daemon=yes, stderr=no
68 daemonize := daemonizeFlag.Value()
69 keepStderr := stderrFlag.Value()
70 if !daemonize && info.systemd {
71 daemonize = true
72 keepStderr = true
73 }
74
75 if daemonize {
76 err := daemon.Daemonize(keepStderr)
77 if err != nil {
78 return err
79 }
80 }
81
82 if pidfileFlag.Value() != "" {
83 info.pidFileName = pidfileFlag.Value()
84
85 err = info.openPIDFile()
86 if err != nil {
87 return err
88 }
89
90 defer info.closePIDFile()
91 }
92
93 return info.runInteractively()
94 }
95
96 func (info *Info) openPIDFile() error {
97 f, err := pidfile.Open(info.pidFileName)
98 info.pidFile = f
99 return err
100 }
101
102 func (info *Info) closePIDFile() {
103 if info.pidFile != nil {
104 info.pidFile.Close()
105 }
106 }
107
108 func (h *ihandler) DropPrivileges() error {
109 if h.dropped {
110 return nil
111 }
112
113 // Extras
114 if !h.info.NoBanSuid {
115 // Try and bansuid, but don't process errors. It may not be supported on
116 // the current platform, and Linux won't allow SECUREBITS to be set unless
117 // one is root (or has the right capability). This is basically a
118 // best-effort thing.
119 bansuid.BanSuid()
120 }
121
122 // Various fixups
123 if uidFlag.Value() != "" && gidFlag.Value() == "" {
124 gid, err := passwd.GetGIDForUID(uidFlag.Value())
125 if err != nil {
126 return err
127 }
128 gidFlag.SetValue(strconv.FormatInt(int64(gid), 10))
129 }
130
131 if h.info.DefaultChroot == "" {
132 h.info.DefaultChroot = "/"
133 }
134
135 chrootPath := chrootFlag.Value()
136 if chrootPath == "" {
137 chrootPath = h.info.DefaultChroot
138 }
139
140 uid := -1
141 gid := -1
142 if uidFlag.Value() != "" {
143 var err error
144 uid, err = passwd.ParseUID(uidFlag.Value())
145 if err != nil {
146 return err
147 }
148
149 gid, err = passwd.ParseGID(gidFlag.Value())
150 if err != nil {
151 return err
152 }
153 }
154
155 if (uid <= 0) != (gid <= 0) {
156 return fmt.Errorf("Either both or neither of the UID and GID must be positive")
157 }
158
159 if uid > 0 {
160 chrootErr, err := daemon.DropPrivileges(uid, gid, chrootPath)
161 if err != nil {
162 return fmt.Errorf("Failed to drop privileges: %v", err)
163 }
164 if chrootErr != nil && chrootFlag.Value() != "" && chrootFlag.Value() != "/" {
165 return fmt.Errorf("Failed to chroot: %v", chrootErr)
166 }
167 } else if chrootFlag.Value() != "" && chrootFlag.Value() != "/" {
168 return fmt.Errorf("Must use privilege dropping to use chroot; set -uid")
169 }
170
171 // If we still have any caps (maybe because we didn't setuid), try and drop them.
172 err := caps.Drop()
173 if err != nil {
174 return fmt.Errorf("cannot drop caps: %v", err)
175 }
176
177 if !h.info.AllowRoot && daemon.IsRoot() {
178 return fmt.Errorf("Daemon must not run as root or with capabilities; run as non-root user or use -uid")
179 }
180
181 h.dropped = true
182 return nil
183 }
184
185 // © 2015 Hugo Landau <[email protected]> ISC License