Module: Yast::UsersCmdlineInclude

Defined in:
../../src/include/users/cmdline.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) bind_and_read_LDAP(options)

set LDAP admin password and read LDAP users and groups



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File '../../src/include/users/cmdline.rb', line 48

def bind_and_read_LDAP(options)
  options = deep_copy(options)
  pw = Ops.get_string(options, "ldap_password", "")
  if Users.LDAPAvailable && Users.LDAPNotRead
    if Ldap.bind_pass == nil
      if pw == "" && !Builtins.haskey(options, "batchmode")
        # password entering label
        pw = CommandLine.PasswordInput(_("LDAP Server Password:"))
      end
      Ldap.SetBindPassword(pw) 
      # TODO check bind...
    end
    error = UsersLDAP.ReadSettings
    if error != ""
      CommandLine.Print(error)
      return false
    end
    error = Users.ReadLDAPSet("Users")
    if error != ""
      CommandLine.Print(error)
      return false
    end
  end
  true
end

- (Object) convert_keys(input)



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File '../../src/include/users/cmdline.rb', line 74

def convert_keys(input)
  input = deep_copy(input)
  ret = {}
  keys = {
    "username"                   => "uid",
    "password"                   => "userPassword",
    "home"                       => "homeDirectory",
    "shell"                      => "loginShell",
    "fullname"                   => "cn",
    "gid"                        => "gidNumber",
    "uid"                        => "uidNumber",
    "no_home"                    => "create_home",
    "groupname"                  => "cn",
    "new_username"               => "uid",
    "new_groupname"              => "cn",
    "new_uid"                    => "uidNumber",
    "new_gid"                    => "gidNumber",
    UsersLDAP.GetMemberAttribute => "userlist"
  }
  Builtins.foreach(input) do |key, value|
    new_key = Ops.get_string(keys, key, key)
    value = false if new_key == "create_home"
    if new_key == "gidNumber" && value != "" &&
        (Builtins.haskey(input, "username") || Builtins.haskey(input, "uid"))
      # check group existence!
      if !UsersCache.GIDExists(Builtins.tointeger(Convert.to_string(value)))
        next
      end
    end
    if new_key == "grouplist" && Ops.is_string?(value)
      value = Builtins.listmap(
        Builtins.splitstring(Convert.to_string(value), ",")
      ) { |g| { g => 1 } }
    end
    if new_key == "userlist" && Ops.is_string?(value)
      sep = ","
      if Builtins.issubstring(Convert.to_string(value), "=") #for DN
        sep = ":"
      end
      value = Builtins.listmap(
        Builtins.splitstring(Convert.to_string(value), sep)
      ) { |u| { u => 1 } }
    end
    next if new_key == "ldap_password"
    if key == "username" && Builtins.haskey(input, "new_username") ||
        key == "uid" && Builtins.haskey(input, "new_uid") ||
        key == "groupname" && Builtins.haskey(input, "new_groupname") ||
        key == "gid" && Builtins.haskey(input, "new_gid")
      next
    end
    Ops.set(ret, new_key, value)
  end
  deep_copy(ret)
end

- (Boolean) GroupAddHandler(options)

Add group

Returns:

  • (Boolean)

    false



633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
# File '../../src/include/users/cmdline.rb', line 633

def GroupAddHandler(options)
  options = deep_copy(options)
  if Ops.get_string(options, "groupname", "") == ""
    # error message
    CommandLine.Print(_("Enter a group name."))
    return false
  end

  group = convert_keys(options)
  type = Ops.get_string(group, "type", "local")
  return false if !bind_and_read_LDAP(options) if type == "ldap"
  member_attr = type == "ldap" ? UsersLDAP.GetMemberAttribute : "userlist"
  if Builtins.haskey(group, "userlist")
    if type == "ldap"
      Ops.set(group, member_attr, Ops.get_map(group, "userlist", {}))
      group = Builtins.remove(group, "userlist")
    end
  else
    Ops.set(group, member_attr, {})
  end

  Users.ResetCurrentGroup
  error = Users.AddGroup(group)

  if error != ""
    CommandLine.Print(error)
    return false
  end

  if Ops.get_string(group, "type", "local") == "ldap"
    Users.SubstituteGroupValues
  end

  error = Users.CheckGroup({})
  if error != ""
    CommandLine.Print(error)
    return false
  end
  Users.CommitGroup
