设为首页 收藏本站
查看: 3790|回复: 0

[经验分享] itop 导入AD用户

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2017-1-19 08:50:17 | 显示全部楼层 |阅读模式
iTop 支持以ldap的方式验证,而微软的Active Directory 本质上也是一个ldap服务器,所以可以通过ldap协议导入AD用户。本文以iTop 2.3.3为例,亲测可用。

新建文件itop/webservices/AD_import_accounts.php,内容见后文,

在命令行运行一下命令(密码根据需要修改)
1
php AD_import_accounts.php --auth_user=admin --auth_pwd=123456




或者以admin身份登录itop,访问http://ip/itop/webservices/AD_import_accounts.php 也可。


进入itop数据库,运行以下sql语句可以验证用户是否已经添加;
1
select * from priv_user ;




附: AD_import_accounts.php,具体内容如下,注意修改31-35行

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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
<?php
// Copyright (C) 2011 Combodo SARL
//
//   This program is free software; you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation; version 3 of the License.
//
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program; if not, write to the Free Software
//   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

/**
* Sample script to import / synchronize users from an Active Directory server
*
* @author      Erwan Taloc <erwan.taloc@combodo.com>
* @author      Romain Quetiez <romain.quetiez@combodo.com>
* @author      Denis Flaven <denis.flaven@combodo.com>
* @license     http://www.opensource.org/licenses/gpl-3.0.html LGPL
*/

////////////////////////////////////////////////////////////////////////////////
// Configuration parameters: adjust them to connect to your AD server
// And configure the mapping between AD groups and iTop profiles
$aConfig = array(
                    // Configuration of the Active Directory connection
                    'host'     => '10.0.0.1', // IP or FQDN of your domain controller
                    'port'     => '389', // LDAP port, 398=LDAP, 636= LDAPS
                    'dn'       => 'DC=abc,DC=com', // Domain DN
                    'username'    => 'admin', // username with read access
                    'password'    => 'LDAP@admin', // password for above

                     // Query to retrieve and filter the users from AD
                     // Example: retrieve all users from the AD Group "iTop Users"
                         //'ldap_query' => '(&(objectCategory=user)(memberOf=CN=iTop Users,CN=Users,DC=combodo,DC=net))',
                         // Example 2: retrieves ALL the users from AD
                     'ldap_query' => '(&(objectCategory=user))', // Retrieve all users

                     // Which field to use as the iTop login samaccountname or userprincipalname ?
                     'login' => 'samaccountname',
                     //'login' => 'userprincipalname',

                     // Mapping between the AD groups and the iTop profiles
                    'profiles_mapping' => array(
                        //AD Group Name => iTop Profile Name
                                                'ITSM_Administrator' => 'Administrator',
                                                'ITSM_ChangeApprover' => 'Change Approver',
                                                'ITSM_ChangeImplementor' => 'Change Implementor',
                                                'ITSM_ChangeSupervisor' => 'Change Supervisor',
                                                'ITSM_ConfigurationManager' => 'Configuration Manager',
                                                'ITSM_DocumentAuthor' => 'Document author',
                                                'ITSM_PortalPowerUser' => 'Portal power user',
                                                'ITSM_PortalUser' => 'Portal user',
                                                'ITSM_ProblemManager' => 'Problem Manager',
                                                'ITSM_ServiceDeskAgent' => 'Service Desk Agent',
                                                'ITSM_ServiceManager' => 'Service Manager',
                                                'ITSM_SupportAgent' => 'Support Agent',
                            ),

                    // Since each iTop user must have at least one profile, assign the profile
                    // Below to users for which there was no match in the above mapping
                    'default_profile' => 'Portal user',
                    'default_language' => 'ZH CN', // Default language for creating new users

                    'default_organization' => 2, // ID of the default organization for creating new contacts
                );
// End of configuration
////////////////////////////////////////////////////////////////////////////////

if (file_exists('../approot.inc.php'))
{
    // iTop 1.0.2
    include('../approot.inc.php');
}
else // iTop 1.0 & 1.0.1
{
    define('APPROOT', '../');
}
require_once(APPROOT.'application/application.inc.php');
require_once(APPROOT.'application/webpage.class.inc.php');
require_once(APPROOT.'application/csvpage.class.inc.php');
require_once(APPROOT.'application/clipage.class.inc.php');
require_once(APPROOT.'application/startup.inc.php');
  
// List of attributes to retrieve
$aAttribs = array(
    'samaccountname',
    'sn',
    'givenname',
    'userprincipalname',
    'cn',
    'memberof',
    'mail',
);
  
$g_aUsersCache = null;       // Cache of all the iTop users to speed up searches
$g_aProfilesCache = null;    // Cache of all iTop profiles
  
