diff --git a/docgen.sh b/docgen.sh index 19b60b89..b37f7cde 100755 --- a/docgen.sh +++ b/docgen.sh @@ -16,6 +16,7 @@ pydocmd simple jc.parsers.env+ > ../docs/parsers/env.md pydocmd simple jc.parsers.free+ > ../docs/parsers/free.md pydocmd simple jc.parsers.fstab+ > ../docs/parsers/fstab.md pydocmd simple jc.parsers.group+ > ../docs/parsers/group.md +pydocmd simple jc.parsers.gshadow+ > ../docs/parsers/gshadow.md pydocmd simple jc.parsers.history+ > ../docs/parsers/history.md pydocmd simple jc.parsers.hosts+ > ../docs/parsers/hosts.md pydocmd simple jc.parsers.id+ > ../docs/parsers/id.md diff --git a/docs/parsers/gshadow.md b/docs/parsers/gshadow.md new file mode 100644 index 00000000..bb7db2b1 --- /dev/null +++ b/docs/parsers/gshadow.md @@ -0,0 +1,109 @@ +# jc.parsers.gshadow +jc - JSON CLI output utility /etc/gshadow file Parser + +Usage: + + specify --gshadow as the first argument if the piped input is coming from /etc/gshadow + +Compatibility: + + 'linux', 'aix', 'freebsd' + +Examples: + + $ cat /etc/gshadow | jc --gshadow -p + [ + { + "group_name": "root", + "password": "*", + "administrators": [], + "members": [] + }, + { + "group_name": "adm", + "password": "*", + "administrators": [], + "members": [ + "syslog", + "joeuser" + ] + }, + ... + ] + + $ cat /etc/gshadow | jc --gshadow -p -r + [ + { + "group_name": "root", + "password": "*", + "administrators": [ + "" + ], + "members": [ + "" + ] + }, + { + "group_name": "adm", + "password": "*", + "administrators": [ + "" + ], + "members": [ + "syslog", + "joeuser" + ] + }, + ... + ] + +## info +```python +info(self, /, *args, **kwargs) +``` + +## process +```python +process(proc_data) +``` + +Final processing to conform to the schema. + +Parameters: + + proc_data: (dictionary) raw structured data to process + +Returns: + + List of dictionaries. Structured data with the following schema: + + [ + { + "group_name": string, + "password": string, + "administrators": [ + string + ], + "members": [ + string + ] + } + ] + +## parse +```python +parse(data, raw=False, quiet=False) +``` + +Main text parsing function + +Parameters: + + data: (string) text data to parse + raw: (boolean) output preprocessed JSON if True + quiet: (boolean) suppress warning messages if True + +Returns: + + List of dictionaries. Raw or processed structured data. + diff --git a/jc/cli.py b/jc/cli.py index 6a7a25bc..7c82a6f5 100644 --- a/jc/cli.py +++ b/jc/cli.py @@ -34,6 +34,7 @@ parsers = [ 'free', 'fstab', 'group', + 'gshadow', 'history', 'hosts', 'id', diff --git a/jc/parsers/gshadow.py b/jc/parsers/gshadow.py new file mode 100644 index 00000000..8a852f19 --- /dev/null +++ b/jc/parsers/gshadow.py @@ -0,0 +1,152 @@ +"""jc - JSON CLI output utility /etc/gshadow file Parser + +Usage: + + specify --gshadow as the first argument if the piped input is coming from /etc/gshadow + +Compatibility: + + 'linux', 'aix', 'freebsd' + +Examples: + + $ cat /etc/gshadow | jc --gshadow -p + [ + { + "group_name": "root", + "password": "*", + "administrators": [], + "members": [] + }, + { + "group_name": "adm", + "password": "*", + "administrators": [], + "members": [ + "syslog", + "joeuser" + ] + }, + ... + ] + + $ cat /etc/gshadow | jc --gshadow -p -r + [ + { + "group_name": "root", + "password": "*", + "administrators": [ + "" + ], + "members": [ + "" + ] + }, + { + "group_name": "adm", + "password": "*", + "administrators": [ + "" + ], + "members": [ + "syslog", + "joeuser" + ] + }, + ... + ] +""" +import jc.utils + + +class info(): + version = '1.0' + description = '/etc/gshadow file parser' + author = 'Kelly Brazil' + author_email = 'kellyjonbrazil@gmail.com' + # details = 'enter any other details here' + + # compatible options: linux, darwin, cygwin, win32, aix, freebsd + compatible = ['linux', 'aix', 'freebsd'] + + +__version__ = info.version + + +def process(proc_data): + """ + Final processing to conform to the schema. + + Parameters: + + proc_data: (dictionary) raw structured data to process + + Returns: + + List of dictionaries. Structured data with the following schema: + + [ + { + "group_name": string, + "password": string, + "administrators": [ + string + ], + "members": [ + string + ] + } + ] + """ + for entry in proc_data: + if entry['administrators'] == ['']: + entry['administrators'] = [] + + if entry['members'] == ['']: + entry['members'] = [] + + return proc_data + + +def parse(data, raw=False, quiet=False): + """ + Main text parsing function + + Parameters: + + data: (string) text data to parse + raw: (boolean) output preprocessed JSON if True + quiet: (boolean) suppress warning messages if True + + Returns: + + List of dictionaries. Raw or processed structured data. + """ + if not quiet: + jc.utils.compatibility(__name__, info.compatible) + + raw_output = [] + cleandata = data.splitlines() + + # Clear any blank lines + cleandata = list(filter(None, cleandata)) + + if cleandata: + for entry in cleandata: + if entry.startswith('#'): + continue + + output_line = {} + fields = entry.split(':') + + output_line['group_name'] = fields[0] + output_line['password'] = fields[1] + output_line['administrators'] = fields[2].split(',') + output_line['members'] = fields[3].split(',') + + raw_output.append(output_line) + + if raw: + return raw_output + else: + return process(raw_output) diff --git a/tests/fixtures/ubuntu-18.04/gshadow.out b/tests/fixtures/ubuntu-18.04/gshadow.out new file mode 100644 index 00000000..973abd9d --- /dev/null +++ b/tests/fixtures/ubuntu-18.04/gshadow.out @@ -0,0 +1,53 @@ +root:*:: +daemon:*:: +bin:*:: +sys:*:: +adm:*::syslog,joeuser +tty:*:: +disk:*:: +lp:*:: +mail:*:: +news:*:: +uucp:*:: +man:*:: +proxy:*:: +kmem:*:: +dialout:*:: +fax:*:: +voice:*:: +cdrom:*::joeuser +floppy:*:: +tape:*:: +sudo:*::joeuser +audio:*:: +dip:*::joeuser +www-data:*:: +backup:*:: +operator:*:: +list:*:: +irc:*:: +src:*:: +gnats:*:: +shadow:*:: +utmp:*:: +video:*:: +sasl:*:: +plugdev:*::joeuser +staff:*:: +games:*:: +users:*:: +nogroup:*:: +systemd-journal:!:: +systemd-network:!:: +systemd-resolve:!:: +input:!:: +crontab:!:: +syslog:!:: +messagebus:!:: +lxd:!::joeuser +mlocate:!:: +uuidd:!:: +ssh:!:: +landscape:!:: +joeuser:!:: +docker:!::