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
43 #include <netinet/in.h>
53 #include <sys/select.h>
54 #include <sys/socket.h>
56 #include <sys/types.h>
59 #include <afs/afsutil.h>
60 #include <afs/cellconfig.h>
61 #include <afs/com_err.h>
62 #include <afs/param.h>
63 #include <afs/ptclient.h>
64 #include <afs/pterror.h>
67 #define HOMEDIR_AUTO 0
68 #define HOMEDIR_ADMINLINK 1
69 #define HOMEDIR_PREFIX 2
71 #define SHELL_ADMINLINK 1
72 #define SHELL_USERLINK 2
74 #define AFS_MAGIC_ANONYMOUS_USERID 32766
75 #define MIN_PAG_GID 0x41000000L
76 #define MAX_PAG_GID 0x41FFFFFFL
77 #define MIN_OLDPAG_GID 0x3f00
78 #define MAX_OLDPAG_GID 0xff00
80 #define MAXCELLNAMELEN 256
82 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
84 extern struct ubik_client
*pruclient
;
86 int afs_initialized
= 0;
87 char cellname
[MAXCELLNAMELEN
];
88 char homedir_prefix
[MAXPATHLEN
];
89 int homedir_prefix_len
=0;
90 char homedirs_method
=0;
94 * The cpstr() function copies a null-terminated string from str*
95 * (the first argument) into buf and updates both buf and buflen. If
96 * the string would overflow the buffer, no action is taken. The
97 * number of bytes copied is returned (zero indicates failure).
99 int cpstr( char *str
, char **buf
, size_t *buflen
) {
100 int len
= strlen(str
);
101 if ( len
>= *buflen
-1 ) return 0;
109 * Look up the name corresponding to uid, store in buffer.
111 enum nss_status
ptsid2name(int uid
, char **buffer
, int *buflen
) {
118 if (uid
==AFS_MAGIC_ANONYMOUS_USERID
) {
119 if (!cpstr("anonymous", buffer
, buflen
)) return NSS_STATUS_UNAVAIL
;
120 return NSS_STATUS_SUCCESS
;
123 if (pthread_mutex_lock(&mutex
)) return NSS_STATUS_UNAVAIL
;
125 lid
.idlist_val
= (afs_int32
*)&uid
;
127 lnames
.namelist_val
= 0;
128 lnames
.namelist_len
= 0;
130 if (ubik_Call(PR_IDToName
,pruclient
,0,&lid
,&lnames
) != PRSUCCESS
) {
131 pthread_mutex_unlock(&mutex
);
132 return NSS_STATUS_UNAVAIL
;
135 ret
= NSS_STATUS_NOTFOUND
;
136 for (i
=0;i
<lnames
.namelist_len
;i
++) {
137 int delta
= strlen(lnames
.namelist_val
[i
]);
138 if ( (delta
< buflen
) && islower(*(lnames
.namelist_val
[i
])) ) {
139 cpstr(lnames
.namelist_val
[i
], buffer
, buflen
);
140 ret
= NSS_STATUS_SUCCESS
;
143 free(lnames
.namelist_val
);
144 /* free(lid.idlist_val); */
148 pthread_mutex_unlock(&mutex
);
153 * Look up the uid corresponding to name in ptserver.
155 enum nss_status
ptsname2id(char *name
, uid_t
* uid
) {
162 if (!strcmp(name
,"anonymous")) {
163 *uid
= AFS_MAGIC_ANONYMOUS_USERID
;
164 return NSS_STATUS_SUCCESS
;
167 if (pthread_mutex_lock(&mutex
)) return NSS_STATUS_UNAVAIL
;
171 lnames
.namelist_val
= (prname
*)name
;
172 lnames
.namelist_len
= 1;
174 if (ubik_Call(PR_NameToID
,pruclient
,0,&lnames
,&lid
) != PRSUCCESS
) {
175 pthread_mutex_unlock(&mutex
);
176 return NSS_STATUS_UNAVAIL
;
178 pthread_mutex_unlock(&mutex
);
180 res
= (uid_t
)lid
.idlist_val
[0];
181 if (res
== AFS_MAGIC_ANONYMOUS_USERID
) return NSS_STATUS_NOTFOUND
;
183 return NSS_STATUS_SUCCESS
;
187 * Initialize the library; returns zero on success
193 if (afs_initialized
) return 0;
195 if (pthread_mutex_lock(&mutex
)) return -1;
197 homedirs_method
=HOMEDIR_PREFIX
;
198 shells_method
=SHELL_USERLINK
;
200 len
= snprintf(cellname
, MAXCELLNAMELEN
,
201 "%s/ThisCell", AFSDIR_CLIENT_ETC_DIRPATH
);
202 if (len
< 0 || len
>= MAXCELLNAMELEN
) return -1;
204 thiscell
=fopen(cellname
,"r");
205 if (thiscell
== NULL
) break;
206 len
=fread(cellname
,1,MAXCELLNAMELEN
,thiscell
);
207 if (!feof(thiscell
)) {
210 strcpy(homedir_prefix
,"/tmp/\0");
211 homedir_prefix_len
=5;
216 if (cellname
[len
-1] == '\n') len
--;
218 sprintf(homedir_prefix
,"/afs/%s/user/",cellname
);
219 homedir_prefix_len
=strlen(homedir_prefix
);
221 if (pr_Initialize(0L,AFSDIR_CLIENT_ETC_DIRPATH
, 0)) break;
224 pthread_mutex_unlock(&mutex
);
228 pthread_mutex_unlock(&mutex
);
234 * Retrieves the homedir for a given user; returns 0 on success.
236 int get_homedir(char *name
, char **buffer
, size_t *buflen
) {
241 switch (homedirs_method
) {
243 homedir_prefix
[homedir_prefix_len
+0]=name
[0];
244 homedir_prefix
[homedir_prefix_len
+1]='/';
245 homedir_prefix
[homedir_prefix_len
+2]=name
[0];
246 homedir_prefix
[homedir_prefix_len
+3]=name
[1];
247 homedir_prefix
[homedir_prefix_len
+4]='/';
248 homedir_prefix
[homedir_prefix_len
+5]=0;
249 strncpy(&homedir_prefix
[homedir_prefix_len
+5],name
,40);
250 if (! cpstr(homedir_prefix
,buffer
,buflen
) ) return -1;
253 homedir_prefix
[homedir_prefix_len
]=0;
254 strncpy(&homedir_prefix
[homedir_prefix_len
],name
,40);
255 if (! cpstr(homedir_prefix
,buffer
,buflen
) ) return -1;
257 case HOMEDIR_ADMINLINK
:
258 if ( snprintf(buf
,256,"/afs/%s/admin/homedirs/%s",cellname
,name
) > 0 ) {
259 temp
=readlink(buf
,*buffer
,*buflen
);
262 *buflen
= *buflen
- temp
- 1;
266 if (! cpstr("/tmp",buffer
,buflen
) ) return -1;
273 * Retrieves the shell for a given user; returns 0 on success.
275 int get_shell(char *name
, char **buffer
, size_t *buflen
) {
283 switch (shells_method
) {
287 case SHELL_ADMINLINK
:
288 if (snprintf(buf
,256,"/afs/%s/admin/shells/%s",cellname
,name
)<=0) break;
289 temp
= readlink(buf
,*buffer
,*buflen
);
292 *buflen
= *buflen
- temp
- 1;
296 if (get_homedir(name
, &bufx
, &bufxlen
)) break;
297 if (strncpy(buf
+strlen(buf
),"/.loginshell",bufxlen
)<=0) break;
298 temp
= readlink(buf
,*buffer
,*buflen
);
301 *buflen
= *buflen
- temp
- 1;
304 if (! cpstr("/bin/bash",buffer
,buflen
) )
311 * This function is exported; glibc will invoke it in order to find
312 * the name and list of members of a group specified by a numerical
315 enum nss_status
_nss_afs_getgrgid_r (gid_t gid
,
316 struct group
*result
,
322 if (gid
>= MIN_PAG_GID
&& gid
<= MAX_PAG_GID
) {
323 showgid
= gid
-MIN_PAG_GID
;
324 } else if (gid
>= MIN_OLDPAG_GID
&& gid
<= MAX_OLDPAG_GID
) {
325 showgid
= gid
-MIN_OLDPAG_GID
;
328 return NSS_STATUS_NOTFOUND
;
333 result
->gr_name
=buffer
;
334 length
=snprintf(buffer
,buflen
,"AfsPag-%x",showgid
);
336 if (length
< 0) break;
341 result
->gr_passwd
=buffer
;
343 if (!cpstr("x",&buffer
,&buflen
)) break;
345 if (buflen
< sizeof(char*)) break;
346 result
->gr_mem
=buffer
;
347 result
->gr_mem
[0] = NULL
;
350 return NSS_STATUS_SUCCESS
;
354 return NSS_STATUS_UNAVAIL
;
358 * A helper function to fill in the fields of "struct passwd"; used by
359 * both _nss_afs_getpwuid_r() and _nss_afs_getpwnam_r().
361 enum nss_status
fill_result_buf(uid_t uid
,
363 struct passwd
*result_buf
,
367 result_buf
->pw_name
= name
;
369 /* set the password to "x" */
370 result_buf
->pw_passwd
= buffer
;
371 if ( ! cpstr("x",&buffer
, &buflen
) ) break;
373 /* the uid and gid are both the uid passed in */
374 result_buf
->pw_uid
= uid
;
375 result_buf
->pw_gid
= 65534;
377 /* make the gecos the same as the PTS name */
378 result_buf
->pw_gecos
= buffer
;
379 if ( ! cpstr(result_buf
->pw_name
, &buffer
, &buflen
) ) break;
381 // Set the homedirectory
382 result_buf
->pw_dir
= buffer
;
383 if ( get_homedir(result_buf
->pw_name
,&buffer
,&buflen
) ) break;
385 // Set the login shell
386 result_buf
->pw_shell
= buffer
;
387 if ( get_shell(result_buf
->pw_name
,&buffer
,&buflen
) ) break;
390 return NSS_STATUS_SUCCESS
;
394 return NSS_STATUS_UNAVAIL
;
399 * This function is exported; glibc will invoke it in order to gather
400 * the user information (userid, homedir, shell) associated with a
403 enum nss_status
_nss_afs_getpwuid_r (uid_t uid
,
404 struct passwd
*result_buf
,
411 if (init_afs()) return NSS_STATUS_UNAVAIL
;
414 temp
= ptsid2name( uid
, &buffer
, &buflen
);
415 if (temp
!= NSS_STATUS_SUCCESS
) {
420 #ifdef LIMIT_USERNAME_CHARS
421 if ( strlen(result_buf
->pw_name
) > LIMIT_USERNAME_CHARS
) {
422 result_buf
->pw_name
[LIMIT_USERNAME_CHARS
] = '\0';
423 buflen
= buflen
+ ( buffer
- &result_buf
->pw_name
[LIMIT_USERNAME_CHARS
+1] );
424 buffer
= &result_buf
->pw_name
[LIMIT_USERNAME_CHARS
+1];
428 return fill_result_buf(uid
, name
, result_buf
, buffer
, buflen
, errnop
);
432 * This function is exported; glibc will invoke it in order to gather
433 * the user information (userid, homedir, shell) associated with a
436 enum nss_status
_nss_afs_getpwnam_r (char *name
,
437 struct passwd
*result_buf
,
444 if (init_afs()) return NSS_STATUS_UNAVAIL
;
446 temp
= ptsname2id(name
,&uid
);
447 if (temp
!= NSS_STATUS_SUCCESS
) {
452 return fill_result_buf(uid
, name
, result_buf
, buffer
, buflen
, errnop
);