| 324 | # Unlike most other *nixes, OS X doesn't provide built in functionality |
| 325 | # for automatically assigning uids and gids to accounts, so we set up these |
| 326 | # methods for consumption by functionality like --mkusers |
| 327 | # By default we restrict to a reasonably sane range for system accounts |
| 328 | def self.next_system_id(id_type, min_id=20) |
| 329 | dscl_args = ['.', '-list'] |
| 330 | if id_type == 'uid' |
| 331 | dscl_args << '/Users' << 'uid' |
| 332 | elsif id_type == 'gid' |
| 333 | dscl_args << '/Groups' << 'gid' |
| 334 | else |
| 335 | fail("Invalid id_type #{id_type}. Only 'uid' and 'gid' supported") |
| 336 | end |
| 337 | dscl_out = dscl(dscl_args) |
| 338 | # We're ok with throwing away negative uids here. |
| 339 | ids = dscl_out.split.compact.collect { |l| l.to_i if l.match(/^\d+$/) } |
| 340 | ids.compact!.sort! { |a,b| a.to_f <=> b.to_f } |
| 341 | # We're just looking for an unused id in our sorted array. |
| 342 | ids.each_index do |i| |
| 343 | next_id = ids[i] + 1 |
| 344 | return next_id if ids[i+1] != next_id and next_id >= min_id |
| 345 | end |
| 346 | end |
| 347 | |
| 348 | |