ZeroCostGoods / pgs-files

Rust Library for handling /etc/{passwd,group,shadow} files
Other
7 stars 2 forks source link

Panicking when using passwd::get_entry_by_uid or group::get_entry_by_gid #3

Open carloslbello opened 8 years ago

carloslbello commented 8 years ago
thread '<main>' panicked at 'index out of bounds: the len is 1 but the index is 3', ../src/libcollections/vec.rs:1035

Code is at aarzee/coreutils. Commenting out both of the relevant lines results in no panicking.

gmjosack commented 8 years ago

It would be helpful to have a bit more information.

This works with the testdata I have but if you have a potentially valid file that this can't parse it would be good to add it to the testdata

carloslbello commented 8 years ago

Nightly Rust.

Not possible; this is a clean install of OS X El Capitan from yesterday. Have not done any modifications to accounts or groups beyond creating my user.

They both panic.

On Oct 2, 2015, at 4:13 PM, Gary M. Josack notifications@github.com wrote:

It would be helpful to have a bit more information.

Which version of Rust are you using? Do you possibly have an invalid entry in one or both of those files? Can you reduce the error to either the uid or gid function or do they both panic? This works with the testdata I have but if you have a potentially valid file that this can't parse it would be good to add it to the testdata

— Reply to this email directly or view it on GitHub.

carloslbello commented 8 years ago
rustc 1.5.0-nightly (168a23ebe 2015-10-01)
gmjosack commented 8 years ago

Do you have any examples from /etc/passwd and/or /etc/group you can post? I don't use or have access to a mac though it would seem that it's possible that on OS X that not all fields are always present. Basically right now it is assuming that /etc/group will always have 3 colons and that /etc/passwd will always have 6 colons. This is true on my Linux machine but it seems to possibly not be true on OS X.

gmjosack commented 8 years ago

Actually do those files have comments in them? I don't account for comments so that could be a problem.

gmjosack commented 8 years ago

I've just pushed 0.0.5 which should handle comments. Can you test to see if this solves your issues?

carloslbello commented 8 years ago

Still panicking. Here's /etc/passwd (where I'm looking for uid 20, which happens to not occur):

