samba-tool: Addd functions to print GPO flags and GPlink options
[metze/samba/wip.git] / source4 / scripting / python / samba / netcmd / gpo.py
1 #!/usr/bin/env python
2 #
3 # implement samba_tool gpo commands
4 #
5 # Copyright Andrew Tridgell 2010
6 # Copyright Giampaolo Lauria 2011 <lauria2@yahoo.com>
7 # Copyright Amitay Isaacs 2011 <amitay@gmail.com>
8 #
9 # based on C implementation by Guenther Deschner and Wilco Baan Hofman
10 #
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 #
24
25 import os
26 import samba.getopt as options
27 import ldb
28
29 from samba.auth import system_session
30 from samba.netcmd import (
31     Command,
32     CommandError,
33     Option,
34     SuperCommand,
35     )
36 from samba.samdb import SamDB
37 from samba import drs_utils, nttime2string, dsdb, dcerpc
38 from samba.dcerpc import misc
39 from samba.ndr import ndr_unpack
40 import samba.security
41 import samba.auth
42 from samba.auth import AUTH_SESSION_INFO_DEFAULT_GROUPS, AUTH_SESSION_INFO_AUTHENTICATED, AUTH_SESSION_INFO_SIMPLE_PRIVILEGES
43 from samba.netcmd.common import netcmd_finddc
44 from samba import policy
45 from samba import smb
46
47
48 def samdb_connect(ctx):
49     '''make a ldap connection to the server'''
50     try:
51         ctx.samdb = SamDB(url=ctx.url,
52                           session_info=system_session(),
53                           credentials=ctx.creds, lp=ctx.lp)
54     except Exception, e:
55         raise CommandError("LDAP connection to %s failed " % ctx.url, e)
56
57
58 def attr_default(msg, attrname, default):
59     '''get an attribute from a ldap msg with a default'''
60     if attrname in msg:
61         return msg[attrname][0]
62     return default
63
64
65 def gpo_flags_string(value):
66     '''return gpo flags string'''
67     flags = policy.get_gpo_flags(value)
68     if not flags:
69         ret = 'NONE'
70     else:
71         ret = ' '.join(flags)
72     return ret
73
74
75 def gplink_options_string(value):
76     '''return gplink options string'''
77     options = policy.get_gplink_options(value)
78     if not options:
79         ret = 'NONE'
80     else:
81         ret = ' '.join(options)
82     return ret
83
84
85 def parse_gplink(gplink):
86     '''parse a gPLink into an array of dn and options'''
87     ret = []
88     a = gplink.split(']')
89     for g in a:
90         if not g:
91             continue
92         d = g.split(';')
93         if len(d) != 2 or not d[0].startswith("[LDAP://"):
94             raise RuntimeError("Badly formed gPLink '%s'" % g)
95         ret.append({ 'dn' : d[0][8:], 'options' : int(d[1])})
96     return ret
97
98
99 def encode_gplink(gplist):
100     '''Encode an array of dn and options into gPLink string'''
101     ret = ''
102     for g in gplist:
103         ret += "[LDAP://%s;%d]" % (g['dn'], g['options'])
104     return ret
105
106
107 def dc_url(lp, creds, url=None, dc=None):
108     '''If URL is not specified, return URL for writable DC.
109     If dc is provided, use that to construct ldap URL'''
110
111     if url is None:
112         if dc is None:
113             try:
114                 dc = netcmd_finddc(lp, creds)
115             except Exception, e:
116                 raise RunTimeError("Could not find a DC for domain", e)
117         url = 'ldap://' + dc
118     return url
119
120
121 def get_gpo_dn(samdb, gpo):
122     '''Construct the DN for gpo'''
123
124     dn = samdb.get_default_basedn()
125     dn.add_child(ldb.Dn(samdb, "CN=Policies,DC=System"))
126     dn.add_child(ldb.Dn(samdb, "CN=%s" % gpo))
127     return dn
128
129
130 def get_gpo_info(samdb, gpo=None, displayname=None, dn=None):
131     '''Get GPO information using gpo, displayname or dn'''
132
133     policies_dn = samdb.get_default_basedn()
134     policies_dn.add_child(ldb.Dn(samdb, "CN=Policies,CN=System"))
135
136     base_dn = policies_dn
137     search_expr = "(objectClass=groupPolicyContainer)"
138     search_scope = ldb.SCOPE_ONELEVEL
139
140     if gpo is not None:
141         search_expr = "(&(objectClass=groupPolicyContainer)(name=%s))" % ldb.binary_encode(gpo)
142
143     if displayname is not None:
144         search_expr = "(&(objectClass=groupPolicyContainer)(displayname=%s))" % ldb.binary_encode(displayname)
145
146     if dn is not None:
147         base_dn = dn
148         search_scope = ldb.SCOPE_BASE
149
150     try:
151         msg = samdb.search(base=base_dn, scope=search_scope,
152                             expression=search_expr,
153                             attrs=['nTSecurityDescriptor',
154                                     'versionNumber',
155                                     'flags',
156                                     'name',
157                                     'displayName',
158                                     'gPCFileSysPath'])
159     except Exception, e:
160         if gpo is not None:
161             mesg = "Cannot get information for GPO %s" % gpo
162         else:
163             mesg = "Cannot get information for GPOs"
164         raise CommandError(mesg, e)
165
166     return msg
167
168
169 def parse_unc(unc):
170     '''Parse UNC string into a hostname, a service, and a filepath'''
171     if unc.startswith('\\\\') and unc.startswith('//'):
172         return []
173     tmp = unc[2:].split('/', 2)
174     if len(tmp) == 3:
175         return tmp
176     tmp = unc[2:].split('\\', 2)
177     if len(tmp) == 3:
178         return tmp;
179     return []
180
181
182 def copy_directory_recurse(conn, remotedir, localdir):
183     if not os.path.isdir(localdir):
184         os.mkdir(localdir)
185     r_dirs = [ remotedir ]
186     l_dirs = [ localdir ]
187     while r_dirs:
188         r_dir = r_dirs.pop()
189         l_dir = l_dirs.pop()
190
191         dirlist = conn.list(r_dir)
192         for e in dirlist:
193             r_name = r_dir + '\\' + e['name']
194             l_name = l_dir + os.path.sep + e['name']
195
196             if e['attrib'] & smb.FILE_ATTRIBUTE_DIRECTORY:
197                 r_dirs.append(r_name)
198                 l_dirs.append(l_name)
199                 os.mkdir(l_name)
200             elif e['attrib'] & smb.FILE_ATTRIBUTE_ARCHIVE:
201                 data = conn.loadfile(r_name)
202                 file(l_name, 'w').write(data)
203
204
205 class cmd_listall(Command):
206     """list all GPOs"""
207
208     synopsis = "%prog gpo listall [options]"
209
210     takes_options = [
211         Option("-H", "--URL", help="LDB URL for database or target server", type=str,
212                metavar="URL", dest="H")
213         ]
214
215     def run(self, H=None, sambaopts=None, credopts=None, versionopts=None):
216
217         self.lp = sambaopts.get_loadparm()
218         self.creds = credopts.get_credentials(self.lp, fallback_machine=True)
219
220         self.url = dc_url(self.lp, self.creds, H)
221
222         samdb_connect(self)
223
224         msg = get_gpo_info(self.samdb, None)
225
226         for m in msg:
227             print("GPO          : %s" % m['name'][0])
228             print("display name : %s" % m['displayName'][0])
229             print("path         : %s" % m['gPCFileSysPath'][0])
230             print("dn           : %s" % m.dn)
231             print("version      : %s" % attr_default(m, 'versionNumber', '0'))
232             print("flags        : %s" % gpo_flags_string(int(attr_default(m, 'flags', 0))))
233             print("")
234
235
236 class cmd_list(Command):
237     """list GPOs for an account"""
238
239     synopsis = "%prog gpo list <username> [options]"
240
241     takes_args = [ 'username' ]
242
243     takes_options = [
244         Option("-H", "--URL", help="LDB URL for database or target server", type=str,
245                metavar="URL", dest="H")
246         ]
247
248     def run(self, username, H=None, sambaopts=None, credopts=None, versionopts=None):
249
250         self.lp = sambaopts.get_loadparm()
251         self.creds = credopts.get_credentials(self.lp, fallback_machine=True)
252
253         self.url = dc_url(self.lp, self.creds, H)
254
255         samdb_connect(self)
256
257         try:
258             msg = self.samdb.search(expression='(&(|(samAccountName=%s)(samAccountName=%s$))(objectClass=User))' %
259                                                 (ldb.binary_encode(username),ldb.binary_encode(username)))
260             user_dn = msg[0].dn
261         except Exception, e:
262             raise CommandError("Failed to find account %s" % username, e)
263
264         # check if its a computer account
265         try:
266             msg = self.samdb.search(base=user_dn, scope=ldb.SCOPE_BASE, attrs=['objectClass'])[0]
267             is_computer = 'computer' in msg['objectClass']
268         except Exception, e:
269             raise CommandError("Failed to find objectClass for user %s" % username, e)
270
271         session_info_flags = ( AUTH_SESSION_INFO_DEFAULT_GROUPS |
272                                AUTH_SESSION_INFO_AUTHENTICATED )
273
274         # When connecting to a remote server, don't look up the local privilege DB
275         if self.url is not None and self.url.startswith('ldap'):
276             session_info_flags |= AUTH_SESSION_INFO_SIMPLE_PRIVILEGES
277
278         session = samba.auth.user_session(self.samdb, lp_ctx=self.lp, dn=user_dn,
279                                           session_info_flags=session_info_flags)
280
281         token = session.security_token
282
283         gpos = []
284
285         inherit = True
286         dn = ldb.Dn(self.samdb, str(user_dn)).parent()
287         while True:
288             msg = self.samdb.search(base=dn, scope=ldb.SCOPE_BASE, attrs=['gPLink', 'gPOptions'])[0]
289             if 'gPLink' in msg:
290                 glist = parse_gplink(msg['gPLink'][0])
291                 for g in glist:
292                     if not inherit and not (g['options'] & dsdb.GPLINK_OPT_ENFORCE):
293                         continue
294                     if g['options'] & dsdb.GPLINK_OPT_DISABLE:
295                         continue
296
297                     try:
298                         gmsg = self.samdb.search(base=g['dn'], scope=ldb.SCOPE_BASE,
299                                                  attrs=['name', 'displayName', 'flags',
300                                                         'ntSecurityDescriptor'])
301                     except Exception:
302                         print("Failed to fetch gpo object %s" % g['dn'])
303                         continue
304
305                     secdesc_ndr = gmsg[0]['ntSecurityDescriptor'][0]
306                     secdesc = ndr_unpack(dcerpc.security.descriptor, secdesc_ndr)
307
308                     try:
309                         samba.security.access_check(secdesc, token,
310                                                     dcerpc.security.SEC_STD_READ_CONTROL |
311                                                     dcerpc.security.SEC_ADS_LIST |
312                                                     dcerpc.security.SEC_ADS_READ_PROP)
313                     except RuntimeError:
314                         print("Failed access check on %s" % msg.dn)
315                         continue
316
317                     # check the flags on the GPO
318                     flags = int(attr_default(gmsg[0], 'flags', 0))
319                     if is_computer and (flags & dsdb.GPO_FLAG_MACHINE_DISABLE):
320                         continue
321                     if not is_computer and (flags & dsdb.GPO_FLAG_USER_DISABLE):
322                         continue
323                     gpos.append((gmsg[0]['displayName'][0], gmsg[0]['name'][0]))
324
325             # check if this blocks inheritance
326             gpoptions = int(attr_default(msg, 'gPOptions', 0))
327             if gpoptions & dsdb.GPO_BLOCK_INHERITANCE:
328                 inherit = False
329
330             if dn == self.samdb.get_default_basedn():
331                 break
332             dn = dn.parent()
333
334         if is_computer:
335             msg_str = 'computer'
336         else:
337             msg_str = 'user'
338
339         print("GPOs for %s %s" % (msg_str, username))
340         for g in gpos:
341             print("    %s %s" % (g[0], g[1]))
342
343
344 class cmd_show(Command):
345     """Show information for a GPO"""
346
347     synopsis = "%prog gpo show <gpo> [options]"
348
349     takes_optiongroups = {
350         "sambaopts": options.SambaOptions,
351         "versionopts": options.VersionOptions,
352         "credopts": options.CredentialsOptions,
353     }
354
355     takes_args = [ 'gpo' ]
356
357     takes_options = [
358         Option("-H", help="LDB URL for database or target server", type=str)
359         ]
360
361     def run(self, gpo, H=None, sambaopts=None, credopts=None, versionopts=None):
362
363         self.lp = sambaopts.get_loadparm()
364         self.creds = credopts.get_credentials(self.lp, fallback_machine=True)
365
366         self.url = dc_url(self.lp, self.creds, H)
367
368         samdb_connect(self)
369
370         try:
371             msg = get_gpo_info(self.samdb, gpo)[0]
372         except Exception, e:
373             raise CommandError("GPO %s does not exist" % gpo, e)
374
375         secdesc_ndr = msg['ntSecurityDescriptor'][0]
376         secdesc = ndr_unpack(dcerpc.security.descriptor, secdesc_ndr)
377
378         print("GPO          : %s" % msg['name'][0])
379         print("display name : %s" % msg['displayName'][0])
380         print("path         : %s" % msg['gPCFileSysPath'][0])
381         print("dn           : %s" % msg.dn)
382         print("version      : %s" % attr_default(msg, 'versionNumber', '0'))
383         print("flags        : %s" % gpo_flags_string(int(attr_default(msg, 'flags', 0))))
384         print("ACL          : %s" % secdesc.as_sddl())
385         print("")
386
387
388 class cmd_getlink(Command):
389     """List GPO Links for a container"""
390
391     synopsis = "%prog gpo getlink <container_dn> [options]"
392
393     takes_optiongroups = {
394         "sambaopts": options.SambaOptions,
395         "versionopts": options.VersionOptions,
396         "credopts": options.CredentialsOptions,
397     }
398
399     takes_args = [ 'container_dn' ]
400
401     takes_options = [
402         Option("-H", help="LDB URL for database or target server", type=str)
403         ]
404
405     def run(self, container_dn, H=None, sambaopts=None, credopts=None,
406                 versionopts=None):
407
408         self.lp = sambaopts.get_loadparm()
409         self.creds = credopts.get_credentials(self.lp, fallback_machine=True)
410
411         self.url = dc_url(self.lp, self.creds, H)
412
413         samdb_connect(self)
414
415         try:
416             msg = self.samdb.search(base=container_dn, scope=ldb.SCOPE_BASE,
417                                     expression="(objectClass=*)",
418                                     attrs=['gPlink'])[0]
419         except Exception, e:
420             raise CommandError("Could not find Container DN %s (%s)" % container_dn, e)
421
422         if 'gPLink' in msg:
423             print("GPO(s) linked to DN %s" % container_dn)
424             gplist = parse_gplink(msg['gPLink'][0])
425             for g in gplist:
426                 msg = get_gpo_info(self.samdb, dn=g['dn'])
427                 print("    GPO     : %s" % msg[0]['name'][0])
428                 print("    Name    : %s" % msg[0]['displayName'][0])
429                 print("    Options : %s" % gplink_options_string(g['options']))
430                 print("")
431         else:
432             print("No GPO(s) linked to DN=%s" % container_dn)
433
434
435 class cmd_setlink(Command):
436     """Add or Update a GPO link to a container"""
437
438     synopsis = "%prog gpo setlink <container_dn> <gpo> [options]"
439
440     takes_optiongroups = {
441         "sambaopts": options.SambaOptions,
442         "versionopts": options.VersionOptions,
443         "credopts": options.CredentialsOptions,
444     }
445
446     takes_args = [ 'container_dn', 'gpo' ]
447
448     takes_options = [
449         Option("-H", help="LDB URL for database or target server", type=str),
450         Option("--disable", dest="disabled", default=False, action='store_true',
451             help="Disable policy"),
452         Option("--enforce", dest="enforced", default=False, action='store_true',
453             help="Enforce policy")
454         ]
455
456     def run(self, container_dn, gpo, H=None, disabled=False, enforced=False,
457                 sambaopts=None, credopts=None, versionopts=None):
458
459         self.lp = sambaopts.get_loadparm()
460         self.creds = credopts.get_credentials(self.lp, fallback_machine=True)
461
462         self.url = dc_url(self.lp, self.creds, H)
463
464         samdb_connect(self)
465
466         gplink_options = 0
467         if disabled:
468             gplink_options |= dsdb.GPLINK_OPT_DISABLE
469         if enforced:
470             gplink_options |= dsdb.GPLINK_OPT_ENFORCE
471
472         # Check if valid GPO DN
473         try:
474             msg = get_gpo_info(self.samdb, gpo=gpo)[0]
475         except Exception, e:
476             raise CommandError("GPO %s does not exist" % gpo_dn, e)
477         gpo_dn = get_gpo_dn(self.samdb, gpo)
478
479         # Check if valid Container DN
480         try:
481             msg = self.samdb.search(base=container_dn, scope=ldb.SCOPE_BASE,
482                                     expression="(objectClass=*)",
483                                     attrs=['gPlink'])[0]
484         except Exception, e:
485             raise CommandError("Could not find container DN %s" % container_dn, e)
486
487         # Update existing GPlinks or Add new one
488         existing_gplink = False
489         if 'gPLink' in msg:
490             gplist = parse_gplink(msg['gPLink'][0])
491             existing_gplink = True
492             found = False
493             for g in gplist:
494                 if g['dn'].lower() == gpo_dn.lower():
495                     g['options'] = gplink_options
496                     found = True
497                     break
498             if not found:
499                 gplist.insert(0, { 'dn' : gpo_dn, 'options' : gplink_options })
500         else:
501             gplist = []
502             gplist.append({ 'dn' : gpo_dn, 'options' : gplink_options })
503
504         gplink_str = encode_gplink(gplist)
505
506         m = ldb.Message()
507         m.dn = ldb.Dn(self.samdb, container_dn)
508
509         if existing_gplink:
510             m['new_value'] = ldb.MessageElement(gplink_str, ldb.FLAG_MOD_REPLACE, 'gPLink')
511         else:
512             m['new_value'] = ldb.MessageElement(gplink_str, ldb.FLAG_MOD_ADD, 'gPLink')
513
514         try:
515             self.samdb.modify(m)
516         except Exception, e:
517             raise CommandError("Error adding GPO Link", e)
518
519         print("Added/Updated GPO link")
520         cmd_getlink().run(container_dn, H, sambaopts, credopts, versionopts)
521
522
523 class cmd_dellink(Command):
524     """Delete GPO link from a container"""
525
526     synopsis = "%prog gpo dellink <container_dn> <gpo> [options]"
527
528     takes_optiongroups = {
529         "sambaopts": options.SambaOptions,
530         "versionopts": options.VersionOptions,
531         "credopts": options.CredentialsOptions,
532     }
533
534     takes_args = [ 'container_dn', 'gpo' ]
535
536     takes_options = [
537         Option("-H", help="LDB URL for database or target server", type=str),
538         ]
539
540     def run(self, container_dn, gpo_dn, H=None, sambaopts=None, credopts=None,
541                 versionopts=None):
542
543         self.lp = sambaopts.get_loadparm()
544         self.creds = credopts.get_credentials(self.lp, fallback_machine=True)
545
546         self.url = dc_url(self.lp, self.creds, H)
547
548         samdb_connect(self)
549
550         # Check if valid GPO
551         try:
552             msg = get_gpo_info(self.sambdb, gpo=gpo)[0]
553         except Exception, e:
554                 raise CommandError("GPO %s does not exist" % gpo, e)
555         gpo_dn = get_gpo_dn(self.samdb, gpo)
556
557         # Check if valid Container DN and get existing GPlinks
558         try:
559             msg = self.samdb.search(base=container_dn, scope=ldb.SCOPE_BASE,
560                                     expression="(objectClass=*)",
561                                     attrs=['gPlink'])[0]
562         except Exception, e:
563             raise CommandError("Could not find container DN %s" % dn, e)
564
565         if 'gPLink' in msg:
566             gplist = parse_gplink(msg['gPLink'][0])
567             for g in gplist:
568                 if g['dn'].lower() == gpo_dn.lower():
569                     gplist.remove(g)
570                     break
571         else:
572             raise CommandError("Specified GPO is not linked to this container");
573
574         m = ldb.Message()
575         m.dn = ldb.Dn(self.samdb, container_dn)
576
577         if gplist:
578             gplink_str = encode_gplink(gplist)
579             m['new_value'] = ldb.MessageElement(gplink_str, ldb.FLAG_MOD_REPLACE, 'gPLink')
580         else:
581             m['new_value'] = ldb.MessageElement('', ldb.FLAG_MOD_DELETE, 'gPLink')
582
583         try:
584             self.samdb.modify(m)
585         except Exception, e:
586             raise CommandError("Error Removing GPO Link (%s)" % e)
587
588         print("Deleted GPO link.")
589         cmd_getlink().run(container_dn, H, sambaopts, credopts, versionopts)
590
591
592 class cmd_getinheritance(Command):
593     """Get inheritance flag for a container"""
594
595     synopsis = "%prog gpo getinheritance <container_dn> [options]"
596
597     takes_optiongroups = {
598         "sambaopts": options.SambaOptions,
599         "versionopts": options.VersionOptions,
600         "credopts": options.CredentialsOptions,
601     }
602
603     takes_args = [ 'container_dn' ]
604
605     takes_options = [
606         Option("-H", help="LDB URL for database or target server", type=str)
607         ]
608
609     def run(self, container_dn, H=None, sambaopts=None, credopts=None,
610                 versionopts=None):
611
612         self.url = H
613         self.lp = sambaopts.get_loadparm()
614
615         self.creds = credopts.get_credentials(self.lp, fallback_machine=True)
616
617         samdb_connect(self)
618
619         try:
620             msg = self.samdb.search(base=container_dn, scope=ldb.SCOPE_BASE,
621                                     expression="(objectClass=*)",
622                                     attrs=['gPOptions'])[0]
623         except Exception, e:
624             raise CommandError("Could not find Container DN %s" % container_dn, e)
625
626         inheritance = 0
627         if 'gPOptions' in msg:
628             inheritance = int(msg['gPOptions'][0]);
629
630         if inheritance == dsdb.GPO_BLOCK_INHERITANCE:
631             print("Container has GPO_BLOCK_INHERITANCE")
632         else:
633             print("Container has GPO_INHERIT")
634
635
636 class cmd_setinheritance(Command):
637     """Set inheritance flag on a container"""
638
639     synopsis = "%prog gpo setinheritance <container_dn> <block|inherit> [options]"
640
641     takes_optiongroups = {
642         "sambaopts": options.SambaOptions,
643         "versionopts": options.VersionOptions,
644         "credopts": options.CredentialsOptions,
645     }
646
647     takes_args = [ 'container_dn', 'inherit_state' ]
648
649     takes_options = [
650         Option("-H", help="LDB URL for database or target server", type=str)
651         ]
652
653     def run(self, container_dn, inherit_state, H=None, sambaopts=None, credopts=None,
654                 versionopts=None):
655
656         if inherit_state.lower() == 'block':
657             inheritance = dsdb.GPO_BLOCK_INHERITANCE
658         elif inherit_state.lower() == 'inherit':
659             inheritance = dsdb.GPO_INHERIT
660         else:
661             raise CommandError("Unknown inheritance state (%s)" % inherit_state)
662
663         self.url = H
664         self.lp = sambaopts.get_loadparm()
665
666         self.creds = credopts.get_credentials(self.lp, fallback_machine=True)
667
668         samdb_connect(self)
669
670         try:
671             msg = self.samdb.search(base=container_dn, scope=ldb.SCOPE_BASE,
672                                     expression="(objectClass=*)",
673                                     attrs=['gPOptions'])[0]
674         except Exception, e:
675             raise CommandError("Could not find Container DN %s" % container_dn, e)
676
677         m = ldb.Message()
678         m.dn = ldb.Dn(self.samdb, container_dn)
679
680         if 'gPOptions' in msg:
681             m['new_value'] = ldb.MessageElement(str(inheritance), ldb.FLAG_MOD_REPLACE, 'gPOptions')
682         else:
683             m['new_value'] = ldb.MessageElement(str(inheritance), ldb.FLAG_MOD_ADD, 'gPOptions');
684
685         try:
686             self.samdb.modify(m)
687         except Exception, e:
688             raise CommandError("Error setting inheritance state %s" % inherit_state, e)
689
690
691 class cmd_fetch(Command):
692     """Download a GPO"""
693
694     synopsis = "%prog gpo fetch <gpo> [options]"
695
696     takes_optiongroups = {
697         "sambaopts": options.SambaOptions,
698         "versionopts": options.VersionOptions,
699         "credopts": options.CredentialsOptions,
700     }
701
702     takes_args = [ 'gpo' ]
703
704     takes_options = [
705         Option("-H", help="LDB URL for database or target server", type=str),
706         Option("--tmpdir", help="Temporary directory for copying policy files", type=str)
707         ]
708
709     def run(self, gpo, H=None, tmpdir=None, sambaopts=None, credopts=None, versionopts=None):
710
711         self.lp = sambaopts.get_loadparm()
712         self.creds = credopts.get_credentials(self.lp, fallback_machine=True)
713
714         dc_hostname = netcmd_finddc(self.lp, self.creds)
715         self.url = dc_url(self.lp, self.creds, H, dc=dc_hostname)
716
717         samdb_connect(self)
718         try:
719             msg = get_gpo_info(self.samdb, gpo)[0]
720         except Exception, e:
721             raise CommandError("GPO %s does not exist" % gpo)
722
723         unc = msg['gPCFileSysPath'][0]
724         try:
725             [dom_name, service, sharepath] = parse_unc(unc)
726         except:
727             raise CommandError("Invalid GPO path (%s)" % unc)
728
729         try:
730             conn = smb.SMB(dc_hostname, service, lp=self.lp, creds=self.creds)
731         except Exception, e:
732             raise CommandError("Error connecting to '%s' using SMB" % dc_hostname, e)
733
734         if tmpdir is None:
735             tmpdir = "/tmp"
736
737         try:
738             localdir = tmpdir + os.path.sep + "policy"
739             if not os.path.isdir(localdir):
740                 os.mkdir(localdir)
741             gpodir = localdir + os.path.sep + gpo
742             if not os.path.isdir(gpodir):
743                 os.mkdir(gpodir)
744             copy_directory_recurse(conn, sharepath, gpodir)
745         except Exception, e:
746             raise CommandError("Error copying GPO", e)
747         print('GPO copied to %s' % gpodir)
748
749
750 class cmd_create(Command):
751     """Create a GPO"""
752
753 class cmd_setacl(Command):
754     """Set ACL on a GPO"""
755
756
757 class cmd_gpo(SuperCommand):
758     """Group Policy Object (GPO) commands"""
759
760     subcommands = {}
761     subcommands["listall"] = cmd_listall()
762     subcommands["list"] = cmd_list()
763     subcommands["show"] = cmd_show()
764     subcommands["getlink"] = cmd_getlink()
765     subcommands["setlink"] = cmd_setlink()
766     subcommands["dellink"] = cmd_dellink()
767     subcommands["getinheritance"] = cmd_getinheritance()
768     subcommands["setinheritance"] = cmd_setinheritance()
769     subcommands["fetch"] = cmd_fetch()
770     subcommands["create"] = cmd_create()
771     subcommands["setacl"] = cmd_setacl()