| 1 |
// Copyright 2021 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
// copied from https://github.com/google/glazier/blob/a00964a46d35de3f6193f0d3f1b4e490e9630e19/go/identity/identity.go |
| 16 |
|
| 17 |
package win32 |
| 18 |
|
| 19 |
import ( |
| 20 |
"errors" |
| 21 |
"os" |
| 22 |
|
| 23 |
"github.com/yusufpapurcu/wmi" |
| 24 |
) |
| 25 |
|
| 26 |
// nolint |
| 27 |
type Win32_ComputerSystem struct { |
| 28 |
Partofdomain bool |
| 29 |
} |
| 30 |
|
| 31 |
// DomainJoined attempts to determine whether the machine is actively domain joined. |
| 32 |
|
| 33 |
func DomainJoined() bool { |
| 34 |
domainJoined, err := domainJoinedWmi() |
| 35 |
if err != nil { |
| 36 |
// simple additional domain check: a local User got the computername = domain |
| 37 |
return os.Getenv("USERDOMAIN") != os.Getenv("COMPUTERNAME") |
| 38 |
} |
| 39 |
return domainJoined |
| 40 |
} |
| 41 |
|
| 42 |
func domainJoinedWmi() (bool, error) { |
| 43 |
var c []Win32_ComputerSystem |
| 44 |
q := wmi.CreateQuery(&c, "") |
| 45 |
|
| 46 |
if err := wmi.Query(q, &c); err != nil { |
| 47 |
return false, err |
| 48 |
} |
| 49 |
if len(c) < 1 { |
| 50 |
return false, errors.New("no result from wmi query") |
| 51 |
} |
| 52 |
return c[0].Partofdomain, nil |
| 53 |
} |