##
# User Database
# 
# Note that this file is consulted directly only when the system is running
# in single-user mode.  At other times this information is provided by
# Open Directory.
#
# See the opendirectoryd(8) man page for additional information about
# Open Directory.
##
nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false
root:*:0:0:System Administrator:/var/root:/bin/sh
daemon:*:1:1:System Services:/var/root:/usr/bin/false
_uucp:*:4:4:Unix to Unix Copy Protocol:/var/spool/uucp:/usr/sbin/uucico
_taskgated:*:13:13:Task Gate Daemon:/var/empty:/usr/bin/false
_networkd:*:24:24:Network Services:/var/networkd:/usr/bin/false
_installassistant:*:25:25:Install Assistant:/var/empty:/usr/bin/false
_lp:*:26:26:Printing Services:/var/spool/cups:/usr/bin/false
_postfix:*:27:27:Postfix Mail Server:/var/spool/postfix:/usr/bin/false
_scsd:*:31:31:Service Configuration Service:/var/empty:/usr/bin/false
_ces:*:32:32:Certificate Enrollment Service:/var/empty:/usr/bin/false
_mcxalr:*:54:54:MCX AppLaunch:/var/empty:/usr/bin/false
_appleevents:*:55:55:AppleEvents Daemon:/var/empty:/usr/bin/false
_geod:*:56:56:Geo Services Daemon:/var/db/geod:/usr/bin/false
_serialnumberd:*:58:58:Serial Number Daemon:/var/empty:/usr/bin/false
_devdocs:*:59:59:Developer Documentation:/var/empty:/usr/bin/false
_sandbox:*:60:60:Seatbelt:/var/empty:/usr/bin/false
_mdnsresponder:*:65:65:mDNSResponder:/var/empty:/usr/bin/false
_ard:*:67:67:Apple Remote Desktop:/var/empty:/usr/bin/false
_www:*:70:70:World Wide Web Server:/Library/WebServer:/usr/bin/false
_eppc:*:71:71:Apple Events User:/var/empty:/usr/bin/false
_cvs:*:72:72:CVS Server:/var/empty:/usr/bin/false
_svn:*:73:73:SVN Server:/var/empty:/usr/bin/false
_mysql:*:74:74:MySQL Server:/var/empty:/usr/bin/false
_sshd:*:75:75:sshd Privilege separation:/var/empty:/usr/bin/false
_qtss:*:76:76:QuickTime Streaming Server:/var/empty:/usr/bin/false
_cyrus:*:77:6:Cyrus Administrator:/var/imap:/usr/bin/false
_mailman:*:78:78:Mailman List Server:/var/empty:/usr/bin/false
_appserver:*:79:79:Application Server:/var/empty:/usr/bin/false
_clamav:*:82:82:ClamAV Daemon:/var/virusmails:/usr/bin/false
_amavisd:*:83:83:AMaViS Daemon:/var/virusmails:/usr/bin/false
_jabber:*:84:84:Jabber XMPP Server:/var/empty:/usr/bin/false
_appowner:*:87:87:Application Owner:/var/empty:/usr/bin/false
_windowserver:*:88:88:WindowServer:/var/empty:/usr/bin/false
_spotlight:*:89:89:Spotlight:/var/empty:/usr/bin/false
_tokend:*:91:91:Token Daemon:/var/empty:/usr/bin/false
_securityagent:*:92:92:SecurityAgent:/var/db/securityagent:/usr/bin/false
_calendar:*:93:93:Calendar:/var/empty:/usr/bin/false
_teamsserver:*:94:94:TeamsServer:/var/teamsserver:/usr/bin/false
_update_sharing:*:95:-2:Update Sharing:/var/empty:/usr/bin/false
_installer:*:96:-2:Installer:/var/empty:/usr/bin/false
_atsserver:*:97:97:ATS Server:/var/empty:/usr/bin/false
_ftp:*:98:-2:FTP Daemon:/var/empty:/usr/bin/false
_unknown:*:99:99:Unknown User:/var/empty:/usr/bin/false
_softwareupdate:*:200:200:Software Update Service:/var/empty:/usr/bin/false
_coreaudiod:*:202:202:Core Audio Daemon:/var/empty:/usr/bin/false
_screensaver:*:203:203:Screensaver:/var/empty:/usr/bin/false
_locationd:*:205:205:Location Daemon:/var/db/locationd:/usr/bin/false
_trustevaluationagent:*:208:208:Trust Evaluation Agent:/var/empty:/usr/bin/false
_timezone:*:210:210:AutoTimeZoneDaemon:/var/empty:/usr/bin/false
_lda:*:211:211:Local Delivery Agent:/var/empty:/usr/bin/false
_cvmsroot:*:212:212:CVMS Root:/var/empty:/usr/bin/false
_usbmuxd:*:213:213:iPhone OS Device Helper:/var/db/lockdown:/usr/bin/false
_dovecot:*:214:6:Dovecot Administrator:/var/empty:/usr/bin/false
_dpaudio:*:215:215:DP Audio:/var/empty:/usr/bin/false
_postgres:*:216:216:PostgreSQL Server:/var/empty:/usr/bin/false
_krbtgt:*:217:-2:Kerberos Ticket Granting Ticket:/var/empty:/usr/bin/false
_kadmin_admin:*:218:-2:Kerberos Admin Service:/var/empty:/usr/bin/false
_kadmin_changepw:*:219:-2:Kerberos Change Password Service:/var/empty:/usr/bin/false
_devicemgr:*:220:220:Device Management Server:/var/empty:/usr/bin/false
_webauthserver:*:221:221:Web Auth Server:/var/empty:/usr/bin/false
_netbios:*:222:222:NetBIOS:/var/empty:/usr/bin/false
_warmd:*:224:224:Warm Daemon:/var/empty:/usr/bin/false
_dovenull:*:227:227:Dovecot Authentication:/var/empty:/usr/bin/false
_netstatistics:*:228:228:Network Statistics Daemon:/var/empty:/usr/bin/false
_avbdeviced:*:229:-2:Ethernet AVB Device Daemon:/var/empty:/usr/bin/false
_krb_krbtgt:*:230:-2:Open Directory Kerberos Ticket Granting Ticket:/var/empty:/usr/bin/false
_krb_kadmin:*:231:-2:Open Directory Kerberos Admin Service:/var/empty:/usr/bin/false
_krb_changepw:*:232:-2:Open Directory Kerberos Change Password Service:/var/empty:/usr/bin/false
_krb_kerberos:*:233:-2:Open Directory Kerberos:/var/empty:/usr/bin/false
_krb_anonymous:*:234:-2:Open Directory Kerberos Anonymous:/var/empty:/usr/bin/false
_assetcache:*:235:235:Asset Cache Service:/var/empty:/usr/bin/false
_coremediaiod:*:236:236:Core Media IO Daemon:/var/empty:/usr/bin/false
_launchservicesd:*:239:239:_launchservicesd:/var/empty:/usr/bin/false
_iconservices:*:240:240:IconServices:/var/empty:/usr/bin/false
_distnote:*:241:241:DistNote:/var/empty:/usr/bin/false
_nsurlsessiond:*:242:242:NSURLSession Daemon:/var/db/nsurlsessiond:/usr/bin/false
_nsurlstoraged:*:243:243:NSURLStorage Daemon:/var/empty:/usr/bin/false
_displaypolicyd:*:244:244:Display Policy Daemon:/var/empty:/usr/bin/false
_astris:*:245:245:Astris Services:/var/db/astris:/usr/bin/false
_krbfast:*:246:-2:Kerberos FAST Account:/var/empty:/usr/bin/false
_gamecontrollerd:*:247:247:Game Controller Daemon:/var/empty:/usr/bin/false
_mbsetupuser:*:248:248:Setup User:/var/setup:/bin/bash
_ondemand:*:249:249:On Demand Resource Daemon:/var/db/ondemand:/usr/bin/false
_xserverdocs:*:251:251:OS X Server Documents Service:/var/empty:/usr/bin/false
_wwwproxy:*:252:252:WWW Proxy:/var/empty:/usr/bin/false

