From 81f6d670901bbabeb7bfdfd6ee65c0c31c1cb457 Mon Sep 17 00:00:00 2001 From: "alex.wan" Date: Thu, 12 May 2016 11:54:27 +0800 Subject: [PATCH 01/15] add testing module --- .gitignore | 1 + libnss_http/Makefile | 4 ++-- libnss_http/nss_http-group.c | 2 +- libnss_http/nss_http-passwd.c | 2 +- libnss_http/nss_http-shadow.c | 2 +- libnss_http/nss_http.c | 13 ++++++++++++- libnss_http/nss_http.h | 4 +++- 7 files changed, 21 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 9fa3b1b..62abd91 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ *.exe *.out *.app +.idea \ No newline at end of file diff --git a/libnss_http/Makefile b/libnss_http/Makefile index 79d0a08..076b405 100644 --- a/libnss_http/Makefile +++ b/libnss_http/Makefile @@ -1,12 +1,12 @@ CC=gcc -CFLAGS=-Wall -Wstrict-prototypes -Werror -fPIC -std=c99 +CFLAGS=-Wall -Wstrict-prototypes -Werror -fPIC -std=c99 -m64 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 +PREFIX=$(DESTDIR)usr LIBDIR=$(PREFIX)/lib BUILD=.libs diff --git a/libnss_http/nss_http-group.c b/libnss_http/nss_http-group.c index 9f18e46..55ac6f1 100644 --- a/libnss_http/nss_http-group.c +++ b/libnss_http/nss_http-group.c @@ -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) { - + nss_log("start func pack_group_struct"); char *next_buf = buffer; size_t bufleft = buflen; diff --git a/libnss_http/nss_http-passwd.c b/libnss_http/nss_http-passwd.c index a788731..807e1fc 100644 --- a/libnss_http/nss_http-passwd.c +++ b/libnss_http/nss_http-passwd.c @@ -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) { - + nss_log("start func pack_passwd_struct"); char *next_buf = buffer; size_t bufleft = buflen; diff --git a/libnss_http/nss_http-shadow.c b/libnss_http/nss_http-shadow.c index 1169256..f11fe0d 100644 --- a/libnss_http/nss_http-shadow.c +++ b/libnss_http/nss_http-shadow.c @@ -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) { - + nss_log("start func pack_shadow_struct"); char *next_buf = buffer; size_t bufleft = buflen; diff --git a/libnss_http/nss_http.c b/libnss_http/nss_http.c index bbaaf66..d27cab2 100644 --- a/libnss_http/nss_http.c +++ b/libnss_http/nss_http.c @@ -19,7 +19,7 @@ static size_t write_response(void *ptr, size_t size, size_t nmemb, void *stream) { struct response *result = (struct response *)stream; size_t required_len = result->pos + size * nmemb; - + nss_log("start write_response"); if(required_len >= NSS_HTTP_INITIAL_BUFFER_SIZE - 1) { if (required_len < NSS_HTTP_MAX_BUFFER_SIZE) @@ -31,6 +31,7 @@ static size_t write_response(void *ptr, size_t size, size_t nmemb, void *stream) } } else { // Request data is too large. + nss_log("Request data is too large"); return 0; } } @@ -83,6 +84,7 @@ nss_http_request(const char *url) return data; error: + nss_log("write_response occured error"); if(data) free(data); if(curl) @@ -94,3 +96,12 @@ nss_http_request(const char *url) return NULL; } +/*my log function*/ +void nss_log(const char *log) +{ + openlog("alexlog", LOG_PID|LOG_CONS, LOG_USER); + syslog(LOG_INFO, log); + closelog(); +} + + diff --git a/libnss_http/nss_http.h b/libnss_http/nss_http.h index 7416dac..fbf82b8 100644 --- a/libnss_http/nss_http.h +++ b/libnss_http/nss_http.h @@ -12,8 +12,10 @@ #include #include #include +#include +#include -#define NSS_HTTP_SERVER "localhost" +#define NSS_HTTP_SERVER "127.0.0.1" #define NSS_HTTP_PORT "9669" #define NSS_HTTP_INITIAL_BUFFER_SIZE (256 * 1024) /* 256 KB */ #define NSS_HTTP_MAX_BUFFER_SIZE (10 * 1024 * 1024) /* 10 MB */ From b8caecb72860eae3e390dfa1a34bb57cab537063 Mon Sep 17 00:00:00 2001 From: "alex.wan" Date: Thu, 12 May 2016 12:03:06 +0800 Subject: [PATCH 02/15] makefile --- libnss_http/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libnss_http/Makefile b/libnss_http/Makefile index 076b405..91c168c 100644 --- a/libnss_http/Makefile +++ b/libnss_http/Makefile @@ -7,7 +7,7 @@ LINKS=libnss_http.so.2 libnss_http.so DESTDIR=/ PREFIX=$(DESTDIR)usr -LIBDIR=$(PREFIX)/lib +LIBDIR=$(PREFIX)/lib64 BUILD=.libs default: build From 44ef42d7edb966852f6e31809166b99bb7d66015 Mon Sep 17 00:00:00 2001 From: "alex.wan" Date: Thu, 12 May 2016 12:06:31 +0800 Subject: [PATCH 03/15] add extern nss-log --- libnss_http/nss_http.h | 1 + 1 file changed, 1 insertion(+) diff --git a/libnss_http/nss_http.h b/libnss_http/nss_http.h index fbf82b8..a718a47 100644 --- a/libnss_http/nss_http.h +++ b/libnss_http/nss_http.h @@ -22,5 +22,6 @@ extern char *nss_http_request(const char *); extern size_t j_strlen(json_t *); +extern void nss_log(const char *log); #endif /* NSS_HTTP_H */ From d32ed266ef7be95c8b630249a703b177e6bb4930 Mon Sep 17 00:00:00 2001 From: "alex.wan" Date: Fri, 13 May 2016 13:20:00 +0800 Subject: [PATCH 04/15] fix --- libnss_http/nss_http-group.c | 1 - libnss_http/nss_http-passwd.c | 1 - libnss_http/nss_http-shadow.c | 3 +-- libnss_http/nss_http.c | 16 ++-------------- libnss_http/nss_http.h | 3 --- 5 files changed, 3 insertions(+), 21 deletions(-) diff --git a/libnss_http/nss_http-group.c b/libnss_http/nss_http-group.c index 55ac6f1..4cfcfec 100644 --- a/libnss_http/nss_http-group.c +++ b/libnss_http/nss_http-group.c @@ -13,7 +13,6 @@ static int ent_json_idx = 0; static int pack_group_struct(json_t *root, struct group *result, char *buffer, size_t buflen) { - nss_log("start func pack_group_struct"); char *next_buf = buffer; size_t bufleft = buflen; diff --git a/libnss_http/nss_http-passwd.c b/libnss_http/nss_http-passwd.c index 807e1fc..a11db81 100644 --- a/libnss_http/nss_http-passwd.c +++ b/libnss_http/nss_http-passwd.c @@ -13,7 +13,6 @@ static int ent_json_idx = 0; static int pack_passwd_struct(json_t *root, struct passwd *result, char *buffer, size_t buflen) { - nss_log("start func pack_passwd_struct"); char *next_buf = buffer; size_t bufleft = buflen; diff --git a/libnss_http/nss_http-shadow.c b/libnss_http/nss_http-shadow.c index f11fe0d..00904b3 100644 --- a/libnss_http/nss_http-shadow.c +++ b/libnss_http/nss_http-shadow.c @@ -13,7 +13,6 @@ static int ent_json_idx = 0; static int pack_shadow_struct(json_t *root, struct spwd *result, char *buffer, size_t buflen) { - nss_log("start func pack_shadow_struct"); char *next_buf = buffer; size_t bufleft = buflen; @@ -227,7 +226,7 @@ _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) { diff --git a/libnss_http/nss_http.c b/libnss_http/nss_http.c index d27cab2..dcdca67 100644 --- a/libnss_http/nss_http.c +++ b/libnss_http/nss_http.c @@ -14,12 +14,11 @@ j_strlen(json_t *str) return strlen(json_string_value(str)); } - +//unknow static size_t write_response(void *ptr, size_t size, size_t nmemb, void *stream) { struct response *result = (struct response *)stream; size_t required_len = result->pos + size * nmemb; - nss_log("start write_response"); if(required_len >= NSS_HTTP_INITIAL_BUFFER_SIZE - 1) { if (required_len < NSS_HTTP_MAX_BUFFER_SIZE) @@ -31,7 +30,6 @@ static size_t write_response(void *ptr, size_t size, size_t nmemb, void *stream) } } else { // Request data is too large. - nss_log("Request data is too large"); return 0; } } @@ -43,6 +41,7 @@ static size_t write_response(void *ptr, size_t size, size_t nmemb, void *stream) } +// char * nss_http_request(const char *url) { @@ -84,7 +83,6 @@ nss_http_request(const char *url) return data; error: - nss_log("write_response occured error"); if(data) free(data); if(curl) @@ -95,13 +93,3 @@ nss_http_request(const char *url) return NULL; } - -/*my log function*/ -void nss_log(const char *log) -{ - openlog("alexlog", LOG_PID|LOG_CONS, LOG_USER); - syslog(LOG_INFO, log); - closelog(); -} - - diff --git a/libnss_http/nss_http.h b/libnss_http/nss_http.h index a718a47..2254bad 100644 --- a/libnss_http/nss_http.h +++ b/libnss_http/nss_http.h @@ -12,8 +12,6 @@ #include #include #include -#include -#include #define NSS_HTTP_SERVER "127.0.0.1" #define NSS_HTTP_PORT "9669" @@ -22,6 +20,5 @@ extern char *nss_http_request(const char *); extern size_t j_strlen(json_t *); -extern void nss_log(const char *log); #endif /* NSS_HTTP_H */ From f70c5b9d2d96017a6240478cd13b774bec5f129d Mon Sep 17 00:00:00 2001 From: "alex.wan" Date: Fri, 13 May 2016 13:20:45 +0800 Subject: [PATCH 05/15] fix --- libnss_http/Makefile | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/libnss_http/Makefile b/libnss_http/Makefile index 91c168c..fb3a125 100644 --- a/libnss_http/Makefile +++ b/libnss_http/Makefile @@ -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)/lib64 +LIBDIR=/lib64 BUILD=.libs default: build From 82159d6355225cf48f02e3d7916c0936267a3de8 Mon Sep 17 00:00:00 2001 From: "alex.wan" Date: Wed, 18 May 2016 18:20:15 +0800 Subject: [PATCH 06/15] add debug model --- libnss_http/Makefile | 2 +- libnss_http/nss_http-config.c | 134 ++++++++++++++++++++++++++++++++++ libnss_http/nss_http-group.c | 6 ++ libnss_http/nss_http-passwd.c | 6 ++ libnss_http/nss_http-shadow.c | 5 ++ libnss_http/nss_http.c | 2 + libnss_http/nss_http.h | 19 ++++- mannul.md | 49 +++++++++++++ 8 files changed, 220 insertions(+), 3 deletions(-) create mode 100644 libnss_http/nss_http-config.c create mode 100644 mannul.md diff --git a/libnss_http/Makefile b/libnss_http/Makefile index fb3a125..8926151 100644 --- a/libnss_http/Makefile +++ b/libnss_http/Makefile @@ -1,5 +1,5 @@ CC=gcc -CFLAGS=-Wall -Wstrict-prototypes -Werror -fPIC -std=c99 -m64 +CFLAGS=-Wall -Wstrict-prototypes -Werror -fPIC -std=c99 LD_SONAME=-Wl,-soname,libnss_http.so.2 LIBRARY=libnss_http.so.2.0 diff --git a/libnss_http/nss_http-config.c b/libnss_http/nss_http-config.c new file mode 100644 index 0000000..397e374 --- /dev/null +++ b/libnss_http/nss_http-config.c @@ -0,0 +1,134 @@ +#include "nss_http.h" + +//settings +#define MAXBUF 1024 +#define DELIM "=" + +// define nss_http.conf key&value model +struct config +{ + char httpserver[MAXBUF]; + char port[MAXBUF]; + char debug[MAXBUF]; +}; + +/* define ip and hostname struct*/ +struct host +{ + char hostname[128]; + char ip[128]; +}; + +struct config readconfig(char *filename) +{ + struct config configstruct; + 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)); + } + } + fclose(file); + } + else + fprintf(stderr, "读取配置文件失败!"); + + return configstruct; +} + +/*Network part*/ +void networks(struct host *hosts) +{ + //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); + /* I want to get an IPv4 IP address */ + ifr.ifr_addr.sa_family = AF_INET; + /* I want IP address attached to "eth0" */ + strncpy(ifr.ifr_name, interface, IFNAMSIZ-1); + ioctl(fd, SIOCGIFADDR, &ifr); + close(fd); + /* display result */ + 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 = readconfig(CONFIG_FILE); + struct host hosts; + networks(&hosts); + char hostname[512]; + char ip[128]; + gethostname(hostname, 512); + + if (strlen(key) == 0){ + snprintf(url, 512, "http://%s:%s/%s?uid=%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&uid=%d&ip=%s&hostname=%s", con.httpserver, con.port, type, key, getuid(), hosts.ip, hosts.hostname); + } +} + +void debug_print(const char *func) +{ + struct config mycon = readconfig(CONFIG_FILE); + if (strcmp("true", mycon.debug) == 0) + fprintf(stderr, "NSS DEBUG: Called %s \n", func); +} + +// +//int main(int argc, char **argv) +//{ +// struct config configstruct; +// char url[512]; +// genurl(url, "passwd", ""); +// printf("%s\n", url); +// +// return 0; +//} \ No newline at end of file diff --git a/libnss_http/nss_http-group.c b/libnss_http/nss_http-group.c index 4cfcfec..4cda778 100644 --- a/libnss_http/nss_http-group.c +++ b/libnss_http/nss_http-group.c @@ -13,6 +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; @@ -103,6 +104,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); @@ -127,6 +129,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(); @@ -175,6 +178,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); @@ -229,6 +233,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); @@ -283,6 +288,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); diff --git a/libnss_http/nss_http-passwd.c b/libnss_http/nss_http-passwd.c index a11db81..b97c44d 100644 --- a/libnss_http/nss_http-passwd.c +++ b/libnss_http/nss_http-passwd.c @@ -13,6 +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; @@ -113,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); @@ -137,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(); @@ -185,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); @@ -239,6 +243,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); @@ -292,6 +297,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); diff --git a/libnss_http/nss_http-shadow.c b/libnss_http/nss_http-shadow.c index 00904b3..a3370fe 100644 --- a/libnss_http/nss_http-shadow.c +++ b/libnss_http/nss_http-shadow.c @@ -13,6 +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; @@ -105,6 +106,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); @@ -129,6 +131,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(); @@ -177,6 +180,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); @@ -230,6 +234,7 @@ _nss_http_getspnam_r_locked(const char *name, struct spwd *result, char *buffer, 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); diff --git a/libnss_http/nss_http.c b/libnss_http/nss_http.c index dcdca67..e1c6b11 100644 --- a/libnss_http/nss_http.c +++ b/libnss_http/nss_http.c @@ -17,6 +17,7 @@ j_strlen(json_t *str) //unknow static size_t write_response(void *ptr, size_t size, size_t nmemb, void *stream) { + DEBUG_LOG; struct response *result = (struct response *)stream; size_t required_len = result->pos + size * nmemb; if(required_len >= NSS_HTTP_INITIAL_BUFFER_SIZE - 1) @@ -45,6 +46,7 @@ static size_t write_response(void *ptr, size_t size, size_t nmemb, void *stream) char * nss_http_request(const char *url) { + DEBUG_LOG; CURL *curl = NULL; CURLcode status; struct curl_slist *headers = NULL; diff --git a/libnss_http/nss_http.h b/libnss_http/nss_http.h index 2254bad..44d4d8b 100644 --- a/libnss_http/nss_http.h +++ b/libnss_http/nss_http.h @@ -12,13 +12,28 @@ #include #include #include +//networks +#include +#include +#include +#include +#include +#include -#define NSS_HTTP_SERVER "127.0.0.1" -#define NSS_HTTP_PORT "9669" +//config file location +#define CONFIG_FILE "/etc/nss_http.conf" +//debug mode +#define DEBUG_LOG debug_print(__FUNCTION__) + +//buffer size #define NSS_HTTP_INITIAL_BUFFER_SIZE (256 * 1024) /* 256 KB */ #define NSS_HTTP_MAX_BUFFER_SIZE (10 * 1024 * 1024) /* 10 MB */ +//funcitons extern char *nss_http_request(const char *); extern size_t j_strlen(json_t *); +extern struct config get_config(char *filename); +extern void debug_print(const char *func); + #endif /* NSS_HTTP_H */ diff --git a/mannul.md b/mannul.md new file mode 100644 index 0000000..562180c --- /dev/null +++ b/mannul.md @@ -0,0 +1,49 @@ +### NSS(name service switch)配置 +``` +passwd: files http +shadow: files http +group: files http +``` +### NSS 模块不必实现所有的DB访问需求 + No service module is forced to have functions for all databases and all kinds to access them. +If a function is not available it is simply treated as if the function would return unavail + +### 系统查询http模块的规则: /lib64/libnss_modulename.so +如:/lib64/libnss_files.so, /lib64/libnss_http.so + +### 函数命名规则 +getxxent: 例如:getgrent, getpwent which means get group entries and password entries +getxxent_r: 例如:getgrent_r + +### 函数解析 + +http://man.chinaunix.net/develop/c&c++/linux_c/function/07.html + +getgrent: 返回一个指向group结构体的指针 +第一次call返回第一条group记录,以此类推,知道返回null或者错误 +setgrent: 使getgrent重新搜索group文件,也就是说从第一条开始 +endgrent: 当group上的所有操作完成后, 关闭group文件 + + + +###setgrent()用来将getgrent()的读写地址指回组文件开头。 + + +endgrent 关闭组文件(由getgrent打开的密码文件) + +endpwent 关闭密码文件(有getpwent打开的密码文件) + +fgetent 从指定的文件来读取组格式 +fgetgrent()会从参数stream指定的文件读取一行数据,然后以group结构将该数据返回。参数stream所指定的文件必须和、etc/group相同的格式。group结构定义请参考getgrent()。 +返回值,返回group结构数据,如果返回NULL则表示已无数据,或有错误发生。 + +fgetpwent(从指定的文件来读取密码格式) +fgetpwent()会从参数stream指定的文件读取一行数据,然后以passwd结构将该数据返回。参数stream所指定的文件必须和/etc/passwd相同的格式。passwd结构定义请参考getpwent()。 +返回值 返回passwd结构数据,如果返回NULL则表示已无数据,或有错误发生。 + +getegid(取得有效的组识别码) + +### 对于加_r的解释, 大概意思就是可以重入的函数,类似cursor,一次读取一条,下一次读取下一条 而不加r的函数就是一次性读取所有的数据 +The functions getgrent_r() and fgetgrent_r() are the reentrant versions of getgrent(3) and fgetgrent(3). +The former reads the next group entry from the stream initialized by setgrent(3). The latter reads the next group entry from the stream fp. + From 99562f9a1ffbbce020a63f1a0e7f3ba9763d379a Mon Sep 17 00:00:00 2001 From: "alex.wan" Date: Wed, 18 May 2016 19:03:14 +0800 Subject: [PATCH 07/15] fix genurl --- libnss_http/nss_http-group.c | 16 +++++++++++++--- libnss_http/nss_http-passwd.c | 14 +++++++++++--- libnss_http/nss_http-shadow.c | 9 +++++++-- libnss_http/nss_http.h | 1 + 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/libnss_http/nss_http-group.c b/libnss_http/nss_http-group.c index 4cda778..08966cf 100644 --- a/libnss_http/nss_http-group.c +++ b/libnss_http/nss_http-group.c @@ -75,7 +75,10 @@ _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", ""); + +// snprintf(url, 512, "http://" NSS_HTTP_SERVER ":" NSS_HTTP_PORT "/group"); char *response = nss_http_request(url); if (!response) { @@ -195,7 +198,11 @@ _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); + +// snprintf(url, 512, "http://" NSS_HTTP_SERVER ":" NSS_HTTP_PORT "/group?gid=%d", gid); char *response = nss_http_request(url); if (!response) { @@ -249,7 +256,10 @@ _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); +// 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) { diff --git a/libnss_http/nss_http-passwd.c b/libnss_http/nss_http-passwd.c index b97c44d..dc31e0a 100644 --- a/libnss_http/nss_http-passwd.c +++ b/libnss_http/nss_http-passwd.c @@ -85,7 +85,9 @@ _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", ""); + +// snprintf(url, 512, "http://" NSS_HTTP_SERVER ":" NSS_HTTP_PORT "/passwd"); char *response = nss_http_request(url); if (!response) { @@ -205,7 +207,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); +// snprintf(url, 512, "http://" NSS_HTTP_SERVER ":" NSS_HTTP_PORT "/passwd?uid=%d", uid); char *response = nss_http_request(url); if (!response) { @@ -259,7 +264,10 @@ _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); +// snprintf(url, 512, "http://" NSS_HTTP_SERVER ":" NSS_HTTP_PORT "/passwd?name=%s", name); char *response = nss_http_request(url); if (!response) { diff --git a/libnss_http/nss_http-shadow.c b/libnss_http/nss_http-shadow.c index a3370fe..e8fbe82 100644 --- a/libnss_http/nss_http-shadow.c +++ b/libnss_http/nss_http-shadow.c @@ -77,7 +77,9 @@ _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", ""); +// snprintf(url, 512, "http://" NSS_HTTP_SERVER ":" NSS_HTTP_PORT "/shadow"); char *response = nss_http_request(url); if (!response) { @@ -196,7 +198,10 @@ _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); +// snprintf(url, 512, "http://" NSS_HTTP_SERVER ":" NSS_HTTP_PORT "/shadow?name=%s", name); char *response = nss_http_request(url); if (!response) { diff --git a/libnss_http/nss_http.h b/libnss_http/nss_http.h index 44d4d8b..1b6dd34 100644 --- a/libnss_http/nss_http.h +++ b/libnss_http/nss_http.h @@ -34,6 +34,7 @@ extern char *nss_http_request(const char *); extern size_t j_strlen(json_t *); extern struct config get_config(char *filename); extern void debug_print(const char *func); +extern void genurl(char* url, const char *type, const char *key); #endif /* NSS_HTTP_H */ From 3c437df179715be892ad3f572a441a9af8818f1e Mon Sep 17 00:00:00 2001 From: "alex.wan" Date: Wed, 18 May 2016 19:04:34 +0800 Subject: [PATCH 08/15] makefile --- libnss_http/Makefile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libnss_http/Makefile b/libnss_http/Makefile index 8926151..764c886 100644 --- a/libnss_http/Makefile +++ b/libnss_http/Makefile @@ -26,10 +26,13 @@ nss_http-shadow: 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-config.c -o $(BUILD)/nss_http-config.o $(CC) $(CFLAGS) -c nss_http.c -o $(BUILD)/nss_http.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 \ From 69c744bebcc012e6de19fc0006ab5ab504b048c1 Mon Sep 17 00:00:00 2001 From: "alex.wan" Date: Wed, 18 May 2016 19:54:21 +0800 Subject: [PATCH 09/15] fix c99 mode --- libnss_http/Makefile | 2 +- libnss_http/nss_http-config.c | 16 +--------------- libnss_http/nss_http.h | 3 +++ 3 files changed, 5 insertions(+), 16 deletions(-) diff --git a/libnss_http/Makefile b/libnss_http/Makefile index 764c886..a97c4fe 100644 --- a/libnss_http/Makefile +++ b/libnss_http/Makefile @@ -26,8 +26,8 @@ nss_http-shadow: 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-config.c -o $(BUILD)/nss_http-config.o $(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) \ diff --git a/libnss_http/nss_http-config.c b/libnss_http/nss_http-config.c index 397e374..c2727f2 100644 --- a/libnss_http/nss_http-config.c +++ b/libnss_http/nss_http-config.c @@ -103,9 +103,6 @@ void genurl(char* url, const char *type, const char *key) struct config con = readconfig(CONFIG_FILE); struct host hosts; networks(&hosts); - char hostname[512]; - char ip[128]; - gethostname(hostname, 512); if (strlen(key) == 0){ snprintf(url, 512, "http://%s:%s/%s?uid=%d&ip=%s&hostname=%s", con.httpserver, con.port, type,getuid(), hosts.ip, hosts.hostname); @@ -120,15 +117,4 @@ void debug_print(const char *func) struct config mycon = readconfig(CONFIG_FILE); if (strcmp("true", mycon.debug) == 0) fprintf(stderr, "NSS DEBUG: Called %s \n", func); -} - -// -//int main(int argc, char **argv) -//{ -// struct config configstruct; -// char url[512]; -// genurl(url, "passwd", ""); -// printf("%s\n", url); -// -// return 0; -//} \ No newline at end of file +} \ No newline at end of file diff --git a/libnss_http/nss_http.h b/libnss_http/nss_http.h index 1b6dd34..1b3cb4a 100644 --- a/libnss_http/nss_http.h +++ b/libnss_http/nss_http.h @@ -1,6 +1,9 @@ #ifndef NSS_HTTP_H #define NSS_HTTP_H +//gethostname not work fine on c99 mode +#define _BSD_SOURCE + #include #include #include From b4f0ae88f0ba12ad723c63aed5f88b360f6cffad Mon Sep 17 00:00:00 2001 From: "alex.wan" Date: Wed, 18 May 2016 20:21:11 +0800 Subject: [PATCH 10/15] channge uid to userid --- libnss_http/nss_http-config.c | 4 ++-- libnss_http/nss_http.h | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/libnss_http/nss_http-config.c b/libnss_http/nss_http-config.c index c2727f2..63e5a41 100644 --- a/libnss_http/nss_http-config.c +++ b/libnss_http/nss_http-config.c @@ -105,10 +105,10 @@ void genurl(char* url, const char *type, const char *key) networks(&hosts); if (strlen(key) == 0){ - snprintf(url, 512, "http://%s:%s/%s?uid=%d&ip=%s&hostname=%s", con.httpserver, con.port, type,getuid(), hosts.ip, hosts.hostname); + 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&uid=%d&ip=%s&hostname=%s", con.httpserver, con.port, type, key, getuid(), hosts.ip, hosts.hostname); + 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); } } diff --git a/libnss_http/nss_http.h b/libnss_http/nss_http.h index 1b3cb4a..c63002a 100644 --- a/libnss_http/nss_http.h +++ b/libnss_http/nss_http.h @@ -15,6 +15,7 @@ #include #include #include +#include //networks #include #include From 4c5ceed53af2a64abbad5ff5a733c7da51d9c382 Mon Sep 17 00:00:00 2001 From: "alex.wan" Date: Wed, 18 May 2016 20:56:19 +0800 Subject: [PATCH 11/15] fix port&httpserver dirty data --- libnss_http/nss_http-config.c | 33 +++++++++++++++++++-------------- libnss_http/nss_http-group.c | 5 ----- libnss_http/nss_http-passwd.c | 5 +---- libnss_http/nss_http-shadow.c | 2 -- 4 files changed, 20 insertions(+), 25 deletions(-) diff --git a/libnss_http/nss_http-config.c b/libnss_http/nss_http-config.c index 63e5a41..033a6e6 100644 --- a/libnss_http/nss_http-config.c +++ b/libnss_http/nss_http-config.c @@ -19,9 +19,8 @@ struct host char ip[128]; }; -struct config readconfig(char *filename) +void readconfig(struct config *configstruct, char *filename) { - struct config configstruct; FILE *file = fopen (filename, "r"); if (file != NULL) @@ -43,23 +42,21 @@ struct config readconfig(char *filename) if (strcmp(part, "HTTPSERVER") == 0) { - memcpy(configstruct.httpserver,cfline,strlen(cfline)); + memcpy(configstruct->httpserver,cfline,strlen(cfline)); } else if (strcmp(part, "PORT") == 0) { - memcpy(configstruct.port,cfline,strlen(cfline)); + memcpy(configstruct->port,cfline,strlen(cfline)); } else if (strcmp(part, "DEBUG") == 0) { - memcpy(configstruct.debug,cfline,strlen(cfline)); + memcpy(configstruct->debug,cfline,strlen(cfline)); } } fclose(file); } else fprintf(stderr, "读取配置文件失败!"); - - return configstruct; } /*Network part*/ @@ -86,22 +83,25 @@ void networks(struct host *hosts) int fd; struct ifreq ifr; fd = socket(AF_INET, SOCK_DGRAM, 0); - /* I want to get an IPv4 IP address */ ifr.ifr_addr.sa_family = AF_INET; - /* I want IP address attached to "eth0" */ strncpy(ifr.ifr_name, interface, IFNAMSIZ-1); ioctl(fd, SIOCGIFADDR, &ifr); close(fd); - /* display result */ 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 = readconfig(CONFIG_FILE); + struct config con; struct host hosts; + // fix httpser & port wrong result + memset(con.httpserver, '\0', sizeof(con.httpserver)); + memset(con.port, '\0', sizeof(con.port)); + memset(con.debug, '\0', sizeof(con.debug)); + + readconfig(&con, CONFIG_FILE); + memset(hosts, '\0', sizeof(hosts)); networks(&hosts); if (strlen(key) == 0){ @@ -114,7 +114,12 @@ void genurl(char* url, const char *type, const char *key) void debug_print(const char *func) { - struct config mycon = readconfig(CONFIG_FILE); - if (strcmp("true", mycon.debug) == 0) + struct config con; + memset(con.httpserver, '\0', sizeof(con.httpserver)); + memset(con.port, '\0', sizeof(con.port)); + memset(con.debug, '\0', sizeof(con.debug)); + + readconfig(&con, CONFIG_FILE); + if (strcmp("true", con.debug) == 0) fprintf(stderr, "NSS DEBUG: Called %s \n", func); } \ No newline at end of file diff --git a/libnss_http/nss_http-group.c b/libnss_http/nss_http-group.c index 08966cf..a7aafed 100644 --- a/libnss_http/nss_http-group.c +++ b/libnss_http/nss_http-group.c @@ -78,8 +78,6 @@ _nss_http_setgrent_locked(int stayopen) //generator url genurl(url, "group", ""); -// snprintf(url, 512, "http://" NSS_HTTP_SERVER ":" NSS_HTTP_PORT "/group"); - char *response = nss_http_request(url); if (!response) { return NSS_STATUS_UNAVAIL; @@ -202,8 +200,6 @@ _nss_http_getgrgid_r_locked(gid_t gid, struct group *result, char *buffer, size_ sprintf(key, "gid=%d", gid); genurl(url, "group", key); -// snprintf(url, 512, "http://" NSS_HTTP_SERVER ":" NSS_HTTP_PORT "/group?gid=%d", gid); - char *response = nss_http_request(url); if (!response) { *errnop = ENOENT; @@ -256,7 +252,6 @@ _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); diff --git a/libnss_http/nss_http-passwd.c b/libnss_http/nss_http-passwd.c index dc31e0a..17e1fb8 100644 --- a/libnss_http/nss_http-passwd.c +++ b/libnss_http/nss_http-passwd.c @@ -87,8 +87,6 @@ _nss_http_setpwent_locked(int stayopen) genurl(url, "passwd", ""); -// snprintf(url, 512, "http://" NSS_HTTP_SERVER ":" NSS_HTTP_PORT "/passwd"); - char *response = nss_http_request(url); if (!response) { return NSS_STATUS_UNAVAIL; @@ -210,7 +208,7 @@ _nss_http_getpwuid_r_locked(uid_t uid, struct passwd *result, char *buffer, size char key[128]; sprintf(key, "uid=%d", uid); genurl(url, "passwd", key); -// snprintf(url, 512, "http://" NSS_HTTP_SERVER ":" NSS_HTTP_PORT "/passwd?uid=%d", uid); + char *response = nss_http_request(url); if (!response) { @@ -267,7 +265,6 @@ _nss_http_getpwnam_r_locked(const char *name, struct passwd *result, char *buffe char key[128]; sprintf(key, "name=%s", name); genurl(url, "passwd", key); -// snprintf(url, 512, "http://" NSS_HTTP_SERVER ":" NSS_HTTP_PORT "/passwd?name=%s", name); char *response = nss_http_request(url); if (!response) { diff --git a/libnss_http/nss_http-shadow.c b/libnss_http/nss_http-shadow.c index e8fbe82..0445190 100644 --- a/libnss_http/nss_http-shadow.c +++ b/libnss_http/nss_http-shadow.c @@ -79,7 +79,6 @@ _nss_http_setspent_locked(int stayopen) genurl(url, "shadow", ""); -// snprintf(url, 512, "http://" NSS_HTTP_SERVER ":" NSS_HTTP_PORT "/shadow"); char *response = nss_http_request(url); if (!response) { @@ -201,7 +200,6 @@ _nss_http_getspnam_r_locked(const char *name, struct spwd *result, char *buffer, char key[128]; sprintf(key, "name=%s", name); genurl(url, "shadow", key); -// snprintf(url, 512, "http://" NSS_HTTP_SERVER ":" NSS_HTTP_PORT "/shadow?name=%s", name); char *response = nss_http_request(url); if (!response) { From 35530dd2139b9243027f8819b0042cf1bdd8424f Mon Sep 17 00:00:00 2001 From: "alex.wan" Date: Wed, 18 May 2016 20:58:20 +0800 Subject: [PATCH 12/15] fix port&httpserver dirty data --- libnss_http/nss_http-config.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libnss_http/nss_http-config.c b/libnss_http/nss_http-config.c index 033a6e6..ecd1e15 100644 --- a/libnss_http/nss_http-config.c +++ b/libnss_http/nss_http-config.c @@ -101,7 +101,8 @@ void genurl(char* url, const char *type, const char *key) memset(con.debug, '\0', sizeof(con.debug)); readconfig(&con, CONFIG_FILE); - memset(hosts, '\0', sizeof(hosts)); + memset(hosts.ip, '\0', sizeof(hosts.ip)); + memset(hosts.hostname, '\0', sizeof(hosts.hostname)); networks(&hosts); if (strlen(key) == 0){ From 707e36a59366784e0c252a73cdfa582ccc655058 Mon Sep 17 00:00:00 2001 From: "alex.wan" Date: Wed, 18 May 2016 21:48:54 +0800 Subject: [PATCH 13/15] add timeout for libcurl --- libnss_http/nss_http-config.c | 27 +++++++++++++++------------ libnss_http/nss_http.c | 10 +++++++++- libnss_http/nss_http.h | 1 + nss_http.conf | 8 ++++++++ 4 files changed, 33 insertions(+), 13 deletions(-) create mode 100644 nss_http.conf diff --git a/libnss_http/nss_http-config.c b/libnss_http/nss_http-config.c index ecd1e15..dd94867 100644 --- a/libnss_http/nss_http-config.c +++ b/libnss_http/nss_http-config.c @@ -10,6 +10,7 @@ struct config char httpserver[MAXBUF]; char port[MAXBUF]; char debug[MAXBUF]; + long timeout; }; /* define ip and hostname struct*/ @@ -21,6 +22,11 @@ struct host 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)); + memset(configstruct->timeout,'\0', sizeof(configstruct->timeout)); FILE *file = fopen (filename, "r"); if (file != NULL) @@ -52,6 +58,13 @@ void readconfig(struct config *configstruct, char *filename) { 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); } @@ -62,6 +75,8 @@ void readconfig(struct config *configstruct, char *filename) /*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); @@ -95,16 +110,8 @@ void genurl(char* url, const char *type, const char *key) { struct config con; struct host hosts; - // fix httpser & port wrong result - memset(con.httpserver, '\0', sizeof(con.httpserver)); - memset(con.port, '\0', sizeof(con.port)); - memset(con.debug, '\0', sizeof(con.debug)); - readconfig(&con, CONFIG_FILE); - memset(hosts.ip, '\0', sizeof(hosts.ip)); - memset(hosts.hostname, '\0', sizeof(hosts.hostname)); 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); } @@ -116,10 +123,6 @@ void genurl(char* url, const char *type, const char *key) void debug_print(const char *func) { struct config con; - memset(con.httpserver, '\0', sizeof(con.httpserver)); - memset(con.port, '\0', sizeof(con.port)); - memset(con.debug, '\0', sizeof(con.debug)); - readconfig(&con, CONFIG_FILE); if (strcmp("true", con.debug) == 0) fprintf(stderr, "NSS DEBUG: Called %s \n", func); diff --git a/libnss_http/nss_http.c b/libnss_http/nss_http.c index e1c6b11..6577e8d 100644 --- a/libnss_http/nss_http.c +++ b/libnss_http/nss_http.c @@ -42,6 +42,13 @@ static size_t write_response(void *ptr, size_t size, size_t nmemb, void *stream) } +long gettimeout() +{ + struct config con; + readconfig(&con, CONFIG_FILE); + return con.timeout; +} + // char * nss_http_request(const char *url) @@ -65,8 +72,9 @@ nss_http_request(const char *url) curl_easy_setopt(curl, CURLOPT_URL, url); headers = curl_slist_append(headers, "User-Agent: NSS-HTTP"); - curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, gettimeout()); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_response); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &write_result); diff --git a/libnss_http/nss_http.h b/libnss_http/nss_http.h index c63002a..a2a14a8 100644 --- a/libnss_http/nss_http.h +++ b/libnss_http/nss_http.h @@ -39,6 +39,7 @@ extern size_t j_strlen(json_t *); extern struct config get_config(char *filename); extern void debug_print(const char *func); extern void genurl(char* url, const char *type, const char *key); +extern struct config #endif /* NSS_HTTP_H */ diff --git a/nss_http.conf b/nss_http.conf new file mode 100644 index 0000000..577285a --- /dev/null +++ b/nss_http.conf @@ -0,0 +1,8 @@ +# http server localtion +HTTPSERVER=localhost +# http server port +PORT=9669 +# debug modle +#DEBUG=true +#timeout for http +TIMEOUT=3 \ No newline at end of file From c945e72f94f68652528705b2f3c8e696d317fd0b Mon Sep 17 00:00:00 2001 From: "alex.wan" Date: Thu, 19 May 2016 11:31:55 +0800 Subject: [PATCH 14/15] add timeout for http request --- libnss_http/nss_http-config.c | 14 -------------- libnss_http/nss_http.h | 18 +++++++++++++++--- nss_http.conf | 2 +- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/libnss_http/nss_http-config.c b/libnss_http/nss_http-config.c index dd94867..7f0ee3b 100644 --- a/libnss_http/nss_http-config.c +++ b/libnss_http/nss_http-config.c @@ -1,18 +1,5 @@ #include "nss_http.h" -//settings -#define MAXBUF 1024 -#define DELIM "=" - -// define nss_http.conf key&value model -struct config -{ - char httpserver[MAXBUF]; - char port[MAXBUF]; - char debug[MAXBUF]; - long timeout; -}; - /* define ip and hostname struct*/ struct host { @@ -26,7 +13,6 @@ void readconfig(struct config *configstruct, char *filename) memset(configstruct->httpserver, '\0', sizeof(configstruct->httpserver)); memset(configstruct->port, '\0', sizeof(configstruct->port)); memset(configstruct->debug, '\0', sizeof(configstruct->debug)); - memset(configstruct->timeout,'\0', sizeof(configstruct->timeout)); FILE *file = fopen (filename, "r"); if (file != NULL) diff --git a/libnss_http/nss_http.h b/libnss_http/nss_http.h index a2a14a8..e422ed9 100644 --- a/libnss_http/nss_http.h +++ b/libnss_http/nss_http.h @@ -4,6 +4,7 @@ //gethostname not work fine on c99 mode #define _BSD_SOURCE + #include #include #include @@ -33,13 +34,24 @@ #define NSS_HTTP_INITIAL_BUFFER_SIZE (256 * 1024) /* 256 KB */ #define NSS_HTTP_MAX_BUFFER_SIZE (10 * 1024 * 1024) /* 10 MB */ +//nss_http-config part +#define MAXBUF 1024 +#define DELIM "=" + +// define nss_http.conf key&value model +struct config +{ + char httpserver[MAXBUF]; + char port[MAXBUF]; + char debug[MAXBUF]; + long timeout; +}; + //funcitons extern char *nss_http_request(const char *); extern size_t j_strlen(json_t *); -extern struct config get_config(char *filename); +extern void readconfig(struct config *con, char *filename); extern void debug_print(const char *func); extern void genurl(char* url, const char *type, const char *key); -extern struct config - #endif /* NSS_HTTP_H */ diff --git a/nss_http.conf b/nss_http.conf index 577285a..efa6f7f 100644 --- a/nss_http.conf +++ b/nss_http.conf @@ -5,4 +5,4 @@ PORT=9669 # debug modle #DEBUG=true #timeout for http -TIMEOUT=3 \ No newline at end of file +TIMEOUT=3 From 8cb5132aedce8d18afcc3abb11e1884f8c175a3a Mon Sep 17 00:00:00 2001 From: "alex.wan" Date: Thu, 19 May 2016 11:33:03 +0800 Subject: [PATCH 15/15] add timeout for http request --- libnss_http/nss_http.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libnss_http/nss_http.c b/libnss_http/nss_http.c index 6577e8d..8dba048 100644 --- a/libnss_http/nss_http.c +++ b/libnss_http/nss_http.c @@ -42,7 +42,7 @@ static size_t write_response(void *ptr, size_t size, size_t nmemb, void *stream) } -long gettimeout() +long gettimeout(void) { struct config con; readconfig(&con, CONFIG_FILE);