File: config_linux.go

package info (click to toggle)
golang-github-tredoe-osutil 1.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 476 kB
  • sloc: makefile: 4
file content (279 lines) | stat: -rw-r--r-- 6,377 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright 2010 Jonas mg
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

package user

import (
	"fmt"
	"strings"
	"sync"

	"github.com/tredoe/osutil/config/shconf"
	"github.com/tredoe/osutil/internal"
	"github.com/tredoe/osutil/user/crypt"
)

// TODO: handle des, bcrypt and rounds in SHA2.

// TODO: Idea: store struct "configData" to run configData.Init() only when
// the configuration files have been modified.

// == System configuration files.

const fileLogin = "/etc/login.defs"

type confLogin struct {
	PASS_MIN_DAYS int
	PASS_MAX_DAYS int
	PASS_MIN_LEN  int
	PASS_WARN_AGE int

	SYS_UID_MIN int
	SYS_UID_MAX int
	SYS_GID_MIN int
	SYS_GID_MAX int

	UID_MIN int
	UID_MAX int
	GID_MIN int
	GID_MAX int

	ENCRYPT_METHOD       string // upper
	SHA_CRYPT_MIN_ROUNDS int
	SHA_CRYPT_MAX_ROUNDS int
	// or
	CRYPT_PREFIX string // $2a$
	CRYPT_ROUNDS int    // 8
}

const fileUseradd = "/etc/default/useradd"

type confUseradd struct {
	HOME  string // Default to '/home'
	SHELL string // Default to '/bin/sh'
}

// == Optional files.

// Used in systems derivated from Debian: Ubuntu, Mint.
const fileAdduser = "/etc/adduser.conf"

type confAdduser struct {
	FIRST_SYSTEM_UID int
	LAST_SYSTEM_UID  int
	FIRST_SYSTEM_GID int
	LAST_SYSTEM_GID  int

	FIRST_UID int
	LAST_UID  int
	FIRST_GID int
	LAST_GID  int
}

// Used in Arch, Manjaro, OpenSUSE.
// But it is only used by 'pam_unix2.so'.
const filePasswd = "/etc/default/passwd"

// TODO: to see the other options of that file.
type confPasswd struct {
	CRYPT string // lower
}

// Used in systems derivated from Red Hat: CentOS, Fedora, Mageia, PCLinuxOS.
const fileLibuser = "/etc/libuser.conf"

type confLibuser struct {
	login_defs  string
	crypt_style string // lower

	// For SHA2
	hash_rounds_min int
	hash_rounds_max int
}

// * * *

var debug bool // For testing

// A configData represents the configuration used to add users and groups.
type configData struct {
	login   confLogin
	useradd confUseradd

	crypter crypt.Crypter
	sync.Once
}

var config configData

