| 1 | from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 2 | from rpython.rtyper.tool import rffi_platform |
| 3 | from rpython.rtyper.lltypesystem import rffi, lltype |
| 4 | from pypy.interpreter.error import OperationError, oefmt |
| 5 | from pypy.interpreter.gateway import unwrap_spec |
| 6 | from rpython.rlib.rarithmetic import r_uint, widen |
| 7 | |
| 8 | |
| 9 | eci = ExternalCompilationInfo(includes=['pwd.h']) |
| 10 | |
| 11 | class CConfig: |
| 12 | _compilation_info_ = eci |
| 13 | |
| 14 | uid_t = rffi_platform.SimpleType("uid_t") |
| 15 | gid_t = rffi_platform.SimpleType("gid_t") |
| 16 | |
| 17 | config = rffi_platform.configure(CConfig) |
| 18 | |
| 19 | uid_t = config['uid_t'] |
| 20 | gid_t = config['gid_t'] |
| 21 | |
| 22 | class CConfig: |
| 23 | _compilation_info_ = eci |
| 24 | |
| 25 | passwd = rffi_platform.Struct( |
| 26 | 'struct passwd', |
| 27 | [('pw_name', rffi.CCHARP), |
| 28 | ('pw_passwd', rffi.CCHARP), |
| 29 | ('pw_uid', uid_t), |
| 30 | ('pw_gid', gid_t), |
| 31 | ('pw_gecos', rffi.CCHARP), |
| 32 | ('pw_dir', rffi.CCHARP), |
| 33 | ('pw_shell', rffi.CCHARP)]) |
| 34 | |
| 35 | config = rffi_platform.configure(CConfig) |
| 36 | |
| 37 | passwd_p = lltype.Ptr(config['passwd']) |
| 38 | |
| 39 | def external(name, args, result, **kwargs): |
| 40 | return rffi.llexternal(name, args, result, compilation_info=eci, |
| 41 | releasegil=False, **kwargs) |
| 42 | |
| 43 | c_getpwuid = external("getpwuid", [uid_t], passwd_p) |
| 44 | c_getpwnam = external("getpwnam", [rffi.CCHARP], passwd_p) |
| 45 | c_setpwent = external("setpwent", [], lltype.Void) |
| 46 | c_getpwent = external("getpwent", [], passwd_p) |
| 47 | c_endpwent = external("endpwent", [], lltype.Void) |
| 48 | |
| 49 | |
| 50 | def uid_converter(space, w_uid): |
| 51 | try: |
| 52 | val = space.int_w(w_uid) |
| 53 | if val == -1: |
| 54 | return rffi.cast(uid_t, -1) |
| 55 | elif val < 0: |
| 56 | raise oefmt(space.w_OverflowError, "user id is less than minimum") |
| 57 | else: |
| 58 | val = r_uint(val) |
| 59 | except OperationError as e: |
| 60 | if not e.match(space, space.w_OverflowError): |
| 61 | raise |
| 62 | try: |
| 63 | val = space.uint_w(w_uid) |
| 64 | except OperationError as e: |
| 65 | if e.match(space, space.w_ValueError): |
| 66 | raise oefmt(space.w_OverflowError, "user id is less than minimum") |
| 67 | elif e.match(space, space.w_OverflowError): |
| 68 | raise oefmt(space.w_OverflowError, "user id is greater than maximum") |
| 69 | raise |
| 70 | uid = rffi.cast(uid_t, val) |
| 71 | if val != uid: |
| 72 | raise oefmt(space.w_OverflowError, "user id is greater than maximum") |
| 73 | return uid |
| 74 | |
| 75 | def make_struct_passwd(space, pw): |
| 76 | w_passwd_struct = space.getattr(space.getbuiltinmodule('pwd'), |
| 77 | space.newtext('struct_passwd')) |
| 78 | w_tuple = space.newtuple([ |
| 79 | space.newtext(rffi.charp2str(pw.c_pw_name)), |
| 80 | space.newtext(rffi.charp2str(pw.c_pw_passwd)), |
| 81 | space.int(space.newint(pw.c_pw_uid)), |
| 82 | space.int(space.newint(pw.c_pw_gid)), |
| 83 | space.newtext(rffi.charp2str(pw.c_pw_gecos)), |
| 84 | space.newtext(rffi.charp2str(pw.c_pw_dir)), |
| 85 | space.newtext(rffi.charp2str(pw.c_pw_shell)), |
| 86 | ]) |
| 87 | return space.call_function(w_passwd_struct, w_tuple) |
| 88 | |
| 89 | |
| 90 | def getpwuid(space, w_uid): |
| 91 | """ |
| 92 | getpwuid(uid) -> (pw_name,pw_passwd,pw_uid, |
| 93 | pw_gid,pw_gecos,pw_dir,pw_shell) |
| 94 | Return the password database entry for the given numeric user ID. |
| 95 | See pwd.__doc__ for more on password database entries. |
| 96 | """ |
| 97 | msg = "getpwuid(): uid not found" |
| 98 | try: |
| 99 | uid = uid_converter(space, w_uid) |
| 100 | except OperationError as e: |
| 101 | if e.match(space, space.w_OverflowError): |
| 102 | raise oefmt(space.w_KeyError, msg) |
| 103 | raise |
| 104 | pw = c_getpwuid(uid) |
| 105 | if not pw: |
| 106 | raise OperationError(space.w_KeyError, space.newtext( |
| 107 | "%s: %d" % (msg, widen(uid)))) |
| 108 | return make_struct_passwd(space, pw) |
| 109 | |
| 110 | @unwrap_spec(name='text0') |
| 111 | def getpwnam(space, name): |
| 112 | """ |
| 113 | getpwnam(name) -> (pw_name,pw_passwd,pw_uid, |
| 114 | pw_gid,pw_gecos,pw_dir,pw_shell) |
| 115 | Return the password database entry for the given user name. |
| 116 | See pwd.__doc__ for more on password database entries. |
| 117 | """ |
| 118 | pw = c_getpwnam(name) |
| 119 | if not pw: |
| 120 | raise oefmt(space.w_KeyError, "getpwnam(): name not found: %s", name) |
| 121 | return make_struct_passwd(space, pw) |
| 122 | |
| 123 | def getpwall(space): |
| 124 | users_w = [] |
| 125 | c_setpwent() |
| 126 | try: |
| 127 | while True: |
| 128 | pw = c_getpwent() |
| 129 | if not pw: |
| 130 | break |
| 131 | users_w.append(make_struct_passwd(space, pw)) |
| 132 | finally: |
| 133 | c_endpwent() |
| 134 | return space.newlist(users_w) |