<<

Samba 4 Python Scripting

Jelmer Vernooij

Why Python?

Changes in the last year

Some trivial Samba 4 Python Scripting examples Demo

Creating bindings Jelmer Vernooij Future

Samba Team

April 18, 2008 Samba 4 Python Agenda Scripting Jelmer Vernooij

Why Python? Why Python? Changes in the last year

Some trivial Changes in the last year examples Demo

Creating bindings Some trivial examples Future

Demo

Creating bindings

Future Samba 4 Python Why scripting? Scripting Jelmer Vernooij

Why Python?

Changes in the last year

Some trivial examples

Demo

I Quicker development Creating bindings I Easier to understand Future

I Easy to use for system administrators I Lower barrier for contributions? Samba 4 Python History of Samba and scripting Scripting Jelmer Vernooij

Why Python?

Changes in the last year

Some trivial examples

I Samba 3.0 had python bindings Demo Creating bindings I Removed because of lack of maintainance Future I Not used for any core infrastructure

I Samba 4 has embedded JavaScript

I Originally used for SWAT, provisioning Samba 4 Python Why Python? Scripting Jelmer Vernooij

Why Python?

Changes in the last year

Some trivial examples I Replaces EJS as internal scripting language Demo I Python is now a build-dependency Creating bindings I Easy to build from source, no dependencies Future I Ported to at least as much platforms as Samba I Available as standard package for most platforms

I The longer we would’ve waited, the more code we’d have to port Samba 4 Python Why Python? Scripting Jelmer Vernooij

Why Python? I Comes “with batteries included” Changes in the I No need to reimplement utility functions and bindings last year for Samba Some trivial examples

I Easy to create bindings Demo I Most existing libraries already have Python bindings Creating bindings Future I GTK+, Qt, HTTP, .ini-parsers...

I Large existing developer base

I Potential contributors

I Better scripting language

I Nested functions I Modularity

I More development tools available

I Debugger, profiler, code coverage analyser, ... Samba 4 Python What exactly is Python? Scripting Jelmer Vernooij

Why Python? I High-level general Changes in the purpose scripting last year Some trivial language examples Demo I Object-oriented, structured or functional Creating bindings Future programming

I Memory-managed, reference-counted

I Standardsized, several implementations

I Portable

I Created in ’91 Samba 4 Python What does it look like? Scripting Jelmer Vernooij

Why Python?

Changes in the Hello world! last year

Some trivial p ri n t ” Hello World ! ” examples

Demo Function usage Creating bindings Future def addone(value ): return value + 1

Using modules import u n i t t e s t Samba 4 Python Some interesting software in Python Scripting Jelmer Vernooij

Written in Python: Why Python? Changes in the I Original BitTorrent last year Mailman Some trivial I examples I Bazaar/Mercurial Demo Creating bindings I Trac Future I Some apps in GNOME/KDE Scriptable in Python

I Blender

I Amarok, Rhythmbox

I Vim

I Totem

I Epiphany Samba 4 Python Python in Samba 4! Scripting Jelmer Vernooij

Why Python?

Changes in the last year

Some trivial examples I pidl can now generate Python bindings Demo Creating bindings I SWIG used for binding several other libraries Future I some bindings written manually I now used instead of EJS in a lot of places

I EJS still there but may be removed at a later point Samba 4 Python Existing Samba Python bindings Scripting Jelmer Vernooij

Why Python?

I Credentials Changes in the last year I SamDB Some trivial I Most DCE/RPC modules examples Demo I LDB Creating bindings I TDB Future

I Registry

I Libnet

I Includes matching unit tests, so all bindings should work

I Bindings should be Pythonic rather than one-on-one wrappers of functions Samba 4 Python Infrastructure in Python Scripting Jelmer Vernooij

Why Python?

Changes in the last year I Provisioning Some trivial examples

I smbclient? Demo

I SoC student will work on this Creating bindings

I Proof-of-concept will be interesting, may not be ideal Future as actual implementation

I SWAT

I SoC student will work on this hopefully

I Samba-GTK

I SoC student will work on this Samba 4 Python Reading TDB files Scripting Jelmer Vernooij

Why Python?

Changes in the last year

