libnss-cache-0.10.2/0000755000175000017500000000000011715066572012247 5ustar jaqjaqlibnss-cache-0.10.2/libnss-cache.spec0000644000175000017500000000163611715066572015464 0ustar jaqjaqSummary: nss wrapper library that uses /etc/{passwd,shadow,group}.cache filenames Name: libnss-cache Version: 0.1 Release: 1 License: GPLv2 Group: System Environment/Base Packager: Oliver Hookins URL: http://code.google.com/p/nsscache/ Source: http://nsscache.googlecode.com/files/%{name}-%{version}.tar.gz BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n) BuildArchitectures: i386, x86_64 %description nsscache is a Python library and a commandline frontend to that library that synchronises a local NSS cache against a remote directory service, such as LDAP. %prep %setup -q -n %{name} %build make %install %{__rm} -rf %{buildroot} make LIBDIR="%{buildroot}%{_libdir}" install %clean %{__rm} -rf %{buildroot} %files %defattr(-, root, root, 0755) %{_libdir} %changelog * Tue Jan 06 2009 Oliver Hookins - 0.1-1 - Initial packaging libnss-cache-0.10.2/nss_cache.c0000644000175000017500000006316111715066572014350 0ustar jaqjaq/* Copyright 2009 Google Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA */ /* An NSS module which adds supports for file maps with a trailing .cache * suffix (/etc/passwd.cache, /etc/group.cache, and /etc/shadow.cache) */ #include "nss_cache.h" #include // Locking implementation: use pthreads. #include static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; #define NSS_CACHE_LOCK() do { pthread_mutex_lock(&mutex); } while (0) #define NSS_CACHE_UNLOCK() do { pthread_mutex_unlock(&mutex); } while (0) static FILE *p_file = NULL; static FILE *g_file = NULL; static FILE *s_file = NULL; static char p_filename[NSS_CACHE_PATH_LENGTH] = "/etc/passwd.cache"; static char g_filename[NSS_CACHE_PATH_LENGTH] = "/etc/group.cache"; static char s_filename[NSS_CACHE_PATH_LENGTH] = "/etc/shadow.cache"; /* Common return code routine for all *ent_r_locked functions. * We need to return TRYAGAIN if the underlying files guy raises ERANGE, * so that our caller knows to try again with a bigger buffer. */ static inline enum nss_status _nss_cache_ent_bad_return_code(int errnoval) { enum nss_status ret; switch (errnoval) { case ERANGE: DEBUG("ERANGE: Try again with a bigger buffer\n"); ret = NSS_STATUS_TRYAGAIN; break; case ENOENT: default: DEBUG("ENOENT or default case: Not found\n"); ret = NSS_STATUS_NOTFOUND; }; return ret; } // // Binary search routines below here // int _nss_cache_bsearch2_compare(const void *key, const void *value) { struct nss_cache_args *args = (struct nss_cache_args*)key; const char *value_text = (const char*)value; // Using strcmp as the generation of the index sorts without // locale awareness. return strcmp(args->lookup_key, value_text); } enum nss_status _nss_cache_bsearch2(struct nss_cache_args *args, int *errnop) { enum nss_cache_match (*lookup)( FILE *, struct nss_cache_args * ) = args->lookup_function; FILE *file = NULL; FILE *system_file_stream = NULL; struct stat system_file; struct stat sorted_file; enum nss_status ret; long offset = 0; void* mapped_data = NULL; file = fopen(args->sorted_filename, "r"); if (file == NULL) { DEBUG("error opening %s\n", args->sorted_filename); return NSS_STATUS_UNAVAIL; } // if the sorted file is older than the system file, do not risk stale // data and abort // TODO(vasilios): should be a compile or runtime option if (stat(args->system_filename, &system_file) != 0) { DEBUG("failed to stat %s\n", args->system_filename); fclose(file); return NSS_STATUS_UNAVAIL; } if (fstat(fileno(file), &sorted_file) != 0) { DEBUG("failed to stat %s\n", args->sorted_filename); fclose(file); return NSS_STATUS_UNAVAIL; } if (difftime(system_file.st_mtime, sorted_file.st_mtime) > 0) { DEBUG("%s may be stale, aborting lookup\n", args->sorted_filename); fclose(file); return NSS_STATUS_UNAVAIL; } mapped_data = mmap(NULL, sorted_file.st_size, PROT_READ, MAP_PRIVATE, fileno(file), 0); if (mapped_data == MAP_FAILED) { DEBUG("mmap failed\n"); fclose(file); return NSS_STATUS_UNAVAIL; } const char* data = (const char*)mapped_data; while (*data != '\n') { ++data; } long entry_size = data - (const char*)mapped_data + 1; long entry_count = sorted_file.st_size / entry_size; void* entry = bsearch(args, mapped_data, entry_count, entry_size, &_nss_cache_bsearch2_compare); if (entry != NULL) { const char *entry_text = entry; sscanf(entry_text + strlen(entry_text) + 1, "%ld", &offset); } if (munmap(mapped_data, sorted_file.st_size) == -1) { DEBUG("munmap failed\n"); } fclose(file); if (entry == NULL) { return NSS_STATUS_NOTFOUND; } system_file_stream = fopen(args->system_filename, "r"); if (system_file_stream == NULL) { DEBUG("error opening %s\n", args->system_filename); return NSS_STATUS_UNAVAIL; } if (fseek(system_file_stream, offset, SEEK_SET) != 0) { DEBUG("fseek fail\n"); return NSS_STATUS_UNAVAIL; } switch (lookup(system_file_stream, args)) { case NSS_CACHE_EXACT: ret = NSS_STATUS_SUCCESS; break; case NSS_CACHE_ERROR: if (errno == ERANGE) { // let the caller retry *errnop = errno; ret = _nss_cache_ent_bad_return_code(*errnop); } break; default: ret = NSS_STATUS_UNAVAIL; break; } fclose(system_file_stream); return ret; } // // Routines for passwd map defined below here // // _nss_cache_setpwent_path() // Helper function for testing extern char* _nss_cache_setpwent_path(const char *path) { DEBUG("%s %s\n", "Setting p_filename to", path); return strncpy(p_filename, path, NSS_CACHE_PATH_LENGTH - 1); } // _nss_cache_pwuid_wrap() // Internal wrapper for binary searches, using uid-specific calls. static enum nss_cache_match _nss_cache_pwuid_wrap(FILE *file, struct nss_cache_args *args) { struct passwd *result = args->lookup_result; uid_t *uid = args->lookup_value; if (fgetpwent_r(file, result, args->buffer, args->buflen, &result) == 0) { if (result->pw_uid == *uid) { DEBUG("SUCCESS: found user %d:%s\n", result->pw_uid, result->pw_name); return NSS_CACHE_EXACT; } DEBUG("Failed match at uid %d\n", result->pw_uid); if (result->pw_uid > *uid) { return NSS_CACHE_HIGH; } else { return NSS_CACHE_LOW; } } return NSS_CACHE_ERROR; } // _nss_cache_pwnam_wrap() // Internal wrapper for binary searches, using username-specific calls. static enum nss_cache_match _nss_cache_pwnam_wrap(FILE *file, struct nss_cache_args *args) { struct passwd *result = args->lookup_result; char *name = args->lookup_value; int ret; if (fgetpwent_r(file, result, args->buffer, args->buflen, &result) == 0) { ret = strcoll(result->pw_name, name); if (ret == 0) { DEBUG("SUCCESS: found user %s\n", result->pw_name); return NSS_CACHE_EXACT; } DEBUG("Failed match at name %s\n", result->pw_name); if (ret > 0) { return NSS_CACHE_HIGH; } else { return NSS_CACHE_LOW; } } return NSS_CACHE_ERROR; } // _nss_cache_setpwent_locked() // Internal setup routine static enum nss_status _nss_cache_setpwent_locked(void) { DEBUG("%s %s\n", "Opening", p_filename); p_file = fopen(p_filename, "r"); if (p_file) { return NSS_STATUS_SUCCESS; } else { return NSS_STATUS_UNAVAIL; } } // _nss_cache_setpwent() // Called by NSS to open the passwd file // 'stayopen' parameter is ignored. enum nss_status _nss_cache_setpwent(int stayopen) { enum nss_status ret; NSS_CACHE_LOCK(); ret = _nss_cache_setpwent_locked(); NSS_CACHE_UNLOCK(); return ret; } // _nss_cache_endpwent_locked() // Internal close routine static enum nss_status _nss_cache_endpwent_locked(void) { DEBUG("Closing passwd.cache\n"); if (p_file) { fclose(p_file); p_file = NULL; } return NSS_STATUS_SUCCESS; } // _nss_cache_endpwent() // Called by NSS to close the passwd file enum nss_status _nss_cache_endpwent(void) { enum nss_status ret; NSS_CACHE_LOCK(); ret = _nss_cache_endpwent_locked(); NSS_CACHE_UNLOCK(); return ret; } // _nss_cache_getpwent_r_locked() // Called internally to return the next entry from the passwd file static enum nss_status _nss_cache_getpwent_r_locked(struct passwd *result, char *buffer, size_t buflen, int *errnop) { enum nss_status ret = NSS_STATUS_SUCCESS; if (p_file == NULL) { DEBUG("p_file == NULL, going to setpwent\n"); ret = _nss_cache_setpwent_locked(); } if (ret == NSS_STATUS_SUCCESS) { if (fgetpwent_r(p_file, result, buffer, buflen, &result) == 0) { DEBUG("Returning user %d:%s\n", result->pw_uid, result->pw_name); } else { *errnop = errno; ret = _nss_cache_ent_bad_return_code(*errnop); } } return ret; } // _nss_cache_getpwent_r() // Called by NSS to look up next entry in passwd file enum nss_status _nss_cache_getpwent_r(struct passwd *result, char *buffer, size_t buflen, int *errnop) { enum nss_status ret; NSS_CACHE_LOCK(); ret = _nss_cache_getpwent_r_locked(result, buffer, buflen, errnop); NSS_CACHE_UNLOCK(); return ret; } // _nss_cache_getpwuid_r() // Find a user account by uid enum nss_status _nss_cache_getpwuid_r(uid_t uid, struct passwd *result, char *buffer, size_t buflen, int *errnop) { char filename[NSS_CACHE_PATH_LENGTH]; struct nss_cache_args args; enum nss_status ret; strncpy(filename, p_filename, NSS_CACHE_PATH_LENGTH - 1); if (strlen(filename) > NSS_CACHE_PATH_LENGTH - 7) { DEBUG("filename too long\n"); return NSS_STATUS_UNAVAIL; } strncat(filename, ".ixuid", 6); args.sorted_filename = filename; args.system_filename = p_filename; args.lookup_function = _nss_cache_pwuid_wrap; args.lookup_value = &uid; args.lookup_result = result; args.buffer = buffer; args.buflen = buflen; char uid_text[11]; snprintf(uid_text, sizeof(uid_text), "%d", uid); args.lookup_key = uid_text; args.lookup_key_length = strlen(uid_text); DEBUG("Binary search for uid %d\n", uid); NSS_CACHE_LOCK(); ret = _nss_cache_bsearch2(&args, errnop); if (ret == NSS_STATUS_UNAVAIL) { DEBUG("Binary search failed, falling back to full linear search\n"); ret = _nss_cache_setpwent_locked(); if (ret == NSS_STATUS_SUCCESS) { while ((ret = _nss_cache_getpwent_r_locked(result, buffer, buflen, errnop)) == NSS_STATUS_SUCCESS) { if (result->pw_uid == uid) break; } } } _nss_cache_endpwent_locked(); NSS_CACHE_UNLOCK(); return ret; } // _nss_cache_getpwnam_r() // Find a user account by name enum nss_status _nss_cache_getpwnam_r(const char *name, struct passwd *result, char *buffer, size_t buflen, int *errnop) { char *pw_name; char filename[NSS_CACHE_PATH_LENGTH]; struct nss_cache_args args; enum nss_status ret; NSS_CACHE_LOCK(); // name is a const char, we need a non-const copy pw_name = malloc(strlen(name) + 1); if (pw_name == NULL) { DEBUG("malloc error\n"); return NSS_STATUS_UNAVAIL; } strncpy(pw_name, name, strlen(name) + 1); strncpy(filename, p_filename, NSS_CACHE_PATH_LENGTH - 1); if (strlen(filename) > NSS_CACHE_PATH_LENGTH - 8) { DEBUG("filename too long\n"); free(pw_name); return NSS_STATUS_UNAVAIL; } strncat(filename, ".ixname", 7); args.sorted_filename = filename; args.system_filename = p_filename; args.lookup_function = _nss_cache_pwnam_wrap; args.lookup_value = pw_name; args.lookup_result = result; args.buffer = buffer; args.buflen = buflen; args.lookup_key = pw_name; args.lookup_key_length = strlen(pw_name); DEBUG("Binary search for user %s\n", pw_name); ret = _nss_cache_bsearch2(&args, errnop); if (ret == NSS_STATUS_UNAVAIL) { DEBUG("Binary search failed, falling back to full linear search\n"); ret = _nss_cache_setpwent_locked(); if (ret == NSS_STATUS_SUCCESS) { while ((ret = _nss_cache_getpwent_r_locked(result, buffer, buflen, errnop)) == NSS_STATUS_SUCCESS) { if (!strcmp(result->pw_name, name)) break; } } } free(pw_name); _nss_cache_endpwent_locked(); NSS_CACHE_UNLOCK(); return ret; } // // Routines for group map defined here. // // _nss_cache_setgrent_path() // Helper function for testing extern char* _nss_cache_setgrent_path(const char *path) { DEBUG("%s %s\n", "Setting g_filename to", path); return strncpy(g_filename, path, NSS_CACHE_PATH_LENGTH - 1); } // _nss_cache_setgrent_locked() // Internal setup routine static enum nss_status _nss_cache_setgrent_locked(void) { DEBUG("%s %s\n", "Opening", g_filename); g_file = fopen(g_filename, "r"); if (g_file) { return NSS_STATUS_SUCCESS; } else { return NSS_STATUS_UNAVAIL; } } // _nss_cache_grgid_wrap() // Internal wrapper for binary searches, using gid-specific calls. static enum nss_cache_match _nss_cache_grgid_wrap(FILE *file, struct nss_cache_args *args) { struct group *result = args->lookup_result; gid_t *gid = args->lookup_value; if (fgetgrent_r(file, result, args->buffer, args->buflen, &result) == 0) { if (result->gr_gid == *gid) { DEBUG("SUCCESS: found group %d:%s\n", result->gr_gid, result->gr_name); return NSS_CACHE_EXACT; } DEBUG("Failed match at gid %d\n", result->gr_gid); if (result->gr_gid > *gid) { return NSS_CACHE_HIGH; } else { return NSS_CACHE_LOW; } } return NSS_CACHE_ERROR; } // _nss_cache_grnam_wrap() // Internal wrapper for binary searches, using groupname-specific calls. static enum nss_cache_match _nss_cache_grnam_wrap(FILE *file, struct nss_cache_args *args) { struct group *result = args->lookup_result; char *name = args->lookup_value; int ret; if (fgetgrent_r(file, result, args->buffer, args->buflen, &result) == 0) { ret = strcoll(result->gr_name, name); if (ret == 0) { DEBUG("SUCCESS: found group %s\n", result->gr_name); return NSS_CACHE_EXACT; } DEBUG("Failed match at name %s\n", result->gr_name); if (ret > 0) { return NSS_CACHE_HIGH; } else { return NSS_CACHE_LOW; } } return NSS_CACHE_ERROR; } // _nss_cache_setgrent() // Called by NSS to open the group file // 'stayopen' parameter is ignored. enum nss_status _nss_cache_setgrent(int stayopen) { enum nss_status ret; NSS_CACHE_LOCK(); ret = _nss_cache_setgrent_locked(); NSS_CACHE_UNLOCK(); return ret; } // _nss_cache_endgrent_locked() // Internal close routine static enum nss_status _nss_cache_endgrent_locked(void) { DEBUG("Closing group.cache\n"); if (g_file) { fclose(g_file); g_file = NULL; } return NSS_STATUS_SUCCESS; } // _nss_cache_endgrent() // Called by NSS to close the group file enum nss_status _nss_cache_endgrent(void) { enum nss_status ret; NSS_CACHE_LOCK(); ret = _nss_cache_endgrent_locked(); NSS_CACHE_UNLOCK(); return ret; } // _nss_cache_getgrent_r_locked() // Called internally to return the next entry from the group file static enum nss_status _nss_cache_getgrent_r_locked(struct group *result, char *buffer, size_t buflen, int *errnop) { enum nss_status ret = NSS_STATUS_SUCCESS; if (g_file == NULL) { DEBUG("g_file == NULL, going to setgrent\n"); ret = _nss_cache_setgrent_locked(); } if (ret == NSS_STATUS_SUCCESS) { fpos_t position; fgetpos(g_file, &position); if (fgetgrent_r(g_file, result, buffer, buflen, &result) == 0) { DEBUG("Returning group %s (%d)\n", result->gr_name, result->gr_gid); } else { /* Rewind back to where we were just before, otherwise the data read * into the buffer is probably going to be lost because there's no * guarantee that the caller is going to have preserved the line we * just read. Note that glibc's nss/nss_files/files-XXX.c does * something similar in CONCAT(_nss_files_get,ENTNAME_r) (around * line 242 in glibc 2.4 sources). */ fsetpos(g_file, &position); *errnop = errno; ret = _nss_cache_ent_bad_return_code(*errnop); } } return ret; } // _nss_cache_getgrent_r() // Called by NSS to look up next entry in group file enum nss_status _nss_cache_getgrent_r(struct group *result, char *buffer, size_t buflen, int *errnop) { enum nss_status ret; NSS_CACHE_LOCK(); ret = _nss_cache_getgrent_r_locked(result, buffer, buflen, errnop); NSS_CACHE_UNLOCK(); return ret; } // _nss_cache_getgrgid_r() // Find a group by gid enum nss_status _nss_cache_getgrgid_r(gid_t gid, struct group *result, char *buffer, size_t buflen, int *errnop) { char filename[NSS_CACHE_PATH_LENGTH]; struct nss_cache_args args; enum nss_status ret; // Since we binary search over the groups using the user provided // buffer, we do not start searching before we have a buffer that // is big enough to have a high chance of succeeding. if (buflen < (1 << 20)) { *errnop = ERANGE; return NSS_STATUS_TRYAGAIN; } strncpy(filename, g_filename, NSS_CACHE_PATH_LENGTH - 1); if (strlen(filename) > NSS_CACHE_PATH_LENGTH - 7) { DEBUG("filename too long\n"); return NSS_STATUS_UNAVAIL; } strncat(filename, ".ixgid", 6); args.sorted_filename = filename; args.system_filename = g_filename; args.lookup_function = _nss_cache_grgid_wrap; args.lookup_value = &gid; args.lookup_result = result; args.buffer = buffer; args.buflen = buflen; char gid_text[11]; snprintf(gid_text, sizeof(gid_text), "%d", gid); args.lookup_key = gid_text; args.lookup_key_length = strlen(gid_text); DEBUG("Binary search for gid %d\n", gid); NSS_CACHE_LOCK(); ret = _nss_cache_bsearch2(&args, errnop); if (ret == NSS_STATUS_UNAVAIL) { DEBUG("Binary search failed, falling back to full linear search\n"); ret = _nss_cache_setgrent_locked(); if (ret == NSS_STATUS_SUCCESS) { while ((ret = _nss_cache_getgrent_r_locked(result, buffer, buflen, errnop)) == NSS_STATUS_SUCCESS) { if (result->gr_gid == gid) break; } } } _nss_cache_endgrent_locked(); NSS_CACHE_UNLOCK(); return ret; } // _nss_cache_getgrnam_r() // Find a group by name enum nss_status _nss_cache_getgrnam_r(const char *name, struct group *result, char *buffer, size_t buflen, int *errnop) { char *gr_name; char filename[NSS_CACHE_PATH_LENGTH]; struct nss_cache_args args; enum nss_status ret; NSS_CACHE_LOCK(); // name is a const char, we need a non-const copy gr_name = malloc(strlen(name) + 1); if (gr_name == NULL) { DEBUG("malloc error\n"); return NSS_STATUS_UNAVAIL; } strncpy(gr_name, name, strlen(name) + 1); strncpy(filename, g_filename, NSS_CACHE_PATH_LENGTH - 1); if (strlen(filename) > NSS_CACHE_PATH_LENGTH - 8) { DEBUG("filename too long\n"); free(gr_name); return NSS_STATUS_UNAVAIL; } strncat(filename, ".ixname", 7); args.sorted_filename = filename; args.system_filename = g_filename; args.lookup_function = _nss_cache_grnam_wrap; args.lookup_value = gr_name; args.lookup_result = result; args.buffer = buffer; args.buflen = buflen; args.lookup_key = gr_name; args.lookup_key_length = strlen(gr_name); DEBUG("Binary search for group %s\n", gr_name); ret = _nss_cache_bsearch2(&args, errnop); if (ret == NSS_STATUS_UNAVAIL) { DEBUG("Binary search failed, falling back to full linear search\n"); ret = _nss_cache_setgrent_locked(); if (ret == NSS_STATUS_SUCCESS) { while ((ret = _nss_cache_getgrent_r_locked(result, buffer, buflen, errnop)) == NSS_STATUS_SUCCESS) { if (!strcmp(result->gr_name, name)) break; } } } free(gr_name); _nss_cache_endgrent_locked(); NSS_CACHE_UNLOCK(); return ret; } // // Routines for shadow map defined here. // // _nss_cache_setspent_path() // Helper function for testing extern char* _nss_cache_setspent_path(const char *path) { DEBUG("%s %s\n", "Setting s_filename to", path); return strncpy(s_filename, path, NSS_CACHE_PATH_LENGTH - 1); } // _nss_cache_setspent_locked() // Internal setup routine static enum nss_status _nss_cache_setspent_locked(void) { DEBUG("%s %s\n", "Opening", g_filename); s_file = fopen(s_filename, "r"); if (s_file) { return NSS_STATUS_SUCCESS; } else { return NSS_STATUS_UNAVAIL; } } // _nss_cache_spnam_wrap() // Internal wrapper for binary searches, using shadow-specific calls. static enum nss_cache_match _nss_cache_spnam_wrap(FILE *file, struct nss_cache_args *args) { struct spwd *result = args->lookup_result; char *name = args->lookup_value; int ret; if (fgetspent_r(file, result, args->buffer, args->buflen, &result) == 0) { ret = strcoll(result->sp_namp, name); if (ret == 0) { DEBUG("SUCCESS: found user %s\n", result->sp_namp); return NSS_CACHE_EXACT; } DEBUG("Failed match at name %s\n", result->sp_namp); if (ret > 0) { return NSS_CACHE_HIGH; } else { return NSS_CACHE_LOW; } } return NSS_CACHE_ERROR; } // _nss_cache_setspent() // Called by NSS to open the shadow file // 'stayopen' parameter is ignored. enum nss_status _nss_cache_setspent(int stayopen) { enum nss_status ret; NSS_CACHE_LOCK(); ret = _nss_cache_setspent_locked(); NSS_CACHE_UNLOCK(); return ret; } // _nss_cache_endspent_locked() // Internal close routine static enum nss_status _nss_cache_endspent_locked(void) { DEBUG("Closing shadow.cache\n"); if (s_file) { fclose(s_file); s_file = NULL; } return NSS_STATUS_SUCCESS; } // _nss_cache_endspent() // Called by NSS to close the shadow file enum nss_status _nss_cache_endspent(void) { enum nss_status ret; NSS_CACHE_LOCK(); ret = _nss_cache_endspent_locked(); NSS_CACHE_UNLOCK(); return ret; } // _nss_cache_getspent_r_locked() // Called internally to return the next entry from the shadow file static enum nss_status _nss_cache_getspent_r_locked(struct spwd *result, char *buffer, size_t buflen, int *errnop) { enum nss_status ret = NSS_STATUS_SUCCESS; if (s_file == NULL) { DEBUG("s_file == NULL, going to setspent\n"); ret = _nss_cache_setspent_locked(); } if (ret == NSS_STATUS_SUCCESS) { if (fgetspent_r(s_file, result, buffer, buflen, &result) == 0) { DEBUG("Returning shadow entry %s\n", result->sp_namp); } else { *errnop = errno; ret = _nss_cache_ent_bad_return_code(*errnop); } } return ret; } // _nss_cache_getspent_r() // Called by NSS to look up next entry in the shadow file enum nss_status _nss_cache_getspent_r(struct spwd *result, char *buffer, size_t buflen, int *errnop) { enum nss_status ret; NSS_CACHE_LOCK(); ret = _nss_cache_getspent_r_locked(result, buffer, buflen, errnop); NSS_CACHE_UNLOCK(); return ret; } // _nss_cache_getspnam_r() // Find a user by name enum nss_status _nss_cache_getspnam_r(const char *name, struct spwd *result, char *buffer, size_t buflen, int *errnop) { char *sp_namp; char filename[NSS_CACHE_PATH_LENGTH]; struct nss_cache_args args; enum nss_status ret; NSS_CACHE_LOCK(); // name is a const char, we need a non-const copy sp_namp = malloc(strlen(name) + 1); if (sp_namp == NULL) { DEBUG("malloc error\n"); return NSS_STATUS_UNAVAIL; } strncpy(sp_namp, name, strlen(name) + 1); strncpy(filename, s_filename, NSS_CACHE_PATH_LENGTH - 1); if (strlen(filename) > NSS_CACHE_PATH_LENGTH - 8) { DEBUG("filename too long\n"); free(sp_namp); return NSS_STATUS_UNAVAIL; } strncat(filename, ".ixname", 7); args.sorted_filename = filename; args.system_filename = s_filename; args.lookup_function = _nss_cache_spnam_wrap; args.lookup_value = sp_namp; args.lookup_result = result; args.buffer = buffer; args.buflen = buflen; args.lookup_key = sp_namp; args.lookup_key_length = strlen(sp_namp); DEBUG("Binary search for user %s\n", sp_namp); ret = _nss_cache_bsearch2(&args, errnop); if (ret == NSS_STATUS_UNAVAIL) { DEBUG("Binary search failed, falling back to full linear search\n"); ret = _nss_cache_setspent_locked(); if (ret == NSS_STATUS_SUCCESS) { while ((ret = _nss_cache_getspent_r_locked(result, buffer, buflen, errnop)) == NSS_STATUS_SUCCESS) { if (!strcmp(result->sp_namp, name)) break; } } } free(sp_namp); _nss_cache_endspent_locked(); NSS_CACHE_UNLOCK(); return ret; } libnss-cache-0.10.2/version0000644000175000017500000000000711715066572013654 0ustar jaqjaq0.10.2 libnss-cache-0.10.2/lookup.c0000644000175000017500000002437111715066572013733 0ustar jaqjaq/* Copyright 2009 Google Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA */ /* * Perform get*id() and get*nam() lookups given simple lists of * ids or names. Used to test correctness and timings of functions in * nss_cache.c * * There's a fair amount of repeated functions, and I thought briefly about * trying to consolidate them with callbacks or put an if clause inside the * while look (making one longer one for lookup_foo() functions). * * But, each function is slightly different: string or uid? struct group * or struct passwd or struct spwd? And I decided stupid repetition was just * easier and very direct to read. ymmv. * */ #include "nss_cache.h" #include "nss_test.h" // usage() // // print out flags and arguments static void usage(char *program) { fprintf(stderr, "Usage: %s -c nss_function -f filename\n", program); } // getpwnam_wrapper() // // perform a getpwnam() lookup via nss_cache.c directly static int getpwnam_wrapper(char *name) { struct passwd result; char *buffer = NULL; size_t buflen = 1024; int found = 255; int errnop; int ret; _nss_cache_setpwent_path(PASSWD_FILE); buffer = malloc(buflen); do { ret = _nss_cache_getpwnam_r(name, &result, buffer, buflen, &errnop); if (ret == NSS_STATUS_SUCCESS) { //printf("found %s, %d\n", result.pw_name, result.pw_uid); found = 0; } if (ret == NSS_STATUS_NOTFOUND) { found = 1; } if (ret == NSS_STATUS_TRYAGAIN) { buflen = buflen * 2; buffer = realloc(buffer, buflen); } if (ret == NSS_STATUS_UNAVAIL) { fprintf(stderr, "ERROR: failed to access passwd test data\n"); return 2; } } while (ret == NSS_STATUS_TRYAGAIN); free(buffer); return found; } // getpwuid_wrapper() // // perform a getpwui() lookup via nss_cache.c directly static int getpwuid_wrapper(uid_t uid) { struct passwd result; char *buffer = NULL; size_t buflen = 1024; int found = 255; int errnop; int ret; _nss_cache_setpwent_path(PASSWD_FILE); buffer = malloc(buflen); do { ret = _nss_cache_getpwuid_r(uid, &result, buffer, buflen, &errnop); if (ret == NSS_STATUS_SUCCESS) { //printf("found %s, %d\n", result.pw_name, result.pw_uid); found = 0; } if (ret == NSS_STATUS_NOTFOUND) { found = 1; } if (ret == NSS_STATUS_TRYAGAIN) { buflen = buflen * 2; buffer = realloc(buffer, buflen); } if (ret == NSS_STATUS_UNAVAIL) { fprintf(stderr, "ERROR: failed to access passwd test data\n"); return 2; } } while (ret == NSS_STATUS_TRYAGAIN); free(buffer); return found; } // getgrnam_wrapper() // // perform a getgrnam() lookup via nss_cache.c directly static int getgrnam_wrapper(char *name) { struct group result; char *buffer = NULL; size_t buflen = 1024; int found = 255; int errnop; int ret; _nss_cache_setgrent_path(GROUP_FILE); buffer = malloc(buflen); do { ret = _nss_cache_getgrnam_r(name, &result, buffer, buflen, &errnop); if (ret == NSS_STATUS_SUCCESS) { //printf("found %s, %d\n", result.gr_name, result.gr_gid); found = 0; } if (ret == NSS_STATUS_NOTFOUND) { found = 1; } if (ret == NSS_STATUS_TRYAGAIN) { buflen = buflen * 2; buffer = realloc(buffer, buflen); } if (ret == NSS_STATUS_UNAVAIL) { fprintf(stderr, "ERROR: failed to access passwd test data\n"); return 2; } } while (ret == NSS_STATUS_TRYAGAIN); free(buffer); return found; } // getgrgid_wrapper() // // perform a getgrgid() lookup via nss_cache.c directly static int getgrgid_wrapper(gid_t gid) { struct group result; char *buffer = NULL; size_t buflen = 1024; int found = 255; int errnop; int ret; _nss_cache_setgrent_path(GROUP_FILE); buffer = malloc(buflen); do { ret = _nss_cache_getgrgid_r(gid, &result, buffer, buflen, &errnop); if (ret == NSS_STATUS_SUCCESS) { //printf("found %s, %d\n", result.gr_name, result.gr_gid); found = 0; } if (ret == NSS_STATUS_NOTFOUND) { found = 1; } if (ret == NSS_STATUS_TRYAGAIN) { buflen = buflen * 2; buffer = realloc(buffer, buflen); } if (ret == NSS_STATUS_UNAVAIL) { fprintf(stderr, "ERROR: failed to access passwd test data\n"); return 2; } } while (ret == NSS_STATUS_TRYAGAIN); free(buffer); return found; } // getspnam_wrapper() // // perform a getspnam() lookup via nss_cache.c directly static int getspnam_wrapper(char *name) { struct spwd result; char *buffer = NULL; size_t buflen = 1024; int found = 255; int errnop; int ret; _nss_cache_setspent_path(SHADOW_FILE); buffer = malloc(buflen); do { ret = _nss_cache_getspnam_r(name, &result, buffer, buflen, &errnop); if (ret == NSS_STATUS_SUCCESS) { //printf("found %s, %s\n", result.sp_namp, result.sp_pwdp); found = 0; } if (ret == NSS_STATUS_NOTFOUND) { found = 1; } if (ret == NSS_STATUS_TRYAGAIN) { buflen = buflen * 2; buffer = realloc(buffer, buflen); } if (ret == NSS_STATUS_UNAVAIL) { fprintf(stderr, "ERROR: failed to access passwd test data\n"); return 2; } } while (ret == NSS_STATUS_TRYAGAIN); free(buffer); return found; } // lookup_getpwnam() // // call getpwnam() from nss_cache.c on each line of a file static int lookup_getpwnam(FILE *input) { int lines = 0; char line[255]; int ret; while (fgets(line, sizeof(line), input)) { lines += 1; // strip trailing newline if (line[strlen(line) - 1] == '\n') { line[strlen(line) - 1] = '\0'; } ret = getpwnam_wrapper(line); if (ret != 0) { break; } } if (ret == 0) { printf("successfully completed %d lookups\n", lines); } else { fprintf(stderr, "failed at line %d: %s\n", lines, line); } return ret; } // lookup_getpwuid() // // call getpwuid() from nss_cache.c on each line of a file static int lookup_getpwuid(FILE *input) { int lines = 0; char line[255]; int ret; while (fgets(line, sizeof(line), input)) { lines += 1; // strip trailing newline if (line[strlen(line) - 1] == '\n') { line[strlen(line) - 1] = '\0'; } ret = getpwuid_wrapper(atoi(line)); if (ret != 0) { break; } } if (ret == 0) { printf("successfully completed %d lookups\n", lines); } else { fprintf(stderr, "failed at line %d: %s\n", lines, line); } return ret; } // lookup_getgrnam() // // call getgrnam() from nss_cache.c on each line of a file static int lookup_getgrnam(FILE *input) { int lines = 0; char line[255]; int ret; while (fgets(line, sizeof(line), input)) { lines += 1; // strip trailing newline if (line[strlen(line) - 1] == '\n') { line[strlen(line) - 1] = '\0'; } ret = getgrnam_wrapper(line); if (ret != 0) { break; } } if (ret == 0) { printf("successfully completed %d lookups\n", lines); } else { fprintf(stderr, "failed at line %d: %s\n", lines, line); } return ret; } // lookup_getgrgid() // // call getgrgid() from nss_cache.c on each line of a file static int lookup_getgrgid(FILE *input) { int lines = 0; char line[255]; int ret; while (fgets(line, sizeof(line), input)) { lines += 1; // strip trailing newline if (line[strlen(line) - 1] == '\n') { line[strlen(line) - 1] = '\0'; } ret = getgrgid_wrapper(atoi(line)); if (ret != 0) { break; } } if (ret == 0) { printf("successfully completed %d lookups\n", lines); } else { fprintf(stderr, "failed at line %d: %s\n", lines, line); } return ret; } // lookup_getspnam() // // call getspnam() from nss_cache.c on each line of a file static int lookup_getspnam(FILE *input) { int lines = 0; char line[255]; int ret; while (fgets(line, sizeof(line), input)) { lines += 1; // strip trailing newline if (line[strlen(line) - 1] == '\n') { line[strlen(line) - 1] = '\0'; } ret = getspnam_wrapper(line); if (ret != 0) { break; } } if (ret == 0) { printf("successfully completed %d lookups\n", lines); } else { fprintf(stderr, "failed at line %d: %s\n", lines, line); } return ret; } // nss_lookup() // // call the nss lookup wrapper that was set by getopt() static int nss_lookup(char *call, FILE *input) { int ret; if (strncmp(call, "getpwnam", 8) == 0) { ret = lookup_getpwnam(input); } else if (strncmp(call, "getpwuid", 8) == 0) { ret = lookup_getpwuid(input); } else if (strncmp(call, "getgrnam", 8) == 0) { ret = lookup_getgrnam(input); } else if (strncmp(call, "getgrgid", 8) == 0) { ret = lookup_getgrgid(input); } else if (strncmp(call, "getspnam", 8) == 0) { ret = lookup_getspnam(input); } else { fprintf(stderr, "unknown nss function: %s\n", call); ret = 1; } return ret; } // main() // // parse commandline options to get the function name and input file. // send it through nss_lookup() which will ultimately call the // function on each line in the file and track success. int main(int argc, char **argv) { char *call = NULL; char *filename = NULL; FILE *input = NULL; int ret; int c; while ((c = getopt(argc, argv, "c:f:")) != -1) { switch (c) { case 'c': call = optarg; break; case 'f': filename = optarg; break; } } if (call == NULL || filename == NULL) { usage(argv[0]); return 1; } input = fopen(filename, "r"); if (input == NULL) { fprintf(stderr, "failed to open %s\n", filename); return 255; } ret = nss_lookup(call, input); fclose(input); return ret; } libnss-cache-0.10.2/nss_cache.h0000644000175000017500000000344211715066572014351 0ustar jaqjaq/* Copyright 2009 Google Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA */ #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef NSS_CACHE_H #define NSS_CACHE_H #ifdef DEBUG #undef DEBUG #define DEBUG(fmt, args...) do { fprintf(stderr, fmt, ##args); } while (0) #else #define DEBUG(fmt, ...) do { } while (0) #endif /* DEBUG */ #define NSS_CACHE_PATH_LENGTH 255 extern char* _nss_cache_setpwent_path(const char *path); extern char* _nss_cache_setgrent_path(const char *path); extern char* _nss_cache_setspent_path(const char *path); enum nss_cache_match { NSS_CACHE_EXACT = 0, NSS_CACHE_HIGH = 1, NSS_CACHE_LOW = 2, NSS_CACHE_ERROR = 3, }; struct nss_cache_args { char *system_filename; char *sorted_filename; void *lookup_function; void *lookup_value; void *lookup_result; char *buffer; size_t buflen; char *lookup_key; size_t lookup_key_length; }; #endif /* NSS_CACHE_H */ libnss-cache-0.10.2/gen_getent.c0000644000175000017500000001601411715066572014534 0ustar jaqjaq/* Copyright 2009 Google Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA */ /* * A helper program to let us do simplistic unit testing of * libnss-cache functions. It does this by generating copies of the * maps in a data directory, using nss_cache.c get*ent() functions. */ #include "nss_cache.h" #include "nss_test.h" // getpwent_to_file() // Call the nss_cache getpwent function to dump the passwd store to a // file. static int getpwent_to_file(FILE *output) { struct passwd result; char *buffer; size_t buflen = 1024; int errnop; enum nss_status ret; _nss_cache_setpwent_path(PASSWD_FILE); buffer = malloc(buflen); do { ret = _nss_cache_getpwent_r(&result, buffer, buflen, &errnop); if (ret == NSS_STATUS_SUCCESS) { fprintf(output, "%s:%s:%d:%d:%s:%s:%s\n", result.pw_name, result.pw_passwd, result.pw_uid, result.pw_gid, result.pw_gecos, result.pw_dir, result.pw_shell ); } if (ret == NSS_STATUS_TRYAGAIN) { buflen = buflen * 2; buffer = realloc(buffer, buflen); } if (ret == NSS_STATUS_UNAVAIL) { fprintf(stderr, "ERROR: failed to access passwd test data\n"); return 1; } } while (ret == NSS_STATUS_SUCCESS || ret == NSS_STATUS_TRYAGAIN); free(buffer); return 0; } // getgrent_to_file() // Call the nss_cache getgrent function to dump the group store to a // file. static int getgrent_to_file(FILE *output) { struct group result; char *buffer; size_t buflen = 1024; int errnop; enum nss_status ret; char *member; int idx; _nss_cache_setgrent_path(GROUP_FILE); buffer = malloc(buflen); do { ret = _nss_cache_getgrent_r(&result, buffer, buflen, &errnop); if (ret == NSS_STATUS_SUCCESS) { fprintf(output, "%s:%s:%d:", result.gr_name, result.gr_passwd, result.gr_gid ); // unroll **gr_mem for (idx = 0; result.gr_mem[idx] != NULL; idx++) { fprintf(output, "%s", result.gr_mem[idx]); if (result.gr_mem[idx+1] != NULL) { fprintf(output, ","); } } fprintf(output, "\n"); } if (ret == NSS_STATUS_TRYAGAIN) { buflen = buflen * 2; buffer = realloc(buffer, buflen); } if (ret == NSS_STATUS_UNAVAIL) { fprintf(stderr, "ERROR: failed to access group test data\n"); return 1; } } while (ret == NSS_STATUS_SUCCESS || ret == NSS_STATUS_TRYAGAIN); free(buffer); return 0; } // getspent_to_file() // Call the nss_cache getspent function to dump the shadow store to a // file. static int getspent_to_file(FILE *output) { struct spwd result; char *buffer; size_t buflen = 1024; int errnop; enum nss_status ret; _nss_cache_setpwent_path(SHADOW_FILE); buffer = malloc(buflen); do { ret = _nss_cache_getspent_r(&result, buffer, buflen, &errnop); if (ret == NSS_STATUS_SUCCESS) { fprintf(output, "%s:%s:", result.sp_namp, result.sp_pwdp ); // sigh, empty numberical fields are -1 in the struct, // so if necessary convert back to empty fields if (result.sp_lstchg != -1) { fprintf(output, "%ld:", result.sp_lstchg); } else { fprintf(output, ":"); } if (result.sp_min != -1) { fprintf(output, "%ld:", result.sp_min); } else { fprintf(output, ":"); } if (result.sp_max != -1) { fprintf(output, "%ld:", result.sp_max); } else { fprintf(output, ":"); } if (result.sp_warn != -1) { fprintf(output, "%ld:", result.sp_warn); } else { fprintf(output, ":"); } if (result.sp_inact != -1) { fprintf(output, "%ld:", result.sp_inact); } else { fprintf(output, ":"); } if (result.sp_expire != -1) { fprintf(output, "%ld:", result.sp_expire); } else { fprintf(output, ":"); } if (result.sp_flag != -1) { fprintf(output, "%ld", result.sp_flag); } fprintf(output, "\n"); } if (ret == NSS_STATUS_TRYAGAIN) { buflen = buflen * 2; buffer = realloc(buffer, buflen); } if (ret == NSS_STATUS_UNAVAIL) { perror("ERROR: failed to access shadow test data"); return 1; } } while (ret == NSS_STATUS_SUCCESS || ret == NSS_STATUS_TRYAGAIN); free(buffer); return 0; } // gen_getpwent_data() // // creates a copy of the passwd map as read by nss_cache.c static int gen_getpwent_data() { char filename[NSS_CACHE_PATH_LENGTH]; FILE *output; int ret; strncpy(filename, PASSWD_FILE, NSS_CACHE_PATH_LENGTH - 4); strncat(filename, ".out", 4); output = fopen(filename, "w"); if (output == NULL) { fprintf(stderr, "failed to open %s!\n", filename); return 255; } ret = getpwent_to_file(output); fclose(output); return ret; } // gen_getgrent_data() // // creates a copy of the group map as read by nss_cache.c static int gen_getgrent_data() { char filename[NSS_CACHE_PATH_LENGTH]; FILE *output; int ret; strncpy(filename, GROUP_FILE, NSS_CACHE_PATH_LENGTH - 4); strncat(filename, ".out", 4); output = fopen(filename, "w"); if (output == NULL) { fprintf(stderr, "failed to open %s!\n", filename); return 255; } ret = getgrent_to_file(output); fclose(output); return ret; } // gen_getspent_data() // // creates a copy of the shadow map as read by nss_cache.c static int gen_getspent_data() { char filename[NSS_CACHE_PATH_LENGTH]; FILE *output; int ret; strncpy(filename, SHADOW_FILE, NSS_CACHE_PATH_LENGTH - 4); strncat(filename, ".out", 4); output = fopen(filename, "w"); if (output == NULL) { fprintf(stderr, "failed to open %s!\n", filename); return 255; } ret = getspent_to_file(output); fclose(output); return ret; } // main() // // generate the passwd, group and shadow files using nss_cache.c functions // which are further checked for correctness by 'make test'. int main(void) { int ret; int failed_tests = 0; ret = gen_getpwent_data(); if (ret != 0) { fprintf(stderr, "Failed to generate password file.\n"); failed_tests = failed_tests + 1; } ret = gen_getgrent_data(); if (ret != 0) { fprintf(stderr, "Failed to generate password file.\n"); failed_tests = failed_tests + 1; } ret = gen_getspent_data(); if (ret != 0) { fprintf(stderr, "Failed to generate password file.\n"); failed_tests = failed_tests + 1; } printf("generated all files.\n"); return failed_tests; } libnss-cache-0.10.2/COPYING0000644000175000017500000001672711715066572013317 0ustar jaqjaq GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library. libnss-cache-0.10.2/Makefile0000644000175000017500000001207511715066572013714 0ustar jaqjaqCC=gcc #CFLAGS=-Wall -Wstrict-prototypes -Werror -fPIC -DDEBUG -g -O0 CFLAGS=-Wall -Wstrict-prototypes -Werror -fPIC LIBRARY=libnss_cache.so.2.0 LINKS=libnss_cache.so.2 libnss_cache.so DESTDIR=/ PREFIX=$(DESTDIR)/usr BUILD=.libs BUILD64=.libs64 BUILD32=.libs32 LIBDIR=$(PREFIX)/lib LIBDIR64=$(PREFIX)/lib64 LIBDIR32=$(PREFIX)/lib32 SONAME=libnss_cache.so.2 LD_SONAME=-Wl,-soname,$(SONAME) TESTBIN=.testbin TESTDATA=.testdata SOURCES = Makefile gen_getent.c lookup.c nss_cache.c nss_cache.h nss_test.h COPYING version libnss-cache.spec VERSION = $(shell cat version) default: build_amd64 echo N.B. Defaulted to build_amd64! test: test_getent time_lookups time_lookups: lookup_data lookup.c nss_cache.c $(CC) $(CFLAGS) -o $(TESTBIN)/lookup lookup.c nss_cache.c @echo Linear username lookups rm -f $(TESTDATA)/passwd.cache.ixname time -f %E $(TESTBIN)/lookup -c getpwnam -f $(TESTDATA)/rand_pwnames @echo Binary username lookups ../vendetta/files/gen_cache.py $(TESTDATA)/passwd.cache 1 $(TESTDATA)/passwd.cache.ixname time -f %E $(TESTBIN)/lookup -c getpwnam -f $(TESTDATA)/rand_pwnames @echo Linear UID lookups rm -f $(TESTDATA)/passwd.cache.ixuid time -f %E $(TESTBIN)/lookup -c getpwuid -f $(TESTDATA)/rand_pwuids @echo Binary UID lookups ../vendetta/files/gen_cache.py $(TESTDATA)/passwd.cache 3 $(TESTDATA)/passwd.cache.ixuid time -f %E $(TESTBIN)/lookup -c getpwuid -f $(TESTDATA)/rand_pwuids @echo Linear groupname lookups rm -f $(TESTDATA)/group.cache.ixname time -f %E $(TESTBIN)/lookup -c getgrnam -f $(TESTDATA)/rand_grnames @echo Binary groupname lookups sort -t: -k1,1 $(TESTDATA)/group.cache > $(TESTDATA)/group.cache.ixname ../vendetta/files/gen_cache.py $(TESTDATA)/group.cache 1 $(TESTDATA)/group.cache.ixname time -f %E $(TESTBIN)/lookup -c getgrnam -f $(TESTDATA)/rand_grnames @echo Linear GID lookups rm -f $(TESTDATA)/group.cache.ixgid time -f %E $(TESTBIN)/lookup -c getgrgid -f $(TESTDATA)/rand_grgids @echo Binary GID lookups ../vendetta/files/gen_cache.py $(TESTDATA)/group.cache 3 $(TESTDATA)/group.cache.ixgid time -f %E $(TESTBIN)/lookup -c getgrgid -f $(TESTDATA)/rand_grgids @echo Linear shadow lookups rm -f $(TESTDATA)/shadow.cache.ixname time -f %E $(TESTBIN)/lookup -c getspnam -f $(TESTDATA)/rand_spnames @echo Binary shadow lookups ../vendetta/files/gen_cache.py $(TESTDATA)/shadow.cache 1 $(TESTDATA)/shadow.cache.ixname time -f %E $(TESTBIN)/lookup -c getspnam -f $(TESTDATA)/rand_spnames test_getent: getent_data gen_getent.c nss_cache.c $(CC) -o $(TESTBIN)/gen_getent gen_getent.c nss_cache.c sudo ./$(TESTBIN)/gen_getent diff $(TESTDATA)/passwd.cache $(TESTDATA)/passwd.cache.out diff $(TESTDATA)/group.cache $(TESTDATA)/group.cache.out diff $(TESTDATA)/shadow.cache $(TESTDATA)/shadow.cache.out lookup_data: getent_data cut -d : -f 1 $(TESTDATA)/passwd.cache |\ sort -R | head -500 > $(TESTDATA)/rand_pwnames cut -d : -f 3 $(TESTDATA)/passwd.cache |\ sort -R | head -500 > $(TESTDATA)/rand_pwuids cut -d : -f 1 $(TESTDATA)/group.cache |\ sort -R | head -500 > $(TESTDATA)/rand_grnames cut -d : -f 3 $(TESTDATA)/group.cache |\ sort -R | head -500 > $(TESTDATA)/rand_grgids cut -d : -f 1 $(TESTDATA)/shadow.cache |\ sort -R | head -500 > $(TESTDATA)/rand_spnames getent_data: testdirs getent passwd > $(TESTDATA)/passwd.cache getent group > $(TESTDATA)/group.cache sudo getent shadow > $(TESTDATA)/shadow.cache testdirs: mkdir -p $(TESTDATA) mkdir -p $(TESTBIN) build_i386: nss_cache64 build_amd64: nss_cache32 install_i386: install64 install_amd64: install32 nss_cache: [ -d $(BUILD) ] || mkdir $(BUILD) $(CC) $(CFLAGS) -c nss_cache.c -o $(BUILD)/nss_cache.o $(CC) -shared $(LD_SONAME) -o $(BUILD)/$(LIBRARY) $(BUILD)/nss_cache.o nss_cache64: nss_cache [ -d $(BUILD64) ] || mkdir $(BUILD64) $(CC) $(CFLAGS) -m64 -c nss_cache.c -o $(BUILD64)/nss_cache.o $(CC) -m64 -shared $(LD_SONAME) -o $(BUILD64)/$(LIBRARY) $(BUILD64)/nss_cache.o nss_cache32: nss_cache [ -d $(BUILD32) ] || mkdir $(BUILD32) $(CC) $(CFLAGS) -m32 -c nss_cache.c -o $(BUILD32)/nss_cache.o $(CC) -m32 -shared $(LD_SONAME) -o $(BUILD32)/$(LIBRARY) $(BUILD32)/nss_cache.o clean: rm -rf $(BUILD) rm -rf $(BUILD32) rm -rf $(BUILD64) rm -rf $(TESTDATA) rm -rf $(TESTBIN) install: [ -d $(LIBDIR) ] || install -d $(LIBDIR) install $(BUILD)/$(LIBRARY) $(LIBDIR) cd $(LIBDIR); for link in $(LINKS); do ln -sf $(LIBRARY) $$link ; done install64: install [ -d $(LIBDIR64) ] || install -d $(LIBDIR64) install $(BUILD64)/$(LIBRARY) $(LIBDIR64) cd $(LIBDIR64); for link in $(LINKS); do ln -sf $(LIBRARY) $$link ; done install32: install [ -d $(LIBDIR32) ] || install -d $(LIBDIR32) install $(BUILD32)/$(LIBRARY) $(LIBDIR32) cd $(LIBDIR32); for link in $(LINKS); do ln -sf $(LIBRARY) $$link ; done distclean: clean rm -f *~ \#* dist: rm -rf libnss-cache-$(VERSION) libnss-cache-$(VERSION).tar libnss-cache-$(VERSION).tar.gz mkdir libnss-cache-$(VERSION) cp $(SOURCES) libnss-cache-$(VERSION) tar cf libnss-cache-$(VERSION).tar libnss-cache-$(VERSION) gzip -9 libnss-cache-$(VERSION).tar rm -rf libnss-cache-$(VERSION) .PHONY: dist clean install install64 install32 distclean libnss-cache-0.10.2/nss_test.h0000644000175000017500000000446111715066572014267 0ustar jaqjaq/* Copyright 2009 Google Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA */ #include #include #include #include #include #ifndef NSS_TEST_H #define NSS_TEST_H const char *PASSWD_FILE = ".testdata/passwd.cache"; const char *GROUP_FILE = ".testdata/group.cache"; const char *SHADOW_FILE = ".testdata/shadow.cache"; extern enum nss_status _nss_cache_getpwnam_r(const char *name, struct passwd *result, char *buffer, size_t buflen, int *errnop); extern enum nss_status _nss_cache_getpwuid_r(uid_t uid, struct passwd *result, char *buffer, size_t buflen, int *errnop); extern enum nss_status _nss_cache_getgrnam_r(const char *name, struct group *result, char *buffer, size_t buflen, int *errnop); extern enum nss_status _nss_cache_getgrgid_r(gid_t gid, struct group *result, char *buffer, size_t buflen, int *errnop); extern enum nss_status _nss_cache_getspnam_r(const char *name, struct spwd *result, char *buffer, size_t buflen, int *errnop); #endif /* NSS_TEST_H */