Getpwnam()?

Before I try to write one, does a julia version already exist?

based on the documentation, there isn’t one in the official library. maybe calling that directly? Running External Programs · The Julia Language

Finally got time to implement:

#
# Simple module to call the getpwnam() function from libc and
# return the results.
#
# Example:
#   using Getpwnam
#   p = getpwnam(ENV["USER"])
#   @show(p)
#
# Mark Leisher <mleisher@cs.nmsu.edu>
# 06 December 2019
#

module Getpwnam

export getpwnam, Passwd

struct Passwd
    name::String
    pswd::String
    uid::UInt32
    gid::UInt32
    gecos::String
    dir::String
    shell::String
end

#
# Internal structure that gets converted to Passwd before being passed
# back from getpwnam.
#
struct passwd
    name::Ptr{UInt8}
    pswd::Ptr{UInt8}
    uid::Cuint
    gid::Cuint
    gecos::Ptr{UInt8}
    dir::Ptr{UInt8}
    shell::Ptr{UInt8}
end

function getpwnam(name::AbstractString)
    pptr = ccall(:getpwnam, Ptr{Ptr{Cvoid}}, (Cstring,), name)
    if pptr == C_NULL
        return nothing
    end
    p = unsafe_load(convert(Ptr{passwd}, pptr))
    #
    # Return the structure.
    #
    Passwd(unsafe_string(p.name),
           unsafe_string(p.pswd),
           p.uid,
           p.gid,
           unsafe_string(p.gecos),
           unsafe_string(p.dir),
           unsafe_string(p.shell))
end

end
3 Likes

I get the error:

could not load symbol "getpwnam":

I saw that there are some symbols defined with the prefix “jl_”, like e.g. “jl_getuid
The reason why I tried to use the code of @mleisher is the fact that I do not know how to determine information regarding the group ID and the user ID of the current user?

Why not just:

id=read(`id`,String)

Thanks for the proposal! Could you be more explicit? E.g. in form of an example?

Perhaps it needs some fine tuning for robustness:

julia> id=read(`id`,String)
"uid=1000(oheil) gid=1000(oheil) groups=1000(oheil),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev)\n"

julia> m=match(r"uid=(\d*)",id)
RegexMatch("uid=1000", 1="1000")

julia> uid=m.captures
1-element Vector{Union{Nothing, SubString{String}}}:
 "1000"

julia> m=match(r"gid=(\d*)",id)
RegexMatch("gid=1000", 1="1000")

julia> gid=m.captures
1-element Vector{Union{Nothing, SubString{String}}}:
 "1000"

And of course, as you try to use getpwnam, I assume that you are on a linux like OS.

@oheil Thanks for your snippet!
Ok, it is OS dependent, on linux it works, on MS Windows not.
It is the same with pptr = ccall(:getpwnam, Ptr{Ptr{Cvoid}}, (Cstring,), ENV["USER"]),
on linux it is possible to execute the command on Windows not :frowning: .
Meanwhile, I found the function: “Base.Libc.getpwuid()”, is there a chance to call this function?
Under Julia v1.7.1 on a “MS Windows 10”-system the call fails with the error message:
operation not supported on socket (ENOTSUP).

No, it’s OS specific. Windows has no UID and GID like Unix, and there is no getpwnam in Windows.

What is your real problem?
It sounds strange that you are on a Windows OS and looking for user ID and group ID. There are user names, principals, group names, and so on, but not an ID.

1 Like

To be honest I’am not familiar with the internals of different OS.
Does other functions in Julia exist, to figure out, what the rights are for a specific person on a MS WIN system?

I don’t know about any Julia functions to get windows user rights.
For Windows it’s probably best to run some powershell commands. I think you have to do some research for your specific task, because I don’t have too much experience with powershell.
This one I have just googled:

julia> run(`powershell '[System.Security.Principal.WindowsIdentity]::GetCurrent().Name'`)
WORKSTATION\Oheil
Process(`powershell '[System.Security.Principal.WindowsIdentity]::GetCurrent().Name'`, ProcessExited(0))
1 Like

Thanks again! Ok, I guess we have to wait a while, before such OS-specific thinks are available in Julia out-of-the box. Maybe,the Python-community was more active in this domain and we can use Python functions inside Julia.

@ellocco, download https://www.cs.nmsu.edu/~mleisher/julia/Getpwnam.jl

I’m looking into a pipe error on Windows now, but it may work for you. It worked the last time I used it three years ago.

It turns out there is an issue with using ‘break’ inside a ‘for line in eachline(filehandle)’ loop in julia 1.7.2 on Windows (but not on Linux or Mac OS). The getpwnam code at https://www.cs.nmsu.edu/~mleisher/julia/Getpwnam.jl should run with most versions of julia from 1.6 up on Windows now.

1 Like