c - 自定义 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
任何帮助将不胜感激!
解决方案
我遇到了同样的问题,但是 pifor 建议将共享库复制到 /usr/lib64 确实在 CentOS 7 机器上解决了这个问题。在您的安装中查找其他 libnss_xxx 库的位置并复制您看到的模式。
需要注意的第二件事是,当您的库在运行时动态加载时,您的所有共享库依赖项(libjanssen、libcurl)都可以正确解析。如果没有,标注将静默失败。
推荐阅读
- c# - 当 body 存在时,不会调用 Web Api 操作。但是没有身体也能工作
- python - 在 python 类中返回 self 只返回 NoneType
- python - 在 IF 语句中使用单元格值
- postgresql - 为什么 max_parallel_workers_per_gather != max_parallel_workers
- c# - 根据给定数量和价格的最新日期查找产品的最低和最高单价(EF Core)
- javascript - 我需要帮助确定我的代码之间的差异,我认为这些代码会产生与另一个相同的结果,但不会被接受
- laravel - Laravel 中用于管理具有字段级别特异性的权限的惯用方式
- python - AttributeError:“ModifiedTensorBoard”对象没有属性“_train_dir”
- javascript - 如何将 SmtpJS 与 Secure Token 和 gmail smtp 一起使用
- compilation - Ada 列出文件.... GNAT 中的正确编译器是什么让它们出来