/**
* Helper function to read attributes from LDAP data
* @param hash The LDAP data for one item as returned by ldap_search
* @param string The name of the attribute to retrieve
* @return mixed null if no such attribute, a scalar or a array depending on the
*                    number of values for the attribute.
*/   
function ReadLdapValue($aEntry, $sValueName)
{
    if (array_key_exists($sValueName, $aEntry))
    {
        $iCount = $aEntry[$sValueName]['count'];
        switch($iCount)
        {
            case 0:
            // No value, return null
            return null;
              
            case 1:
            // Just one value, return it
            return $aEntry[$sValueName][0];
              
            default:
            // Many values, return all of them as an array
            // except the 'count' entry
            $aValues = $aEntry[$sValueName];
            unset($aValues['count']);
            return $aValues;
        }
    }
    return null;
}
/**
* Helper function that processes 1 user at a time
* @param $aData hash The input data from Active Directory
* @param $index integer The index of the current user in the AD query (for reporting)
* @param $aConfig hash The configuration parameter
* @param $oChange CMDBChange Change to record all the changes or null if simulation mode
* @return string The action undertaken 'created', 'synchronized', 'error'
*/
function ProcessUser($aData, $index, $aConfig, $oChange = null)
{
    $sAction = 'error';
      
    $sUserLogin = $aData['samaccountname'];
    if (!is_array($aData['memberof']))
    {
        $aADGroups = array($aData['memberof']);
    }
    else
    {
        $aADGroups = $aData['memberof'];
    }
    $aITopProfiles = array();
    foreach($aADGroups as $sGroupString)
    {
        $aMatches = array();
        $sShortGroupString = '';
        if (preg_match('/^CN=([^,]+)/', $sGroupString, $aMatches))
        {
            $sShortGroupString = $aMatches[1];
        }
        //echo "<p>GroupString: $sGroupString => $sShortGroupString</p>";
        if (isset($aConfig['profiles_mapping'][$sShortGroupString]))
        {
            $aITopProfiles[] = $aConfig['profiles_mapping'][$sShortGroupString];
        }
    }
    if (count($aITopProfiles) == 0)
    {
        // Each user must have at least one profile
        // Assign the 'default_profile' to this user
        $aITopProfiles[] = $aConfig['default_profile'];
    }
    echo "<h2>User#{$index}: {$aData['cn']}</h2>\n";
    echo "<table>";
    foreach($aData as $sAttrib => $value)
    {
        echo "<tr><td style=\"vertical-align:top;background-color:eee;\">$sAttrib</td>";
        echo "<td style=\"vertical-align:top;background-color:eee;\">";
        if (is_array($value))
        {
            echo implode('<br/>', $value);
        }
        else
        {
            echo htmlentities($value);
        }
        echo "</td></tr>\n";
    }
    echo "<tr><td style=\"vertical-align:top;background-color:eee;\">iTop Profiles</td>";
    echo "<td style=\"vertical-align:top;background-color:eee;\">";
    echo implode('<br/>', $aITopProfiles);
    echo "</td></tr>\n";
    echo "</table>";
    $sLogin = $aData[$aConfig['login']];
    $oITopUser = GetUserByLogin($sLogin);
    if ($oITopUser == null)
    {
        // Check if a contact needs to be created or not
        $oPerson = GetPersonByEmail($aData['mail']);
        if (is_object($oPerson))
        {
            echo "<p>A person with the email='{$aData['mail']}' was found ".$oPerson->GetHyperlink().". This person will be used when creating the account.</p>";
        }
        else if ($oPerson == null)
        {
            echo "<p>A new person will be created.</p>";
            $oPerson = new Person();
            $oPerson->Set('name', $aData['sn']);
            $oPerson->Set('first_name', $aData['givenname']);
            $oPerson->Set('email', $aData['mail']);
            $oPerson->Set('org_id', $aConfig['default_organization']);
            if ($oChange != null)
            {
                $oPerson->DBInsertTracked($oChange);
            }
        }
        else
        {
            // Error ! Several matches found ??
            throw new Exception($oPerson);
        }
        $sAction = 'created';
        echo "<h2>User $sLogin will be <em>created</em> in iTop</h2>";
        $oITopUser = new UserLDAP;
        $oITopUser->Set('login', $sLogin);
        $oITopUser->Set('contactid', $oPerson->GetKey());
        $oITopUser->Set('language', $aConfig['default_language']);
        // Update the profiles
        $oLinkSet = DBObjectSet::FromScratch('URP_UserProfile');
        foreach($aITopProfiles as $sProfile)
        {
            $oLink = new URP_UserProfile;
            $iProfileId = GetProfileByName($sProfile);
            if ($iProfileId != null)
            {
                $oLink->Set('profileid', $iProfileId);
                $oLinkSet->AddObject($oLink);
            }
            else
            {
                echo "<p><b>Error: the profile '$sProfile' does not exist in iTop, verify the profiles_mapping configuration!</b></p>";
            }
        }
        $oITopUser->Set('profile_list', $oLinkSet);
        if ($oChange != null)
        {
            $oITopUser->DBInsertTracked($oChange);
        }
    }
    else if(is_object($oITopUser))
    {
        $sAction = 'synchronized';
        echo "<h2>User $sLogin (UserLDAP::".$oITopUser->GetKey().") will be <em>synchronized</em> in iTop</h2>";
        // Update the profiles
        $oLinkSet = DBObjectSet::FromScratch('URP_UserProfile');
        $oITopUser->Set('login', $sLogin);
        foreach($aITopProfiles as $sProfile)
        {
            $oLink = new URP_UserProfile;
            $iProfileId = GetProfileByName($sProfile);
            if ($iProfileId != null)
            {
                $oLink->Set('profileid', $iProfileId);
                $oLinkSet->AddObject($oLink);
            }
            else
            {
                echo "<p><b>Error: the profile '$sProfile' does not exist in iTop, verify the profiles_mapping configuration!</b></p>";
            }
        }
        $oITopUser->Set('profile_list', $oLinkSet);
        if ($oChange != null)
        {
            $oITopUser->DBUpdateTracked($oChange);
        }
    }
    else
    {
        // Error, another kind of user already exists with the same login ?
        echo "<h2 style=\"color:#C00\">Error: $oITopUser</h2>";
    }
    return $sAction;
}
  
/**
* Search the given user (identified by its login) in the iTop database
* @param $sLogin string The login of the user
* @return mixed null => nothing found, object => the user to synchronize, string => error message
*/
function GetUserByLogin($sLogin)
{
    global $g_aUsersCache;
    $result = null;
    if ($g_aUsersCache == null) InitUsersCache();
    if (isset($g_aUsersCache[$sLogin]))
    {
        $oUser = $g_aUsersCache[$sLogin];
        if (get_class($oUser) != 'UserLDAP')
        {
            $result = "A user with the same login ($sLogin), but not managed by LDAP already exists in iTop, the AD record will be ignored.";
        }
        else
        {
            $result = $oUser;
        }
    }
    return $result;
}
  
/**
* Initializes the cache for quickly searching iTop users
* @param none
* @return integer Number of users fetched from iTop  
*/
function InitUsersCache()
{
    global $g_aUsersCache;
    $sOQL = "SELECT User";
    $oSearch = DBObjectSearch::FromOQL($sOQL);
    $oSet = new CMDBObjectSet($oSearch);
    $iRet = $oSet->Count();
    while($oUser = $oSet->Fetch())
    {
        $g_aUsersCache[$oUser->Get('login')] = $oUser;
    }
    return $iRet;
}
  
/**
* Retrieves the ID of a profile (in iTop) base on its name
* @param $sProfile string Name of the profile
* @return integer ID of the profile, or null is not found
*/
function GetProfileByName($sProfileName)
{
    global $g_aProfilesCache;
    $iRet = null;
    if ($g_aProfilesCache == null) InitProfilesCache();
      
    if (isset($g_aProfilesCache[$sProfileName]))
    {
        $iRet = $g_aProfilesCache[$sProfileName];
    }
    return $iRet;
}
  
/**
* Initializes the cache of the iTop profiles
* @param none
* @return void
*/
function InitProfilesCache()
{
    global $g_aProfilesCache;
    $sOQL = "SELECT URP_Profiles";
    $oSearch = DBObjectSearch::FromOQL($sOQL);
    $oSet = new CMDBObjectSet($oSearch);
    while($oProfile = $oSet->Fetch())
    {
        $g_aProfilesCache[$oProfile->GetName()] = $oProfile->GetKey();
    }
}
  