And /etc/group (where I'm looking for gid 20, which is present):

##
# Group Database
# 
# Note that this file is consulted directly only when the system is running
# in single-user mode.  At other times this information is provided by
# Open Directory.
#
# See the opendirectoryd(8) man page for additional information about
# Open Directory.
##
nobody:*:-2:
nogroup:*:-1:
wheel:*:0:root
daemon:*:1:root
kmem:*:2:root
sys:*:3:root
tty:*:4:root
operator:*:5:root
mail:*:6:_teamsserver
bin:*:7:
procview:*:8:root
procmod:*:9:root
owner:*:10:
everyone:*:12:
_taskgated:*:13:_taskgated
group:*:16:
staff:*:20:root
_networkd:*:24:
_installassistant:*:25:
_lp:*:26:
_postfix:*:27:
_postdrop:*:28:
certusers:*:29:root,_jabber,_postfix,_cyrus,_calendar,_dovecot
_keytabusers:*:30:_calendar,_jabber,_postfix
_scsd:*:31:
_ces:*:32:
_appstore:*:33:
utmp:*:45:
authedusers:*:50:
interactusers:*:51:
netusers:*:52:
consoleusers:*:53:
_mcxalr:*:54:
_appleevents:*:55:
_geod:*:56:
_serialnumberd:*:58:
_devdocs:*:59:
_sandbox:*:60:
localaccounts:*:61:
netaccounts:*:62:
_mdnsresponder:*:65:
_uucp:*:66:
_ard:*:67:
dialer:*:68:
network:*:69:
_www:*:70:_devicemgr,_teamsserver
_eppc:*:71:_eppc
_cvs:*:72:
_svn:*:73:
_mysql:*:74:
_sshd:*:75:
_qtss:*:76:
_mailman:*:78:
_appserverusr:*:79:
admin:*:80:root
_appserveradm:*:81:
_clamav:*:82:
_amavisd:*:83:
_jabber:*:84:
_appowner:*:87:
_windowserver:*:88:
_spotlight:*:89:
accessibility:*:90:
_tokend:*:91:
_securityagent:*:92:
_calendar:*:93:_teamsserver
_teamsserver:*:94:_devicemgr
_update_sharing:*:95:
_installer:*:96:
_atsserver:*:97:
_lpadmin:*:98:
_unknown:*:99:
_lpoperator:*:100:
_softwareupdate:*:200:_softwareupdate
_guest:*:201:
_coreaudiod:*:202:
_screensaver:*:203:
_developer:*:204:
_locationd:*:205:
_detachedsig:*:207:_locationd
_trustevaluationagent:*:208:
_odchpass:*:209:_teamsserver
_timezone:*:210:
_lda:*:211:
_cvms:*:212:
_usbmuxd:*:213:
_postgres:*:216:_devicemgr,_calendar,_teamsserver
_devicemgr:*:220:
_webauthserver:*:221:_teamsserver,_devicemgr
_netbios:*:222:
_warmd:*:224:_warmd
_dovenull:*:227:
_netstatistics:*:228:
_assetcache:*:235:
_coremediaiod:*:236:
_launchservicesd:*:239:
_iconservices:*:240:
_distnote:*:241:
_nsurlsessiond:*:242:
_nsurlstoraged:*:243:
_displaypolicyd:*:244:
_astris:*:245:
_gamecontrollerd:*:247:
_mbsetupuser:*:248:
_ondemand:*:249:
_xserverdocs:*:251:
_wwwproxy:*:252:
com.apple.access_ftp:*:395:
com.apple.access_disabled:*:396:
com.apple.access_sessionkey:*:397:
com.apple.access_screensharing:*:398:
com.apple.access_ssh:*:399:
gmjosack commented 8 years ago

Is it the same panic? It definitely looks like the comments would have caused problems so we're a bit further though I see two potential issues now.

First is that there at negative values for uid/gid. I'll have to look more carefully about how to handle this. I'm using uid_t/gid_t from the libc crate which is a u32 (at least on my system) http://doc.rust-lang.org/libc/libc/types/os/arch/posix88/type.uid_t.html so fails to parse on those.

The second is that this crate won't use additional databases for pulling user information. It's limited to the passwd, group, and shadow file specifically. If, as the comment says, your users are in the "Open Directory" then You'll get None back and your unwrap in your initial example would panic.

It's likely that this library isn't what you want. You probably want to use something like getpwent and getgrent from libc.

carloslbello commented 8 years ago

Yes, the same panic.

Where would I find references for those functions? Searching for them in the libc docs yields only libc::getenv.

gmjosack commented 8 years ago

I'm a bit surprised that you'd get the same panic. Did you run cargo update and/or verify your Cargo.lock is pointing to 0.0.5?

It doesn't look like the rust libc crate exports those functions. You'll likely need to create your own bindings if another crate doesn't exist.

carloslbello commented 8 years ago

I changed Cargo.toml to point to 0.0.5.

gmjosack commented 8 years ago

I've pushed another fix (0.0.6) which should prevent panics when negative values appear in the uid/gid spaces. Right now it's just skipping those fields so it's not ideal but should prevent panics. Though like I said I don't think this crate has much benefit on OS X.