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
81 #define MAXUSERNAMELEN 256
83 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
85 extern struct ubik_client
*pruclient
;
87 int afs_initialized
= 0;
88 char cellname
[MAXCELLNAMELEN
];
89 char homedir_prefix
[MAXPATHLEN
];
90 int homedir_prefix_len
=0;
91 char homedirs_method
=0;
95 * The cpstr() function copies a null-terminated string from str*
96 * (the first argument) into buf and updates both buf and buflen. If
97 * the string would overflow the buffer, no action is taken. The
98 * number of bytes copied is returned (zero indicates failure).
100 int cpstr( char *str
, char **buf
, size_t *buflen
) {
101 int len
= strlen(str
);
102 if ( len
>= *buflen
-1 ) return 0;
110 * Look up the name corresponding to uid, store in buffer.
112 enum nss_status
ptsid2name(int uid
, char **buffer
, int *buflen
) {
119 if (uid
==AFS_MAGIC_ANONYMOUS_USERID
) {
120 if (!cpstr("anonymous", buffer
, buflen
)) return NSS_STATUS_UNAVAIL
;
121 return NSS_STATUS_SUCCESS
;
124 if (pthread_mutex_lock(&mutex
)) return NSS_STATUS_UNAVAIL
;
126 lid
.idlist_val
= (afs_int32
*)&uid
;
128 lnames
.namelist_val
= 0;
129 lnames
.namelist_len
= 0;
131 if (ubik_Call(PR_IDToName
,pruclient
,0,&lid
,&lnames
) != PRSUCCESS
) {
132 pthread_mutex_unlock(&mutex
);
133 return NSS_STATUS_UNAVAIL
;
136 ret
= NSS_STATUS_NOTFOUND
;
137 for (i
=0;i
<lnames
.namelist_len
;i
++) {
138 int delta
= strlen(lnames
.namelist_val
[i
]);
139 if ( (delta
< buflen
) && islower(*(lnames
.namelist_val
[i
])) ) {
140 cpstr(lnames
.namelist_val
[i
], buffer
, buflen
);
141 ret
= NSS_STATUS_SUCCESS
;
144 free(lnames
.namelist_val
);
145 /* free(lid.idlist_val); */
149 pthread_mutex_unlock(&mutex
);
154 * Look up the uid corresponding to name in ptserver.
156 enum nss_status
ptsname2id(char *name
, uid_t
* uid
) {
160 char uname
[MAXUSERNAMELEN
];
164 if (!strcmp(name
,"anonymous")) {
165 *uid
= AFS_MAGIC_ANONYMOUS_USERID
;
166 return NSS_STATUS_SUCCESS
;
169 if (pthread_mutex_lock(&mutex
)) return NSS_STATUS_UNAVAIL
;
173 lnames
.namelist_val
= (prname
*)uname
;
174 // apparently ubik expects to be able to modify this?
175 strncpy(uname
, name
, MAXUSERNAMELEN
);
176 lnames
.namelist_len
= 1;
178 if (ubik_Call(PR_NameToID
,pruclient
,0,&lnames
,&lid
) != PRSUCCESS
) {
179 pthread_mutex_unlock(&mutex
);
180 return NSS_STATUS_UNAVAIL
;
182 pthread_mutex_unlock(&mutex
);
184 res
= (uid_t
)lid
.idlist_val
[0];
185 if (res
== AFS_MAGIC_ANONYMOUS_USERID
) return NSS_STATUS_NOTFOUND
;
187 return NSS_STATUS_SUCCESS
;
191 * Initialize the library; returns zero on success
197 if (afs_initialized
) return 0;
199 if (pthread_mutex_lock(&mutex
)) return -1;
201 homedirs_method
=HOMEDIR_PREFIX
;
202 shells_method
=SHELL_USERLINK
;
204 len
= snprintf(cellname
, MAXCELLNAMELEN
,
205 "%s/ThisCell", AFSDIR_CLIENT_ETC_DIRPATH
);
206 if (len
< 0 || len
>= MAXCELLNAMELEN
) return -1;
208 thiscell
=fopen(cellname
,"r");
209 if (thiscell
== NULL
) break;
210 len
=fread(cellname
,1,MAXCELLNAMELEN
,thiscell
);
211 if (!feof(thiscell
)) {
214 strcpy(homedir_prefix
,"/tmp/\0");
215 homedir_prefix_len
=5;
220 if (cellname
[len
-1] == '\n') len
--;
222 sprintf(homedir_prefix
,"/afs/%s/user/",cellname
);
223 homedir_prefix_len
=strlen(homedir_prefix
);
225 if (pr_Initialize(0L,AFSDIR_CLIENT_ETC_DIRPATH
, 0)) break;
228 pthread_mutex_unlock(&mutex
);
232 pthread_mutex_unlock(&mutex
);
238 * Retrieves the homedir for a given user; returns 0 on success.
240 int get_homedir(char *name
, char **buffer
, size_t *buflen
) {
245 switch (homedirs_method
) {
247 homedir_prefix
[homedir_prefix_len
+0]=name
[0];
248 homedir_prefix
[homedir_prefix_len
+1]='/';
249 homedir_prefix
[homedir_prefix_len
+2]=name
[0];
250 homedir_prefix
[homedir_prefix_len
+3]=name
[1];
251 homedir_prefix
[homedir_prefix_len
+4]='/';
252 homedir_prefix
[homedir_prefix_len
+5]=0;
253 strncpy(&homedir_prefix
[homedir_prefix_len
+5],name
,40);
254 if (! cpstr(homedir_prefix
,buffer
,buflen
) ) return -1;
257 homedir_prefix
[homedir_prefix_len
]=0;
258 strncpy(&homedir_prefix
[homedir_prefix_len
],name
,40);
259 if (! cpstr(homedir_prefix
,buffer
,buflen
) ) return -1;
261 case HOMEDIR_ADMINLINK
:
262 if ( snprintf(buf
,256,"/afs/%s/admin/homedirs/%s",cellname
,name
) > 0 ) {
263 temp
=readlink(buf
,*buffer
,*buflen
);
266 *buflen
= *buflen
- temp
- 1;
270 if (! cpstr("/tmp",buffer
,buflen
) ) return -1;
277 * Retrieves the shell for a given user; returns 0 on success.
279 int get_shell(char *name
, char **buffer
, size_t *buflen
) {
287 switch (shells_method
) {
291 case SHELL_ADMINLINK
:
292 if (snprintf(buf
,256,"/afs/%s/admin/shells/%s",cellname
,name
)<=0) break;
293 temp
= readlink(buf
,*buffer
,*buflen
);
296 *buflen
= *buflen
- temp
- 1;
300 if (get_homedir(name
, &bufx
, &bufxlen
)) break;
301 if (strncpy(buf
+strlen(buf
),"/.loginshell",bufxlen
)<=0) break;
302 temp
= readlink(buf
,*buffer
,*buflen
);
305 *buflen
= *buflen
- temp
- 1;
308 if (! cpstr("/bin/bash",buffer
,buflen
) )
315 * This function is exported; glibc will invoke it in order to find
316 * the name and list of members of a group specified by a numerical
319 enum nss_status
_nss_afs_getgrgid_r (gid_t gid
,
320 struct group
*result
,
326 if (gid
>= MIN_PAG_GID
&& gid
<= MAX_PAG_GID
) {
327 showgid
= gid
-MIN_PAG_GID
;
328 } else if (gid
>= MIN_OLDPAG_GID
&& gid
<= MAX_OLDPAG_GID
) {
329 showgid
= gid
-MIN_OLDPAG_GID
;
332 return NSS_STATUS_NOTFOUND
;
337 result
->gr_name
=buffer
;
338 length
=snprintf(buffer
,buflen
,"AfsPag-%x",showgid
);
340 if (length
< 0) break;
345 result
->gr_passwd
=buffer
;
347 if (!cpstr("x",&buffer
,&buflen
)) break;
349 if (buflen
< sizeof(char*)) break;
350 result
->gr_mem
=buffer
;
351 result
->gr_mem
[0] = NULL
;
354 return NSS_STATUS_SUCCESS
;
358 return NSS_STATUS_UNAVAIL
;
362 * A helper function to fill in the fields of "struct passwd"; used by
363 * both _nss_afs_getpwuid_r() and _nss_afs_getpwnam_r().
365 enum nss_status
fill_result_buf(uid_t uid
,
367 struct passwd
*result_buf
,
371 result_buf
->pw_name
= name
;
373 /* set the password to "x" */
374 result_buf
->pw_passwd
= buffer
;
375 if ( ! cpstr("x",&buffer
, &buflen
) ) break;
377 /* the uid and gid are both the uid passed in */
378 result_buf
->pw_uid
= uid
;
379 result_buf
->pw_gid
= 65534;
381 /* make the gecos the same as the PTS name */
382 result_buf
->pw_gecos
= buffer
;
383 if ( ! cpstr(result_buf
->pw_name
, &buffer
, &buflen
) ) break;
385 // Set the homedirectory
386 result_buf
->pw_dir
= buffer
;
387 if ( get_homedir(result_buf
->pw_name
,&buffer
,&buflen
) ) break;
389 // Set the login shell
390 result_buf
->pw_shell
= buffer
;
391 if ( get_shell(result_buf
->pw_name
,&buffer
,&buflen
) ) break;
394 return NSS_STATUS_SUCCESS
;
398 return NSS_STATUS_UNAVAIL
;
403 * This function is exported; glibc will invoke it in order to gather
404 * the user information (userid, homedir, shell) associated with a
407 enum nss_status
_nss_afs_getpwuid_r (uid_t uid
,
408 struct passwd
*result_buf
,
415 if (init_afs()) return NSS_STATUS_UNAVAIL
;
418 temp
= ptsid2name( uid
, &buffer
, &buflen
);
419 if (temp
!= NSS_STATUS_SUCCESS
) {
424 #ifdef LIMIT_USERNAME_CHARS
425 if ( strlen(result_buf
->pw_name
) > LIMIT_USERNAME_CHARS
) {
426 result_buf
->pw_name
[LIMIT_USERNAME_CHARS
] = '\0';
427 buflen
= buflen
+ ( buffer
- &result_buf
->pw_name
[LIMIT_USERNAME_CHARS
+1] );
428 buffer
= &result_buf
->pw_name
[LIMIT_USERNAME_CHARS
+1];
432 return fill_result_buf(uid
, name
, result_buf
, buffer
, buflen
, errnop
);
436 * This function is exported; glibc will invoke it in order to gather
437 * the user information (userid, homedir, shell) associated with a
440 enum nss_status
_nss_afs_getpwnam_r (char *name
,
441 struct passwd
*result_buf
,
448 if (init_afs()) return NSS_STATUS_UNAVAIL
;
450 temp
= ptsname2id(name
,&uid
);
451 if (temp
!= NSS_STATUS_SUCCESS
) {
456 return fill_result_buf(uid
, name
, result_buf
, buffer
, buflen
, errnop
);