IP Reg is a IPAM tool to keep track of assets, nodes (IP addresses, MAC addresses, DNS aliases) within different subnets, over different locations or even VLAN's. Written in PHP, used with a MySQL-database to have a unique insight in your local network.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
ipreg/login.php

118 lines
4.2 KiB

<?php
/*****************************************************************************
IP Reg, a PHP/MySQL IPAM tool
Copyright (C) 2007-2009 Wietse Warendorff (up to v0.5)
Copyright (C) 2011-2023 Thomas Hooge
SPDX-License-Identifier: GPL-3.0-or-later
*****************************************************************************/
session_name('ipreg');
session_start();
include("config.php");
// connect to database
$dbh = new PDO("mysql:host=$config_mysql_host;dbname=$config_mysql_dbname;charset=utf8", $config_mysql_username, $config_mysql_password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
include("lib.php"); // only for get_language from browser. TODO: simplify
function user_login ($user_name, $user_pass) {
global $dbh;
if (strlen($user_name) < 1) {
return FALSE;
}
if (strlen($user_pass) < 1) {
return FALSE;
}
$sql = "SELECT user_id, user_pass, user_displayname, user_language,
user_imagesize, user_imagecount, user_mac, user_dateformat,
user_dns1suffix, user_dns2suffix, user_tooltips,
user_menu, user_role, user_flags
FROM user
WHERE user_name=?";
$sth = $dbh->prepare($sql);
$sth->execute([$user_name]);
if (!$user = $sth->fetch(PDO::FETCH_OBJ)) {
// no user record found
return FALSE;
}
if (strcmp(md5($user_pass), rtrim($user->user_pass)) != 0) {
// password does not match with md5, check if new hash matches
// For future expansion: $pwd_peppered = hash_hmac('sha256', $user_pass, $config_pepper);
if (! password_verify($user_pass, $user->user_pass)) {
return FALSE;
}
} else {
// md5 match but outdated. rewrite with new algo
$sth = $dbh->prepare("UPDATE user SET user_pass=? WHERE user_id=?");
$newhash = password_hash($user_pass, PASSWORD_BCRYPT);
$sth->execute([$newhash, $user->user_id]);
}
// all ok: user is logged in, register session data
$_SESSION['suser_id'] = $user->user_id;
$_SESSION['suser_displayname'] = $user->user_displayname;
$_SESSION['suser_language'] = $user->user_language;
$_SESSION['suser_imagesize'] = $user->user_imagesize;
$_SESSION['suser_imagecount'] = $user->user_imagecount;
$_SESSION['suser_mac'] = $user->user_mac;
$_SESSION['suser_dateformat'] = $user->user_dateformat;
$_SESSION['suser_dns1suffix'] = $user->user_dns1suffix;
$_SESSION['suser_dns2suffix'] = $user->user_dns2suffix;
$_SESSION['suser_tooltips'] = $user->user_tooltips;
$roles = explode(',', $user->user_role);
$_SESSION['suser_role_add'] = in_array('add', $roles);
$_SESSION['suser_role_edit'] = in_array('edit', $roles);
$_SESSION['suser_role_delete'] = in_array('delete', $roles);
$_SESSION['suser_role_manage'] = in_array('manage', $roles);
$_SESSION['suser_role_admin'] = in_array('admin', $roles);
$menu = explode(',', $user->user_menu);
$_SESSION['suser_menu_assets'] = in_array('asset', $menu);
$_SESSION['suser_menu_assetclasses'] = in_array('class', $menu);
$_SESSION['suser_menu_assetclassgroups'] = in_array('group', $menu);
$_SESSION['suser_menu_cables'] = in_array('cable', $menu);
$_SESSION['suser_menu_locations'] = in_array('location', $menu);
$_SESSION['suser_menu_nodes'] = in_array('node', $menu);
$_SESSION['suser_menu_subnets'] = in_array('subnet', $menu);
$_SESSION['suser_menu_vlans'] = in_array('vlan', $menu);
$_SESSION['suser_menu_zones'] = in_array('zone', $menu);
return TRUE;
}
// No header included, this page has no menu
$language = lang_getfrombrowser($config_lang, $config_lang_default, null, false);
include('lang/' . $language . '.php');
if ($_SERVER['REQUEST_METHOD'] == "POST" ) {
$user_name = sanitize($_POST['user_name']);
$user_pass = sanitize($_POST['user_pass']);
if (user_login($user_name, $user_pass) == TRUE) {
header_location($_SESSION['prelogin'] ?? 'index.php');
} else {
$_SESSION = array();
session_destroy();
}
}
$smarty->assign("config_version", $config_version);
$smarty->assign($lang);
$smarty->display("login.tpl");
include("footer.php");
?>