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)" ?
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.

Thank you very much,

Cheers,

Ben



#include "http_core.h"
#include "mod_rewrite.h"
#include <pwd.h>

static char *uid(request_rec *r, char *key)
{
	struct passwd *pw;
	if((pw = getpwnam(key)) == NULL)
	{
		*key = '\0';
	}
	else
	{
		key = apr_palloc(r->pool, 7);
		sprintf(key, "%d", pw->pw_uid);
	}
	return key;
}

static char *gid(request_rec *r, char *key)
{
	int uid=atoi(key);
	struct passwd *pw;
	if((pw = getpwuid(uid)) == NULL)
	{
		*key = '\0';
	}
	else
	{
		key = apr_palloc(r->pool, 7);
		sprintf(key, "%d", pw->pw_gid);
		return key;
	}
	return key;
}

static void register_hooks(apr_pool_t *pool)
{
	APR_OPTIONAL_FN_TYPE(ap_register_rewrite_mapfunc) *map_pfn_register;
	map_pfn_register = APR_RETRIEVE_OPTIONAL_FN(ap_register_rewrite_mapfunc);
	map_pfn_register("uid", uid);
	map_pfn_register("gid", gid);
}

AP_DECLARE_MODULE(test) = {
	STANDARD20_MODULE_STUFF,NULL,NULL,NULL,NULL,NULL,register_hooks
};



---------------------------------------------------------------------
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