Re: Get UID/GID from a username string

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



>>> Ben: have a look at how mod_rewrite accesses its own ap_register_rewrite_mapfunc via
>> 
>> So Rainer, I just quickly wrote my module, it works, many thanks for your help.
>> 
>> Below is my code, I just have 2 questions regarding it :
>> 1 - do I need to "free(pw)" ?
> 
> (...)
> So you must not free the returned pointer. Two of the man pages don't guarantee thread safety so to stay on the safe side you probably better use the reentrant variants getpwnam_r() and getpwuid_r(). For those you need to allocate the storage before calling them. You can either allocate storage from the request pool (apr_palloc(r->pool, N)) or using malloc/free. Modules typically prefer pool allocation. Pool allocation doesn't need to be freed. The request pool is automatically freed at the end of each request. Even if you need the memory only shorter, if it is not many bytes it should be OK to allocate from the request pool and let it free only at the end of the request.

Thank you for the reentrant tip, I'll use them you're right, we never know.
And nice thing that pool allocations are automatically freed :)

>> 2 - is "key = apr_palloc(r->pool, 7)" the right method ? Doing this I want to avoid buffer overflow in case of uid/gid greater in length than the key parameter.
> 
> Instead of
> 
> key = apr_palloc(r->pool, 7);
> printf(key, "%d", pw->pw_uid);
> 
> you can also use
> 
> key = apr_psprintf(r->pool, "%d", pw->pw_uid);
> 
> And if you are not going to use more complex formatting tokens
> 
> key = apr_ltoa(r->pool, (long)pw->pw_uid);

Very elegant !

>> Thank you very much,
> 
> You're welcome. Nice seeing you picking up that ball so quickly. Welcome to the world of module development :) Your example was a nice one, because you don't need much clutter to realize that module.

Very happy too to, it's totally worth it !
Will be much more performant than a rewritemap program.

I'm already thinking about my next module :)

Ben


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@xxxxxxxxxxxxxxxx
For additional commands, e-mail: users-help@xxxxxxxxxxxxxxxx





[Index of Archives]     [Open SSH Users]     [Linux ACPI]     [Linux Kernel]     [Linux Laptop]     [Kernel Newbies]     [Security]     [Netfilter]     [Bugtraq]     [Squid]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Samba]     [Video 4 Linux]     [Device Mapper]

  Powered by Linux