Skip to content

Develop #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
*.exe
*.out
*.app
.idea
7 changes: 4 additions & 3 deletions libnss_http/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ LD_SONAME=-Wl,-soname,libnss_http.so.2
LIBRARY=libnss_http.so.2.0
LINKS=libnss_http.so.2 libnss_http.so

DESTDIR=/
PREFIX=$(DESTDIR)/usr
LIBDIR=$(PREFIX)/lib
LIBDIR=/lib64
BUILD=.libs

default: build
Expand All @@ -29,9 +27,12 @@ nss_http_services: nss_http-passwd nss_http-group nss_http-shadow

nss_http: nss_http_build_dir nss_http_services
$(CC) $(CFLAGS) -c nss_http.c -o $(BUILD)/nss_http.o
$(CC) $(CFLAGS) -c nss_http-config.c -o $(BUILD)/nss_http-config.o


$(CC) -shared $(LD_SONAME) -o $(BUILD)/$(LIBRARY) \
$(BUILD)/nss_http.o \
$(BUILD)/nss_http-config.o \
$(BUILD)/nss_http-passwd.o \
$(BUILD)/nss_http-group.o \
$(BUILD)/nss_http-shadow.o \
Expand Down
115 changes: 115 additions & 0 deletions libnss_http/nss_http-config.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#include "nss_http.h"

/* define ip and hostname struct*/
struct host
{
char hostname[128];
char ip[128];
};

void readconfig(struct config *configstruct, char *filename)
{
// fix httpser & port wrong result
memset(configstruct->httpserver, '\0', sizeof(configstruct->httpserver));
memset(configstruct->port, '\0', sizeof(configstruct->port));
memset(configstruct->debug, '\0', sizeof(configstruct->debug));
FILE *file = fopen (filename, "r");

if (file != NULL)
{
char line[MAXBUF];

while(fgets(line, sizeof(line), file) != NULL)
{
// remove \n
if(strlen(line) != sizeof(line)-1){line[strlen(line)-1] = '\0';}
char *cfline;

// escape comment line
if (strncmp(DELIM, line, strlen(DELIM)) == 0) continue;
cfline = strstr((char *)line,DELIM);
// pointer
cfline = cfline + strlen(DELIM);
char *part = strtok(line, DELIM);

if (strcmp(part, "HTTPSERVER") == 0)
{
memcpy(configstruct->httpserver,cfline,strlen(cfline));
}
else if (strcmp(part, "PORT") == 0)
{
memcpy(configstruct->port,cfline,strlen(cfline));
}
else if (strcmp(part, "DEBUG") == 0)
{
memcpy(configstruct->debug,cfline,strlen(cfline));
}
else if (strcmp(part, "TIMEOUT") == 0)
{
char *e;
errno=0;
// force convert string to long number
configstruct->timeout = strtoll(cfline, &e, 0);
}
}
fclose(file);
}
else
fprintf(stderr, "读取配置文件失败!");
}

