def change_password(userid, pswd):
  ret, rc = command.execute_with_conf_and_rc(r'smbpasswd -s %s'%(userid), "%s\n%s"%(pswd, pswd))
  if rc != 0:
    print ret
    #print command.get_error_list(ret)
    raise Exception("Error changing password. Return code : %d. "%rc)
  ul = command.get_output_list(ret)
def create_local_user(userid, name, pswd):

  error_list = []
  #First check if samba user exists. if so kick out
  ul = get_local_users()
  if ul:
    for ud in ul:
      if ud["userid"] == userid:
        raise Exception("Error creating user. The user \"%s\" already exists. "%userid)

  # Now check if system user exists. If not create..
  create_system_user = False
  try:
    pwd.getpwnam(userid)
  except KeyError:
    create_system_user = True

  if create_system_user:
    #enc_pswd = crypt.crypt(pswd, "28")
    #Set a standard system password - not the one given by the user as the user should not have access to the system
    enc_pswd = crypt.crypt("integralstor_pswd_%s"%userid, "28")
    client = salt.client.LocalClient()
    rc = client.cmd('*', 'user.add', [userid,None,501])
    for hostname, status in rc.items():
      if not status:
        error_list.append("Error creating the userid on GRIDCell %s"%hostname)
    rc = client.cmd('*', 'shadow.set_password', [userid, enc_pswd] )
    for hostname, status in rc.items():
      if not status:
        error_list.append("Error setting the password for userid on GRIDCell %s"%hostname)
    rc = client.cmd('*', 'user.chfullname', [userid, "integralstor_user_%s"%name] )
    for hostname, status in rc.items():
      if not status:
        error_list.append("Error setting the name for userid on GRIDCell %s"%hostname)
    '''
    ret, rc = command.execute_with_rc(r'useradd -p %s -c integralstor_user_%s %s'%(enc_pswd, name, userid))
    if rc != 0:
      raise Exception("Error creating system user. Return code : %d. "%rc)
    '''

  print '/usr/bin/pdbedit  -d 1 -t -a  -u %s -f %s'%(userid, name), "%s\n%s"%(pswd, pswd)
  # Now all set to create samba user
  ret, rc = command.execute_with_conf_and_rc(r'/usr/bin/pdbedit  -d 1 -t -a  -u %s -f %s'%(userid, name), "%s\n%s"%(pswd, pswd))
  if rc != 0:
    #print command.get_error_list(ret)
    raise Exception("Error creating user. Return code : %d. "%rc)
  ul = command.get_output_list(ret)
  #print ul
  return error_list
def change_password(username, pswd):
  try:
    if not username:
      raise Exception('No username specified')
    d, err = get_local_user(username)
    if not d:
      if err:
        raise Exception('Error locating user : %s'%err)
      else:
        raise Exception('Error locating user')
    lines, err = command.get_command_output(r'echo %s:%s|chpasswd'%(username, pswd), shell=True)
    if err:
      raise Exception(err)

    if 'smb_user' in d and d['smb_user']:
      #lines, err = command.get_command_output(r'smbpasswd -s %s'%(username), "%s\n%s"%(pswd, pswd))
      (ret, rc), err = command.execute_with_conf_and_rc(r'smbpasswd -s %s'%(username), "%s\n%s"%(pswd, pswd))
      if rc == 0:
        lines, er = command.get_output_list(ret)
        if er:
          raise Exception(er)
      else:
        err = ''
        tl, er = command.get_output_list(ret)
        if er:
          raise Exception(er)
        if tl:
          err = ','.join(tl)
        tl, er = command.get_error_list(ret)
        if er:
          raise Exception(er)
        if tl:
          err = err + ','.join(tl)
        raise Exception(err)
  except Exception, e:
    return False, 'Error changing local user password : %s'%str(e)
def change_password(username, pswd):
  try:
    if not username:
      raise Exception('No username specified')
    d, err = get_local_user(username)
    if not d:
      if err:
        raise Exception('Error locating user : %s'%err)
      else:
        raise Exception('Error locating user')
    lines, err = command.get_command_output(r'echo \"%s:%s\"|chpasswd'%(username, pswd))
    if err:
      raise Exception(err)

    if 'smb_user' in d and d['smb_user']:
      #lines, err = command.get_command_output(r'smbpasswd -s %s'%(username), "%s\n%s"%(pswd, pswd))
      (ret, rc), err = command.execute_with_conf_and_rc(r'smbpasswd -s %s'%(username), "%s\n%s"%(pswd, pswd))
      if rc == 0:
        lines, er = command.get_output_list(ret)
        if er:
          raise Exception(er)
      else:
        err = ''
        tl, er = command.get_output_list(ret)
        if er:
          raise Exception(er)
        if tl:
          err = ','.join(tl)
        tl, er = command.get_error_list(ret)
        if er:
          raise Exception(er)
        if tl:
          err = err + ','.join(tl)
        raise Exception(err)
  except Exception, e:
    return False, 'Error changing local user password : %s'%str(e)
