s4:netcmd/gpo.py: we don't need to set autogenerated attributes
[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 import uuid
47
48
49 def samdb_connect(ctx):
50     '''make a ldap connection to the server'''
51     try:
52         ctx.samdb = SamDB(url=ctx.url,
53                           session_info=system_session(),
54                           credentials=ctx.creds, lp=ctx.lp)
55     except Exception, e:
56         raise CommandError("LDAP connection to %s failed " % ctx.url, e)
57
58
59 def attr_default(msg, attrname, default):
60     '''get an attribute from a ldap msg with a default'''
61     if attrname in msg:
62         return msg[attrname][0]
63     return default
64
65
66 def gpo_flags_string(value):
67     '''return gpo flags string'''
68     flags = policy.get_gpo_flags(value)
69     if not flags:
70         ret = 'NONE'
71     else:
72         ret = ' '.join(flags)
73     return ret
74
75
76 def gplink_options_string(value):
77     '''return gplink options string'''
78     options = policy.get_gplink_options(value)
79     if not options:
80         ret = 'NONE'
81     else:
82         ret = ' '.join(options)
83     return ret
84
85
86 def parse_gplink(gplink):
87     '''parse a gPLink into an array of dn and options'''
88     ret = []
89     a = gplink.split(']')
90     for g in a:
91         if not g:
92             continue
93         d = g.split(';')
94         if len(d) != 2 or not d[0].startswith("[LDAP://"):
95             raise RuntimeError("Badly formed gPLink '%s'" % g)
96         ret.append({ 'dn' : d[0][8:], 'options' : int(d[1])})
97     return ret
98
99
100 def encode_gplink(gplist):
101     '''Encode an array of dn and options into gPLink string'''
102     ret = ''
103     for g in gplist:
104         ret += "[LDAP://%s;%d]" % (g['dn'], g['options'])
105     return ret
106
107
108 def dc_url(lp, creds, url=None, dc=None):
109     '''If URL is not specified, return URL for writable DC.
110     If dc is provided, use that to construct ldap URL'''
111
112     if url is None:
113         if dc is None:
114             try:
115                 dc = netcmd_finddc(lp, creds)
116             except Exception, e:
117                 raise RunTimeError("Could not find a DC for domain", e)
118         url = 'ldap://' + dc
119     return url
120
121
122 def get_gpo_dn(samdb, gpo):
123     '''Construct the DN for gpo'''
124
125     dn = samdb.get_default_basedn()
126     dn.add_child(ldb.Dn(samdb, "CN=Policies,DC=System"))
127     dn.add_child(ldb.Dn(samdb, "CN=%s" % gpo))
128     return dn
129
130
131 def get_gpo_info(samdb, gpo=None, displayname=None, dn=None):
132     '''Get GPO information using gpo, displayname or dn'''
133
134     policies_dn = samdb.get_default_basedn()
135     policies_dn.add_child(ldb.Dn(samdb, "CN=Policies,CN=System"))
136
137     base_dn = policies_dn
138     search_expr = "(objectClass=groupPolicyContainer)"
139     search_scope = ldb.SCOPE_ONELEVEL
140
141     if gpo is not None:
142         search_expr = "(&(objectClass=groupPolicyContainer)(name=%s))" % ldb.binary_encode(gpo)
143
144     if displayname is not None:
145         search_expr = "(&(objectClass=groupPolicyContainer)(displayname=%s))" % ldb.binary_encode(displayname)
146
147     if dn is not None:
148         base_dn = dn
149         search_scope = ldb.SCOPE_BASE
150
151     try:
152         msg = samdb.search(base=base_dn, scope=search_scope,
153                             expression=search_expr,
154                             attrs=['nTSecurityDescriptor',
155                                     'versionNumber',
156                                     'flags',
157                                     'name',
158                                     'displayName',
159                                     'gPCFileSysPath'])
160     except Exception, e:
161         if gpo is not None:
162             mesg = "Cannot get information for GPO %s" % gpo
163         else:
164             mesg = "Cannot get information for GPOs"
165         raise CommandError(mesg, e)
166
167     return msg
168
169
170 def parse_unc(unc):
171     '''Parse UNC string into a hostname, a service, and a filepath'''
172     if unc.startswith('\\\\') and unc.startswith('//'):
173         return []
174     tmp = unc[2:].split('/', 2)
175     if len(tmp) == 3:
176         return tmp
177     tmp = unc[2:].split('\\', 2)
178     if len(tmp) == 3:
179         return tmp;
180     return []
181
182
183 def copy_directory_remote_to_local(conn, remotedir, localdir):
184     if not os.path.isdir(localdir):
185         os.mkdir(localdir)
186     r_dirs = [ remotedir ]
187     l_dirs = [ localdir ]
188     while r_dirs:
189         r_dir = r_dirs.pop()
190         l_dir = l_dirs.pop()
191
192         dirlist = conn.list(r_dir)
193         for e in dirlist:
194             r_name = r_dir + '\\' + e['name']
195             l_name = os.path.join(l_dir, e['name'])
196
197             if e['attrib'] & smb.FILE_ATTRIBUTE_DIRECTORY:
198                 r_dirs.append(r_name)
199                 l_dirs.append(l_name)
200                 os.mkdir(l_name)
201             else:
202                 data = conn.loadfile(r_name)
203                 file(l_name, 'w').write(data)
204
205
206 def copy_directory_local_to_remote(conn, localdir, remotedir):
207     if not conn.chkpath(remotedir):
208         conn.mkdir(remotedir)
209     l_dirs = [ localdir ]
210     r_dirs = [ remotedir ]
211     while l_dirs:
212         l_dir = l_dirs.pop()
213         r_dir = r_dirs.pop()
214
215         dirlist = os.listdir(l_dir)
216         for e in dirlist:
217             l_name = os.path.join(l_dir, e)
218             r_name = r_dir + '\\' + e
219
220             if os.path.isdir(l_name):
221                 l_dirs.append(l_name)
222                 r_dirs.append(r_name)
223                 conn.mkdir(r_name)
224             else:
225                 data = file(l_name, 'r').read()
226                 conn.savefile(r_name, data)
227
228
229 def create_directory_hier(conn, remotedir):
230     elems = remotedir.replace('/', '\\').split('\\')
231     path = ""
232     for e in elems:
233         path = path + '\\' + e
234         if not conn.chkpath(path):
235             conn.mkdir(path)
236
237
238 class cmd_listall(Command):
239     """list all GPOs"""
240
241     synopsis = "%prog gpo listall [options]"
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, 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         msg = get_gpo_info(self.samdb, None)
258
259         for m in msg:
260             print("GPO          : %s" % m['name'][0])
261             print("display name : %s" % m['displayName'][0])
262             print("path         : %s" % m['gPCFileSysPath'][0])
263             print("dn           : %s" % m.dn)
264             print("version      : %s" % attr_default(m, 'versionNumber', '0'))
265             print("flags        : %s" % gpo_flags_string(int(attr_default(m, 'flags', 0))))
266             print("")
267
268
269 class cmd_list(Command):
270     """list GPOs for an account"""
271
272     synopsis = "%prog gpo list <username> [options]"
273
274     takes_args = [ 'username' ]
275
276     takes_options = [
277         Option("-H", "--URL", help="LDB URL for database or target server", type=str,
278                metavar="URL", dest="H")
279         ]
280
281     def run(self, username, H=None, sambaopts=None, credopts=None, versionopts=None):
282
283         self.lp = sambaopts.get_loadparm()
284         self.creds = credopts.get_credentials(self.lp, fallback_machine=True)
285
286         self.url = dc_url(self.lp, self.creds, H)
287
288         samdb_connect(self)
289
290         try:
291             msg = self.samdb.search(expression='(&(|(samAccountName=%s)(samAccountName=%s$))(objectClass=User))' %
292                                                 (ldb.binary_encode(username),ldb.binary_encode(username)))
293             user_dn = msg[0].dn
294         except Exception, e:
295             raise CommandError("Failed to find account %s" % username, e)
296
297         # check if its a computer account
298         try:
299             msg = self.samdb.search(base=user_dn, scope=ldb.SCOPE_BASE, attrs=['objectClass'])[0]
300             is_computer = 'computer' in msg['objectClass']
301         except Exception, e:
302             raise CommandError("Failed to find objectClass for user %s" % username, e)
303
304         session_info_flags = ( AUTH_SESSION_INFO_DEFAULT_GROUPS |
305                                AUTH_SESSION_INFO_AUTHENTICATED )
306
307         # When connecting to a remote server, don't look up the local privilege DB
308         if self.url is not None and self.url.startswith('ldap'):
309             session_info_flags |= AUTH_SESSION_INFO_SIMPLE_PRIVILEGES
310
311         session = samba.auth.user_session(self.samdb, lp_ctx=self.lp, dn=user_dn,
312                                           session_info_flags=session_info_flags)
313
314         token = session.security_token
315
316         gpos = []
317
318         inherit = True
319         dn = ldb.Dn(self.samdb, str(user_dn)).parent()
320         while True:
321             msg = self.samdb.search(base=dn, scope=ldb.SCOPE_BASE, attrs=['gPLink', 'gPOptions'])[0]
322             if 'gPLink' in msg:
323                 glist = parse_gplink(msg['gPLink'][0])
324                 for g in glist:
325                     if not inherit and not (g['options'] & dsdb.GPLINK_OPT_ENFORCE):
326                         continue
327                     if g['options'] & dsdb.GPLINK_OPT_DISABLE:
328                         continue
329
330                     try:
331                         gmsg = self.samdb.search(base=g['dn'], scope=ldb.SCOPE_BASE,
332                                                  attrs=['name', 'displayName', 'flags',
333                                                         'ntSecurityDescriptor'])
334                     except Exception:
335                         print("Failed to fetch gpo object %s" % g['dn'])
336                         continue
337
338                     secdesc_ndr = gmsg[0]['ntSecurityDescriptor'][0]
339                     secdesc = ndr_unpack(dcerpc.security.descriptor, secdesc_ndr)
340
341                     try:
342                         samba.security.access_check(secdesc, token,
343                                                     dcerpc.security.SEC_STD_READ_CONTROL |
344                                                     dcerpc.security.SEC_ADS_LIST |
345                                                     dcerpc.security.SEC_ADS_READ_PROP)
346                     except RuntimeError:
347                         print("Failed access check on %s" % msg.dn)
348                         continue
349
350                     # check the flags on the GPO
351                     flags = int(attr_default(gmsg[0], 'flags', 0))
352                     if is_computer and (flags & dsdb.GPO_FLAG_MACHINE_DISABLE):
353                         continue
354                     if not is_computer and (flags & dsdb.GPO_FLAG_USER_DISABLE):
355                         continue
356                     gpos.append((gmsg[0]['displayName'][0], gmsg[0]['name'][0]))
357
358             # check if this blocks inheritance
359             gpoptions = int(attr_default(msg, 'gPOptions', 0))
360             if gpoptions & dsdb.GPO_BLOCK_INHERITANCE:
361                 inherit = False
362
363             if dn == self.samdb.get_default_basedn():
364                 break
365             dn = dn.parent()
366
367         if is_computer:
368             msg_str = 'computer'
369         else:
370             msg_str = 'user'
371
372         print("GPOs for %s %s" % (msg_str, username))
373         for g in gpos:
374             print("    %s %s" % (g[0], g[1]))
375
376
377 class cmd_show(Command):
378     """Show information for a GPO"""
379
380     synopsis = "%prog gpo show <gpo> [options]"
381
382     takes_optiongroups = {
383         "sambaopts": options.SambaOptions,
384         "versionopts": options.VersionOptions,
385         "credopts": options.CredentialsOptions,
386     }
387
388     takes_args = [ 'gpo' ]
389
390     takes_options = [
391         Option("-H", help="LDB URL for database or target server", type=str)
392         ]
393
394     def run(self, gpo, H=None, sambaopts=None, credopts=None, versionopts=None):
395
396         self.lp = sambaopts.get_loadparm()
397         self.creds = credopts.get_credentials(self.lp, fallback_machine=True)
398
399         self.url = dc_url(self.lp, self.creds, H)
400
401         samdb_connect(self)
402
403         try:
404             msg = get_gpo_info(self.samdb, gpo)[0]
405         except Exception, e:
406             raise CommandError("GPO %s does not exist" % gpo, e)
407
408         secdesc_ndr = msg['ntSecurityDescriptor'][0]
409         secdesc = ndr_unpack(dcerpc.security.descriptor, secdesc_ndr)
410
411         print("GPO          : %s" % msg['name'][0])
412         print("display name : %s" % msg['displayName'][0])
413         print("path         : %s" % msg['gPCFileSysPath'][0])
414         print("dn           : %s" % msg.dn)
415         print("version      : %s" % attr_default(msg, 'versionNumber', '0'))
416         print("flags        : %s" % gpo_flags_string(int(attr_default(msg, 'flags', 0))))
417         print("ACL          : %s" % secdesc.as_sddl())
418         print("")
419
420
421 class cmd_getlink(Command):
422     """List GPO Links for a container"""
423
424     synopsis = "%prog gpo getlink <container_dn> [options]"
425
426     takes_optiongroups = {
427         "sambaopts": options.SambaOptions,
428         "versionopts": options.VersionOptions,
429         "credopts": options.CredentialsOptions,
430     }
431
432     takes_args = [ 'container_dn' ]
433
434     takes_options = [
435         Option("-H", help="LDB URL for database or target server", type=str)
436         ]
437
438     def run(self, container_dn, H=None, sambaopts=None, credopts=None,
439                 versionopts=None):
440
441         self.lp = sambaopts.get_loadparm()
442         self.creds = credopts.get_credentials(self.lp, fallback_machine=True)
443
444         self.url = dc_url(self.lp, self.creds, H)
445
446         samdb_connect(self)
447
448         try:
449             msg = self.samdb.search(base=container_dn, scope=ldb.SCOPE_BASE,
450                                     expression="(objectClass=*)",
451                                     attrs=['gPlink'])[0]
452         except Exception, e:
453             raise CommandError("Could not find Container DN %s (%s)" % container_dn, e)
454
455         if 'gPLink' in msg:
456             print("GPO(s) linked to DN %s" % container_dn)
457             gplist = parse_gplink(msg['gPLink'][0])
458             for g in gplist:
459                 msg = get_gpo_info(self.samdb, dn=g['dn'])
460                 print("    GPO     : %s" % msg[0]['name'][0])
461                 print("    Name    : %s" % msg[0]['displayName'][0])
462                 print("    Options : %s" % gplink_options_string(g['options']))
463                 print("")
464         else:
465             print("No GPO(s) linked to DN=%s" % container_dn)
466
467
468 class cmd_setlink(Command):
469     """Add or Update a GPO link to a container"""
470
471     synopsis = "%prog gpo setlink <container_dn> <gpo> [options]"
472
473     takes_optiongroups = {
474         "sambaopts": options.SambaOptions,
475         "versionopts": options.VersionOptions,
476         "credopts": options.CredentialsOptions,
477     }
478
479     takes_args = [ 'container_dn', 'gpo' ]
480
481     takes_options = [
482         Option("-H", help="LDB URL for database or target server", type=str),
483         Option("--disable", dest="disabled", default=False, action='store_true',
484             help="Disable policy"),
485         Option("--enforce", dest="enforced", default=False, action='store_true',
486             help="Enforce policy")
487         ]
488
489     def run(self, container_dn, gpo, H=None, disabled=False, enforced=False,
490                 sambaopts=None, credopts=None, versionopts=None):
491
492         self.lp = sambaopts.get_loadparm()
493         self.creds = credopts.get_credentials(self.lp, fallback_machine=True)
494
495         self.url = dc_url(self.lp, self.creds, H)
496
497         samdb_connect(self)
498
499         gplink_options = 0
500         if disabled:
501             gplink_options |= dsdb.GPLINK_OPT_DISABLE
502         if enforced:
503             gplink_options |= dsdb.GPLINK_OPT_ENFORCE
504
505         # Check if valid GPO DN
506         try:
507             msg = get_gpo_info(self.samdb, gpo=gpo)[0]
508         except Exception, e:
509             raise CommandError("GPO %s does not exist" % gpo_dn, e)
510         gpo_dn = get_gpo_dn(self.samdb, gpo)
511
512         # Check if valid Container DN
513         try:
514             msg = self.samdb.search(base=container_dn, scope=ldb.SCOPE_BASE,
515                                     expression="(objectClass=*)",
516                                     attrs=['gPlink'])[0]
517         except Exception, e:
518             raise CommandError("Could not find container DN %s" % container_dn, e)
519
520         # Update existing GPlinks or Add new one
521         existing_gplink = False
522         if 'gPLink' in msg:
523             gplist = parse_gplink(msg['gPLink'][0])
524             existing_gplink = True
525             found = False
526             for g in gplist:
527                 if g['dn'].lower() == gpo_dn.lower():
528                     g['options'] = gplink_options
529                     found = True
530                     break
531             if not found:
532                 gplist.insert(0, { 'dn' : gpo_dn, 'options' : gplink_options })
533         else:
534             gplist = []
535             gplist.append({ 'dn' : gpo_dn, 'options' : gplink_options })
536
537         gplink_str = encode_gplink(gplist)
538
539         m = ldb.Message()
540         m.dn = ldb.Dn(self.samdb, container_dn)
541
542         if existing_gplink:
543             m['new_value'] = ldb.MessageElement(gplink_str, ldb.FLAG_MOD_REPLACE, 'gPLink')
544         else:
545             m['new_value'] = ldb.MessageElement(gplink_str, ldb.FLAG_MOD_ADD, 'gPLink')
546
547         try:
548             self.samdb.modify(m)
549         except Exception, e:
550             raise CommandError("Error adding GPO Link", e)
551
552         print("Added/Updated GPO link")
553         cmd_getlink().run(container_dn, H, sambaopts, credopts, versionopts)
554
555
556 class cmd_dellink(Command):
557     """Delete GPO link from a container"""
558
559     synopsis = "%prog gpo dellink <container_dn> <gpo> [options]"
560
561     takes_optiongroups = {
562         "sambaopts": options.SambaOptions,
563         "versionopts": options.VersionOptions,
564         "credopts": options.CredentialsOptions,
565     }
566
567     takes_args = [ 'container_dn', 'gpo' ]
568
569     takes_options = [
570         Option("-H", help="LDB URL for database or target server", type=str),
571         ]
572
573     def run(self, container_dn, gpo_dn, H=None, sambaopts=None, credopts=None,
574                 versionopts=None):
575
576         self.lp = sambaopts.get_loadparm()
577         self.creds = credopts.get_credentials(self.lp, fallback_machine=True)
578
579         self.url = dc_url(self.lp, self.creds, H)
580
581         samdb_connect(self)
582
583         # Check if valid GPO
584         try:
585             msg = get_gpo_info(self.sambdb, gpo=gpo)[0]
586         except Exception, e:
587                 raise CommandError("GPO %s does not exist" % gpo, e)
588         gpo_dn = get_gpo_dn(self.samdb, gpo)
589
590         # Check if valid Container DN and get existing GPlinks
591         try:
592             msg = self.samdb.search(base=container_dn, scope=ldb.SCOPE_BASE,
593                                     expression="(objectClass=*)",
594                                     attrs=['gPlink'])[0]
595         except Exception, e:
596             raise CommandError("Could not find container DN %s" % dn, e)
597
598         if 'gPLink' in msg:
599             gplist = parse_gplink(msg['gPLink'][0])
600             for g in gplist:
601                 if g['dn'].lower() == gpo_dn.lower():
602                     gplist.remove(g)
603                     break
604         else:
605             raise CommandError("Specified GPO is not linked to this container");
606
607         m = ldb.Message()
608         m.dn = ldb.Dn(self.samdb, container_dn)
609
610         if gplist:
611             gplink_str = encode_gplink(gplist)
612             m['new_value'] = ldb.MessageElement(gplink_str, ldb.FLAG_MOD_REPLACE, 'gPLink')
613         else:
614             m['new_value'] = ldb.MessageElement('', ldb.FLAG_MOD_DELETE, 'gPLink')
615
616         try:
617             self.samdb.modify(m)
618         except Exception, e:
619             raise CommandError("Error Removing GPO Link (%s)" % e)
620
621         print("Deleted GPO link.")
622         cmd_getlink().run(container_dn, H, sambaopts, credopts, versionopts)
623
624
625 class cmd_getinheritance(Command):
626     """Get inheritance flag for a container"""
627
628     synopsis = "%prog gpo getinheritance <container_dn> [options]"
629
630     takes_optiongroups = {
631         "sambaopts": options.SambaOptions,
632         "versionopts": options.VersionOptions,
633         "credopts": options.CredentialsOptions,
634     }
635
636     takes_args = [ 'container_dn' ]
637
638     takes_options = [
639         Option("-H", help="LDB URL for database or target server", type=str)
640         ]
641
642     def run(self, container_dn, H=None, sambaopts=None, credopts=None,
643                 versionopts=None):
644
645         self.url = H
646         self.lp = sambaopts.get_loadparm()
647
648         self.creds = credopts.get_credentials(self.lp, fallback_machine=True)
649
650         samdb_connect(self)
651
652         try:
653             msg = self.samdb.search(base=container_dn, scope=ldb.SCOPE_BASE,
654                                     expression="(objectClass=*)",
655                                     attrs=['gPOptions'])[0]
656         except Exception, e:
657             raise CommandError("Could not find Container DN %s" % container_dn, e)
658
659         inheritance = 0
660         if 'gPOptions' in msg:
661             inheritance = int(msg['gPOptions'][0]);
662
663         if inheritance == dsdb.GPO_BLOCK_INHERITANCE:
664             print("Container has GPO_BLOCK_INHERITANCE")
665         else:
666             print("Container has GPO_INHERIT")
667
668
669 class cmd_setinheritance(Command):
670     """Set inheritance flag on a container"""
671
672     synopsis = "%prog gpo setinheritance <container_dn> <block|inherit> [options]"
673
674     takes_optiongroups = {
675         "sambaopts": options.SambaOptions,
676         "versionopts": options.VersionOptions,
677         "credopts": options.CredentialsOptions,
678     }
679
680     takes_args = [ 'container_dn', 'inherit_state' ]
681
682     takes_options = [
683         Option("-H", help="LDB URL for database or target server", type=str)
684         ]
685
686     def run(self, container_dn, inherit_state, H=None, sambaopts=None, credopts=None,
687                 versionopts=None):
688
689         if inherit_state.lower() == 'block':
690             inheritance = dsdb.GPO_BLOCK_INHERITANCE
691         elif inherit_state.lower() == 'inherit':
692             inheritance = dsdb.GPO_INHERIT
693         else:
694             raise CommandError("Unknown inheritance state (%s)" % inherit_state)
695
696         self.url = H
697         self.lp = sambaopts.get_loadparm()
698
699         self.creds = credopts.get_credentials(self.lp, fallback_machine=True)
700
701         samdb_connect(self)
702
703         try:
704             msg = self.samdb.search(base=container_dn, scope=ldb.SCOPE_BASE,
705                                     expression="(objectClass=*)",
706                                     attrs=['gPOptions'])[0]
707         except Exception, e:
708             raise CommandError("Could not find Container DN %s" % container_dn, e)
709
710         m = ldb.Message()
711         m.dn = ldb.Dn(self.samdb, container_dn)
712
713         if 'gPOptions' in msg:
714             m['new_value'] = ldb.MessageElement(str(inheritance), ldb.FLAG_MOD_REPLACE, 'gPOptions')
715         else:
716             m['new_value'] = ldb.MessageElement(str(inheritance), ldb.FLAG_MOD_ADD, 'gPOptions');
717
718         try:
719             self.samdb.modify(m)
720         except Exception, e:
721             raise CommandError("Error setting inheritance state %s" % inherit_state, e)
722
723
724 class cmd_fetch(Command):
725     """Download a GPO"""
726
727     synopsis = "%prog gpo fetch <gpo> [options]"
728
729     takes_optiongroups = {
730         "sambaopts": options.SambaOptions,
731         "versionopts": options.VersionOptions,
732         "credopts": options.CredentialsOptions,
733     }
734
735     takes_args = [ 'gpo' ]
736
737     takes_options = [
738         Option("-H", help="LDB URL for database or target server", type=str),
739         Option("--tmpdir", help="Temporary directory for copying policy files", type=str)
740         ]
741
742     def run(self, gpo, H=None, tmpdir=None, sambaopts=None, credopts=None, versionopts=None):
743
744         self.lp = sambaopts.get_loadparm()
745         self.creds = credopts.get_credentials(self.lp, fallback_machine=True)
746
747         dc_hostname = netcmd_finddc(self.lp, self.creds)
748         self.url = dc_url(self.lp, self.creds, H, dc=dc_hostname)
749
750         samdb_connect(self)
751         try:
752             msg = get_gpo_info(self.samdb, gpo)[0]
753         except Exception, e:
754             raise CommandError("GPO %s does not exist" % gpo)
755
756         # verify UNC path
757         unc = msg['gPCFileSysPath'][0]
758         try:
759             [dom_name, service, sharepath] = parse_unc(unc)
760         except:
761             raise CommandError("Invalid GPO path (%s)" % unc)
762
763         # SMB connect to DC
764         try:
765             conn = smb.SMB(dc_hostname, service, lp=self.lp, creds=self.creds)
766         except Exception, e:
767             raise CommandError("Error connecting to '%s' using SMB" % dc_hostname, e)
768
769         # Copy GPT
770         if tmpdir is None:
771             tmpdir = "/tmp"
772         if not os.path.isdir(tmpdir):
773             raise CommandError("Temoprary directory '%s' does not exist" % tmpdir)
774
775         localdir = os.path.join(tmpdir, "policy")
776         if not os.path.isdir(localdir):
777             os.mkdir(localdir)
778
779         gpodir = os.path.join(localdir, gpo)
780         if os.path.isdir(gpodir):
781             raise CommandError("GPO directory '%s' already exists, refusing to overwrite" % gpodir)
782
783         try:
784             os.mkdir(gpodir)
785             copy_directory_remote_to_local(conn, sharepath, gpodir)
786         except Exception, e:
787             raise CommandError("Error copying GPO from DC", e)
788         print('GPO copied to %s' % gpodir)
789
790
791 class cmd_create(Command):
792     """Create an empty GPO"""
793
794     synopsis = "%prog gpo create <displayname> [options]"
795
796     takes_optiongroups = {
797         "sambaopts": options.SambaOptions,
798         "versionopts": options.VersionOptions,
799         "credopts": options.CredentialsOptions,
800     }
801
802     takes_args = [ 'displayname' ]
803
804     takes_options = [
805         Option("-H", help="LDB URL for database or target server", type=str),
806         Option("--tmpdir", help="Temporary directory for copying policy files", type=str)
807         ]
808
809     def run(self, displayname, H=None, tmpdir=None, sambaopts=None, credopts=None, 
810             versionopts=None):
811
812         self.lp = sambaopts.get_loadparm()
813         self.creds = credopts.get_credentials(self.lp, fallback_machine=True)
814
815         self.url = dc_url(self.lp, self.creds, H)
816
817         dc_hostname = netcmd_finddc(self.lp, self.creds)
818         samdb_connect(self)
819
820         msg = get_gpo_info(self.samdb, displayname=displayname)
821         if msg.count > 0:
822             raise CommandError("A GPO already existing with name '%s'" % displayname)
823
824         # Create new GUID
825         guid  = str(uuid.uuid4())
826         gpo = "{%s}" % guid.upper()
827         realm = self.lp.get('realm')
828         unc_path = "\\\\%s\\sysvol\\%s\\Policies\\%s" % (realm, realm, gpo)
829
830         # Create GPT
831         if tmpdir is None:
832             tmpdir = "/tmp"
833         if not os.path.isdir(tmpdir):
834             raise CommandError("Temporary directory '%s' does not exist" % tmpdir)
835
836         localdir = os.path.join(tmpdir, "policy")
837         if not os.path.isdir(localdir):
838             os.mkdir(localdir)
839
840         gpodir = os.path.join(localdir, gpo)
841         if os.path.isdir(gpodir):
842             raise CommandError("GPO directory '%s' already exists, refusing to overwrite" % gpodir)
843
844         try:
845             os.mkdir(gpodir)
846             os.mkdir(os.path.join(gpodir, "Machine"))
847             os.mkdir(os.path.join(gpodir, "User"))
848             gpt_contents = "[General]\r\nVersion=0\r\n"
849             file(os.path.join(gpodir, "GPT.INI"), "w").write(gpt_contents)
850         except Exception, e:
851             raise CommandError("Error Creating GPO files", e) 
852
853         # Add cn=<guid>
854         gpo_dn = self.samdb.get_default_basedn()
855         gpo_dn.add_child(ldb.Dn(self.samdb, "CN=Policies,CN=System"))
856         gpo_dn.add_child(ldb.Dn(self.samdb, "CN=%s" % gpo))
857
858         m = ldb.Message()
859         m.dn = ldb.Dn(self.samdb, gpo_dn.get_linearized())
860         m['a01'] = ldb.MessageElement("groupPolicyContainer", ldb.FLAG_MOD_ADD, "objectClass")
861         m['a02'] = ldb.MessageElement(displayname, ldb.FLAG_MOD_ADD, "displayName")
862         m['a03'] = ldb.MessageElement(unc_path, ldb.FLAG_MOD_ADD, "gPCFileSysPath")
863         m['a04'] = ldb.MessageElement("0", ldb.FLAG_MOD_ADD, "flags")
864         m['a05'] = ldb.MessageElement("0", ldb.FLAG_MOD_ADD, "versionNumber")
865         m['a06'] = ldb.MessageElement("TRUE", ldb.FLAG_MOD_ADD, "showInAdvancedViewOnly")
866         m['a07'] = ldb.MessageElement("2", ldb.FLAG_MOD_ADD, "gpcFunctionalityVersion")
867         try:
868             self.samdb.add(m)
869         except Exception, e:
870             raise CommandError("Error adding GPO in AD", e)
871
872         # Add cn=User,cn=<guid>
873         child_dn = gpo_dn
874         child_dn.add_child(ldb.Dn(self.samdb, "CN=User"))
875
876         m = ldb.Message()
877         m.dn = ldb.Dn(self.samdb, child_dn.get_linearized())
878         m['a01'] = ldb.MessageElement("container", ldb.FLAG_MOD_ADD, "objectClass")
879         m['a02'] = ldb.MessageElement("TRUE", ldb.FLAG_MOD_ADD, "showInAdvancedViewOnly")
880         try:
881             self.samdb.add(m)
882         except Exception, e:
883             raise CommandError("Error adding GPO in AD", e)
884
885         # Add cn=User,cn=<guid>
886         child_dn = gpo_dn
887         child_dn.add_child(ldb.Dn(self.samdb, "CN=Machine"))
888
889         m = ldb.Message()
890         m.dn = ldb.Dn(self.samdb, child_dn.get_linearized())
891         m['a01'] = ldb.MessageElement("container", ldb.FLAG_MOD_ADD, "objectClass")
892         m['a02'] = ldb.MessageElement("TRUE", ldb.FLAG_MOD_ADD, "showInAdvancedViewOnly")
893         try:
894             self.samdb.add(m)
895         except Exception, e:
896             raise CommandError("Error adding GPO in AD", e)
897
898         # Copy GPO files over SMB
899         [dom_name, service, sharepath] = parse_unc(unc_path)
900         try:
901             conn = smb.SMB(dc_hostname, service, lp=self.lp, creds=self.creds)
902         except Exception, e:
903             raise CommandError("Error connecting to '%s' using SMB" % dc_hostname, e)
904
905         try:
906             create_directory_hier(conn, sharepath)
907             copy_directory_local_to_remote(conn, gpodir, sharepath)
908         except Exception, e:
909             raise CommandError("Error Copying GPO to DC", e)
910
911         # Get new security descriptor
912         msg = get_gpo_info(self.samdb, gpo=gpo)[0]
913         ds_sd_ndr = msg['ntSecurityDescriptor'][0]
914         ds_sd = ndr_unpack(dcerpc.security.descriptor, ds_sd_ndr)
915
916         # Create a file system security descriptor
917         fs_sd = dcerpc.security.descriptor()
918         fs_sd.owner_sid = ds_sd.owner_sid
919         fs_sd.group_sid = ds_sd.group_sid
920         fs_sd.type = ds_sd.type
921         fs_sd.revision = ds_sd.revision
922
923         # Copy sacl
924         fs_sd.sacl = ds_sd.sacl
925
926         # Copy dacl
927         dacl = ds_sd.dacl
928         for ace in dacl.aces:
929             # Don't add the allow for SID_BUILTIN_PREW2K
930             if (not (ace.type & dcerpc.security.SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT) and 
931                     ace.trustee == dcerpc.security.SID_BUILTIN_PREW2K):
932                 continue
933
934             # Copy the ace from the directory server security descriptor
935             new_ace = ace
936
937             # Set specific inheritance flags for within the GPO
938             new_ace.flags |= (dcerpc.security.SEC_ACE_FLAG_OBJECT_INHERIT |
939                              dcerpc.security.SEC_ACE_FLAG_CONTAINER_INHERIT)
940             if ace.trustee == dcerpc.security.SID_CREATOR_OWNER:
941                 new_ace.flags |= dcerpc.security.SEC_ACE_FLAG_INHERIT_ONLY
942
943             # Get a directory access mask from the assigned access mask on the ldap object
944             new_ace.access_mask = policy.ads_to_dir_access_mask(ace.access_mask)
945
946             # Add the ace to DACL
947             fs_sd.dacl_add(new_ace)
948
949         # Set ACL
950         try:
951             conn.set_acl(sharepath, fs_sd)
952         except Exception, e:
953             raise CommandError("Error setting ACL on GPT", e)
954
955         print "GPO '%s' created as %s" % (displayname, gpo)
956
957
958 class cmd_setacl(Command):
959     """Set ACL on a GPO"""
960
961
962 class cmd_gpo(SuperCommand):
963     """Group Policy Object (GPO) commands"""
964
965     subcommands = {}
966     subcommands["listall"] = cmd_listall()
967     subcommands["list"] = cmd_list()
968     subcommands["show"] = cmd_show()
969     subcommands["getlink"] = cmd_getlink()
970     subcommands["setlink"] = cmd_setlink()
971     subcommands["dellink"] = cmd_dellink()
972     subcommands["getinheritance"] = cmd_getinheritance()
973     subcommands["setinheritance"] = cmd_setinheritance()
974     subcommands["fetch"] = cmd_fetch()
975     subcommands["create"] = cmd_create()
976     subcommands["setacl"] = cmd_setacl()