~lefteris-nikoltsios/+junk/samba-lp1016895

« back to all changes in this revision

Viewing changes to source4/scripting/python/samba/netcmd/newuser.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2011-12-21 13:18:04 UTC
  • mfrom: (0.39.21 sid)
  • Revision ID: package-import@ubuntu.com-20111221131804-xtlr39wx6njehxxr
Tags: 2:3.6.1-3ubuntu1
* Merge from Debian testing.  Remaining changes:
  + debian/patches/VERSION.patch:
    - set SAMBA_VERSION_SUFFIX to Ubuntu.
  + debian/patches/error-trans.fix-276472:
    - Add the translation of Unix Error code -ENOTSUP to NT Error Code
    - NT_STATUS_NOT_SUPPORTED to prevent the Permission denied error.
  + debian/smb.conf:
    - add "(Samba, Ubuntu)" to server string.
    - comment out the default [homes] share, and add a comment about
      "valid users = %S" to show users how to restrict access to
      \\server\username to only username.
    - Set 'usershare allow guests', so that usershare admins are 
      allowed to create public shares in addition to authenticated
      ones.
    - add map to guest = Bad user, maps bad username to guest access.
  + debian/samba-common.config:
    - Do not change priority to high if dhclient3 is installed.
    - Use priority medium instead of high for the workgroup question.
  + debian/control:
    - Don't build against or suggest ctdb.
    - Add dependency on samba-common-bin to samba.
  + Add ufw integration:
    - Created debian/samba.ufw.profile
    - debian/rules, debian/samba.dirs, debian/samba.files: install
      profile
    - debian/control: have samba suggest ufw
  + Add apport hook:
    - Created debian/source_samba.py.
    - debian/rules, debian/samba.dirs, debian/samba-common-bin.files: install
  + Switch to upstart:
    - Add debian/samba.{nmbd,smbd}.upstart.
  + debian/samba.logrotate, debian/samba-common.dhcp, debian/samba.if-up:
    - Make them upstart compatible
  + debian/samba.postinst: 
    - Avoid scary pdbedit warnings on first import.
  + debian/samba-common.postinst: Add more informative error message for
    the case where smb.conf was manually deleted
  + debian/patches/fix-debuglevel-name-conflict.patch: don't use 'debug_level'
    as a global variable name in an NSS module 
  + Dropped:
    - debian/patches/error-trans.fix-276472
    - debian/patches/fix-debuglevel-name-conflict.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#
 
3
# Adds a new user to a Samba4 server
 
4
# Copyright Jelmer Vernooij 2008
 
5
#
 
6
# Based on the original in EJS:
 
7
# Copyright Andrew Tridgell 2005
 
8
#
 
9
# This program is free software; you can redistribute it and/or modify
 
10
# it under the terms of the GNU General Public License as published by
 
11
# the Free Software Foundation; either version 3 of the License, or
 
12
# (at your option) any later version.
 
13
#
 
14
# This program is distributed in the hope that it will be useful,
 
15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
# GNU General Public License for more details.
 
18
#
 
19
# You should have received a copy of the GNU General Public License
 
20
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
21
 
 
22
import samba.getopt as options
 
23
from samba.netcmd import Command, CommandError, Option
 
24
import ldb
 
25
 
 
26
from getpass import getpass
 
27
from samba.auth import system_session
 
28
from samba.samdb import SamDB
 
29
 
 
30
class cmd_newuser(Command):
 
31
    """Creates a new user"""
 
32
 
 
33
    synopsis = "newuser [options] <username> [<password>]"
 
34
 
 
35
    takes_optiongroups = {
 
36
        "sambaopts": options.SambaOptions,
 
37
        "versionopts": options.VersionOptions,
 
38
        "credopts": options.CredentialsOptions,
 
39
    }
 
40
 
 
41
    takes_options = [
 
42
        Option("-H", help="LDB URL for database or target server", type=str),
 
43
        Option("--must-change-at-next-login",
 
44
            help="Force password to be changed on next login",
 
45
            action="store_true"),
 
46
        Option("--use-username-as-cn",
 
47
            help="Force use of username as user's CN",
 
48
            action="store_true"),
 
49
        Option("--userou",
 
50
           help="Alternative location (without domainDN counterpart) to default CN=Users in which new user object will be created",
 
51
           type=str),
 
52
        Option("--surname", help="User's surname", type=str),
 
53
        Option("--given-name", help="User's given name", type=str),
 
54
        Option("--initials", help="User's initials", type=str),
 
55
        Option("--profile-path", help="User's profile path", type=str),
 
56
        Option("--script-path", help="User's logon script path", type=str),
 
57
        Option("--home-drive", help="User's home drive letter", type=str),
 
58
        Option("--home-directory", help="User's home directory path", type=str),
 
59
        Option("--job-title", help="User's job title", type=str),
 
60
        Option("--department", help="User's department", type=str),
 
61
        Option("--company", help="User's company", type=str),
 
62
        Option("--description", help="User's description", type=str),
 
63
        Option("--mail-address", help="User's email address", type=str),
 
64
        Option("--internet-address", help="User's home page", type=str),
 
65
        Option("--telephone-number", help="User's phone number", type=str),
 
66
        Option("--physical-delivery-office", help="User's office location", type=str),
 
67
    ]
 
68
 
 
69
    takes_args = ["username", "password?"]
 
70
 
 
71
    def run(self, username, password=None, credopts=None, sambaopts=None,
 
72
            versionopts=None, H=None, must_change_at_next_login=None,
 
73
            use_username_as_cn=None, userou=None, surname=None, given_name=None, initials=None,
 
74
            profile_path=None, script_path=None, home_drive=None, home_directory=None,
 
75
            job_title=None, department=None, company=None, description=None,
 
76
            mail_address=None, internet_address=None, telephone_number=None, physical_delivery_office=None):
 
77
 
 
78
        if password is None:
 
79
            password = getpass("New Password: ")
 
80
 
 
81
        lp = sambaopts.get_loadparm()
 
82
        creds = credopts.get_credentials(lp)
 
83
 
 
84
        try:
 
85
            samdb = SamDB(url=H, session_info=system_session(),
 
86
                          credentials=creds, lp=lp)
 
87
            samdb.newuser(username, password,
 
88
                          force_password_change_at_next_login_req=must_change_at_next_login,
 
89
                          useusernameascn=use_username_as_cn, userou=userou, surname=surname, givenname=given_name, initials=initials,
 
90
                          profilepath=profile_path, homedrive=home_drive, scriptpath=script_path, homedirectory=home_directory,
 
91
                          jobtitle=job_title, department=department, company=company, description=description,
 
92
                          mailaddress=mail_address, internetaddress=internet_address,
 
93
                          telephonenumber=telephone_number, physicaldeliveryoffice=physical_delivery_office)
 
94
        except Exception, e:
 
95
            raise CommandError('Failed to create user "%s"' % username, e)
 
96
 
 
97
        print("User %s created successfully" % username)