summaryrefslogtreecommitdiffstats
path: root/tools/accounts2xml.py
blob: 749f3b68cad0c401233557bed225bccaadf6aded (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
73
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python
#===============================================================================
#
#         FILE: accounts2xml.py
#
#        USAGE: ./accounts2xml.py filename node_name
#
#  DESCRIPTION: A python script to generate accounts.xml containing only the login
#               users from the given /etc/passwd file
#
#      OPTIONS: ---
# REQUIREMENTS: ---
#         BUGS: ---
#        NOTES: ---
#       AUTHOR: DongInn Kim (), dikim@cs.indiana.edu
# ORGANIZATION: Center for Research in Extreme Scale Technologies
#      VERSION: 1.0
#      CREATED: 05/13/2012 01:44:43 PM
#     REVISION: ---
#===============================================================================

# encoding: utf-8

"""
accounts2xml.py

This script coverts a csv file to an XML.
The script takes 1 paramenters
* filename

e.g., ./accounts2xml.py /etc/passwd

Created by Giovanni Collazo on 2011-02-19.
Copyright (c) 2011 24veces.com. All rights reserved.

Modified by DongInn Kim on 2012-05-13
Copyright (c) 2012 Indiana University. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

"""

import sys
import csv
import os
import re
import grp
from xml.dom.minidom import Document

def main(args):
  
  try:
    filename = "./copied_passwd"
    with file(args[1], 'r') as original: data = original.read()
    with file(filename, 'w') as modified: modified.write("name:pass:uid:gid:gecos:home:shell\n" + data); modified.close()
    safe_filename = "Properties"
  except IndexError:
    print("ERROR: Please provide a filename.csv as the first argument")
    sys.exit()
    
  node_user = "UnixUser"
  node_group = "UnixGroup"

  f = csv.reader(open(filename, 'rb'), delimiter=':')
  
  doc = Document()
  root_element = doc.createElement(safe_filename)
  doc.appendChild(root_element)
  
  columns = f.next()
  
  groups = dict()
  for row in f:
    match = re.search(r'/bin/\w*sh', row[6])  # adjust this line to match the right shell path
    if match:
        item = doc.createElement(node_user)
        root_element.appendChild(item)
        extra_groups = os.popen("groups %s" % row[0]).readline()[:-1] 
        p_group = os.popen("id -gn %s" % row[0]).readline()[:-1]
        extra_groups_str = extra_groups.split(' : ')[1]
        populate_groups(groups, extra_groups_str)
        item.setAttribute('extra_groups', get_extra_group_str(extra_groups_str, p_group))
        create_col_nodes(columns, item, doc, row)
    
  for gkey, gval in groups.items():
    item = doc.createElement(node_group)
    root_element.appendChild(item)
    item.setAttribute('name', gkey)
    (gid,extra) = gval.split(':')
    item.setAttribute('gid', gid)

  output_file = "accounts.xml"
  doc.writexml(open(output_file, 'w'), addindent='    ', newl='\n') # Write file
  
  print("Done: Created %s" % output_file)
  os.remove(filename)

def get_extra_group_str(group_str, p_group):
    groups = group_str.split(' ')
    groups = [x for x in groups  if p_group != x]
    return ' '.join(groups)

  
def create_col_nodes(cols, item, doc, row): 
  for col in cols:
    if col == "gid":
        att = doc.createAttribute("group")
        att.nodeValue = grp.getgrgid(int(row.pop(0)))[0]
    else:
        att = doc.createAttribute(str.replace(col, " ", "_").lower())
        att.nodeValue = row.pop(0)

    if col != "pass":
        item.setAttributeNode(att)

def populate_groups(group_dic, group_str):
    for g in group_str.split(' '):
        if not group_dic.has_key(g):
            group_ent = os.popen("getent group %s" % g).readline()[:-1].split(':')
            gid = group_ent[2]
            extra = group_ent[3]
            extra_list = list(extra)
            for e in extra_list:
                if e == ',':
                    loc = extra_list.index(e)
                    extra_list[loc] = ' '
            extra = "".join(extra_list)
            group_dic[g] = gid + ":" + extra


if __name__ == "__main__":
  sys.exit(main(sys.argv))

# vim:set sr et ts=4 sw=4 ft=python fenc=utf-8: // See Vim, :help 'modeline'
# Created: Sun, 13 May 2012 13:44:43 -0400