LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-21-2011, 12:21 AM   #1
adampar
LQ Newbie
 
Registered: Sep 2011
Posts: 7

Rep: Reputation: Disabled
Writing nss module


Hi all,

I'm try to write my own nss module for authenticating Active Directory users who want to get access to my filesystem.
followd by -
http://www.gnu.org/software/libc/man...tion-Internals

I've tried to write a so file with the following functions:

Code:
extern "C" int _nss_adam_getpwuid(uid_t uid, struct passwd *result, char *buffer, size_t buflen, int *errnop)
extern "C" int _nss_adam_getpwnam(uid_t uid, struct passwd *result, char *buffer, size_t buflen, int *errnop)
extern "C" int _nss_adam_setpwent(void)
extern "C" int _nss_adam_endpwent(void)
extern "C" int _nss_adam_getpwent_r(void)
extern "C" int _nss_adam_getpwnam(const char *nam, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result)
extern "C" int _nss_adam_getpwnam_r(const char *nam, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result)
and set their body to be

Code:
commentlog(); //My function for writing log lines
return NSS_STATUS_SUCCEED.
I build so object from these lines using
Code:
g++ -fPIC -c libnss_adam.cpp
g++ -shared -o libnss_adam.so libnss_adam.o
and copied libnss_adam.so to /etc/lib
after that I edited /etc/nsswitch.conf and set:
passwd: adam files
shadow: adam files
Now, when I try to use su or ssh, it seem that my library doesnít work (I donít see my log lines) and I just get user is not exists message.
I also know that winbind (for example) are doing exactly what I did.

Can some help me please?

Thanks,
Adam
 
Old 09-21-2011, 02:37 AM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Well, I wrote a dummy adam.c in C, implementing
Code:
#include <nss.h>
#include <pwd.h>
#include <grp.h>

enum nss_status _nss_adam_setpwent (void);
enum nss_status _nss_adam_endpwent (void);
enum nss_status _nss_adam_getpwent_r (struct passwd *result, char *buffer, size_t buflen, int *errnop);
enum nss_status _nss_adam_getpwbyuid_r (uid_t uid, struct passwd *result, char *buffer, size_t buflen, int *errnop);
enum nss_status _nss_adam_getpwbynam_r (const char *name, struct passwd *result, char *buffer, size_t buflen, int *errnop);
I compiled it using the command given in the GNU libc documentation, in info libc 'Adding another Service to NSS' ,
Code:
gcc -shared -o libnss_adam.so.2 -Wl,-soname,libnss_adam.so.2 adam.c
This handles all the versioning magic. I installed the resulting library using
Code:
sudo install -m 0644 libnss_adam.so.2 /lib
sudo /sbin/ldconfig -n /lib /usr/lib
which created a symlink in /usr/lib for me, automagically. Note that it also tells the linker to refresh its cache.

After putting passwd: adam whatever.. in /etc/nsswitch.conf my functions did get called.

I believe you have omitted some necessary flags while compiling, and have forgotten to tell the linker (ldconfig) that you have installed a new library. You can install inotify-tools, and run sudo inotifywatch -m /lib/libnss_adam.so.2 to see if the library is read (used) when you test, say id -u nominalanimal , which definitely uses the passwd service.

Aside from the above, I am a bit sceptical whether it makes any sense writing an NSS module in C++. All NSS libraries I've looked at were written in pure C; perhaps you too might consider your choice of language. My main concern is that since the NSS library is loaded as part of each program using name services, the C++ libraries might cause unwanted side effects in non-C++ programs.
 
Old 09-21-2011, 03:05 AM   #3
adampar
LQ Newbie
 
Registered: Sep 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
Hi again,

I followed your instructions and got

/usr/bin/ld: /tmp/ccOlpcx7.o: relocation R_X86_64_32 against `a local symbol' cannot be used when making a shared object; recompile with -fPIC
/tmp/ccOlpcx7.o: could not read symbols: Bad value
collect2: ld returned 1 exit status

after I tried to compile it.
Can u help me with this?

Thanks,
Adam
 
Old 09-21-2011, 10:08 AM   #4
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
On the 32-bit x86 I tested it with, position independent code was not required. On your x86_64, it seems to be. So, do as the error suggests, and use
Code:
gcc -fPIC -shared -o libnss_adam.so.2 -Wl,-soname,libnss_adam.so.2 adam.c
Does this work for you?
 
Old 09-22-2011, 02:36 AM   #5
adampar
LQ Newbie
 
Registered: Sep 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
Yes.

Thank you.
 
Old 08-16-2016, 04:35 AM   #6
getgoodgrade
LQ Newbie
 
Registered: Aug 2016
Location: New York
Posts: 17

Rep: Reputation: 0
Thumbs up

Used your tips, and they also workes for me. Thanks!
 
Old 08-16-2016, 07:38 PM   #7
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,517

Rep: Reputation: 2619Reputation: 2619Reputation: 2619Reputation: 2619Reputation: 2619Reputation: 2619Reputation: 2619Reputation: 2619Reputation: 2619Reputation: 2619Reputation: 2619
Quote:
Used your tips, and they also workes for me. Thanks!
what tip ?
-fpic ?? if so that is a normal textbook option

also have you read the forum rules
this is a 5 year old thread

please do not Neckro post on old threads
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] writing a trivial kernel module - help kaching Programming 4 06-08-2010 07:12 AM
Module writing Ashok_mittal Linux - Newbie 1 12-07-2007 05:47 AM
Kernel Module Writing itz2000 Programming 6 10-05-2007 03:54 PM
Problem with writing module mohsena Programming 3 10-16-2005 09:51 AM
writing to proc module dmaxj Linux - General 5 07-02-2003 09:44 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration