summaryrefslogtreecommitdiffstats
path: root/glustolibs-gluster/glustolibs/gluster/samba_libs.py
blob: c25eeb891dece203e09252591baea2264229ab6d (plain)
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
#  Copyright (C) 2017-2018 Red Hat, Inc. <http://www.redhat.com>
#
#  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; either version 2 of the License, or
#  any later version.
#
#  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.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

"""
    Description: Library for samba operations.
"""
import re
import time
from glusto.core import Glusto as g
from glustolibs.gluster.volume_libs import is_volume_exported
from glustolibs.gluster.mount_ops import GlusterMount


def start_smb_service(mnode):
    """Start smb service on the specified node.

    Args:
        mnode (str): Node on which smb service has to be started

    Returns:
        bool: True on successfully starting smb service. False otherwise.
    """
    g.log.info("Starting SMB Service on %s", mnode)

    # Enable Samba to start on boot
    ret, _, _ = g.run(mnode, "chkconfig smb on")
    if ret != 0:
        g.log.error("Unable to set chkconfig smb on")
        return False
    g.log.info("chkconfig smb on successful")

    # Start smb service
    ret, _, _ = g.run(mnode, "service smb start")
    if ret != 0:
        g.log.error("Unable to start the smb service")
        return False
    g.log.info("Successfully started smb service")
    return True


def smb_service_status(mnode):
    """Status of smb service on the specified node.

    Args:
        mnode (str): Node on which smb service has to be started

    Returns:
        tuple: Tuple containing three elements (ret, out, err).
            The first element 'ret' is of type 'int' and is the return value
            of command execution.

            The second element 'out' is of type 'str' and is the stdout value
            of the command execution.

            The third element 'err' is of type 'str' and is the stderr value
            of the command execution.
    """
    g.log.info("Getting SMB Service status on %s", mnode)
    return g.run(mnode, "service smb status")


def is_smb_service_running(mnode):
    """Check if smb service is running on node

    Args:
        mnode (str): Node on which smb service status has to be verified.

    Returns:
        bool: True if smb service is running. False otherwise.
    """
    g.log.info("Check if SMB service is running on %s", mnode)
    ret, out, _ = smb_service_status(mnode)
    if ret != 0:
        return False
    if "Active: active (running)" in out:
        return True
    else:
        return False


def stop_smb_service(mnode):
    """Stop smb service on the specified node.

    Args:
        mnode (str): Node on which smb service has to be stopped.

    Returns:
        bool: True on successfully stopping smb service. False otherwise.
    """
    g.log.info("Stopping SMB Service on %s", mnode)

    # Disable Samba to start on boot
    ret, _, _ = g.run(mnode, "chkconfig smb off")
    if ret != 0:
        g.log.error("Unable to set chkconfig smb off")
        return False
    g.log.info("chkconfig smb off successful")

    # Stop smb service
    ret, _, _ = g.run(mnode, "service smb stop")
    if ret != 0:
        g.log.error("Unable to stop the smb service")
        return False
    g.log.info("Successfully stopped smb service")
    return True


def list_smb_shares(mnode):
    """List all the gluster volumes that are exported as SMB Shares

    Args:
        mnode (str): Node on which commands has to be executed.

    Returns:
        list: List of all volume names that are exported as SMB Shares.
             Empty list if no volumes are exported as SMB Share.
    """
    g.log.info("List all SMB Shares")
    smb_shares_list = []
    cmd = "smbclient -L localhost"
    ret, out, _ = g.run(mnode, cmd)
    if ret != 0:
        g.log.error("Failed to find the SMB Shares")
        return smb_shares_list
    else:
        out = out.splitlines()
        for line in out:
            if 'gluster-' in line:
                smb_shares_list.append(line.split(" ")[0].strip())
    return smb_shares_list


def enable_mounting_volume_over_smb(mnode, volname, smb_users_info):
    """Enable mounting volume over SMB. Set ACL's for non-root users.

    Args:
        mnode (str): Node on which commands are executed.
        volname (str): Name of the volume on which acl's has to be set.
        smb_users_info (dict): Dict containing users info. Example:
            smb_users_info = {
                'root': {'password': 'foobar',
                         'acl': ''
                         },
                'user1': {'password': 'abc',
                          'acl': ''
                          },
                'user2': {'password': 'xyz',
                          'acl': ''
                          }
                }
    Returns:
        bool: True on successfully enabling to mount volume using SMB.
            False otherwise.
    """
    g.log.info("Enable mounting volume over SMB")
    # Create a temp mount to provide required permissions to the smb user
    mount = {
        'protocol': 'glusterfs',
        'server': mnode,
        'volname': volname,
        'client': {
            'host': mnode
            },
        'mountpoint': '/tmp/gluster_smb_set_user_permissions_%s' % volname,
        'options': 'acl'
        }
    mount_obj = GlusterMount(mount)
    ret = mount_obj.mount()
    if not ret:
        g.log.error("Unable to create temporary mount for providing "
                    "required permissions to the smb users")
        return False
    g.log.info("Successfully created temporary mount for providing "
               "required permissions to the smb users")

    # Provide required permissions to the smb user
    for smb_user in smb_users_info.keys():
        if smb_user != 'root':
            if 'acl' in smb_users_info[smb_user]:
                acl = smb_users_info[smb_user]['acl']
                if not acl:
                    acl = "rwx"
            else:
                acl = "rwx"

            cmd = ("setfacl -m user:%s:%s %s" % (smb_user, acl,
                                                 mount_obj.mountpoint))
            ret, _, _ = g.run(mnode, cmd)
            if ret != 0:
                g.log.error("Unable to provide required permissions to the "
                            "smb user %s ", smb_user)
                return False
            g.log.info("Successfully provided required permissions to the "
                       "smb user %s ", smb_user)

    # Verify SMB/CIFS share  can be accessed by the user

    # Unmount the temp mount created
    ret = mount_obj.unmount()
    if not ret:
        g.log.error("Unable to unmount the temp mount")
    g.log.info("Successfully unmounted the temp mount")

    return True


def share_volume_over_smb(mnode, volname, smb_users_info):
    """Sharing volumes over SMB

    Args:
        mnode (str): Node on which commands has to be executed.
        volname (str): Name of the volume to be shared.
        smb_users_info (dict): Dict containing users info. Example:
            smb_users_info = {
                'root': {'password': 'foobar',
                         'acl': ''
                         },
                'user1': {'password': 'abc',
                          'acl': ''
                          },
                'user2': {'password': 'xyz',
                          'acl': ''
                          }
                }

    Returns:
        bool : True on successfully sharing the volume over SMB.
            False otherwise
    """
    g.log.info("Start sharing the volume over SMB")

    # Set volume option 'user.cifs' to 'on'.
    cmd = "gluster volume set %s user.cifs on" % volname
    ret, _, _ = g.run(mnode, cmd)
    if ret != 0:
        g.log.error("Failed to set the volume option user.cifs on")
        return False
    g.log.info("Successfully set 'user.cifs' to 'on' on %s", volname)

    # Set volume option 'stat-prefetch' to 'on'.
    cmd = "gluster volume set %s stat-prefetch on" % volname
    ret, _, _ = g.run(mnode, cmd)
    if ret != 0:
        g.log.error("Failed to set the volume option stat-prefetch on")
        return False
    g.log.info("Successfully set 'stat-prefetch' to 'on' on %s", volname)

    # Set volume option 'server.allow-insecure' to 'on'.
    cmd = "gluster volume set %s server.allow-insecure on" % volname
    ret, _, _ = g.run(mnode, cmd)
    if ret != 0:
        g.log.error("Failed to set the volume option server-allow-insecure")
        return False
    g.log.info("Successfully set 'server-allow-insecure' to 'on' on %s",
               volname)

    # Set 'storage.batch-fsync-delay-usec' to 0.
    # This is to ensure ping_pong's lock and I/O coherency tests works on CIFS.
    cmd = ("gluster volume set %s storage.batch-fsync-delay-usec 0" % volname)
    ret, _, _ = g.run(mnode, cmd)
    if ret != 0:
        g.log.error("Failed to set the volume option "
                    "'storage.batch-fsync-delay-usec' to 0 on %s", volname)
        return False
    g.log.info("Successfully set 'storage.batch-fsync-delay-usec' to 0 on %s",
               volname)

    # Verify if the volume can be accessed from the SMB/CIFS share.
    cmd = ("smbclient -L localhost -U | grep -i -Fw gluster-%s " % volname)
    ret, _, _ = g.run(mnode, cmd)
    if ret != 0:
        g.log.error("volume '%s' not accessible via SMB/CIFS share", volname)
        return False
    g.log.info("volume '%s' can be accessed from SMB/CIFS share", volname)

    # To verify if the SMB/CIFS share can be accessed by the root/non-root user
    # TBD

    # Enable mounting volumes over SMB
    ret = enable_mounting_volume_over_smb(mnode, volname, smb_users_info)
    if not ret:
        g.log.error("Failed to enable mounting volumes using SMB")
        return False
    g.log.info("Successfully enabled mounting volumes using SMV for the "
               "smbusers: %s", str(smb_users_info.keys()))

    # Verify if volume is shared
    ret = is_volume_exported(mnode, volname, "smb")
    if not ret:
        g.log.info("Volume %s is not exported as 'cifs/smb' share", volname)
        return False
    g.log.info("Volume %s is exported as 'cifs/smb' share", volname)
    return True


def is_ctdb_service_running(mnode):
    """Check if ctdb services is running on node

    Args:
        mnode (str): Node on which ctdb service status has to be verified.

    Returns:
        bool: True if ctdb service is running. False otherwise.
    """
    g.log.info("Check if CTDB service is running on %s", mnode)
    ret, out, _ = g.run(mnode, "service ctdb status")
    if "Active: active (running)" in out:
        return True
    return False


def stop_ctdb_service(mnode):
    """Stop ctdb service on the specified node.

    Args:
        mnode (str): Node on which ctdb service has to be stopped.

    Returns:
        bool: True on successfully stopping ctdb service. False otherwise.
    """
    g.log.info("Stopping CTDB Service on %s", mnode)
    # Stop ctdb service
    ret, _, _ = g.run(mnode, "service ctdb stop")
    if ret != 0:
        g.log.error("Unable to stop the ctdb service")
        return False
    g.log.info("Successfully stopped ctdb service")
    return True


def start_ctdb_service(mnode):
    """Start ctdb service on the specified node.

    Args:
        mnode (str): Node on which ctdb service has to be started

    Returns:
        bool: True on successfully starting ctdb service. False otherwise.
    """
    g.log.info("Starting CTDB Service on %s", mnode)

    # Start ctdb service
    ret, _, _ = g.run(mnode, "service ctdb start")
    if ret != 0:
        g.log.error("Unable to start the ctdb service")
        return False
    g.log.info("Successfully started ctdb service")

    return True


def start_nmb_service(mnode):
    """Start nmb service on the specified node.

    Args:
        mnode (str): Node on which nmb service has to be started

    Returns:
        bool: True on successfully starting nmb service. False otherwise.
    """
    g.log.info("Starting nmb Service on %s", mnode)

    # Start nmb service
    ret, _, _ = g.run(mnode, "service nmb start")
    if ret != 0:
        g.log.error("Unable to start the nmb service")
        return False
    g.log.info("Successfully started nmb service")

    return True


def is_nmb_service_running(mnode):
    """Check if nmb service is running on node

    Args:
        mnode (str): Node on which nmb service status has to be verified.

    Returns:
        bool: True if nmb service is running. False otherwise.
    """
    g.log.info("Check if nmb service is running on %s", mnode)
    ret, out, _ = g.run(mnode, "service nmb status")
    if "Active: active (running)" in out:
        return True
    return False


