5 * Copyright 2008, licensed under GNU Library General Public License (LGPL)
6 * see COPYING file for details
8 * derived from Frank Burkhardt's libnss_ptdb,
9 * which was derived from Todd M. Lewis' libnss_pts
13 * if you are reading this code for the first time, start at the
14 * bottom and work your way upwards
23 #include <netinet/in.h>
33 #include <sys/select.h>
34 #include <sys/socket.h>
36 #include <sys/types.h>
39 #include <afs/afsutil.h>
40 #include <afs/cellconfig.h>
41 #include <afs/com_err.h>
42 #include <afs/param.h>
43 #include <afs/ptclient.h>
44 #include <afs/pterror.h>
47 #define HOMEDIR_AUTO 0
48 #define HOMEDIR_ADMINLINK 1
49 #define HOMEDIR_PREFIX 2
51 #define SHELL_ADMINLINK 1
52 #define SHELL_USERLINK 2
54 #define AFS_MAGIC_ANONYMOUS_USERID 32766
55 #define MIN_PAG_GID 0x41000000L
56 #define MAX_PAG_GID 0x41FFFFFFL
58 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
60 extern int cpstr( char *str
, char **buf
, size_t *buflen
);
62 extern struct ubik_client
*pruclient
;
64 int afs_initialized
= 0;
66 char homedir_prefix
[300];
67 int homedir_prefix_len
=0;
68 char homedirs_method
=0;
71 int cpstr( char *str
, char **buf
, size_t *buflen
) {
72 int len
= strlen(str
);
73 if ( len
>= *buflen
-1 ) return 0;
82 * Look up the name corresponding to uid, store in buffer.
84 * returns NSS_STATUS_SUCCESS, NSS_STATUS_NOTFOUND, or NSS_STATUS_UNAVAIL
86 int ptsid2name(int uid
, char **buffer
, int *buflen
) {
93 if (uid
==AFS_MAGIC_ANONYMOUS_USERID
) {
94 if (!cpstr("anonymous", buffer
, buflen
)) return NSS_STATUS_UNAVAIL
;
95 return NSS_STATUS_SUCCESS
;
98 if (pthread_mutex_lock(&mutex
)) return NSS_STATUS_UNAVAIL
;
100 lid
.idlist_val
= (afs_int32
*)&uid
;
102 lnames
.namelist_val
= 0;
103 lnames
.namelist_len
= 0;
105 if (ubik_Call(PR_IDToName
,pruclient
,0,&lid
,&lnames
) != PRSUCCESS
) {
106 pthread_mutex_unlock(&mutex
);
107 return NSS_STATUS_UNAVAIL
;
110 ret
= NSS_STATUS_NOTFOUND
;
111 for (i
=0;i
<lnames
.namelist_len
;i
++) {
112 int delta
= strlen(lnames
.namelist_val
[i
]);
113 if ( (delta
< buflen
) && islower(*(lnames
.namelist_val
[i
])) ) {
114 cpstr(lnames
.namelist_val
[i
], buffer
, buflen
);
115 ret
= NSS_STATUS_SUCCESS
;
118 free(lnames
.namelist_val
);
119 /* free(lid.idlist_val); */
123 pthread_mutex_unlock(&mutex
);
128 * Look up the uid corresponding to name, stores it in *uid.
130 * returns NSS_STATUS_SUCCESS, NSS_STATUS_NOTFOUND, or NSS_STATUS_UNAVAIL
132 int ptsname2id(char *name
, uid_t
* uid
) {
139 if (!strcmp(name
,"anonymous")) {
140 *uid
= AFS_MAGIC_ANONYMOUS_USERID
;
141 return NSS_STATUS_SUCCESS
;
144 if (pthread_mutex_lock(&mutex
)) return NSS_STATUS_UNAVAIL
;
148 lnames
.namelist_val
= (prname
*)name
;
149 lnames
.namelist_len
= 1;
151 if (ubik_Call(PR_NameToID
,pruclient
,0,&lnames
,&lid
) != PRSUCCESS
) {
152 pthread_mutex_unlock(&mutex
);
153 return NSS_STATUS_UNAVAIL
;
155 pthread_mutex_unlock(&mutex
);
157 res
= (uid_t
)lid
.idlist_val
[0];
158 if (res
== AFS_MAGIC_ANONYMOUS_USERID
) return NSS_STATUS_NOTFOUND
;
160 return NSS_STATUS_SUCCESS
;
164 * returns zero on success
170 if (afs_initialized
) return 0;
172 if (pthread_mutex_lock(&mutex
)) return -1;
174 homedirs_method
=HOMEDIR_PREFIX
;
175 shells_method
=SHELL_USERLINK
;
177 thiscell
=fopen("/etc/openafs/ThisCell","r");
178 if (thiscell
== NULL
) break;
179 len
=fread(cellname
,1,256,thiscell
);
180 if (!feof(thiscell
)) {
183 strcpy(homedir_prefix
,"/tmp/\0");
184 homedir_prefix_len
=5;
188 if (cellname
[len
-1] == '\n') len
--;
190 sprintf(homedir_prefix
,"/afs/%s/user/",cellname
);
191 homedir_prefix_len
=strlen(homedir_prefix
);
193 if (pr_Initialize(0L,AFSDIR_CLIENT_ETC_DIRPATH
, 0)) break;
196 pthread_mutex_unlock(&mutex
);
200 pthread_mutex_unlock(&mutex
);
206 result=get_homedir(char *name,char **buffer,size_t *buflen)
207 Writes the guessed Homedirectory of a given user 'name' into
208 '*buffer', increasing *buflen accordingly.
209 result == 1, only if the buffer was big enough.
211 int get_homedir(char *name
, char **buffer
, size_t *buflen
) {
216 switch (homedirs_method
) {
218 homedir_prefix
[homedir_prefix_len
+0]=name
[0];
219 homedir_prefix
[homedir_prefix_len
+1]='/';
220 homedir_prefix
[homedir_prefix_len
+2]=name
[0];
221 homedir_prefix
[homedir_prefix_len
+3]=name
[1];
222 homedir_prefix
[homedir_prefix_len
+4]='/';
223 homedir_prefix
[homedir_prefix_len
+5]=0;
224 strncpy(&homedir_prefix
[homedir_prefix_len
+5],name
,40);
225 if (! cpstr(homedir_prefix
,buffer
,buflen
) ) return -1;
228 homedir_prefix
[homedir_prefix_len
]=0;
229 strncpy(&homedir_prefix
[homedir_prefix_len
],name
,40);
230 if (! cpstr(homedir_prefix
,buffer
,buflen
) ) return -1;
232 case HOMEDIR_ADMINLINK
:
233 if ( snprintf(buf
,256,"/afs/%s/admin/homedirs/%s",cellname
,name
) > 0 ) {
234 temp
=readlink(buf
,*buffer
,*buflen
);
237 *buflen
= *buflen
- temp
- 1;
241 if (! cpstr("/tmp",buffer
,buflen
) ) return -1;
247 int get_shell(char *name
, char **buffer
, size_t *buflen
) {
255 switch (shells_method
) {
257 if (!cpstr("/bin/bash",buffer
, buflen
)) return -1;
260 case SHELL_ADMINLINK
:
261 if ( snprintf(buf
,256,"/afs/%s/admin/shells/%s",cellname
,name
) > 0 ) {
262 temp
=readlink(buf
,*buffer
,*buflen
);
265 *buflen
= *buflen
- temp
- 1;
269 if (! cpstr("/bin/bash",buffer
,buflen
) )
274 if (get_homedir(name
, &bufx
, &bufxlen
)) return -1;
275 strncpy(buf
+strlen(buf
),"/.loginshell",bufxlen
);
276 temp
=readlink(buf
,*buffer
,*buflen
);
279 *buflen
= *buflen
- temp
- 1;
282 if (! cpstr("/bin/bash",buffer
,buflen
) )
291 * this function is invoked by glibc to resolve the name of a numeric groupid
293 enum nss_status
_nss_afs_getgrgid_r (gid_t gid
, struct group
*result
,
294 char *buffer
, size_t buflen
, int *errnop
) {
296 if ( gid
< MIN_PAG_GID
|| gid
> MAX_PAG_GID
) {
298 return NSS_STATUS_NOTFOUND
;
303 result
->gr_name
=buffer
;
304 length
=snprintf(buffer
,buflen
,"AfsPag-%x",gid
-MIN_PAG_GID
);
306 if (length
< 0) break;
311 result
->gr_passwd
=buffer
;
313 if (!cpstr("x",&buffer
,&buflen
)) break;
315 if (buflen
< sizeof(char*)) break;
316 result
->gr_mem
=buffer
;
317 result
->gr_mem
[0] = NULL
;
320 return NSS_STATUS_SUCCESS
;
324 return NSS_STATUS_UNAVAIL
;
328 This is a the ptdb-getpwuid-function.
330 enum nss_status
_nss_afs_getpwuid_r (uid_t uid
, struct passwd
*result_buf
, char *buffer
, size_t buflen
, int *errnop
) {
333 if (init_afs()) return NSS_STATUS_UNAVAIL
;
335 result_buf
->pw_name
= buffer
;
336 temp
= ptsid2name( uid
, &buffer
, &buflen
);
337 if (temp
!= NSS_STATUS_SUCCESS
) {
342 #ifdef LIMIT_USERNAME_CHARS
343 if ( strlen(result_buf
->pw_name
) > LIMIT_USERNAME_CHARS
) {
344 result_buf
->pw_name
[LIMIT_USERNAME_CHARS
] = '\0';
345 buflen
= buflen
+ ( buffer
- &result_buf
->pw_name
[LIMIT_USERNAME_CHARS
+1] );
346 buffer
= &result_buf
->pw_name
[LIMIT_USERNAME_CHARS
+1];
351 /* set the password to "x" */
352 result_buf
->pw_passwd
= buffer
;
353 if ( ! cpstr("x",&buffer
, &buflen
) ) break;
354 /* the uid and gid are both the uid passed in */
355 result_buf
->pw_uid
= uid
;
356 result_buf
->pw_gid
= 65534;
357 /* make the gecos the same as the PTS name */
358 result_buf
->pw_gecos
= buffer
;
359 if ( ! cpstr(result_buf
->pw_name
, &buffer
, &buflen
) ) break;
361 // Set the homedirectory
362 result_buf
->pw_dir
= buffer
;
363 if ( get_homedir(result_buf
->pw_name
,&buffer
,&buflen
) ) break;
365 // Set the login shell
366 result_buf
->pw_shell
= buffer
;
367 if ( get_shell(result_buf
->pw_name
,&buffer
,&buflen
) ) break;
370 return NSS_STATUS_SUCCESS
;
374 return NSS_STATUS_UNAVAIL
;
379 * This is the ptdb-getpwnam-function.
381 enum nss_status
_nss_afs_getpwnam_r (char *name
, struct passwd
*result_buf
, char *buffer
, size_t buflen
, int *errnop
) {
385 if (init_afs()) return NSS_STATUS_UNAVAIL
;
387 temp
= ptsname2id(name
,&uid
);
388 if (temp
!= NSS_STATUS_SUCCESS
) {
393 // This causes an additional PTDB-lookup and should be removed in the future
394 return _nss_afs_getpwuid_r (uid
, result_buf
,buffer
,buflen
, errnop
);