src/pkg/syscall/sockcmsg_linux.go - The Go Programming Language

Golang

Source file src/pkg/syscall/sockcmsg_linux.go

     1	// Copyright 2011 The Go Authors.  All rights reserved.
     2	// Use of this source code is governed by a BSD-style
     3	// license that can be found in the LICENSE file.
     4	
     5	// Socket control messages
     6	
     7	package syscall
     8	
     9	import (
    10		"unsafe"
    11	)
    12	
    13	// UnixCredentials encodes credentials into a socket control message
    14	// for sending to another process. This can be used for
    15	// authentication.
    16	func UnixCredentials(ucred *Ucred) []byte {
    17		buf := make([]byte, CmsgSpace(SizeofUcred))
    18		cmsg := (*Cmsghdr)(unsafe.Pointer(&buf[0]))
    19		cmsg.Level = SOL_SOCKET
    20		cmsg.Type = SCM_CREDENTIALS
    21		cmsg.SetLen(CmsgLen(SizeofUcred))
    22		*((*Ucred)(cmsgData(cmsg))) = *ucred
    23		return buf
    24	}
    25	
    26	// ParseUnixCredentials decodes a socket control message that contains
    27	// credentials in a Ucred structure. To receive such a message, the
    28	// SO_PASSCRED option must be enabled on the socket.
    29	func ParseUnixCredentials(msg *SocketControlMessage) (*Ucred, error) {
    30		if msg.Header.Level != SOL_SOCKET {
    31			return nil, EINVAL
    32		}
    33		if msg.Header.Type != SCM_CREDENTIALS {
    34			return nil, EINVAL
    35		}
    36		ucred := *(*Ucred)(unsafe.Pointer(&msg.Data[0]))
    37		return &ucred, nil
    38	}