Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
pfSense 2.0 captive portal self registration
<?php
require_once("functions.inc");
require_once("config.lib.inc");
require_once("auth.inc");
if ($_POST) {
$a_user = &$config['system']['user'];
unset($input_errors);
$pconfig = $_POST;
/* input validation */
if ($_POST['username'] == "") {
$input_errors[] = "The username is required.";
}
if ($_POST['password'] == "") {
$input_errors[] = "The password is required.";
}
if (preg_match("/[^a-zA-Z0-9\.\-_]/", $_POST['username'])) {
$input_errors[] = "The username contains invalid characters.";
}
if (strlen($_POST['username']) > 16) {
$input_errors[] = "The username is longer than 16 characters.";
}
if (($_POST['password']) && ($_POST['password'] != $_POST['password2'])) {
$input_errors[] = "The passwords do not match.";
}
/* check the username is unique */
if (!$input_errors) {
foreach ($a_user as $userent) {
if ($userent['name'] == $_POST['username']) {
$input_errors[] = "Another entry with the same username already exists.";
break;
}
}
}
/* ... and that it's not reserved */
if (!$input_errors) {
$system_users = explode("\n", file_get_contents("/etc/passwd"));
foreach ($system_users as $s_user) {
$ent = explode(":", $s_user);
if ($ent[0] == $_POST['username']) {
$input_errors[] = "That username is reserved by the system.";
break;
}
}
}
/* save it */
if (!$input_errors) {
$userent = array();
if ($_POST['password']) {
local_user_set_password($userent, $_POST['password']);
}
$userent['uid'] = $config['system']['nextuid']++;
$userent['name'] = $_POST['username'];
$userent['descr'] = $_POST['fullname'];
$userent['expires'] = "";
conf_mount_rw();
/* add the user to "All Users" group */
foreach ($config['system']['group'] as $gidx => $group) {
if ($group['name'] == "all") {
if (!is_array($config['system']['group'][$gidx]['member']))
$config['system']['group'][$gidx]['member'] = array();
$config['system']['group'][$gidx]['member'][] = $userent['uid'];
break;
}
}
$a_user[] = $userent;
local_user_set_groups($userent, array("captiveportal")); // <- Remove this line if you don't want / have a "captiveportal" group
local_user_set($userent);
write_config();
conf_mount_ro();
$done = true;
}
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>User Registration</title>
</head>
<body>
<?php if (isset($done)) { ?>
<br/>
Your registration is processed. You now can login <a href="/">here</a>.
<?php } else {
if ($input_errors) {
echo "Error <br/>";
foreach ($input_errors as $input_error) {
echo $input_error . "<br>";
}
}
?>
<form method="post">
<table width="450" border="1">
<tr>
<td colspan="2">Registration</td>
</tr>
<tr>
<td width="132">Username</td>
<td width="302"><input type="text" name="username" id="txt_username" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" id="txt_pwd1" /></td>
</tr>
<tr>
<td>Verify Password </td>
<td><input type="password" name="password2" id="txt_pwd2" /></td>
</tr>
<tr>
<td>Fullname</td>
<td><input type="text" name="fullname" id="txt_name" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><label>
<input type="submit" name="btn_submit" id="btn_submit" value="Register" />
</label></td>
</tr>
</table>
<?php } ?>
</form>
</body>
</html>
@prasadrajuv

This comment has been minimized.

Copy link

commented Dec 7, 2015

Hi, I used this code in pfSense 2.2.5, its working and able to register and showing users in pf portal. but after register it is redirect to captive portal login page and when type username and password and submit showing invalid credential. but able to login after restart pfsense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.