Menu

[r1244]: / trunk / etc / inc / system.inc  Maximize  Restore  History

Download this file

310 lines (255 with data), 7.9 kB

  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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
/*
system.inc
part of FreeNAS (http://www.freenas.org)
Copyright (C) 2005-2007 Olivier Cochard-Labbe <olivier@freenas.org>.
All rights reserved.
Based on m0n0wall (http://m0n0.ch/wall)
Copyright (C) 2003-2006 Manuel Kasper <mk@neon1.net>.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
require_once("functions.inc");
require_once("util.inc");
require_once("rc.inc");
function system_stop_common() {
/* Umount all disk s*/
disks_umount_all();
/* Detach all encrypted disks */
disks_geli_detach_all();
/* Stop all raid disks */
disks_raid_stop();
}
function system_reboot() {
// Initiate halt. Everything will be done automatically
// in /etc/rc.dhutdown by executing rc-init scripts in
// reverse order (the 'KEYWORD: shutdown' must be defined).
mwexec("/sbin/shutdown -r now");
}
function system_halt() {
// Initiate halt. Everything will be done automatically
// in /etc/rc.dhutdown by executing rc-init scripts in
// reverse order (the 'KEYWORD: shutdown' must be defined).
mwexec("/sbin/shutdown -p now");
}
function system_do_shell_commands($early = 0) {
global $config;
if ($early)
$cmdn = "earlyshellcmd";
else
$cmdn = "shellcmd";
if (is_array($config['system'][$cmdn])) {
foreach ($config['system'][$cmdn] as $cmd) {
system($cmd);
}
}
}
function system_do_extensions($early = false) {
global $config, $g;
if (!is_dir("{$g['etc_path']}/inc/ext"))
return;
$dh = @opendir("{$g['etc_path']}/inc/ext");
if ($dh) {
while (($extd = readdir($dh)) !== false) {
if (($extd === ".") || ($extd === ".."))
continue;
$rcfile = "{$g['etc_path']}/inc/ext/" . $extd . "/" . ($early ? "rc.early" : "rc");
if (file_exists($rcfile))
passthru($rcfile);
}
closedir($dh);
}
}
// Create the following files:
// - /var/etc/master.passwd
// - /var/etc/group
// - /var/etc/passwd, pwd.db and spwd.db
// - /var/run/.htpasswd
// Return 0 if successful, otherwise 1
function system_create_usermanagement()
{
global $g;
$result = 0;
if (is_booting())
write_console("Generating user/password database(s)... ");
$result |= system_create_masterpasswd();
$result |= system_create_group();
$result |= system_create_pwdmkdb();
$result |= system_create_htpasswd();
if (0 == $result) {
if(is_booting())
write_console("done\n");
write_log("User/password database(s) created");
} else {
if(is_booting())
write_console("failed\n");
write_log("Error: Failed to create user/password database(s)");
}
return $result;
}
// Create the /var/etc/master.passwd file.
// Return 0 if successful, otherwise 1
function system_create_masterpasswd()
{
global $config, $g;
$masterpasswd = <<<EOD
root:{$config['system']['password']}:0:0::0:0:Charlie &:/root:/bin/tcsh
toor:*:0:0::0:0:Bourne-again Superuser:/root:
daemon:*:1:1::0:0:Owner of many system processes:/root:/usr/sbin/nologin
operator:*:2:5::0:0:System &:/:/usr/sbin/nologin
bin:*:3:7::0:0:Binaries Commands and Source:/:/usr/sbin/nologin
tty:*:4:65533::0:0:Tty Sandbox:/:/usr/sbin/nologin
kmem:*:5:65533::0:0:KMem Sandbox:/:/usr/sbin/nologin
www:*:80:80::0:0:World Wide Web Owner:/nonexistent:/usr/sbin/nologin
nobody:*:65534:65534::0:0:Unprivileged user:/nonexistent:/usr/sbin/nologin
ftp:*:21:50::0:0:FTP user:/mnt:/sbin/nologin
man:*:9:9::0:0:Mister Man Pages:/usr/share/man:/usr/sbin/nologin
sshd:*:22:22::0:0:Secure Shell Daemon:/var/empty:/usr/sbin/nologin
_dhcp:*:65:65::0:0:dhcp programs:/var/empty:/usr/sbin/nologin
EOD;
if (is_array($config['access']['user']))
{
foreach ($config['access']['user'] as $user)
{
$password=crypt($user['password']);
if (isset($user['fullshell']))
{
$masterpasswd .= <<<EOD
{$user['login']}:{$password}:{$user['id']}:{$user['usergroupid']}::0:0:{$user['fullname']}:/mnt:/bin/tcsh
EOD;
}
else
{
$masterpasswd .= <<<EOD
{$user['login']}:{$password}:{$user['id']}:{$user['usergroupid']}::0:0:{$user['fullname']}:/mnt:/usr/local/bin/scponly
EOD;
}
}
}
$fd = fopen("{$g['varetc_path']}/master.passwd", "w");
if (!$fd) {
$message = "Error: Can't open master.passwd in system_create_masterpasswd()";
write_console($message . ".\n");
write_log($message);
return 1;
}
fwrite($fd, $masterpasswd);
fclose($fd);
return 0;
}
// Generate the /var/etc/passwd, pwd.db and spwd.db files.
// Return 0 if successful, otherwise 1
function system_create_pwdmkdb()
{
global $g;
return (mwexec("/usr/sbin/pwd_mkdb -p -d {$g['varetc_path']} {$g['varetc_path']}/master.passwd"));
}
// Create the /var/etc/group file.
// Return 0 if successful, otherwise 1
function system_create_group()
{
global $config, $g;
$groupfile = <<<EOD
wheel:*:0:root
EOD;
/* If user exist with admin right, put them on the wheel group */
if (is_array($config['access']['user'])) {
foreach ($config['access']['user'] as $user) {
if (isset($user['admin'])) {
$groupfile .= <<<EOD
,{$user['login']}
EOD;
}
}
}
$groupfile .= <<<EOD
daemon:*:1:
kmem:*:2:
sys:*:3:
tty:*:4:
operator:*:5:root
bin:*:7:
staff:*:20:
man:*:9:
sshd:*:22:
guest:*:31:
ftp:*:50:
_pflogd:*:64:
_dhcp:*:65:
network:*:69:
www:*:80:
nogroup:*:65533:
nobody:*:65534:
admin:*:1000:
EOD;
if (is_array($config['access']['group'])) {
foreach ($config['access']['group'] as $group) {
$groupfile .= <<<EOD
{$group['name']}:*:{$group['id']}:
EOD;
}
}
$fd = fopen("{$g['varetc_path']}/group", "w");
if (!$fd) {
$message = "Error: Can't open group in system_create_group()";
write_console($message . ".\n");
write_log($message);
return 1;
}
fwrite($fd, $groupfile);
fclose($fd);
return 0;
}
// Generate the /var/run/.htpasswd file used by lighttpd.
// Return 0 if successful, otherwise 1
function system_create_htpasswd() {
global $config, $g;
$result = 0 ;
$fd = fopen("{$g['varrun_path']}/.htpasswd", "w");
if (!$fd) {
$message = "Error: Can't create .htpasswd in system_create_htpasswd()";
write_console($message . ".\n");
write_log($message);
return 1;
}
if ($config['system']['username'])
$username = $config['system']['username'];
else
$username = "admin";
fwrite($fd, $username . ":" . $config['system']['password'] . "\n");
fclose($fd);
$result = mwexec("/bin/chmod 0600 {$g['varrun_path']}/.htpasswd");
return $result;
}
/* Init language environment */
function system_language_load()
{
global $config, $g_languages;
/* Get the language configured*/
$language = $config['system']['language'];
$locale = $g_languages[$language];
$domain = strtolower( get_product_name());
$codeset = ltrim(strstr($locale,"."),".");
putenv( "LANG=$locale");
setlocale( LC_MESSAGES, $locale);
bindtextdomain( $domain, "/usr/local/share/locale");
bind_textdomain_codeset( $domain, $codeset);
textdomain( $domain);
}
?>
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.