添加服务到名称服务开关

时间:2022-03-14 15:30:49

So I am trying to add a service to NSS (Name Service Switch). Please note the GNU guide on how to do it here. I have been following that guide. I need to implement a service that works with the passwd database.

所以我正在尝试向NSS(名称服务交换机)添加服务。请注意GNU指南,了解如何在此处执行此操作。我一直在关注那个指南。我需要实现一个与passwd数据库一起使用的服务。

The problem I am having is my module is not being called for certain functions. Let me reproduce some of my code here...

我遇到的问题是我的模块没有被调用某些功能。让我在这里重现一些代码......

enum nss_status
_nss_myservice_setpwent (void) {
 printf( "@ %s\n", __FUNCTION__ ) ;
 return NSS_STATUS_SUCCESS ;
} ;

enum nss_status
_nss_myservice_endpwent (void) {
 printf( "@ %s\n", __FUNCTION__ ) ;
 return NSS_STATUS_SUCCESS ;
} ;

enum nss_status
_nss_myservice_getpwent_r (struct passwd *result, char *buffer,
   size_t buflen, int *errnop) {

 static int i = 0 ;

 if( i++ == 0 ) {
   printf( "@ %s\n", __FUNCTION__ ) ;
   return init_result( result, buffer, buflen, errnop ) ;
 } else {
   i = 0 ;
   return NSS_STATUS_NOTFOUND ;
 }
} ;

enum nss_status
_nss_myservice_getpwbynam (const char *nam, struct passwd *result, char *buffer,
   size_t buflen, int *errnop) {
 printf( "@ %s with name %s\n", __FUNCTION__, nam ) ;
 return init_result( result, buffer, buflen, errnop ) ;
} ;

enum nss_status
_nss_myservice_getpwbynam_r (const char *nam, struct passwd *result, char *buffer,
   size_t buflen, int *errnop) {
 printf( "@ %s with name_r %s\n", __FUNCTION__, nam ) ;
 return init_result( result, buffer, buflen, errnop ) ;
} ;

Init_result is an inline function that simply fills in the result with a dummy user no matter what the PARAMS are.

Init_result是一个内联函数,无论PARAMS是什么,只需用虚拟用户填充结果。

Now I have my /etc/nsswitch.conf setup as follows:

现在我的/etc/nsswitch.conf设置如下:

passwd:         myservice compat

And for completeness here is my Makefile.

为了完整性,这里是我的Makefile。

all:
       gcc -fPIC -shared -o libnss_myservice.so.2 -Wl,-soname,libnss_myservice.so.2 myservice.c
install:
       sudo install -m 0644 libnss_myservice.so.2 /lib
       sudo /sbin/ldconfig -n /lib /usr/lib
clean:
       /bin/rf -rf libnss_myservice.so.2

Now after installing this nss module I run getent on the command line and here is my output:

现在安装了这个nss模块后,我在命令行上运行getent,这是我的输出:

username@host:~/nss$ getent passwd
@ _nss_myservice_setpwent
@ _nss_myservice_getpwent_r
myuser:mypass:1:1:realname::
root:x:0:0:root:/root:/bin/bash
...
@ _nss_myservice_endpwent

So as you can see that is working as I would expect. The iterative call is made which returns the user and then the compat service is called which returns all the user from /etc/passwd.

所以你可以看到它正如我所期望的那样工作。进行迭代调用,返回用户,然后调用compat服务,该服务从/ etc / passwd返回所有用户。

The problem is when I make this call, "getent passwd myuser", I get a return value of 2, "Key not found in database". This shows me my _nss_myservice_getpwbynam_r function is not being called. Any ideas why? I can provide the complete code if that would help.

问题是当我拨打这个电话时,“getent passwd myuser”,我得到的返回值为2,“数据库中找不到密钥”。这告诉我我的_nss_myservice_getpwbynam_r函数没有被调用。有什么想法吗?如果有帮助,我可以提供完整的代码。

1 个解决方案

#1


7  

You need to call the function _nss_myservice_getpwnam_r instead of _nss_myservice_getpwbynam_r.

您需要调用函数_nss_myservice_getpwnam_r而不是_nss_myservice_getpwbynam_r。

After looking at ftp://ftp.acer-euro.com/gpl/Utility/glibc/glibc-2.2.5.tar/include/pwd.h :

看了ftp://ftp.acer-euro.com/gpl/Utility/glibc/glibc-2.2.5.tar/include/pwd.h:

#define DECLARE_NSS_PROTOTYPES(service)                 \
extern enum nss_status _nss_ ## service ## _setpwent (int);     \
extern enum nss_status _nss_ ## service ## _endpwent (void);        \
extern enum nss_status _nss_ ## service ## _getpwnam_r          \        <<< this line
                       (const char *name, struct passwd *pwd,       \
            char *buffer, size_t buflen, int *errnop);  \
extern enum nss_status _nss_ ## service ## _getpwuid_r          \
                       (uid_t uid, struct passwd *pwd,          \
            char *buffer, size_t buflen, int *errnop);  \
extern enum nss_status _nss_ ## service ##_getpwent_r           \
                       (struct passwd *result, char *buffer,        \
            size_t buflen, int *errnop);

#1


7  

You need to call the function _nss_myservice_getpwnam_r instead of _nss_myservice_getpwbynam_r.

您需要调用函数_nss_myservice_getpwnam_r而不是_nss_myservice_getpwbynam_r。

After looking at ftp://ftp.acer-euro.com/gpl/Utility/glibc/glibc-2.2.5.tar/include/pwd.h :

看了ftp://ftp.acer-euro.com/gpl/Utility/glibc/glibc-2.2.5.tar/include/pwd.h:

#define DECLARE_NSS_PROTOTYPES(service)                 \
extern enum nss_status _nss_ ## service ## _setpwent (int);     \
extern enum nss_status _nss_ ## service ## _endpwent (void);        \
extern enum nss_status _nss_ ## service ## _getpwnam_r          \        <<< this line
                       (const char *name, struct passwd *pwd,       \
            char *buffer, size_t buflen, int *errnop);  \
extern enum nss_status _nss_ ## service ## _getpwuid_r          \
                       (uid_t uid, struct passwd *pwd,          \
            char *buffer, size_t buflen, int *errnop);  \
extern enum nss_status _nss_ ## service ##_getpwent_r           \
                       (struct passwd *result, char *buffer,        \
            size_t buflen, int *errnop);