首页 > 解决方案 > 自定义 NSS 模块 - 成功编译但未调用且未生成任何日志

问题描述

我正在尝试实现基于 HTTP 的自定义 NSS 模块。

但是我无法实现将工作作为基本示例,甚至没有调用代码。根据https://www.gnu.org/software/libc/manual/html_node/Services-in-the-NSS-configuration.html#Services-in-the-NSS-configuration,我得到了一个libnss_http.so.2文件存储/usr/lib并设置像/etc/nsswitch.conf这样

# /etc/nsswitch.conf
#
# Example configuration of GNU Name Service Switch functionality.
# If you have the `glibc-doc-reference' and `info' packages installed, try:
# `info libc "Name Service Switch"' for information about this file.

passwd:         files http
group:          files
shadow:         files
gshadow:        files

hosts:          files dns
networks:       files

protocols:      db files
services:       db files
ethers:         db files
rpc:            db files

netgroup:       nis

getent passwd unknown不创建任何文件日志。

nss_http-passwd.c

#include "nss_http.h"

// Called to open the passwd file
enum nss_status
_nss_http_setpwent(void)
{
    FILE *fp;
   fp = fopen("/tmp/test.txt", "w+");
   fputs("This is testing for fputs...\n", fp);
   fclose(fp);

    return NSS_STATUS_UNAVAIL;
}

// Called to close the passwd file
enum nss_status
_nss_http_endpwent(void)
{
    return NSS_STATUS_UNAVAIL;
}


// Called to look up next entry in passwd file
enum nss_status
_nss_http_getpwent_r(struct passwd *result, char *buffer, size_t buflen, int *errnop)
{
    return NSS_STATUS_UNAVAIL;
}

enum nss_status
_nss_http_getpwbyuid_r(uid_t uid, struct passwd *result, char *buffer, size_t buflen, int *errnop)
{
    return NSS_STATUS_UNAVAIL;
}

// Find a passwd by name
enum nss_status
_nss_http_getpwbyname_r(const char *name, struct passwd *result, char *buffer, size_t buflen, int *errnop)
{
    return NSS_STATUS_UNAVAIL;
}

nss_http.h

#ifndef NSS_HTTP_H
#define NSS_HTTP_H

#include <curl/curl.h>
#include <errno.h>
#include <grp.h>
#include <jansson.h>
#include <nss.h>
#include <pthread.h>
#include <pwd.h>
#include <shadow.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define NSS_HTTP_INITIAL_BUFFER_SIZE (256 * 1024)  /* 256 KB */
#define NSS_HTTP_MAX_BUFFER_SIZE (10 * 1024 * 1024)  /* 10 MB */

extern char *nss_http_request(const char *);
extern size_t j_strlen(json_t *);

#endif /* NSS_HTTP_H */

Makefile

CC=gcc
CFLAGS=-Wall -Wstrict-prototypes -Werror -fPIC -std=c99

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
BUILD=.libs

default: build
build: nss_http

nss_http_build_dir:
    [ -d $(BUILD) ] || mkdir $(BUILD)

nss_http-passwd:
    $(CC) $(CFLAGS) -c nss_http-passwd.c -o $(BUILD)/nss_http-passwd.o



nss_http_services: nss_http-passwd

nss_http: nss_http_build_dir nss_http_services
    $(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-passwd.o \
        -lcurl -ljansson

clean:
    rm -rf $(BUILD)

install:
    [ -d $(LIBDIR) ] || install -d $(LIBDIR)
    install $(BUILD)/$(LIBRARY) $(LIBDIR)
    cd $(LIBDIR); for link in $(LINKS); do ln -sf $(LIBRARY) $$link ; done

.PHONY: clean install nss_http_build_dir nss_http

任何帮助将不胜感激!

标签: clinux

解决方案


我遇到了同样的问题,但是 pifor 建议将共享库复制到 /usr/lib64 确实在 CentOS 7 机器上解决了这个问题。在您的安装中查找其他 libnss_xxx 库的位置并复制您看到的模式。

需要注意的第二件事是,当您的库在运行时动态加载时,您的所有共享库依赖项(libjanssen、libcurl)都可以正确解析。如果没有,标注将静默失败。


推荐阅读