2011-05-20 02:18:15 +02:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
// +build darwin freebsd linux netbsd
|
2012-01-13 06:11:45 +01:00
|
|
|
// +build cgo
|
2011-10-27 01:57:58 +02:00
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2012-02-09 09:19:58 +01:00
|
|
|
"strconv"
|
2011-05-20 02:18:15 +02:00
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
static int mygetpwuid_r(int uid, struct passwd *pwd,
|
|
|
|
char *buf, size_t buflen, struct passwd **result) {
|
|
|
|
return getpwuid_r(uid, pwd, buf, buflen, result);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2012-02-07 20:26:30 +01:00
|
|
|
//extern getpwnam_r
|
|
|
|
func libc_getpwnam_r(name *byte, pwd *syscall.Passwd, buf *byte, buflen syscall.Size_t, result **syscall.Passwd) int
|
|
|
|
|
|
|
|
//extern getpwuid_r
|
|
|
|
func libc_getpwuid_r(uid syscall.Uid_t, pwd *syscall.Passwd, buf *byte, buflen syscall.Size_t, result **syscall.Passwd) int
|
2011-05-20 02:18:15 +02:00
|
|
|
|
2011-10-23 21:04:37 +02:00
|
|
|
// bytePtrToString takes a NUL-terminated array of bytes and convert
|
|
|
|
// it to a Go string.
|
|
|
|
func bytePtrToString(p *byte) string {
|
|
|
|
a := (*[10000]byte)(unsafe.Pointer(p))
|
|
|
|
i := 0
|
|
|
|
for a[i] != 0 {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return string(a[:i])
|
|
|
|
}
|
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
// Current returns the current user.
|
|
|
|
func Current() (*User, error) {
|
|
|
|
return lookup(syscall.Getuid(), "", false)
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
// Lookup looks up a user by username. If the user cannot be found,
|
|
|
|
// the returned error is of type UnknownUserError.
|
2011-12-03 03:17:34 +01:00
|
|
|
func Lookup(username string) (*User, error) {
|
2011-05-20 02:18:15 +02:00
|
|
|
return lookup(-1, username, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LookupId looks up a user by userid. If the user cannot be found,
|
|
|
|
// the returned error is of type UnknownUserIdError.
|
2012-02-09 09:19:58 +01:00
|
|
|
func LookupId(uid string) (*User, error) {
|
|
|
|
i, e := strconv.Atoi(uid)
|
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
return lookup(i, "", false)
|
2011-05-20 02:18:15 +02:00
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func lookup(uid int, username string, lookupByName bool) (*User, error) {
|
2011-05-20 02:18:15 +02:00
|
|
|
var pwd syscall.Passwd
|
|
|
|
var result *syscall.Passwd
|
|
|
|
|
|
|
|
// FIXME: Should let buf grow if necessary.
|
|
|
|
const bufSize = 1024
|
|
|
|
buf := make([]byte, bufSize)
|
|
|
|
if lookupByName {
|
2012-06-13 07:56:09 +02:00
|
|
|
p := syscall.StringBytePtr(username)
|
|
|
|
syscall.Entersyscall()
|
|
|
|
rv := libc_getpwnam_r(p,
|
2011-05-20 02:18:15 +02:00
|
|
|
&pwd,
|
|
|
|
&buf[0],
|
|
|
|
bufSize,
|
|
|
|
&result)
|
2012-06-13 07:56:09 +02:00
|
|
|
syscall.Exitsyscall()
|
2011-05-20 02:18:15 +02:00
|
|
|
if rv != 0 {
|
2011-12-13 00:40:51 +01:00
|
|
|
return nil, fmt.Errorf("user: lookup username %s: %s", username, syscall.GetErrno())
|
2011-05-20 02:18:15 +02:00
|
|
|
}
|
|
|
|
if result == nil {
|
|
|
|
return nil, UnknownUserError(username)
|
|
|
|
}
|
|
|
|
} else {
|
2012-06-13 07:56:09 +02:00
|
|
|
syscall.Entersyscall()
|
2011-05-20 02:18:15 +02:00
|
|
|
rv := libc_getpwuid_r(syscall.Uid_t(uid),
|
|
|
|
&pwd,
|
|
|
|
&buf[0],
|
|
|
|
bufSize,
|
|
|
|
&result)
|
2012-06-13 07:56:09 +02:00
|
|
|
syscall.Exitsyscall()
|
2011-05-20 02:18:15 +02:00
|
|
|
if rv != 0 {
|
2011-12-13 00:40:51 +01:00
|
|
|
return nil, fmt.Errorf("user: lookup userid %d: %s", uid, syscall.GetErrno())
|
2011-05-20 02:18:15 +02:00
|
|
|
}
|
|
|
|
if result == nil {
|
|
|
|
return nil, UnknownUserIdError(uid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
u := &User{
|
2012-02-09 09:19:58 +01:00
|
|
|
Uid: strconv.Itoa(int(pwd.Pw_uid)),
|
|
|
|
Gid: strconv.Itoa(int(pwd.Pw_gid)),
|
2011-10-23 21:04:37 +02:00
|
|
|
Username: bytePtrToString((*byte)(unsafe.Pointer(pwd.Pw_name))),
|
|
|
|
Name: bytePtrToString((*byte)(unsafe.Pointer(pwd.Pw_gecos))),
|
|
|
|
HomeDir: bytePtrToString((*byte)(unsafe.Pointer(pwd.Pw_dir))),
|
2011-05-20 02:18:15 +02:00
|
|
|
}
|
|
|
|
// The pw_gecos field isn't quite standardized. Some docs
|
|
|
|
// say: "It is expected to be a comma separated list of
|
|
|
|
// personal data where the first item is the full name of the
|
|
|
|
// user."
|
|
|
|
if i := strings.Index(u.Name, ","); i >= 0 {
|
|
|
|
u.Name = u.Name[:i]
|
|
|
|
}
|
|
|
|
return u, nil
|
|
|
|
}
|