2 /*****************************************************************************
3 * libnss-afs (nss_afs.c)
5 * Copyright 2008, licensed under GNU Library General Public License (LGPL)
6 * see COPYING file for details
8 * by Adam Megacz <megacz@hcoop.net>
9 * derived from Frank Burkhardt's libnss_ptdb,
10 * which was derived from Todd M. Lewis' libnss_pts
11 *****************************************************************************/
14 * If you are reading this code for the first time, read the rest of
15 * this comment block, then start at the bottom of the file and work
18 * All functions which return an int use zero to signal success --
19 * except cpstr(), which returns zero on *failure*. This should be
22 * A note about memory allocation:
24 * NSS plugins generally ought to work without attempting to call
25 * malloc() (which may fail). Therefore, glibc allocates a buffer
26 * before calling NSS library functions, and passes that buffer to
27 * the NSS library; library functions store their results in the
28 * buffer and return pointers into that buffer.
30 * The convention used throughout this library is to pass around a
31 * char** which points to a pointer to the first unused byte in the
32 * provided buffer, and a size_t* which points to an int indicating
33 * how many bytes are left between the char** and the end of the
44 #include <netinet/in.h>
54 #include <sys/select.h>
55 #include <sys/socket.h>
58 #include <sys/types.h>
60 #include <afs/afsutil.h>
61 #include <afs/cellconfig.h>
62 #include <afs/com_err.h>
63 #include <afs/param.h>
64 #include <afs/ptclient.h>
65 #include <afs/pterror.h>
66 #include <afs/ptuser.h>
69 #define HOMEDIR_AUTO 0
70 #define HOMEDIR_ADMINLINK 1
71 #define HOMEDIR_PREFIX 2
73 #define SHELL_ADMINLINK 1
74 #define SHELL_USERLINK 2
76 #define AFS_MAGIC_ANONYMOUS_USERID 32766
77 #define MIN_PAG_GID 0x41000000L
78 #define MAX_PAG_GID 0x41FFFFFFL
79 #define MIN_OLDPAG_GID 0x3f00
80 #define MAX_OLDPAG_GID 0xff00
82 #define MAXCELLNAMELEN 256
83 #define MAXUSERNAMELEN 256
85 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
87 extern struct ubik_client
*pruclient
;
89 int afs_initialized
= 0;
90 char cellname
[MAXCELLNAMELEN
];
91 char homedir_prefix
[PATH_MAX
];
92 char cell_root
[PATH_MAX
];
93 int homedir_prefix_len
=0;
94 char homedirs_method
=0;
100 * The cpstr() function copies a null-terminated string from str*
101 * (the first argument) into buf and updates both buf and buflen. If
102 * the string would overflow the buffer, no action is taken. The
103 * number of bytes copied is returned (zero indicates failure).
105 int cpstr( char *str
, char **buf
, size_t *buflen
) {
106 int len
= strlen(str
);
107 if ( len
>= *buflen
-1 ) return 0;
115 * Look up the name corresponding to uid, store in buffer.
117 enum nss_status
ptsid2name(int uid
, char **buffer
, size_t *buflen
) {
122 if (init_afs()) return NSS_STATUS_UNAVAIL
;
124 if (uid
==AFS_MAGIC_ANONYMOUS_USERID
) {
125 if (!cpstr("anonymous", buffer
, buflen
)) return NSS_STATUS_UNAVAIL
;
126 return NSS_STATUS_SUCCESS
;
129 if (pthread_mutex_lock(&mutex
)) return NSS_STATUS_UNAVAIL
;
131 lid
.idlist_val
= (afs_int32
*)&uid
;
133 lnames
.namelist_val
= 0;
134 lnames
.namelist_len
= 0;
136 if (ubik_PR_IDToName(pruclient
,0,&lid
,&lnames
) != PRSUCCESS
) {
137 perror("ubik_Call() in ptsid2name() failed\n");
138 pthread_mutex_unlock(&mutex
);
139 return NSS_STATUS_UNAVAIL
;
142 ret
= NSS_STATUS_NOTFOUND
;
143 for (i
=0;i
<lnames
.namelist_len
;i
++) {
144 int delta
= strlen(lnames
.namelist_val
[i
]);
145 if ( (delta
< *buflen
) && islower(*(lnames
.namelist_val
[i
])) ) {
146 cpstr(lnames
.namelist_val
[i
], buffer
, buflen
);
147 ret
= NSS_STATUS_SUCCESS
;
150 free(lnames
.namelist_val
);
151 /* free(lid.idlist_val); */
155 pthread_mutex_unlock(&mutex
);
160 * Look up the uid corresponding to name in ptserver.
162 enum nss_status
ptsname2id(char *name
, uid_t
* uid
) {
166 char uname
[MAXUSERNAMELEN
];
168 if (init_afs()) return NSS_STATUS_UNAVAIL
;
170 if (!strcmp(name
,"anonymous")) {
171 *uid
= AFS_MAGIC_ANONYMOUS_USERID
;
172 return NSS_STATUS_SUCCESS
;
175 if (pthread_mutex_lock(&mutex
)) return NSS_STATUS_UNAVAIL
;
179 lnames
.namelist_val
= (prname
*)uname
;
180 // apparently ubik expects to be able to modify this?
181 strncpy(uname
, name
, MAXUSERNAMELEN
);
182 lnames
.namelist_len
= 1;
184 if (ubik_PR_NameToID(pruclient
,0,&lnames
,&lid
) != PRSUCCESS
) {
185 perror("ubik_Call() in ptsname2id() failed\n");
186 pthread_mutex_unlock(&mutex
);
187 return NSS_STATUS_UNAVAIL
;
189 pthread_mutex_unlock(&mutex
);
191 res
= (uid_t
)lid
.idlist_val
[0];
192 if (res
== AFS_MAGIC_ANONYMOUS_USERID
) return NSS_STATUS_NOTFOUND
;
194 return NSS_STATUS_SUCCESS
;
198 * Initialize the library; returns zero on success
209 if (afs_initialized
) {
210 /* wait until /afs/@cell/ appears as a proxy for "the network is up" */
211 if (stat(cell_root
, &statbuf
)) return -1;
215 // check to make sure that we are running inside nscd
217 fd
= open("/proc/self/cmdline", O_RDONLY
);
218 if (fd
==-1) return -1;
221 numread
= read(fd
, buf
+pos
, 1);
222 if (buf
[ (pos
+5)%6 ] == 'd' &&
223 buf
[ (pos
+4)%6 ] == 'c' &&
224 buf
[ (pos
+3)%6 ] == 's' &&
225 buf
[ (pos
+2)%6 ] == 'n' &&
226 (buf
[(pos
+1)%6 ] == '/' || pos
==4) &&
227 (buf
[(pos
+0)%6 ] == 0 || numread
==-1)
231 if (numread
==0) { close(fd
); return -1; }
235 if (pthread_mutex_lock(&mutex
)) return -1;
237 homedirs_method
=HOMEDIR_PREFIX
;
238 shells_method
=SHELL_USERLINK
;
240 len
= snprintf(cellname
, MAXCELLNAMELEN
,
241 "%s/ThisCell", AFSDIR_CLIENT_ETC_DIRPATH
);
242 if (len
< 0 || len
>= MAXCELLNAMELEN
) return -1;
244 thiscell
=fopen(cellname
,"r");
245 if (thiscell
== NULL
) break;
246 len
=fread(cellname
,1,MAXCELLNAMELEN
,thiscell
);
247 if (!feof(thiscell
)) {
250 strcpy(homedir_prefix
,"/tmp/\0");
251 homedir_prefix_len
=5;
256 if (cellname
[len
-1] == '\n') len
--;
259 /* wait until /afs/@cell/ appears as a proxy for "the network is up" */
260 sprintf(cell_root
,"/afs/%s/",cellname
);
261 if (stat(cell_root
, &statbuf
)) break;
263 sprintf(homedir_prefix
,"/afs/%s/user/",cellname
);
264 homedir_prefix_len
=strlen(homedir_prefix
);
266 /* time out requests after 5 seconds to avoid hanging things */
269 if (pr_Initialize(0L,AFSDIR_CLIENT_ETC_DIRPATH
, 0)) {
270 perror("pr_Initialize() failed\n");
275 pthread_mutex_unlock(&mutex
);
279 pthread_mutex_unlock(&mutex
);
285 * Retrieves the homedir for a given user; returns 0 on success.
287 int get_homedir(char *name
, char **buffer
, size_t *buflen
) {
292 switch (homedirs_method
) {
294 homedir_prefix
[homedir_prefix_len
+0]=name
[0];
295 homedir_prefix
[homedir_prefix_len
+1]='/';
296 homedir_prefix
[homedir_prefix_len
+2]=name
[0];
297 homedir_prefix
[homedir_prefix_len
+3]=name
[1];
298 homedir_prefix
[homedir_prefix_len
+4]='/';
299 homedir_prefix
[homedir_prefix_len
+5]=0;
300 strncpy(&homedir_prefix
[homedir_prefix_len
+5],name
,40);
301 if (! cpstr(homedir_prefix
,buffer
,buflen
) ) return -1;
304 homedir_prefix
[homedir_prefix_len
]=0;
305 strncpy(&homedir_prefix
[homedir_prefix_len
],name
,40);
306 if (! cpstr(homedir_prefix
,buffer
,buflen
) ) return -1;
308 case HOMEDIR_ADMINLINK
:
309 if ( snprintf(buf
,256,"/afs/%s/admin/homedirs/%s",cellname
,name
) > 0 ) {
310 temp
=readlink(buf
,*buffer
,*buflen
);
313 *buflen
= *buflen
- temp
- 1;
317 if (! cpstr("/tmp",buffer
,buflen
) ) return -1;
324 * Retrieves the shell for a given user; returns 0 on success.
326 int get_shell(char *name
, char **buffer
, size_t *buflen
) {
331 size_t bufxlen
= 256;
334 switch (shells_method
) {
338 case SHELL_ADMINLINK
:
339 if (snprintf(buf
,256,"/afs/%s/admin/shells/%s",cellname
,name
)<=0) break;
340 temp
= readlink(buf
,*buffer
,*buflen
);
343 *buflen
= *buflen
- temp
- 1;
347 if (get_homedir(name
, &bufx
, &bufxlen
)) break;
348 if (strncpy(buf
+strlen(buf
),"/.loginshell",bufxlen
)<=0) break;
349 temp
= readlink(buf
,*buffer
,*buflen
);
352 *buflen
= *buflen
- temp
- 1;
355 if (! cpstr("/bin/bash",buffer
,buflen
) )
362 * This function is exported; glibc will invoke it in order to find
363 * the name and list of members of a group specified by a numerical
366 enum nss_status
_nss_afs_getgrgid_r (gid_t gid
,
367 struct group
*result
,
373 if (gid
>= MIN_PAG_GID
&& gid
<= MAX_PAG_GID
) {
374 showgid
= gid
-MIN_PAG_GID
;
375 } else if (gid
>= MIN_OLDPAG_GID
&& gid
<= MAX_OLDPAG_GID
) {
376 showgid
= gid
-MIN_OLDPAG_GID
;
379 return NSS_STATUS_NOTFOUND
;
384 result
->gr_name
=buffer
;
385 length
=snprintf(buffer
,buflen
,"AfsPag-%x",showgid
);
387 if (length
< 0) break;
392 result
->gr_passwd
=buffer
;
394 if (!cpstr("z",&buffer
,&buflen
)) break;
396 if (buflen
< sizeof(char*)) break;
397 result
->gr_mem
=&buffer
;
398 result
->gr_mem
[0] = NULL
;
401 return NSS_STATUS_SUCCESS
;
405 return NSS_STATUS_UNAVAIL
;
409 * A helper function to fill in the fields of "struct passwd"; used by
410 * both _nss_afs_getpwuid_r() and _nss_afs_getpwnam_r().
412 enum nss_status
fill_result_buf(uid_t uid
,
414 struct passwd
*result_buf
,
418 result_buf
->pw_name
= name
;
420 /* set the password to "z"; we can't use "x" because of pam_unix.so */
421 result_buf
->pw_passwd
= buffer
;
422 if ( ! cpstr("z",&buffer
, &buflen
) ) break;
424 /* the uid and gid are both the uid passed in */
425 result_buf
->pw_uid
= uid
;
426 result_buf
->pw_gid
= 65534;
428 /* make the gecos the same as the PTS name */
429 result_buf
->pw_gecos
= buffer
;
430 if ( ! cpstr(result_buf
->pw_name
, &buffer
, &buflen
) ) break;
432 // Set the homedirectory
433 result_buf
->pw_dir
= buffer
;
434 if ( get_homedir(result_buf
->pw_name
,&buffer
,&buflen
) ) break;
436 // Set the login shell
437 result_buf
->pw_shell
= buffer
;
438 if ( get_shell(result_buf
->pw_name
,&buffer
,&buflen
) ) break;
440 #ifdef LIMIT_USERNAME_CHARS
441 if ( strlen(result_buf
->pw_name
) > LIMIT_USERNAME_CHARS
) {
442 result_buf
->pw_name
[LIMIT_USERNAME_CHARS
] = '\0';
443 buflen
= buflen
+ ( buffer
- &result_buf
->pw_name
[LIMIT_USERNAME_CHARS
+1] );
444 buffer
= &result_buf
->pw_name
[LIMIT_USERNAME_CHARS
+1];
449 return NSS_STATUS_SUCCESS
;
453 return NSS_STATUS_UNAVAIL
;
458 * This function is exported; glibc will invoke it in order to gather
459 * the user information (userid, homedir, shell) associated with a
462 enum nss_status
_nss_afs_getpwuid_r (uid_t uid
,
463 struct passwd
*result_buf
,
470 if (init_afs()) return NSS_STATUS_UNAVAIL
;
473 temp
= ptsid2name( uid
, &buffer
, &buflen
);
474 if (temp
!= NSS_STATUS_SUCCESS
) {
479 return fill_result_buf(uid
, name
, result_buf
, buffer
, buflen
, errnop
);
483 * This function is exported; glibc will invoke it in order to gather
484 * the user information (userid, homedir, shell) associated with a
487 enum nss_status
_nss_afs_getpwnam_r (char *name
,
488 struct passwd
*result_buf
,
495 if (init_afs()) return NSS_STATUS_UNAVAIL
;
497 temp
= ptsname2id(name
,&uid
);
498 if (temp
!= NSS_STATUS_SUCCESS
) {
503 return fill_result_buf(uid
, name
, result_buf
, buffer
, buflen
, errnop
);