Ejemplo n.º 1
0
// IsAnInteractiveSession determines if calling process is running interactively.
// It queries the process token for membership in the Interactive group.
// http://stackoverflow.com/questions/2668851/how-do-i-detect-that-my-application-is-running-as-service-or-in-an-interactive-s
func IsAnInteractiveSession() (bool, error) {
	interSid, err := allocSid(winapi.SECURITY_INTERACTIVE_RID)
	if err != nil {
		return false, err
	}
	defer winapi.FreeSid(interSid)

	serviceSid, err := allocSid(winapi.SECURITY_SERVICE_RID)
	if err != nil {
		return false, err
	}
	defer winapi.FreeSid(serviceSid)

	t, err := syscall.OpenCurrentProcessToken()
	if err != nil {
		return false, err
	}
	defer t.Close()

	gs, err := getTokenGroups(t)
	if err != nil {
		return false, err
	}
	p := unsafe.Pointer(&gs.Groups[0])
	groups := (*[2 << 20]syscall.SIDAndAttributes)(p)[:gs.GroupCount]
	for _, g := range groups {
		if winapi.EqualSid(g.Sid, interSid) {
			return true, nil
		}
		if winapi.EqualSid(g.Sid, serviceSid) {
			return false, nil
		}
	}
	return false, nil
}
Ejemplo n.º 2
0
func (fs *osFileSystem) currentHomeDir() (string, error) {
	t, err := syscall.OpenCurrentProcessToken()
	if err != nil {
		return "", err
	}
	defer t.Close()
	return t.GetUserProfileDirectory()
}
Ejemplo n.º 3
0
// Current returns the current user.
func Current() (*User, error) {
	t, e := syscall.OpenCurrentProcessToken()
	if e != nil {
		return nil, e
	}
	u, e := t.GetTokenUser()
	if e != nil {
		return nil, e
	}
	pg, e := t.GetTokenPrimaryGroup()
	if e != nil {
		return nil, e
	}
	gid, e := pg.PrimaryGroup.String()
	if e != nil {
		return nil, e
	}
	dir, e := t.GetUserProfileDirectory()
	if e != nil {
		return nil, e
	}
	return newUser(u.User.Sid, gid, dir)
}