end

- (Boolean) GroupDeleteHandler(options)

Delete group

Returns:

  • (Boolean)

    false



601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
# File '../../src/include/users/cmdline.rb', line 601

def GroupDeleteHandler(options)
  options = deep_copy(options)
  gid = Builtins.tointeger(
    Ops.get_string(
      options,
      "gidNumber",
      Ops.get_string(options, "gid", "-1")
    )
  )
  groupname = Ops.get_string(
    options,
    "cn",
    Ops.get_string(options, "groupname", "")
  )

  type = Ops.get_string(options, "type", "local")
  return false if !bind_and_read_LDAP(options) if type == "ldap"
  if gid != -1 && gid != nil
    Users.SelectGroup(gid)
  elsif groupname != ""
    Users.SelectGroupByName(groupname)
  end
  if Users.GetCurrentGroup == {}
    # error message
    CommandLine.Print(_("There is no such group."))
    return false
  end
  Users.DeleteGroup && Users.CommitGroup
end

- (Boolean) GroupEditHandler(options)

Edit group

Returns:

  • (Boolean)

    false



676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
# File '../../src/include/users/cmdline.rb', line 676

def GroupEditHandler(options)
  options = deep_copy(options)
  gid = Builtins.tointeger(
    Ops.get_string(
      options,
      "gidNumber",
      Ops.get_string(options, "gid", "-1")
    )
  )
  groupname = Ops.get_string(
    options,
    "cn",
    Ops.get_string(options, "groupname", "")
  )

  type = Ops.get_string(options, "type", "local")
  return false if !bind_and_read_LDAP(options) if type == "ldap"
  if gid != -1 && gid != nil
    Users.SelectGroup(gid)
  elsif groupname != ""
    Users.SelectGroupByName(groupname)
  end
  group = Users.GetCurrentGroup
  if group == {}
    # error message
    CommandLine.Print(_("There is no such group."))
    return false
  end

  changes = convert_keys(options)
  if type == "ldap" && !Builtins.haskey(changes, "dn")
    Ops.set(changes, "dn", Ops.get_string(group, "dn", "")) 
    # for groupname changes...
  end
  if type == "ldap" && Builtins.haskey(changes, "userlist")
    Ops.set(
      changes,
      UsersLDAP.GetMemberAttribute,
      Ops.get_map(changes, "userlist", {})
    )
    changes = Builtins.remove(changes, "userlist")
  end
  error = Users.EditGroup(
    Convert.convert(changes, :from => "map", :to => "map <string, any>")
  )
  if error != ""
    CommandLine.Print(error)
    return false
  end
  error = Users.CheckGroup({})
  if error != ""
    CommandLine.Print(error)
    return false
  end
  Users.CommitGroup
end

- (Boolean) GroupShowHandler(options)

Show one group information

Returns:

  • (Boolean)

    false



529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
# File '../../src/include/users/cmdline.rb', line 529

def GroupShowHandler(options)
  options = deep_copy(options)
  group = {}
  gid = Builtins.tointeger(
    Ops.get_string(
      options,
      "gidNumber",
      Ops.get_string(options, "gid", "-1")
    )
  )
  groupname = Ops.get_string(options, "groupname", "")

  type = Ops.get_string(options, "type", "local")

  if type == "nis" && Users.NISAvailable && Users.NISNotRead
    Users.ReadNewSet("nis")
  end
  if type == "ldap" && Users.LDAPAvailable && Users.LDAPNotRead
    Ldap.SetAnonymous(true)
    Users.ReadNewSet("ldap")
  end

  if gid != -1 && gid != nil
    group = Users.GetGroup(gid, "")
  elsif groupname != ""
    group = Users.GetGroupByName(groupname, "")
  end
  if group == {}
    # error message
    CommandLine.Print(_("There is no such group."))
    return false
  end

  out = ""
  keys = {
    # label shown at command line (user attribute)
    "cn"                         => _(
      "Group Name:"
    ),
    # label shown at command line (user attribute)
    "gidNumber"                  => _(
      "GID:"
    ),
    # label shown at command line (user attribute)
    "userlist"                   => _(
      "List of Members:"
    ),
    # label shown at command line (user attribute)
    UsersLDAP.GetMemberAttribute => _(
      "List of Members:"
    )
  }
  Builtins.foreach(group) do |key, value|
    key = Ops.get_string(keys, key, "")
    next if key == ""
    svalue = Builtins.sformat("%1", value)
    if Ops.is_map?(value)
      svalue = Builtins.mergestring(
        Builtins.maplist(
          Convert.convert(value, :from => "any", :to => "map <string, any>")
        ) { |k, v| k },
        ","
      )
    end
    CommandLine.Print(Builtins.sformat("%1\n\t%2", key, svalue))
  end

  false # do not call Write...
end

- (Boolean) GroupsListHandler(options)

List groups

Returns:

  • (Boolean)

    false



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
# File '../../src/include/users/cmdline.rb', line 461

def GroupsListHandler(options)
  options = deep_copy(options)
  sets = []
  attributes = []
  amap = {}
  Builtins.foreach(options) do |key, val|
    if Builtins.contains(["local", "system", "ldap", "nis"], key)
      sets = Builtins.add(sets, key)
    else
      Ops.set(amap, key, val)
    end
  end
  sets = ["local"] if sets == []

  attributes = Builtins.maplist(convert_keys(amap)) { |k, val| k }
  if !Builtins.contains(attributes, "cn")
    attributes = Builtins.prepend(attributes, "cn")
  end
  if Builtins.contains(attributes, "userlist")
    attributes = Builtins.add(attributes, UsersLDAP.GetMemberAttribute)
  end

  Builtins.foreach(sets) do |type|
    if type == "nis" && Users.NISAvailable && Users.NISNotRead
      Users.ReadNewSet("nis")
    end
    if type == "ldap" && Users.LDAPAvailable && Users.LDAPNotRead
      Ldap.SetAnonymous(true)
      Users.ReadNewSet("ldap")
    end
    Builtins.foreach(
      Convert.convert(
        Users.GetGroups("cn", type),
        :from => "map",
        :to   => "map <string, map>"
      )
    ) do |name, group|
      out = ""
      Builtins.foreach(attributes) do |attr|
        if Builtins.haskey(group, attr)
          if Ops.is_string?(Ops.get(group, attr))
            out = Ops.add(
              Ops.add(out, Ops.get_string(group, attr, "")),
              " "
            )
          end
          if Ops.is_map?(Ops.get(group, attr))
            out = Ops.add(
              Ops.add(
                out,
                Builtins.mergestring(
                  Builtins.maplist(Ops.get_map(group, attr, {})) { |k, v| k },
                  ","
                )
              ),
              " "
            )
          end
        end
      end
      CommandLine.Print(out)
    end
  end
  false # do not call Write...
end

- (Object) initialize_users_cmdline(include_target)



32
33
34
35
36
37
38
39
40
41
42
# File '../../src/include/users/cmdline.rb', line 32

def initialize_users_cmdline(include_target)
  textdomain "users"

  Yast.import "CommandLine"
  Yast.import "Ldap"
  Yast.import "Users"
  Yast.import "UsersCache"
  Yast.import "UsersLDAP"
  Yast.import "UsersSimple"
  Yast.import "Report"
end

- (Boolean) UserAddHandler(options)

Add user

Returns:

  • (Boolean)

    false



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File '../../src/include/users/cmdline.rb', line 281

def UserAddHandler(options)
  options = deep_copy(options)
  if Ops.get_string(options, "username", "") == ""
    # error message
    CommandLine.Print(_("Enter a user name."))
    return false
  end

  user = convert_keys(options)
  type = Ops.get_string(user, "type", "local")
  if type == "ldap"
    return false if !bind_and_read_LDAP(options)
    if !Builtins.haskey(user, "sn")
      Ops.set(user, "sn", Ops.get_string(user, "uid", ""))
    end
  end

  Users.ResetCurrentUser

  if !Builtins.haskey(user, "userPassword") &&
      !Builtins.haskey(user, "batchmode")
    pw = ""
    i = 0
    while true
      # question on command line
      pw = CommandLine.PasswordInput(_("Password for New User:"))
      p2 = CommandLine.PasswordInput(
        # question on command line
        _("Confirm the password:")
      )
      if pw != p2
        if Ops.less_than(i, 2)
          # error message
          CommandLine.Print(_("Passwords do not match. Try again."))
          i = Ops.add(i, 1)
        else
          # error message
          CommandLine.Print(_("Passwords do not match. Exiting."))
          return false
        end
      else
        break
      end
    end
    Ops.set(user, "userPassword", pw)
  end
  error = UsersSimple.CheckPassword(
    Ops.get_string(user, "userPassword", ""),
    type
  )

  if error != ""
    CommandLine.Print(error)
    return false
  end

  error = Users.AddUser(user)

  if error != ""
    CommandLine.Print(error)
    return false
  end

  if Ops.get_string(user, "type", "local") == "ldap"
    Users.SubstituteUserValues
  end

  error = Users.CheckUser({})
  if error != ""
    CommandLine.Print(error)
    return false
  end

  Users.CommitUser