def start_winbind_service(mnode):
    """Start winbind service on the specified node.

    Args:
        mnode (str): Node on which winbind service has to be started

    Returns:
        bool: True on successfully starting winbind service. False otherwise.
    """
    g.log.info("Starting winbind Service on %s", mnode)

    # Start winbind service
    ret, _, _ = g.run(mnode, "service winbind start")
    if ret != 0:
        g.log.error("Unable to start the winbind service")
        return False
    g.log.info("Successfully started winbind service")
    return True


def is_winbind_service_running(mnode):
    """Check if winbind service is running on node

    Args:
        mnode (str): Node on which winbind service status has to be verified.

    Returns:
        bool: True if winbind service is running. False otherwise.
    """
    g.log.info("Check if winbind service is running on %s", mnode)
    ret, out, _ = g.run(mnode, "service winbind status")
    if "Active: active (running)" in out:
        return True
    return False


def samba_ad(all_servers, netbios_name, domain_name, ad_admin_user,
             ad_admin_passwd, idmap_range=None):
    """Active Directory Integration

    Args:
        all_servers [list]: List of all servers where AD needs to be setup.
        netbios_name (str): Provide netbios name
        domain_name (str): Provide domain name
        ad_admin_user (str): Provide admin user
        ad_admin_passwd (str): Provide admin password
        idmap_range (str): Provide idmap range

    Returns:
        bool: True on successfully setting up AD. False otherwise.
    """
    g.log.info("Setting up AD Integration on %s", all_servers)
    mnode = all_servers[0]
    if netbios_name == '':
        g.log.error("netbios name is missing")
        return False
    # Validate netbios name
    if len(netbios_name) < 1 or len(netbios_name) > 15:
        g.log.error("The NetBIOS name must be 1 to 15 characters in length.")
        return False
    validate_netbios_name = re.compile(r"(^[A-Za-z\d_!@#$%^()\-'"
                                       r"{}\.~]{1,15}$)")
    isnetbiosname = validate_netbios_name.match(netbios_name)
    if isnetbiosname is None:
        g.log.error("The NetBIOS name entered is invalid.")
        return False

    if domain_name == '':
        g.log.error("domain name is missing")
        return False
    validate_domain_name = re.compile(r"^(?=.{1,253}$)(?!.*\.\..*)(?!\..*)"
                                      r"([a-zA-Z0-9-]{,63}\.)"
                                      r"{,127}[a-zA-Z0-9-]{1,63}$")
    isdomain = validate_domain_name.match(domain_name)
    if isdomain is None:
        g.log.error("The AD domain name string is invalid")
        return False
    # ad_workgroup should be in capital letters
    ad_workgroup = domain_name.split(".")[0].upper()

    if idmap_range is None:
        idmap_range = '1000000-1999999'
    else:
        try:
            idmap_range_start = int(idmap_range.split("-")[0])
            idmap_range_end = int(idmap_range.split("-")[1])
        except Exception as e:
            g.log.error("Invalid format.Use \'m-n\' for the range %s", str(e))
            return False
        if int(idmap_range_start) < 10000:
            g.log.error("Please select a starting value 10000 or above")
            return False
        # Maximum UIDs is 2^32
        elif int(idmap_range_end) > 4294967296:
            g.log.error("Please select an ending value 4294967296 or below")
            return False

    # Run the below in all servers
    for node in all_servers:
        smb_conf_file = "/etc/samba/smb.conf"
        add_netbios_name = r"sed -i '/^\[global\]/a netbios name = %s' %s"
        ret, _, err = g.run(node, add_netbios_name
                            % (netbios_name, smb_conf_file))
        if ret != 0:
            g.log.error("Failed to set netbios name parameters in smb.conf "
                        "file due to %s", str(err))
            return False
        add_realm = r"sed -i '/^\[global\]/a realm = %s' %s"
        ret, _, err = g.run(node, add_realm % (domain_name, smb_conf_file))
        if ret != 0:
            g.log.error("Failed to set realm parameters in smb.conf file "
                        "due to %s", str(err))
            return False
        add_idmap_range = (r"sed -i '/^\[global\]/a idmap config \* : "
                           "range = %s' %s")
        ret, _, err = g.run(node, add_idmap_range
                            % (idmap_range, smb_conf_file))
        if ret != 0:
            g.log.error("Failed to set idmap range parameters in smb.conf "
                        "file due to %s", str(err))
            return False
        add_idmap_bcknd = (r"sed -i '/^\[global\]/a idmap config \* : "
                           "backend = tdb' %s")
        ret, _, err = g.run(node, add_idmap_bcknd % smb_conf_file)
        if ret != 0:
            g.log.error("Failed to set idmap bcknd parameters in smb.conf "
                        "file due to %s", str(err))
            return False
        add_workgroup = ("sed -i '/^\\tworkgroup = "
                         "MYGROUP/c\\\tworkgroup = %s' %s")
        ret, _, err = g.run(node, add_workgroup
                            % (ad_workgroup, smb_conf_file))
        if ret != 0:
            g.log.error("Failed to set workgroup parameters in smb.conf file "
                        " due to %s", str(add_workgroup))
            return False
        add_security = "sed -i '/^\\tsecurity = user/c\\\tsecurity = ads' %s"
        ret, _, err = g.run(node, add_security % smb_conf_file)
        if ret != 0:
            g.log.error("Failed to set security parameters in smb.conf "
                        "file due to %s", str(err))
            return False

    # Verifying the Samba AD Configuration running testparm
    smb_ad_list = ["netbios name = "+netbios_name,
                   "workgroup = "+ad_workgroup,
                   "realm = " + domain_name.upper(), "security = ADS",
                   "idmap config * : backend = tdb", "idmap config * "
                   ": range = "+str(idmap_range)]
    testparm_cmd = "echo -e "+'"'+"\n"+'"'+" | testparm -v"
    ret, out, _ = g.run(node, testparm_cmd)
    if ret != 0:
        g.log.error("Testparm Command Failed to Execute")
    g.log.info("Testparm Command Execute Success")
    for smb_options in smb_ad_list:
        smb_options = smb_options.strip()
        if smb_options not in str(out).strip():
            g.log.info("Option %s entry present not in testparm" % smb_options)
            return False
    g.log.info("All required samba ad options set in smb.conf")

    if ad_admin_user == '':
        ad_admin_user = 'Administrator'

    # nsswitch Configuration
    # Run these in all servers
    for node in all_servers:
        winbind_passwd = ("sed -i '/^passwd:     files sss/cpasswd:     "
                          "files winbind' /etc/nsswitch.conf")
        ret, _, err = g.run(node, winbind_passwd)
        g.log.info("MASTER %s" % str(ret))
        if ret != 0:
            g.log.error("Failed to set winbind passwd  parameters in "
                        "nsswitch.conf file due to %s", str(err))
            return False
        winbind_group = ("sed -i '/^group:      files sss/cgroup:      "
                         "files winbind' /etc/nsswitch.conf")
        ret, _, err = g.run(node, winbind_group)
        if ret != 0:
            g.log.error("Failed to set winbind group parameters "
                        "in nsswitch.conf file due to %s", str(err))
            return False

        # Disable samba & winbind scripts
        samba_script = "/etc/ctdb/events.d/50.samba"
        winbind_script = "/etc/ctdb/events.d/49.winbind"
        ret, _, err = g.run(node, "chmod -x " + samba_script)
        if ret != 0:
            g.log.error("Failed to disable samba script as %s", str(err))
            return False
        ret, _, err = g.run(node, "chmod -x " + winbind_script)
        if ret != 0:
            g.log.error("Failed to disable winbind script as %s", str(err))
            return False
        # stop ctdb if already running
        ret = is_ctdb_service_running(node)
        if ret:
            ret = stop_ctdb_service(node)
            if not ret:
                return ret
        ret = start_ctdb_service(node)
        ret = is_ctdb_service_running(node)
        if ret:
            ret = is_smb_service_running(node)
            if ret:
                g.log.error("Samba services still running even after "
                            "samba script is disable")
                return False
            ret = is_winbind_service_running(node)
            if ret:
                g.log.error("Winbind services still running even after "
                            "winbind script is disable")
                return False

    # Join Active Directory Domain
    # One node only
    net_join_cmd = "net ads join -U "
    success_out = ("Joined '" + netbios_name +
                   "' to dns domain '" + domain_name + "'")
    ret, out, err = g.run(mnode, net_join_cmd + ad_admin_user +
                          "%" + ad_admin_passwd)
    if success_out not in str(out).strip():
        g.log.error("net ads join failed %s", str(err))
        return False
    g.log.info("Net ads join success")

    # RUN THESE IN ALL NODES
    for node in all_servers:
        ret = start_nmb_service(node)
        ret = is_nmb_service_running(node)
        if not ret:
            g.log.error("Failed to start nmb service")
            return False
        ret, _, err = g.run(node, "chmod +x " + samba_script)
        if ret != 0:
            g.log.error("Failed to enable samba script as %s", str(err))
            return False
        ret, _, err = g.run(node, "chmod +x " + winbind_script)
        if ret != 0:
            g.log.error("Failed to enable winbind script as %s", str(err))
            return False
        ret = stop_ctdb_service(node)
        if not ret:
            return False
        ret = start_ctdb_service(node)
        ret = is_ctdb_service_running(node)
        if ret:
            count = 0
            while count < 95:
                ret = is_smb_service_running(node)
                if ret:
                    break
                time.sleep(2)
                count += 1
            if not ret:
                g.log.error("Samba services not started running even "
                            "after samba "
                            "script is enabled")
                return False
            ret = start_winbind_service(node)
            ret = is_winbind_service_running(node)
            if not ret:
                g.log.error("Winbind services not running even after winbind "
                            "script is enabled")
                return False

    # Verify/Test Active Directory and Services
    ret, out, err = g.run(mnode, "net ads testjoin")
    if "Join is OK" not in str(out).strip():
        g.log.error("net ads join validation failed %s", str(err))
        return False
    # Verify if winbind is operating correctly by executing the following steps
    ret, out, err = g.run(mnode, "wbinfo -t")
    if "succeeded" not in str(out):
        g.log.error("wbinfo -t command failed, ad setup is not correct %s",
                    str(err))
        return False

    # Execute the following command to resolve the given name to a Windows SID
    sid_cmd = ("wbinfo --name-to-sid '" + ad_workgroup +
               "\\" + ad_admin_user + "'")
    ret, out, err = g.run(mnode, sid_cmd)
    if "-500 SID_USER" not in str(out):
        g.log.error("Failed to execute wbinfo --name-to-sid command %s",
                    str(err))
        return False
    sid = str(out).split('SID')[0].strip()

    # Execute the following command to verify authentication:
    wbinfo_auth_cmd = ("wbinfo -a '" + ad_workgroup + "\\" + ad_admin_user +
                       "%" + ad_admin_passwd + "'")
    ret, out, err = g.run(mnode, wbinfo_auth_cmd)
    if "password authentication succeeded" not in str(out).strip():
        g.log.error("winbind does nothave authentication to acess "
                    "ad server %s", str(err))
        return False

    # Execute the following command to verify if the id-mapping is
    # working properly
    idmap_range_start = str(idmap_range.split("-")[0])
    id_map_cmd = "wbinfo --sid-to-uid " + sid
    ret, out, err = g.run(mnode, id_map_cmd)
    if str(out).strip() != str(idmap_range_start):
        g.log.error("id mapping is not correct %s", str(err))
        return False
    # getent password validation
    getent_cmd = "getent passwd '" + ad_workgroup + "\\" + ad_admin_user + "'"
    getent_expected = "/home/" + ad_workgroup + "/" + ad_admin_user.lower()
    ret, out, err = g.run(mnode, getent_cmd)
    if getent_expected not in str(out).strip():
        g.log.error("winbind Name Service Switch failed %s", str(err))
        return False
    return True