// init sets the configuration data.
func (c *configData) init() error {
	_confLogin := &confLogin{}

	cfg, err := shconf.ParseFile(fileLogin)
	if err != nil {
		return err
	}
	if err = cfg.Unmarshal(_confLogin); err != nil {
		return err
	}
	if debug {
		fmt.Printf("\n* %s\n", fileLogin)
		internal.PrintStruct(_confLogin)
	}

	if _confLogin.PASS_MAX_DAYS == 0 {
		_confLogin.PASS_MAX_DAYS = 99999
	}
	if _confLogin.PASS_WARN_AGE == 0 {
		_confLogin.PASS_WARN_AGE = 7
	}

	cfg, err = shconf.ParseFile(fileUseradd)
	if err != nil {
		return err
	}
	_confUseradd := &confUseradd{}
	if err = cfg.Unmarshal(_confUseradd); err != nil {
		return err
	}
	if debug {
		fmt.Printf("\n* %s\n", fileUseradd)
		internal.PrintStruct(_confUseradd)
	}

	if _confUseradd.HOME == "" {
		_confUseradd.HOME = "/home"
	}
	if _confUseradd.SHELL == "" {
		_confUseradd.SHELL = "/bin/sh"
	}
	config.useradd = *_confUseradd

	// Optional files

	found, err := exist(fileAdduser) // Based in Debian.
	if found {
		cfg, err := shconf.ParseFile(fileAdduser)
		if err != nil {
			return err
		}
		_confAdduser := &confAdduser{}
		if err = cfg.Unmarshal(_confAdduser); err != nil {
			return err
		}
		if debug {
			fmt.Printf("\n* %s\n", fileAdduser)
			internal.PrintStruct(_confAdduser)
		}

		if _confLogin.SYS_UID_MIN == 0 || _confLogin.SYS_UID_MAX == 0 ||
			_confLogin.SYS_GID_MIN == 0 || _confLogin.SYS_GID_MAX == 0 ||
			_confLogin.UID_MIN == 0 || _confLogin.UID_MAX == 0 ||
			_confLogin.GID_MIN == 0 || _confLogin.GID_MAX == 0 {

			_confLogin.SYS_UID_MIN = _confAdduser.FIRST_SYSTEM_UID
			_confLogin.SYS_UID_MAX = _confAdduser.LAST_SYSTEM_UID
			_confLogin.SYS_GID_MIN = _confAdduser.FIRST_SYSTEM_GID
			_confLogin.SYS_GID_MAX = _confAdduser.LAST_SYSTEM_GID

			_confLogin.UID_MIN = _confAdduser.FIRST_UID
			_confLogin.UID_MAX = _confAdduser.LAST_UID
			_confLogin.GID_MIN = _confAdduser.FIRST_GID
			_confLogin.GID_MAX = _confAdduser.LAST_GID
		}
	} else if err != nil {
		return err

	} else if found, err = exist(fileLibuser); found { // Based in Red Hat.
		cfg, err := shconf.ParseFile(fileLibuser)
		if err != nil {
			return err
		}
		_confLibuser := &confLibuser{}
		if err = cfg.Unmarshal(_confLibuser); err != nil {
			return err
		}
		if debug {
			fmt.Printf("\n* %s\n", fileLibuser)
			internal.PrintStruct(_confLibuser)
		}

		if _confLibuser.login_defs != fileLogin {
			_confLogin.ENCRYPT_METHOD = _confLibuser.crypt_style
			_confLogin.SHA_CRYPT_MIN_ROUNDS = _confLibuser.hash_rounds_min
			_confLogin.SHA_CRYPT_MAX_ROUNDS = _confLibuser.hash_rounds_max
		}
	} else if err != nil {
		return err

	} /*else if found, err = exist(filePasswd); found {
		cfg, err := shconf.ParseFile(filePasswd)
		if err != nil {
			return err
		}
		_confPasswd := &confPasswd{}
		if err = cfg.Unmarshal(_confPasswd); err != nil {
			return err
		}
		if debug {
			fmt.Printf("\n* %s\n", filePasswd)
			internal.PrintStruct(_confPasswd)
		}

		if _confPasswd.CRYPT != "" {
			_confLogin.ENCRYPT_METHOD = _confPasswd.CRYPT
		}
	} else if err != nil {
		return err
	}*/

	switch strings.ToUpper(_confLogin.ENCRYPT_METHOD) {
	case "MD5":
		c.crypter = crypt.New(crypt.MD5)
	case "SHA256":
		c.crypter = crypt.New(crypt.SHA256)
	case "SHA512":
		c.crypter = crypt.New(crypt.SHA512)
	case "":
		if c.crypter, err = lookupCrypter(); err != nil {
			return err
		}
	default:
		return fmt.Errorf("user: requested cryp function is unavailable: %s",
			c.login.ENCRYPT_METHOD)
	}

	if _confLogin.SYS_UID_MIN == 0 || _confLogin.SYS_UID_MAX == 0 ||
		_confLogin.SYS_GID_MIN == 0 || _confLogin.SYS_GID_MAX == 0 ||
		_confLogin.UID_MIN == 0 || _confLogin.UID_MAX == 0 ||
		_confLogin.GID_MIN == 0 || _confLogin.GID_MAX == 0 {

		_confLogin.SYS_UID_MIN = 100
		_confLogin.SYS_UID_MAX = 999
		_confLogin.SYS_GID_MIN = 100
		_confLogin.SYS_GID_MAX = 999

		_confLogin.UID_MIN = 1000
		_confLogin.UID_MAX = 29999
		_confLogin.GID_MIN = 1000
		_confLogin.GID_MAX = 29999
	}

	config.login = *_confLogin
	return nil
}

// loadConfig loads user configuration.
// It has to be loaded before of edit some file.
func loadConfig() {
	config.Do(func() {
		//checkRoot()
		if err := config.init(); err != nil {
			panic(err)
		}
	})
}