end

- (Boolean) UserDeleteHandler(options)

Delete user

Returns:

  • (Boolean)

    false



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File '../../src/include/users/cmdline.rb', line 359

def UserDeleteHandler(options)
  options = deep_copy(options)
  uid = Builtins.tointeger(
    Ops.get_string(
      options,
      "uidNumber",
      Ops.get_string(options, "uid", "-1")
    )
  )
  username = Ops.get_string(options, "username", "")
  delete_home = Builtins.haskey(options, "delete_home")

  type = Ops.get_string(options, "type", "local")
  return false if !bind_and_read_LDAP(options) if type == "ldap"
  if uid != -1 && uid != nil
    Users.SelectUser(uid)
  elsif username != ""
    Users.SelectUserByName(username)
    u = Users.GetCurrentUser
    if Ops.is_integer?(Ops.get(u, "uidNumber"))
      uid = Ops.get_integer(u, "uidNumber", -1)
    elsif Ops.is_string?(Ops.get(u, "uidNumber"))
      uid = Builtins.tointeger(Ops.get_string(u, "uidNumber", "-1"))
    end
  end
  if Users.GetCurrentUser == {}
    # error message
    CommandLine.Print(_("There is no such user."))
    return false
  end
  # if the user has log on system
  out = Convert.to_map(
    SCR.Execute(
      path(".target.bash_output"),
      Builtins.sformat("ps --no-headers -u %1", uid)
    )
  )
  if Builtins.size(Ops.get_string(out, "stdout", "")) != 0
    # error popup
    Report.Error(
      _(
        "You can not delete this user, because the user is present.\nPlease log off the user first."
      )
    )
    return false
  end
  Users.DeleteUser(delete_home) && Users.CommitUser
end

- (Boolean) UserEditHandler(options)

Edit user

Returns:

  • (Boolean)

    false



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File '../../src/include/users/cmdline.rb', line 410

def UserEditHandler(options)
  options = deep_copy(options)
  uid = Builtins.tointeger(
    Ops.get_string(
      options,
      "uidNumber",
      Ops.get_string(options, "uid", "-1")
    )
  )
  username = Ops.get_string(options, "username", "")

  type = Ops.get_string(options, "type", "local")
  return false if !bind_and_read_LDAP(options) if type == "ldap"
  if uid != -1 && uid != nil
    Users.SelectUser(uid)
  elsif username != ""
    Users.SelectUserByName(username)
  end
  user = Users.GetCurrentUser
  if user == {}
    # error message
    CommandLine.Print(_("There is no such user."))
    return false
  end

  changes = convert_keys(options)
  if type == "ldap" && !Builtins.haskey(changes, "dn")
    Ops.set(changes, "dn", Ops.get_string(user, "dn", "")) 
    # for username changes...
  end
  error = Users.EditUser(
    Convert.convert(changes, :from => "map", :to => "map <string, any>")
  )
  if error != ""
    CommandLine.Print(error)
    return false
  end
  error = Users.CheckUser({})
  if error != ""
    CommandLine.Print(error)
    return false
  end
  Users.CommitUser
end

- (Object) UsersGUI



752
753
754
# File '../../src/include/users/cmdline.rb', line 752

def UsersGUI
  UsersSequence(Users.GetStartDialog) == :next
end

- (Boolean) UserShowHandler(options)

Show one user information

Returns:

  • (Boolean)

    false



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File '../../src/include/users/cmdline.rb', line 197

