2013-05-03 18 views
9

Così sto provando ad aggiungere un servizio a NSS (Name Service Switch). Si prega di notare la guida GNU su come farlo here. Ho seguito quella guida. Devo implementare un servizio che funzioni con il database passwd.Aggiunta di un servizio al nome Interruttore di servizio

Il problema che sto riscontrando è che il mio modulo non viene chiamato per determinate funzioni. Permettetemi di riporto alcuni del mio codice qui ...

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 è una funzione inline che riempie semplicemente il risultato con un utente fittizio non importa ciò che i parametri sono.

ora ho il mio setup /etc/nsswitch.conf come segue:

passwd:   myservice compat 

E per completezza ecco la mia 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 

Ora, dopo l'installazione di questo modulo NSS corro getent sulla riga di comando e qui è la mia uscita:

[email protected]:~/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 

Quindi, come si può vedere che sta lavorando come mi sarei aspettato. Viene effettuata la chiamata iterativa che restituisce l'utente e quindi viene chiamato il servizio compat che restituisce tutti gli utenti da/etc/passwd.

Il problema è quando faccio questa chiamata, "getent passwd myuser", ottengo un valore di ritorno di 2, "Chiave non trovata nel database". Questo mi mostra che la mia funzione _nss_myservice_getpwbynam_r non viene chiamata. Qualche idea, perché? Posso fornire il codice completo se ciò sarebbe di aiuto.

+1

cosa se lo si chiama '_nss_myservice_getpwnam_r' invece? Solo una supposizione, ma vedo che c'è una funzione 'pwd.h' chiamata' getpwnam_r' –

risposta

6

È necessario chiamare la funzione _nss_myservice_getpwnam_r anziché _nss_myservice_getpwbynam_r.

Dopo aver guardato 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

Grazie, funziona. Il che mi fa chiedere perché la documentazione GNU sia formulata così com'è. (enum nss_status _nss_DATABASE_getdbbyXX_r (PARAMS, STRUCTURE * result, char * buffer, size_t buflen, int * errnop)) Mi manca qualcosa? In caso contrario, invierò loro un messaggio ... –

+0

Ecco un collegamento diretto a questo codice nel repository degli glibc: http://repo.or.cz/w/glibc.git/blob/HEAD:/include/pwd .h # L35 –

Problemi correlati