Some trivial 1 import tdb , sys examples 2 Demo 3 db = tdb.Tdb(sys.argv[1]) Creating bindings 4 for ( k , v ) in db.items(): Future 5 p ri n t ” { ” 6 p ri n t ” key(%d ) = %r” % (len(k), k) 7 p ri n t ” data(%d ) = %r” % (len(v), v) 8 p ri n t ” } ” Samba 4 Python Using LDB Scripting Jelmer Vernooij

Why Python?

Changes in the last year 1 #!/usr/bin/python Some trivial 2 examples 3 import ldb Demo 4 Creating bindings 5 conn = ldb.Ldb(”msg.tdb”) Future 6 7 conn . add ({ ” dn ” : ” dc=samba , dc=org ” , ” a t t r 1 ” : ” foo ” }) 8 9 for msg in conn . search ( ” dc=samba , dc=org ” ) : 10 p ri n t str (msg.dn) Samba 4 Python Connecting to LDAP using LDB Scripting Jelmer Vernooij

Why Python?

Changes in the last year 1 #!/usr/bin/python Some trivial 2 examples Demo 3 import ldb Creating bindings

4 Future 5 # Connect to the LDAP server 6 conn = ldb . Ldb ( ” ldap : / / ldap . abmas . org / ” ) 7 8 for msg in conn . search ( ” dc=samba , dc=org ” ) : 9 p ri n t str (msg.dn) Samba 4 Python Browsing the registry Scripting Jelmer Vernooij

Why Python?

Changes in the last year

Some trivial examples

Demo

Creating bindings

1 #!/usr/bin/python Future Samba 4 Python Adding users Scripting Jelmer Vernooij

1 #!/usr/bin/python Why Python? 2 import samr , l s a Changes in the last year 3 Some trivial 4 # Connect to the local SAM examples 5 conn = samr.samr(”ncalrpc:”, ”st/dc/etc/smb.conf”)Demo

6 Creating bindings

7 # Get SAMR connect handle Future 8 samr handle = conn.Connect(0, 0xfffffff ) 9 10 domainname = lsa.String() 11 domainname. string = u”SAMBADOMAIN” 12 13 sid = conn.LookupDomain(samr handle , domainname) 14 p ri n t ” Found s i d %s f o r SAMBADOMAIN” % s i d 15 16 conn . Close ( samr handle ) Samba 4 Python Unit tests Scripting Jelmer Vernooij

Why Python?

Changes in the last year 1 import winreg Some trivial 2 from samba . t e s t s import RpcInterfaceTestCase examples 3 Demo

4 class WinregTests(RpcInterfaceTestCase ): Creating bindings

5 def setUp(self ): Future 6 self.conn = winreg.winreg(”ncalrpc:”, self.get loadparm(), self.get credentials ()) 7 8 def t e s t hklm(self ): 9 handle = self.conn.OpenHKLM(None, 10 winreg .KEY QUERY VALUE | winreg .KEY ENUMERATE SUB KEYS) 11 self.conn.CloseKey(handle) Samba 4 Python Demo Scripting Demo Time Jelmer Vernooij Why Python?

Changes in the last year

Some trivial examples

Demo

Creating bindings

Future Samba 4 Python Creating bindings Scripting Jelmer Vernooij

Why Python?

Changes in the last year

Some trivial examples

Demo

Creating bindings I We use SWIG, http://www.swig.org/. Future I Hard to grasp language but very powerful Samba 4 Python Missing bindings Scripting Jelmer Vernooij

Why Python?

Changes in the last year

Some trivial examples I NetBIOS Demo Creating bindings I SMB Future I WINS

I CLDAP

I LDAP Samba 4 Python Where to get? Scripting Jelmer Vernooij

Why Python?

Changes in the last year I ldb Some trivial examples I http://ldb.samba.org/ Demo I python-ldb in Debian/Ubuntu Creating bindings

I tdb Future I http://tdb.samba.org/ I python-tdb in Debian/Ubuntu

I . . . others:

I Samba 4 I . . . not packaged yet, but hopefully soon Samba 4 Python Learning more Scripting Jelmer Vernooij

Why Python?

Changes in the last year

Some trivial examples

Demo I www.python.org Creating bindings

I pydoc < name > Future I pydoctor

I Maybe public API docs on Samba.org? Samba 4 Python Future expansions Scripting Jelmer Vernooij

Why Python?

Changes in the last year

Some trivial examples

Demo I Port to Samba 3? Creating bindings

I Need to make sure it stays maintained Future

I Help welcome :-)