summaryrefslogtreecommitdiff
path: root/nslcd_server/ctx.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2017-12-18 15:07:24 -0500
committerLuke Shumaker <lukeshu@lukeshu.com>2017-12-18 22:37:34 -0500
commit379e4d7e4d2539f35d559d4b0752531e546238a9 (patch)
tree844ba145ef8bb03824985d3e7a47740b2b5438c2 /nslcd_server/ctx.go
parent44798efb9ec9a89416658707d952e60e8e5fe01b (diff)
nslcd_{server,systemd}: BREAKING CHANGE: use contexts
Diffstat (limited to 'nslcd_server/ctx.go')
-rw-r--r--nslcd_server/ctx.go46
1 files changed, 46 insertions, 0 deletions
diff --git a/nslcd_server/ctx.go b/nslcd_server/ctx.go
new file mode 100644
index 0000000..5214adc
--- /dev/null
+++ b/nslcd_server/ctx.go
@@ -0,0 +1,46 @@
+// Copyright (C) 2017 Luke Shumaker <lukeshu@sbcglobal.net>
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+// 02110-1301 USA
+
+package nslcd_server
+
+import (
+ "context"
+
+ "golang.org/x/sys/unix"
+)
+
+// contextKey is a value for use with context.WithValue. It's used as
+// a pointer so it fits in an interface{} without allocation.
+type contextKey struct {
+ name string
+}
+
+var (
+ // PeerCredKey is a context key. It can be used in backend
+ // methods to access the credentials of the client process.
+ // The associated value will be of type
+ // "golang.org/x/sys/unix".Ucred
+ PeerCredKey = &contextKey{"peercred"}
+)
+
+// PeerCredFromContext is a convenience function for
+//
+// cred, ok := ctx.Value(nslcd_server.PeerCredKey).(unix.Ucred)
+func PeerCredFromContext(ctx context.Context) (unix.Ucred, bool) {
+ cred, ok := ctx.Value(PeerCredKey).(unix.Ucred)
+ return cred, ok
+}