summaryrefslogtreecommitdiff
path: root/nslcd_server/ctx.go
diff options
context:
space:
mode:
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
+}