def UserShowHandler(options)
  options = deep_copy(options)
  user = {}
  uid = Builtins.tointeger(
    Ops.get_string(
      options,
      "uidNumber",
      Ops.get_string(options, "uid", "-1")
    )
  )
  username = Ops.get_string(options, "username", "")

  type = Ops.get_string(options, "type", "local")

  if type == "nis" && Users.NISAvailable && Users.NISNotRead
    Users.ReadNewSet("nis")
  end
  if type == "ldap" && Users.LDAPAvailable && Users.LDAPNotRead
    Ldap.SetAnonymous(true)
    Users.ReadNewSet("ldap")
  end

  if uid != -1 && uid != nil
    user = Users.GetUser(uid, "")
  elsif username != ""
    user = Users.GetUserByName(username, "")
  end
  if user == {}
    # error message
    CommandLine.Print(_("There is no such user."))
    return false
  end

  out = ""
  keys = {
    # label shown at command line (user attribute)
    "cn"            => _(
      "Full Name:"
    ),
    # label shown at command line (user attribute)
    "uid"           => _(
      "Login Name:"
    ),
    # label shown at command line (user attribute)
    "homeDirectory" => _(
      "Home Directory:"
    ),
    # label shown at command line (user attribute)
    "loginShell"    => _(
      "Login Shell:"
    ),
    # label shown at command line (user attribute)
    "uidNumber"     => _(
      "UID:"
    ),
    # label shown at command line (user attribute)
    "groupname"     => _(
      "Default Group:"
    ),
    # label shown at command line (user attribute)
    "grouplist"     => _(
      "List of Groups:"
    )
  }
  Builtins.foreach(user) do |key, value|
    key = Ops.get_string(keys, key, "")
    next if key == ""
    svalue = Builtins.sformat("%1", value)
    if Ops.is_map?(value)
      svalue = Builtins.mergestring(
        Builtins.maplist(
          Convert.convert(value, :from => "any", :to => "map <string, any>")
        ) { |k, v| k },
        ","
      )
    end
    CommandLine.Print(Builtins.sformat("%1\n\t%2", key, svalue))
  end

  false # do not call Write...
end

- (Boolean) UsersListHandler(options)

List users

Returns:

  • (Boolean)

    false



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File '../../src/include/users/cmdline.rb', line 134

def UsersListHandler(options)
  options = deep_copy(options)
  sets = []
  attributes = []
  amap = {}
  Builtins.foreach(options) do |key, val|
    if Builtins.contains(["local", "system", "ldap", "nis"], key)
      sets = Builtins.add(sets, key)
    else
      #	    attributes	= (list<string>) union (attributes, [type]);
      Ops.set(amap, key, val)
    end
  end
  sets = ["local"] if sets == []
  attributes = Builtins.maplist(convert_keys(amap)) { |k, val| k }
  if !Builtins.contains(attributes, "uid")
    attributes = Builtins.prepend(attributes, "uid")
  end

  Builtins.foreach(sets) do |type|
    if type == "nis" && Users.NISAvailable && Users.NISNotRead
      Users.ReadNewSet("nis")
    end
    if type == "ldap" && Users.LDAPAvailable && Users.LDAPNotRead
      Ldap.SetAnonymous(true)
      Users.ReadNewSet("ldap")
    end
    Builtins.foreach(
      Convert.convert(
        Users.GetUsers("uid", type),
        :from => "map",
        :to   => "map <string, map>"
      )
    ) do |uname, user|
      out = ""
      # FIXME when using convert_keys, the order is broken...
      Builtins.foreach(attributes) do |attr|
        if Builtins.haskey(user, attr)
          if Ops.is_string?(Ops.get(user, attr))
            out = Ops.add(Ops.add(out, Ops.get_string(user, attr, "")), " ")
          end
          if Ops.is_map?(Ops.get(user, attr))
            out = Ops.add(
              Ops.add(
                out,
                Builtins.mergestring(
                  Builtins.maplist(Ops.get_map(user, attr, {})) { |k, v| k },
                  ","
                )
              ),
              " "
            )
          end
        end
      end
      CommandLine.Print(out)
    end
  end
  false # do not call Write...
end

- (Object) UsersRead



737
738
739
740
# File '../../src/include/users/cmdline.rb', line 737

def UsersRead
  Users.SetGUI(false)
  Users.Read == ""
end

- (Object) UsersWrite



742
743
744
745
746
747
748
749
750
# File '../../src/include/users/cmdline.rb', line 742

def UsersWrite
  Users.SetGUI(false)
  error = Users.Write
  if error != ""
    CommandLine.Print(error)
    return false
  end
  true
end