/**
* Search for a Person by email address
* @param $sEmail string
* @return mixed Person (if found) or null (not found) or string (error)
*/
function GetPersonByEmail($sEmail)
{
    static $oSearch = null; // OQL Query cache
    $person = null;
    if ($oSearch == null)
    {
        $sOQL = 'SELECT Person WHERE email = :email';
        $oSearch = DBObjectSearch::FromOQL($sOQL);
    }
    $oSet = new CMDBObjectSet($oSearch, array(), array('email' => $sEmail));
    switch($oSet->Count())
    {
        case 0:
        $person = null;
        break;
         
        case 1:
        $person = $oSet->Fetch();
        break;
         
        default:
        $person = ' several matches found: '.$oSet->Count()." persons have the email address '$sEmail'";
    }
    return $person;   
}            
/******************************************************************************
*
* Main program
*  
******************************************************************************/
if (utils::IsModeCLI())
{
    $sAuthUser = utils::ReadParam('auth_user', 'admin_user', true);
    $sAuthPwd = utils::ReadParam('auth_pwd', 'admin_user_password', true);
    if (UserRights::CheckCredentials($sAuthUser, $sAuthPwd))
    {
        UserRights::Login($sAuthUser); // Login & set the user's language
    }
    else
    {
        echo "Access restricted or wrong credentials ('$sAuthUser')";
        exit;
    }
}
else
{
    $_SESSION['login_mode'] = 'basic';
    require_once('../application/loginwebpage.class.inc.php');
    LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  
    $sCSVData = utils::ReadPostedParam('csvdata');
}
if (!UserRights::IsAdministrator())
{
    echo '<p>Access is restricted to administrators</p>';
    exit;
}
// By default, run in simulation mode (i.e do nothing)
$bSimulationMode = utils::ReadParam('simulation', 0, true);
$oMyChange = null;
if (!$bSimulationMode)
{
    $oMyChange = MetaModel::NewObject("CMDBChange");
    $oMyChange->Set("date", time());
    if (UserRights::IsImpersonated())
    {
        $sUserString = Dict::Format('UI:Archive_User_OnBehalfOf_User', UserRights::GetRealUser(), UserRights::GetUser());
    }
    else
    {
        $sUserString = UserRights::GetUser();
    }
    $oMyChange->Set("userinfo", $sUserString);
    $oMyChange->DBInsert();
}
else
{
    echo "<h1 style=\"color:#900\">Simulation mode -- no action will be performed</h1>";
    echo "<p>Set the parameter simulation=0 to trigger the actual execution.</p>";
}
$ad = ldap_connect($aConfig['host'], $aConfig['port']) or die( "Could not connect to {$aConfig['host']} on port {$aConfig['port']}!" );
echo "<p>Connected to {$aConfig['host']} on port {$aConfig['port']}</p>\n";
// Set version number
ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3) or die ("Could not set ldap protocol");
ldap_set_option($ad, LDAP_OPT_REFERRALS,0) or die ("could no se the ldap referrals");
  
// Binding to ldap server
$bd = ldap_bind($ad, $aConfig['username'], $aConfig['password']) or die ("Could not bind");
echo "<p>Identified as {$aConfig['username']}</p>\n";
  
$sLdapSearch = $aConfig['ldap_query'];
  
echo "<p>LDAP Query: '$sLdapSearch'</p>";
$search = ldap_search($ad, $aConfig['dn'], $sLdapSearch /*, $aAttribs*/) or die ("ldap search failed");
  
$entries = ldap_get_entries($ad, $search);
$index = 1;
$aStatistics = array(
    'created' => 0,
    'synchronized' => 0,
    'error' => 0,
);
$iCreated = 0;
$iSynchronized = 0;
$iErrors = 0;   
if ($entries["count"] > 0)
{
    $iITopUsers = InitUsersCache();
    echo "<h1>{$entries["count"]} user(s) found in Active Directory, $iITopUsers (including non-LDAP users) found in iTop.</h1>\n";
    foreach($entries as $key => $aEntry)
    {
        //echo "<pre>$key\n";
        //print_r($aEntry);
        //echo "</pre>\n";
        if (strcmp($key,'count') != 0)
        {
            $aData = array();
            foreach($aAttribs as $sName)
            {
                $aData[$sName] = ReadLdapValue($aEntry, $sName);
            }
            if (empty($aData['mail']))
            {
                $aData['mail'] = $aData['userprincipalname'];
            }
            try
            {
                $sAction = ProcessUser($aData, $index, $aConfig, $oMyChange);
            }
            catch(Exception $e)
            {
                echo "<p><b>An error occured while processing $index: ".$e->getMessage()."</b></p>";
                $sAction = 'error';
            }
            echo "<hr/>\n";
            $aStatistics[$sAction]++;
            $index++;
        }
    }
}
else
{
    echo "<p>Nothing found !</p>\n";
    echo "<p>LDAP query was: $sLdapSearch</p>\n";
}
ldap_unbind($ad);
if ($bSimulationMode)
{
    echo "<h1 style=\"color:#900\">Simulation mode -- no action was performed</h1>";
}
echo "<h1>Statistics:</h1>";
echo "<table>";
foreach($aStatistics as $sKey => $iValue)
{
    echo "<tr><td style=\"vertical-align:top;background-color:eee;\">$sKey</td>\n";
    echo "<td style=\"vertical-align:top;background-color:eee;\">$iValue</td></tr>\n";
}
echo "</table>";
?>



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-330476-1-1.html 上篇帖子: 关于使用RedHat编译,make menuconfig命令报错需要下载相关文... 下篇帖子: NFS错误Starting NFS quotas: Cannot register service: RPC: 用户
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表