| 1 | n/a | # this module is an OS/2 oriented replacement for the pwd standard |
|---|
| 2 | n/a | # extension module. |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | # written by Andrew MacIntyre, April 2001. |
|---|
| 5 | n/a | # updated July 2003, adding field accessor support |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | # note that this implementation checks whether ":" or ";" as used as |
|---|
| 8 | n/a | # the field separator character. Path conversions are are applied when |
|---|
| 9 | n/a | # the database uses ":" as the field separator character. |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | """Replacement for pwd standard extension module, intended for use on |
|---|
| 12 | n/a | OS/2 and similar systems which don't normally have an /etc/passwd file. |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | The standard Unix password database is an ASCII text file with 7 fields |
|---|
| 15 | n/a | per record (line), separated by a colon: |
|---|
| 16 | n/a | - user name (string) |
|---|
| 17 | n/a | - password (encrypted string, or "*" or "") |
|---|
| 18 | n/a | - user id (integer) |
|---|
| 19 | n/a | - group id (integer) |
|---|
| 20 | n/a | - description (usually user's name) |
|---|
| 21 | n/a | - home directory (path to user's home directory) |
|---|
| 22 | n/a | - shell (path to the user's login shell) |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | (see the section 8.1 of the Python Library Reference) |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | This implementation differs from the standard Unix implementation by |
|---|
| 27 | n/a | allowing use of the platform's native path separator character - ';' on OS/2, |
|---|
| 28 | n/a | DOS and MS-Windows - as the field separator in addition to the Unix |
|---|
| 29 | n/a | standard ":". Additionally, when ":" is the separator path conversions |
|---|
| 30 | n/a | are applied to deal with any munging of the drive letter reference. |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | The module looks for the password database at the following locations |
|---|
| 33 | n/a | (in order first to last): |
|---|
| 34 | n/a | - ${ETC_PASSWD} (or %ETC_PASSWD%) |
|---|
| 35 | n/a | - ${ETC}/passwd (or %ETC%/passwd) |
|---|
| 36 | n/a | - ${PYTHONHOME}/Etc/passwd (or %PYTHONHOME%/Etc/passwd) |
|---|
| 37 | n/a | |
|---|
| 38 | n/a | Classes |
|---|
| 39 | n/a | ------- |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | None |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | Functions |
|---|
| 44 | n/a | --------- |
|---|
| 45 | n/a | |
|---|
| 46 | n/a | getpwuid(uid) - return the record for user-id uid as a 7-tuple |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | getpwnam(name) - return the record for user 'name' as a 7-tuple |
|---|
| 49 | n/a | |
|---|
| 50 | n/a | getpwall() - return a list of 7-tuples, each tuple being one record |
|---|
| 51 | n/a | (NOTE: the order is arbitrary) |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | Attributes |
|---|
| 54 | n/a | ---------- |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | passwd_file - the path of the password database file |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | """ |
|---|
| 59 | n/a | |
|---|
| 60 | n/a | import os |
|---|
| 61 | n/a | |
|---|
| 62 | n/a | # try and find the passwd file |
|---|
| 63 | n/a | __passwd_path = [] |
|---|
| 64 | n/a | if 'ETC_PASSWD' in os.environ: |
|---|
| 65 | n/a | __passwd_path.append(os.environ['ETC_PASSWD']) |
|---|
| 66 | n/a | if 'ETC' in os.environ: |
|---|
| 67 | n/a | __passwd_path.append('%s/passwd' % os.environ['ETC']) |
|---|
| 68 | n/a | if 'PYTHONHOME' in os.environ: |
|---|
| 69 | n/a | __passwd_path.append('%s/Etc/passwd' % os.environ['PYTHONHOME']) |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | passwd_file = None |
|---|
| 72 | n/a | for __i in __passwd_path: |
|---|
| 73 | n/a | try: |
|---|
| 74 | n/a | __f = open(__i, 'r') |
|---|
| 75 | n/a | __f.close() |
|---|
| 76 | n/a | passwd_file = __i |
|---|
| 77 | n/a | break |
|---|
| 78 | n/a | except: |
|---|
| 79 | n/a | pass |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | # path conversion handlers |
|---|
| 82 | n/a | def __nullpathconv(path): |
|---|
| 83 | n/a | return path.replace(os.altsep, os.sep) |
|---|
| 84 | n/a | |
|---|
| 85 | n/a | def __unixpathconv(path): |
|---|
| 86 | n/a | # two known drive letter variations: "x;" and "$x" |
|---|
| 87 | n/a | if path[0] == '$': |
|---|
| 88 | n/a | conv = path[1] + ':' + path[2:] |
|---|
| 89 | n/a | elif path[1] == ';': |
|---|
| 90 | n/a | conv = path[0] + ':' + path[2:] |
|---|
| 91 | n/a | else: |
|---|
| 92 | n/a | conv = path |
|---|
| 93 | n/a | return conv.replace(os.altsep, os.sep) |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | # decide what field separator we can try to use - Unix standard, with |
|---|
| 96 | n/a | # the platform's path separator as an option. No special field conversion |
|---|
| 97 | n/a | # handler is required when using the platform's path separator as field |
|---|
| 98 | n/a | # separator, but are required for the home directory and shell fields when |
|---|
| 99 | n/a | # using the standard Unix (":") field separator. |
|---|
| 100 | n/a | __field_sep = {':': __unixpathconv} |
|---|
| 101 | n/a | if os.pathsep: |
|---|
| 102 | n/a | if os.pathsep != ':': |
|---|
| 103 | n/a | __field_sep[os.pathsep] = __nullpathconv |
|---|
| 104 | n/a | |
|---|
| 105 | n/a | # helper routine to identify which separator character is in use |
|---|
| 106 | n/a | def __get_field_sep(record): |
|---|
| 107 | n/a | fs = None |
|---|
| 108 | n/a | for c in __field_sep.keys(): |
|---|
| 109 | n/a | # there should be 6 delimiter characters (for 7 fields) |
|---|
| 110 | n/a | if record.count(c) == 6: |
|---|
| 111 | n/a | fs = c |
|---|
| 112 | n/a | break |
|---|
| 113 | n/a | if fs: |
|---|
| 114 | n/a | return fs |
|---|
| 115 | n/a | else: |
|---|
| 116 | n/a | raise KeyError('>> passwd database fields not delimited <<') |
|---|
| 117 | n/a | |
|---|
| 118 | n/a | # class to match the new record field name accessors. |
|---|
| 119 | n/a | # the resulting object is intended to behave like a read-only tuple, |
|---|
| 120 | n/a | # with each member also accessible by a field name. |
|---|
| 121 | n/a | class Passwd: |
|---|
| 122 | n/a | def __init__(self, name, passwd, uid, gid, gecos, dir, shell): |
|---|
| 123 | n/a | self.__dict__['pw_name'] = name |
|---|
| 124 | n/a | self.__dict__['pw_passwd'] = passwd |
|---|
| 125 | n/a | self.__dict__['pw_uid'] = uid |
|---|
| 126 | n/a | self.__dict__['pw_gid'] = gid |
|---|
| 127 | n/a | self.__dict__['pw_gecos'] = gecos |
|---|
| 128 | n/a | self.__dict__['pw_dir'] = dir |
|---|
| 129 | n/a | self.__dict__['pw_shell'] = shell |
|---|
| 130 | n/a | self.__dict__['_record'] = (self.pw_name, self.pw_passwd, |
|---|
| 131 | n/a | self.pw_uid, self.pw_gid, |
|---|
| 132 | n/a | self.pw_gecos, self.pw_dir, |
|---|
| 133 | n/a | self.pw_shell) |
|---|
| 134 | n/a | |
|---|
| 135 | n/a | def __len__(self): |
|---|
| 136 | n/a | return 7 |
|---|
| 137 | n/a | |
|---|
| 138 | n/a | def __getitem__(self, key): |
|---|
| 139 | n/a | return self._record[key] |
|---|
| 140 | n/a | |
|---|
| 141 | n/a | def __setattr__(self, name, value): |
|---|
| 142 | n/a | raise AttributeError('attribute read-only: %s' % name) |
|---|
| 143 | n/a | |
|---|
| 144 | n/a | def __repr__(self): |
|---|
| 145 | n/a | return str(self._record) |
|---|
| 146 | n/a | |
|---|
| 147 | n/a | def __cmp__(self, other): |
|---|
| 148 | n/a | this = str(self._record) |
|---|
| 149 | n/a | if this == other: |
|---|
| 150 | n/a | return 0 |
|---|
| 151 | n/a | elif this < other: |
|---|
| 152 | n/a | return -1 |
|---|
| 153 | n/a | else: |
|---|
| 154 | n/a | return 1 |
|---|
| 155 | n/a | |
|---|
| 156 | n/a | |
|---|
| 157 | n/a | # read the whole file, parsing each entry into tuple form |
|---|
| 158 | n/a | # with dictionaries to speed recall by UID or passwd name |
|---|
| 159 | n/a | def __read_passwd_file(): |
|---|
| 160 | n/a | if passwd_file: |
|---|
| 161 | n/a | passwd = open(passwd_file, 'r') |
|---|
| 162 | n/a | else: |
|---|
| 163 | n/a | raise KeyError('>> no password database <<') |
|---|
| 164 | n/a | uidx = {} |
|---|
| 165 | n/a | namx = {} |
|---|
| 166 | n/a | sep = None |
|---|
| 167 | n/a | while True: |
|---|
| 168 | n/a | entry = passwd.readline().strip() |
|---|
| 169 | n/a | if len(entry) > 6: |
|---|
| 170 | n/a | if sep is None: |
|---|
| 171 | n/a | sep = __get_field_sep(entry) |
|---|
| 172 | n/a | fields = entry.split(sep) |
|---|
| 173 | n/a | for i in (2, 3): |
|---|
| 174 | n/a | fields[i] = int(fields[i]) |
|---|
| 175 | n/a | for i in (5, 6): |
|---|
| 176 | n/a | fields[i] = __field_sep[sep](fields[i]) |
|---|
| 177 | n/a | record = Passwd(*fields) |
|---|
| 178 | n/a | if fields[2] not in uidx: |
|---|
| 179 | n/a | uidx[fields[2]] = record |
|---|
| 180 | n/a | if fields[0] not in namx: |
|---|
| 181 | n/a | namx[fields[0]] = record |
|---|
| 182 | n/a | elif len(entry) > 0: |
|---|
| 183 | n/a | pass # skip empty or malformed records |
|---|
| 184 | n/a | else: |
|---|
| 185 | n/a | break |
|---|
| 186 | n/a | passwd.close() |
|---|
| 187 | n/a | if len(uidx) == 0: |
|---|
| 188 | n/a | raise KeyError |
|---|
| 189 | n/a | return (uidx, namx) |
|---|
| 190 | n/a | |
|---|
| 191 | n/a | # return the passwd database entry by UID |
|---|
| 192 | n/a | def getpwuid(uid): |
|---|
| 193 | n/a | u, n = __read_passwd_file() |
|---|
| 194 | n/a | return u[uid] |
|---|
| 195 | n/a | |
|---|
| 196 | n/a | # return the passwd database entry by passwd name |
|---|
| 197 | n/a | def getpwnam(name): |
|---|
| 198 | n/a | u, n = __read_passwd_file() |
|---|
| 199 | n/a | return n[name] |
|---|
| 200 | n/a | |
|---|
| 201 | n/a | # return all the passwd database entries |
|---|
| 202 | n/a | def getpwall(): |
|---|
| 203 | n/a | u, n = __read_passwd_file() |
|---|
| 204 | n/a | return n.values() |
|---|
| 205 | n/a | |
|---|
| 206 | n/a | # test harness |
|---|
| 207 | n/a | if __name__ == '__main__': |
|---|
| 208 | n/a | getpwall() |
|---|