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>
57 #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>
68 #define HOMEDIR_AUTO 0
69 #define HOMEDIR_ADMINLINK 1
70 #define HOMEDIR_PREFIX 2
72 #define SHELL_ADMINLINK 1
73 #define SHELL_USERLINK 2
75 #define AFS_MAGIC_ANONYMOUS_USERID 32766
76 #define MIN_PAG_GID 0x41000000L
77 #define MAX_PAG_GID 0x41FFFFFFL
78 #define MIN_OLDPAG_GID 0x3f00
79 #define MAX_OLDPAG_GID 0xff00
81 #define MAXCELLNAMELEN 256
82 #define MAXUSERNAMELEN 256
84 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
86 extern struct ubik_client *pruclient;
88 int afs_initialized = 0;
89 char cellname[MAXCELLNAMELEN];
90 char homedir_prefix[MAXPATHLEN];
91 char cell_root[MAXPATHLEN];
92 int homedir_prefix_len=0;
93 char homedirs_method=0;
97 * The cpstr() function copies a null-terminated string from str*
98 * (the first argument) into buf and updates both buf and buflen. If
99 * the string would overflow the buffer, no action is taken. The
100 * number of bytes copied is returned (zero indicates failure).
102 int cpstr( char *str, char **buf, size_t *buflen) {
103 int len = strlen(str);
104 if ( len >= *buflen-1 ) return 0;
112 * Look up the name corresponding to uid, store in buffer.
114 enum nss_status ptsid2name(int uid, char **buffer, int *buflen) {
121 if (uid==AFS_MAGIC_ANONYMOUS_USERID) {
122 if (!cpstr("anonymous", buffer, buflen)) return NSS_STATUS_UNAVAIL;
123 return NSS_STATUS_SUCCESS;
126 if (pthread_mutex_lock(&mutex)) return NSS_STATUS_UNAVAIL;
128 lid.idlist_val = (afs_int32*)&uid;
130 lnames.namelist_val = 0;
131 lnames.namelist_len = 0;
133 if (ubik_Call(PR_IDToName,pruclient,0,&lid,&lnames) != PRSUCCESS) {
134 pthread_mutex_unlock(&mutex);
135 return NSS_STATUS_UNAVAIL;
138 ret = NSS_STATUS_NOTFOUND;
139 for (i=0;i<lnames.namelist_len;i++) {
140 int delta = strlen(lnames.namelist_val[i]);
141 if ( (delta < buflen) && islower(*(lnames.namelist_val[i])) ) {
142 cpstr(lnames.namelist_val[i], buffer, buflen);
143 ret = NSS_STATUS_SUCCESS;
146 free(lnames.namelist_val);
147 /* free(lid.idlist_val); */
151 pthread_mutex_unlock(&mutex);
156 * Look up the uid corresponding to name in ptserver.
158 enum nss_status ptsname2id(char *name, uid_t* uid) {
162 char uname[MAXUSERNAMELEN];
166 if (!strcmp(name,"anonymous")) {
167 *uid = AFS_MAGIC_ANONYMOUS_USERID;
168 return NSS_STATUS_SUCCESS;
171 if (pthread_mutex_lock(&mutex)) return NSS_STATUS_UNAVAIL;
175 lnames.namelist_val = (prname*)uname;
176 // apparently ubik expects to be able to modify this?
177 strncpy(uname, name, MAXUSERNAMELEN);
178 lnames.namelist_len = 1;
180 if (ubik_Call(PR_NameToID,pruclient,0,&lnames,&lid) != PRSUCCESS) {
181 pthread_mutex_unlock(&mutex);
182 return NSS_STATUS_UNAVAIL;
184 pthread_mutex_unlock(&mutex);
186 res = (uid_t)lid.idlist_val[0];
187 if (res == AFS_MAGIC_ANONYMOUS_USERID) return NSS_STATUS_NOTFOUND;
189 return NSS_STATUS_SUCCESS;
193 * Initialize the library; returns zero on success
200 if (afs_initialized) {
201 /* wait until /afs/@cell/ appears as a proxy for "the network is up" */
202 if (stat(cell_root, &statbuf)) return -1;
206 if (pthread_mutex_lock(&mutex)) return -1;
208 homedirs_method=HOMEDIR_PREFIX;
209 shells_method=SHELL_USERLINK;
211 len = snprintf(cellname, MAXCELLNAMELEN,
212 "%s/ThisCell", AFSDIR_CLIENT_ETC_DIRPATH);
213 if (len < 0 || len >= MAXCELLNAMELEN) return -1;
215 thiscell=fopen(cellname,"r");
216 if (thiscell == NULL) break;
217 len=fread(cellname,1,MAXCELLNAMELEN,thiscell);
218 if (!feof(thiscell)) {
221 strcpy(homedir_prefix,"/tmp/\0");
222 homedir_prefix_len=5;
227 if (cellname[len-1] == '\n') len--;
230 /* wait until /afs/@cell/ appears as a proxy for "the network is up" */
231 sprintf(cell_root,"/afs/%s/",cellname);
232 if (stat(cell_root, &statbuf)) break;
234 sprintf(homedir_prefix,"/afs/%s/user/",cellname);
235 homedir_prefix_len=strlen(homedir_prefix);
237 /* time out requests after 5 seconds to avoid hanging things */
240 if (pr_Initialize(0L,AFSDIR_CLIENT_ETC_DIRPATH, 0)) break;
243 pthread_mutex_unlock(&mutex);
247 pthread_mutex_unlock(&mutex);
253 * Retrieves the homedir for a given user; returns 0 on success.
255 int get_homedir(char *name, char **buffer, size_t *buflen) {
260 switch (homedirs_method) {
262 homedir_prefix[homedir_prefix_len+0]=name[0];
263 homedir_prefix[homedir_prefix_len+1]='/';
264 homedir_prefix[homedir_prefix_len+2]=name[0];
265 homedir_prefix[homedir_prefix_len+3]=name[1];
266 homedir_prefix[homedir_prefix_len+4]='/';
267 homedir_prefix[homedir_prefix_len+5]=0;
268 strncpy(&homedir_prefix[homedir_prefix_len+5],name,40);
269 if (! cpstr(homedir_prefix,buffer,buflen) ) return -1;
272 homedir_prefix[homedir_prefix_len]=0;
273 strncpy(&homedir_prefix[homedir_prefix_len],name,40);
274 if (! cpstr(homedir_prefix,buffer,buflen) ) return -1;
276 case HOMEDIR_ADMINLINK:
277 if ( snprintf(buf,256,"/afs/%s/admin/homedirs/%s",cellname,name) > 0 ) {
278 temp=readlink(buf,*buffer,*buflen);
281 *buflen = *buflen - temp - 1;
285 if (! cpstr("/tmp",buffer,buflen) ) return -1;
292 * Retrieves the shell for a given user; returns 0 on success.
294 int get_shell(char *name, char **buffer, size_t *buflen) {
302 switch (shells_method) {
306 case SHELL_ADMINLINK:
307 if (snprintf(buf,256,"/afs/%s/admin/shells/%s",cellname,name)<=0) break;
308 temp = readlink(buf,*buffer,*buflen);
311 *buflen = *buflen - temp - 1;
315 if (get_homedir(name, &bufx, &bufxlen)) break;
316 if (strncpy(buf+strlen(buf),"/.loginshell",bufxlen)<=0) break;
317 temp = readlink(buf,*buffer,*buflen);
320 *buflen = *buflen - temp - 1;
323 if (! cpstr("/bin/bash",buffer,buflen) )
330 * This function is exported; glibc will invoke it in order to find
331 * the name and list of members of a group specified by a numerical
334 enum nss_status _nss_afs_getgrgid_r (gid_t gid,
335 struct group *result,
341 if (gid >= MIN_PAG_GID && gid <= MAX_PAG_GID) {
342 showgid = gid-MIN_PAG_GID;
343 } else if (gid >= MIN_OLDPAG_GID && gid <= MAX_OLDPAG_GID) {
344 showgid = gid-MIN_OLDPAG_GID;
347 return NSS_STATUS_NOTFOUND;
352 result->gr_name=buffer;
353 length=snprintf(buffer,buflen,"AfsPag-%x",showgid);
355 if (length < 0) break;
360 result->gr_passwd=buffer;
362 if (!cpstr("x",&buffer,&buflen)) break;
364 if (buflen < sizeof(char*)) break;
365 result->gr_mem=buffer;
366 result->gr_mem[0] = NULL;
369 return NSS_STATUS_SUCCESS;
373 return NSS_STATUS_UNAVAIL;
377 * A helper function to fill in the fields of "struct passwd"; used by
378 * both _nss_afs_getpwuid_r() and _nss_afs_getpwnam_r().
380 enum nss_status fill_result_buf(uid_t uid,
382 struct passwd *result_buf,
386 result_buf->pw_name = name;
388 /* set the password to "x" */
389 result_buf->pw_passwd = buffer;
390 if ( ! cpstr("x",&buffer, &buflen) ) break;
392 /* the uid and gid are both the uid passed in */
393 result_buf->pw_uid = uid;
394 result_buf->pw_gid = 65534;
396 /* make the gecos the same as the PTS name */
397 result_buf->pw_gecos = buffer;
398 if ( ! cpstr(result_buf->pw_name, &buffer, &buflen ) ) break;
400 // Set the homedirectory
401 result_buf->pw_dir = buffer;
402 if ( get_homedir(result_buf->pw_name,&buffer,&buflen) ) break;
404 // Set the login shell
405 result_buf->pw_shell = buffer;
406 if ( get_shell(result_buf->pw_name,&buffer,&buflen) ) break;
408 #ifdef LIMIT_USERNAME_CHARS
409 if ( strlen(result_buf->pw_name) > LIMIT_USERNAME_CHARS ) {
410 result_buf->pw_name[LIMIT_USERNAME_CHARS] = '\0';
411 buflen = buflen + ( buffer - &result_buf->pw_name[LIMIT_USERNAME_CHARS+1] );
412 buffer = &result_buf->pw_name[LIMIT_USERNAME_CHARS+1];
417 return NSS_STATUS_SUCCESS;
421 return NSS_STATUS_UNAVAIL;
426 * This function is exported; glibc will invoke it in order to gather
427 * the user information (userid, homedir, shell) associated with a
430 enum nss_status _nss_afs_getpwuid_r (uid_t uid,
431 struct passwd *result_buf,
438 if (init_afs()) return NSS_STATUS_UNAVAIL;
441 temp = ptsid2name( uid, &buffer, &buflen);
442 if (temp != NSS_STATUS_SUCCESS) {
447 return fill_result_buf(uid, name, result_buf, buffer, buflen, errnop);
451 * This function is exported; glibc will invoke it in order to gather
452 * the user information (userid, homedir, shell) associated with a
455 enum nss_status _nss_afs_getpwnam_r (char *name,
456 struct passwd *result_buf,
463 if (init_afs()) return NSS_STATUS_UNAVAIL;
465 temp = ptsname2id(name,&uid);
466 if (temp != NSS_STATUS_SUCCESS) {
471 return fill_result_buf(uid, name, result_buf, buffer, buflen, errnop);