/*Network part*/
void networks(struct host *hosts)
{
memset(hosts->ip, '\0', sizeof(hosts->ip));
memset(hosts->hostname, '\0', sizeof(hosts->hostname));
//get hostname
gethostname(hosts->hostname, 128);

//get default interface
FILE *f;
char line[100], *interface, *c;
f = fopen("/proc/net/route", "r");
while(fgets(line, 100, f))
{
interface = strtok(line , " \t");
c = strtok(NULL , " \t");
if(interface!=NULL && c!=NULL)
{
if(strcmp(c , "00000000") == 0) break;
}
}

/* get ip from default interface*/
int fd;
struct ifreq ifr;
fd = socket(AF_INET, SOCK_DGRAM, 0);
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
ioctl(fd, SIOCGIFADDR, &ifr);
close(fd);
snprintf(hosts->ip, 128, inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
}


void genurl(char* url, const char *type, const char *key)
{
struct config con;
struct host hosts;
readconfig(&con, CONFIG_FILE);
networks(&hosts);
if (strlen(key) == 0){
snprintf(url, 512, "http://%s:%s/%s?userid=%d&ip=%s&hostname=%s", con.httpserver, con.port, type,getuid(), hosts.ip, hosts.hostname);
}
else if ( strlen(key) != 0){
snprintf(url, 512, "http://%s:%s/%s?%s&userid=%d&ip=%s&hostname=%s", con.httpserver, con.port, type, key, getuid(), hosts.ip, hosts.hostname);
}
}

void debug_print(const char *func)
{
struct config con;
readconfig(&con, CONFIG_FILE);
if (strcmp("true", con.debug) == 0)
fprintf(stderr, "NSS DEBUG: Called %s \n", func);
}
18 changes: 14 additions & 4 deletions libnss_http/nss_http-group.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ static int ent_json_idx = 0;
static int
pack_group_struct(json_t *root, struct group *result, char *buffer, size_t buflen)
{

DEBUG_LOG;
char *next_buf = buffer;
size_t bufleft = buflen;

Expand Down Expand Up @@ -75,7 +75,8 @@ _nss_http_setgrent_locked(int stayopen)
json_t *json_root;
json_error_t json_error;

snprintf(url, 512, "http://" NSS_HTTP_SERVER ":" NSS_HTTP_PORT "/group");
//generator url
genurl(url, "group", "");

char *response = nss_http_request(url);
if (!response) {
Expand Down Expand Up @@ -104,6 +105,7 @@ _nss_http_setgrent_locked(int stayopen)
enum nss_status
_nss_http_setgrent(int stayopen)
{
DEBUG_LOG;
enum nss_status ret;
NSS_HTTP_LOCK();
ret = _nss_http_setgrent_locked(stayopen);
Expand All @@ -128,6 +130,7 @@ _nss_http_endgrent_locked(void)
enum nss_status
_nss_http_endgrent(void)
{
DEBUG_LOG;
enum nss_status ret;
NSS_HTTP_LOCK();
ret = _nss_http_endgrent_locked();
Expand Down Expand Up @@ -176,6 +179,7 @@ _nss_http_getgrent_r_locked(struct group *result, char *buffer, size_t buflen, i
enum nss_status
_nss_http_getgrent_r(struct group *result, char *buffer, size_t buflen, int *errnop)
{
DEBUG_LOG;
enum nss_status ret;
NSS_HTTP_LOCK();
ret = _nss_http_getgrent_r_locked(result, buffer, buflen, errnop);
Expand All @@ -192,7 +196,9 @@ _nss_http_getgrgid_r_locked(gid_t gid, struct group *result, char *buffer, size_
json_t *json_root;
json_error_t json_error;

snprintf(url, 512, "http://" NSS_HTTP_SERVER ":" NSS_HTTP_PORT "/group?gid=%d", gid);
char key[128];
sprintf(key, "gid=%d", gid);
genurl(url, "group", key);

char *response = nss_http_request(url);
if (!response) {
Expand Down Expand Up @@ -230,6 +236,7 @@ _nss_http_getgrgid_r_locked(gid_t gid, struct group *result, char *buffer, size_
enum nss_status
_nss_http_getgrgid_r(gid_t gid, struct group *result, char *buffer, size_t buflen, int *errnop)
{
DEBUG_LOG;
enum nss_status ret;
NSS_HTTP_LOCK();
ret = _nss_http_getgrgid_r_locked(gid, result, buffer, buflen, errnop);
Expand All @@ -245,7 +252,9 @@ _nss_http_getgrnam_r_locked(const char *name, struct group *result, char *buffer
json_t *json_root;
json_error_t json_error;

snprintf(url, 512, "http://" NSS_HTTP_SERVER ":" NSS_HTTP_PORT "/group?name=%s", name);
char key[128];
sprintf(key, "name=%s", name);
genurl(url, "group", key);

char *response = nss_http_request(url);
if (!response) {
Expand Down Expand Up @@ -284,6 +293,7 @@ _nss_http_getgrnam_r_locked(const char *name, struct group *result, char *buffer
enum nss_status
_nss_http_getgrnam_r(const char *name, struct group *result, char *buffer, size_t buflen, int *errnop)
{
DEBUG_LOG;
enum nss_status ret;
NSS_HTTP_LOCK();
ret = _nss_http_getgrnam_r_locked(name, result, buffer, buflen, errnop);
Expand Down
18 changes: 14 additions & 4 deletions libnss_http/nss_http-passwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ static int ent_json_idx = 0;
static int
pack_passwd_struct(json_t *root, struct passwd *result, char *buffer, size_t buflen)
{

DEBUG_LOG;
char *next_buf = buffer;
size_t bufleft = buflen;

Expand Down Expand Up @@ -85,7 +85,7 @@ _nss_http_setpwent_locked(int stayopen)
json_t *json_root;
json_error_t json_error;

snprintf(url, 512, "http://" NSS_HTTP_SERVER ":" NSS_HTTP_PORT "/passwd");
genurl(url, "passwd", "");

char *response = nss_http_request(url);
if (!response) {
Expand Down Expand Up @@ -114,6 +114,7 @@ _nss_http_setpwent_locked(int stayopen)
enum nss_status
_nss_http_setpwent(int stayopen)
{
DEBUG_LOG;
enum nss_status ret;
NSS_HTTP_LOCK();
ret = _nss_http_setpwent_locked(stayopen);
Expand All @@ -138,6 +139,7 @@ _nss_http_endpwent_locked(void)
enum nss_status
_nss_http_endpwent(void)
{
DEBUG_LOG;
enum nss_status ret;
NSS_HTTP_LOCK();
ret = _nss_http_endpwent_locked();
Expand Down Expand Up @@ -186,6 +188,7 @@ _nss_http_getpwent_r_locked(struct passwd *result, char *buffer, size_t buflen,
enum nss_status
_nss_http_getpwent_r(struct passwd *result, char *buffer, size_t buflen, int *errnop)
{
DEBUG_LOG;
enum nss_status ret;
NSS_HTTP_LOCK();
ret = _nss_http_getpwent_r_locked(result, buffer, buflen, errnop);
Expand All @@ -202,7 +205,10 @@ _nss_http_getpwuid_r_locked(uid_t uid, struct passwd *result, char *buffer, size
json_t *json_root;
json_error_t json_error;

snprintf(url, 512, "http://" NSS_HTTP_SERVER ":" NSS_HTTP_PORT "/passwd?uid=%d", uid);
char key[128];
sprintf(key, "uid=%d", uid);
genurl(url, "passwd", key);


char *response = nss_http_request(url);
if (!response) {
Expand Down Expand Up @@ -240,6 +246,7 @@ _nss_http_getpwuid_r_locked(uid_t uid, struct passwd *result, char *buffer, size
enum nss_status
_nss_http_getpwuid_r(uid_t uid, struct passwd *result, char *buffer, size_t buflen, int *errnop)
{
DEBUG_LOG;
enum nss_status ret;
NSS_HTTP_LOCK();
ret = _nss_http_getpwuid_r_locked(uid, result, buffer, buflen, errnop);
Expand All @@ -255,7 +262,9 @@ _nss_http_getpwnam_r_locked(const char *name, struct passwd *result, char *buffe
json_t *json_root;
json_error_t json_error;

snprintf(url, 512, "http://" NSS_HTTP_SERVER ":" NSS_HTTP_PORT "/passwd?name=%s", name);
char key[128];
sprintf(key, "name=%s", name);
genurl(url, "passwd", key);

char *response = nss_http_request(url);
if (!response) {
Expand Down Expand Up @@ -293,6 +302,7 @@ _nss_http_getpwnam_r_locked(const char *name, struct passwd *result, char *buffe
enum nss_status
_nss_http_getpwnam_r(const char *name, struct passwd *result, char *buffer, size_t buflen, int *errnop)
{
DEBUG_LOG;
enum nss_status ret;
NSS_HTTP_LOCK();
ret = _nss_http_getpwnam_r_locked(name, result, buffer, buflen, errnop);
Expand Down
15 changes: 11 additions & 4 deletions libnss_http/nss_http-shadow.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ static int ent_json_idx = 0;
static int
pack_shadow_struct(json_t *root, struct spwd *result, char *buffer, size_t buflen)
{

DEBUG_LOG;
char *next_buf = buffer;
size_t bufleft = buflen;

Expand Down Expand Up @@ -77,7 +77,8 @@ _nss_http_setspent_locked(int stayopen)
json_t *json_root;
json_error_t json_error;

snprintf(url, 512, "http://" NSS_HTTP_SERVER ":" NSS_HTTP_PORT "/shadow");

genurl(url, "shadow", "");

char *response = nss_http_request(url);
if (!response) {
Expand Down Expand Up @@ -106,6 +107,7 @@ _nss_http_setspent_locked(int stayopen)
enum nss_status
_nss_http_setspent(int stayopen)
{
DEBUG_LOG;
enum nss_status ret;
NSS_HTTP_LOCK();
ret = _nss_http_setspent_locked(stayopen);
Expand All @@ -130,6 +132,7 @@ _nss_http_endspent_locked(void)
enum nss_status
_nss_http_endspent(void)
{
DEBUG_LOG;
enum nss_status ret;
NSS_HTTP_LOCK();
ret = _nss_http_endspent_locked();
Expand Down Expand Up @@ -178,6 +181,7 @@ _nss_http_getspent_r_locked(struct spwd *result, char *buffer, size_t buflen, in
enum nss_status
_nss_http_getspent_r(struct spwd *result, char *buffer, size_t buflen, int *errnop)
{
DEBUG_LOG;
enum nss_status ret;
NSS_HTTP_LOCK();
ret = _nss_http_getspent_r_locked(result, buffer, buflen, errnop);
Expand All @@ -193,7 +197,9 @@ _nss_http_getspnam_r_locked(const char *name, struct spwd *result, char *buffer,
json_t *json_root;
json_error_t json_error;

snprintf(url, 512, "http://" NSS_HTTP_SERVER ":" NSS_HTTP_PORT "/shadow?name=%s", name);
char key[128];
sprintf(key, "name=%s", name);
genurl(url, "shadow", key);

char *response = nss_http_request(url);
if (!response) {
Expand Down Expand Up @@ -227,10 +233,11 @@ _nss_http_getspnam_r_locked(const char *name, struct spwd *result, char *buffer,
}


// Find a shadow by name
// Find a shadow entry by name
enum nss_status
_nss_http_getspnam_r(const char *name, struct spwd *result, char *buffer, size_t buflen, int *errnop)
{
DEBUG_LOG;
enum nss_status ret;
NSS_HTTP_LOCK();
ret = _nss_http_getspnam_r_locked(name, result, buffer, buflen, errnop);
Expand Down
Loading