def create_local_user(username, name, pswd, gid = None, smb_user=True):

  try:

    #First check if user exists. if so kick out
    ul, err = get_local_users()
    if ul:
      for ud in ul:
        if ud["username"] == username:
          raise Exception("Error creating user. The user \"%s\" already exists. "%username)
    elif err:
      raise Exception("Error retrieving user list : %s"%err)

  
    enc_pswd = crypt.crypt(pswd, "28")
    use_salt, err = common.use_salt()
    if err:
      raise Exception(err)
    if use_salt:
      client = salt.client.LocalClient()
      if gid:
        rc = client.cmd('*', 'user.add', [username, None, gid])
      else:
        rc = client.cmd('*', 'user.add', [username])
      #print rc
      if not rc:
        error_list.append("Error creating the username")
      for hostname, status in rc.items():
        if not status:
          error_list.append("Error creating the username on node"%hostname)
      rc = client.cmd('*', 'shadow.set_password', [username, enc_pswd] )
      for hostname, status in rc.items():
        if not status:
          error_list.append("Error setting the password for username on GRIDCell %s"%hostname)
      rc = client.cmd('*', 'user.chfullname', [username, "integralstor_user_%s"%name] )
      for hostname, status in rc.items():
        if not status:
          error_list.append("Error setting the name for username on node %s"%hostname)
    else:
      #print '1'
      if gid:
        cmd_to_run = 'useradd -g %s -p %s -c integralstor_user_%s %s'%(gid, enc_pswd, name, username)
      else:
        cmd_to_run = 'useradd  -p %s -c integralstor_user_%s %s'%(enc_pswd, name, username)
      lines, err = command.get_command_output(cmd_to_run)
      #print '2'
      #print lines, err
      if err:
        raise Exception(err)
  
    if smb_user:
      #print '/usr/bin/pdbedit  -d 1 -t -a  -u %s -f %s'%(username, name), "%s\n%s"%(pswd, pswd)
      # Now all set to create samba user
      #print '3'
      #lines, err = command.get_command_output(r'/usr/bin/pdbedit  -d 1 -t -a  -u %s -f %s'%(username, name), "%s\n%s"%(pswd, pswd))
      (ret, rc), err = command.execute_with_conf_and_rc(r'/usr/bin/pdbedit  -d 1 -t -a  -u %s -f %s'%(username, name), "%s\n%s"%(pswd, pswd))
      if rc == 0:
        lines, er = command.get_output_list(ret)
        if er:
          raise Exception(er)
      else:
        err = ''
        tl, er = command.get_output_list(ret)
        if er:
          raise Exception(er)
        if tl:
          err = ','.join(tl)
        tl, er = command.get_error_list(ret)
        if er:
          raise Exception(er)
        if tl:
          err = err + ','.join(tl)
        raise Exception(err)
      #print '4'
      #print lines, err
  except Exception, e:
    return False, 'Error creating local user : %s'%str(e)
def create_local_user(username, name, pswd, gid = None, smb_user=True):

  try:

    #First check if user exists. if so kick out
    ul, err = get_local_users()
    if ul:
      for ud in ul:
        if ud["username"] == username:
          raise Exception("Error creating user. The user \"%s\" already exists. "%username)
    elif err:
      raise Exception("Error retrieving user list : %s"%err)

  
    enc_pswd = crypt.crypt(pswd, "28")
    use_salt, err = common.use_salt()
    if err:
      raise Exception(err)
    if use_salt:
      client = salt.client.LocalClient()
      if gid:
        rc = client.cmd('*', 'user.add', [username, None, gid])
      else:
        rc = client.cmd('*', 'user.add', [username])
      #print rc
      if not rc:
        error_list.append("Error creating the username")
      for hostname, status in rc.items():
        if not status:
          error_list.append("Error creating the username on node"%hostname)
      rc = client.cmd('*', 'shadow.set_password', [username, enc_pswd] )
      for hostname, status in rc.items():
        if not status:
          error_list.append("Error setting the password for username on GRIDCell %s"%hostname)
      rc = client.cmd('*', 'user.chfullname', [username, "integralstor_user_%s"%name] )
      for hostname, status in rc.items():
        if not status:
          error_list.append("Error setting the name for username on node %s"%hostname)
    else:
      #print '1'
      if gid:
        cmd_to_run = 'useradd -g %s -p %s -c integralstor_user_%s %s'%(gid, enc_pswd, name, username)
      else:
        cmd_to_run = 'useradd  -p %s -c integralstor_user_%s %s'%(enc_pswd, name, username)
      lines, err = command.get_command_output(cmd_to_run)
      #print '2'
      #print lines, err
      if err:
        raise Exception(err)
  
    if smb_user:
      #print '/usr/bin/pdbedit  -d 1 -t -a  -u %s -f %s'%(username, name), "%s\n%s"%(pswd, pswd)
      # Now all set to create samba user
      #print '3'
      #lines, err = command.get_command_output(r'/usr/bin/pdbedit  -d 1 -t -a  -u %s -f %s'%(username, name), "%s\n%s"%(pswd, pswd))
      (ret, rc), err = command.execute_with_conf_and_rc(r'/usr/bin/pdbedit  -d 1 -t -a  -u %s -f %s'%(username, name), "%s\n%s"%(pswd, pswd))
      if rc == 0:
        lines, er = command.get_output_list(ret)
        if er:
          raise Exception(er)
      else:
        err = ''
        tl, er = command.get_output_list(ret)
        if er:
          raise Exception(er)
        if tl:
          err = ','.join(tl)
        tl, er = command.get_error_list(ret)
        if er:
          raise Exception(er)
        if tl:
          err = err + ','.join(tl)
        raise Exception(err)
      #print '4'
      #print lines, err
  except Exception, e:
    return False, 'Error creating local user : %s'%str(e)