system_usermanager_import_ldap.php 5.77 KB
Newer Older
1
<?php
2

3
/*
4 5 6
    Copyright (C) 2014-2015 Deciso B.V.
    Copyright (C) 2007 Scott Ullrich <sullrich@gmail.com>
    All rights reserved.
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

    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("guiconfig.inc");
require_once("auth.inc");

33
function add_local_user($username, $userdn, $userfullname) {
34
  global $config;
35 36 37 38 39

  // generate new random user_password
  $bytes = openssl_random_pseudo_bytes(50);
  $user_password = pack('H*',bin2hex($bytes));

40 41 42 43
  foreach ($config['system']['user'] as &$user) {
      if ($user['name'] == $username && $user['name'] != 'root') {
        // link local user to remote server by updating user_dn
        $user['user_dn'] = $userdn;
44 45 46 47 48
        // trash user password when linking to ldap, avoid accidental login
        // using fall-back local password. User could still reset it's
        // local password, but only by choice.
        local_user_set_password($user, $user_password);
        local_user_set($user);
49 50 51 52 53 54 55 56
        return;
      }
  }
  // new user, add
  $new_user = array();
  $new_user['scope'] = 'user';
  $new_user['name'] = $username;
  $new_user['user_dn'] = $userdn;
57
  $new_user['descr'] = $userfullname;
58
  local_user_set_password($new_user, $user_password);
59 60
  $new_user['uid'] = $config['system']['nextuid']++;
  $config['system']['user'][] = $new_user;
61
  local_user_set($new_user);
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
}

global $config;
// attributes used in page
$ldap_users= array();
$ldap_is_connected = false;
$exit_form = false;

// find gui auth server
$authcfg = auth_get_authserver($config['system']['webgui']['authmode']);

if ($authcfg['type'] == 'ldap') {
  // setup peer ca
  ldap_setup_caenv($authcfg);
  // connect to ldap server
  $ldap_auth = new OPNsense\Auth\LDAP($authcfg['ldap_basedn'], $authcfg['ldap_protver']);
  $ldap_is_connected = $ldap_auth->connect($authcfg['ldap_full_url']
                      , $authcfg['ldap_binddn']
                      , $authcfg['ldap_bindpw']
                    );
  if ($ldap_is_connected) {
    // collect list of current ldap users from config
    $confDNs = array();
    foreach ($config['system']['user'] as $confUser) {
     if (!empty($confUser['user_dn'])) {
       $confDNs[] = trim($confUser['user_dn']);
     }
    }

    // search ldap
    $result = $ldap_auth->searchUsers("*"
              , $authcfg['ldap_attr_user']
              , $authcfg['ldap_extended_query']
            );

    // actual form action, either save new accounts or list missing
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
      // create selected accounts
      $exit_form = true;
      if (isset($_POST['user_dn'])) {
        $update_count = 0;
        foreach ($result as $ldap_user ) {
          foreach ($_POST['user_dn'] as $userDN) {
            if ($userDN == $ldap_user['dn'] && !in_array($ldap_user['dn'], $confDNs)) {
106
              add_local_user($ldap_user['name'] , $ldap_user['dn'], $ldap_user['fullname']);
107 108 109 110 111 112 113 114 115 116
              $update_count++;
            }
          }
          if ($update_count > 0){
            // write config when changed
            write_config();
          }
        }
      }
    } else {
117 118 119 120 121 122
      if (is_array($result)) {
        // list all missing accounts
        foreach ($result as $ldap_user ) {
          if (!in_array($ldap_user['dn'], $confDNs)) {
            $ldap_users[$ldap_user['name']] = $ldap_user['dn'];
          }
123
        }
124
        ksort($ldap_users);
125 126 127 128 129 130 131 132 133 134 135 136 137 138
      }
    }
  }
}

include('head.inc');
?>

 <body>
<?php if ($exit_form) :
?>
	<script type="text/javascript">
    // exit form and reload parent after save
    window.opener.location.href = window.opener.location.href;
139
	window.close();
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
  </script>
<?php elseif (!$ldap_is_connected) :
?>
	<p><?=gettext("Could not connect to the LDAP server. Please check your LDAP configuration.");?></p>
	<input type='button' class="btn btn-default" value='<?=gettext("Close"); ?>' onClick="window.close();">
<?php
else :
?>
<form method="post">
	<table class="table table-striped">
		<tbody>
			<tr>
			<th colspan="3">
				<?=gettext("Please select users to import:");?>
			</th>
			</tr>
			<?php foreach ($ldap_users as $username => $userDN) :
?>
        <tr><td><?=$username?></td><td><?=$userDN?></td><td> <input type='checkbox' value="<?=$userDN?>" id='user_dn' name='user_dn[]'>  </td></tr>
      <?php endforeach;
?>
			<tr>
				<td align="left" colspan="3">
					<input type='submit' class="btn btn-primary" value='<?=gettext("Save");?>'>
				</td>
			</tr>
		</tbody>
	</table>
  </form>
<?php
endif; ?>
<!-- bootstrap script -->
172
<script type="text/javascript" src="/ui/js/bootstrap.min.js"></script>
173
<!-- Fancy select with search options -->
174
<script type="text/javascript" src="/ui/js/bootstrap-select.min.js"></script>
175 176
 </body>
</html>