Sha256: a4c5bdbf585d17d86630030d89447cfa30e48bb4c96cc1044c4d66fba1368e2f

Contents?: true

Size: 1.81 KB

Versions: 2

Compression:

Stored size: 1.81 KB

Contents

#!/usr/bin/env ruby

require 'active_samba_ldap'
require 'active_samba_ldap/command'

argv, opts, options = ActiveSambaLdap::Command.parse_options do |opts, options|
  options.ou = nil

  opts.banner += " COMPUTER_NAME"

  opts.on("-o", "--ou=OU",
          "add the user in the organizational unit OU",
          "(relative to the user suffix)") do |ou|
    if /^ou=/ =~ ou
      options.ou = ou
    else
      options.ou = "ou=#{ou}"
    end
  end
end

name = nil
if argv.size == 1
  name = argv.first.chomp("$") + "$"
else
  $stderr.puts opts
  exit 1
end

unless Process.uid.zero?
  $stderr.puts "need root authority."
  exit 1
end

ActiveSambaLdap::Base.establish_connection("update")


class User < ActiveSambaLdap::User
  ldap_mapping
end

class Computer < ActiveSambaLdap::Computer
  ldap_mapping
end

class Group < ActiveSambaLdap::Group
  ldap_mapping
end

class UnixIdPool < ActiveSambaLdap::UnixIdPool
  ldap_mapping
end

unless Computer.valid_name?(name)
  $stderr.puts "'#{name}' is illegal computer name"
  exit 1
end

if Computer.exists?(name)
  $stderr.puts "computer '#{name}' already exists."
  exit 1
end

create_options = {
  :uid => [name, options.ou].compact.join(","),
  :group_class => Group,
}

create_options[:description] = "Computer"
create_options[:gecos] = "Computer"

computer = nil
begin
  computer = Computer.create(:uid => name, :group_class => Group)
rescue ActiveSambaLdap::UidNumberAlreadyExists
  $stderr.puts "UID '#{uid_number}' already exists"
  exit 1
rescue ActiveSambaLdap::GidNumberDoesNotExist,
  ActiveSambaLdap::GroupDoesNotExist,
  ActiveSambaLdap::GroupDoesNotHaveSambaSID
  $stderr.puts $!
  exit 1
end

unless computer.errors.empty?
  computer.errors.each_full do |message|
    $stderr.puts(message)
  end
  exit 1
end

ActiveSambaLdap::Base.restart_nscd

ActiveSambaLdap::Base.clear_active_connections!

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activesambaldap-0.0.1 bin/asl-samba-computeradd
activesambaldap-0.0.2 bin/asl-samba-computeradd