summaryrefslogtreecommitdiff
path: root/packages/google-compute-engine-oslogin/src/nss
diff options
context:
space:
mode:
Diffstat (limited to 'packages/google-compute-engine-oslogin/src/nss')
-rw-r--r--packages/google-compute-engine-oslogin/src/nss/compat/getpwent_r.c87
-rw-r--r--packages/google-compute-engine-oslogin/src/nss/nss_cache_oslogin.c274
-rw-r--r--packages/google-compute-engine-oslogin/src/nss/nss_oslogin.cc209
3 files changed, 0 insertions, 570 deletions
diff --git a/packages/google-compute-engine-oslogin/src/nss/compat/getpwent_r.c b/packages/google-compute-engine-oslogin/src/nss/compat/getpwent_r.c
deleted file mode 100644
index b1be6fc..0000000
--- a/packages/google-compute-engine-oslogin/src/nss/compat/getpwent_r.c
+++ /dev/null
@@ -1,87 +0,0 @@
-/*
- * ----------------------------------------------------------------------
- * Copyright © 2005-2014 Rich Felker, et al.
- *
- * Permission is hereby granted, free of charge, to any person obtaining
- * a copy of this software and associated documentation files (the
- * "Software"), to deal in the Software without restriction, including
- * without limitation the rights to use, copy, modify, merge, publish,
- * distribute, sublicense, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to
- * the following conditions:
- *
- * The above copyright notice and this permission notice shall be
- * included in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
- * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
- * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
- * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
- * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- * ----------------------------------------------------------------------
- *
- * Adapted from http://www.musl-libc.org/ for libnss-cache
- * Copyright © 2015 Kevin Bowling <k@kev009.com>
- */
-
-#include <sys/param.h>
-
-#ifdef BSD
-
-#include <pwd.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <string.h>
-
-static unsigned atou(char **s)
-{
- unsigned x;
- for (x=0; **s-'0'<10U; ++*s) x=10*x+(**s-'0');
- return x;
-}
-
-int fgetpwent_r(FILE *f, struct passwd *pw, char *line, size_t size, struct passwd **res)
-{
- char *s;
- int rv = 0;
- for (;;) {
- line[size-1] = '\xff';
- if ( (fgets(line, size, f) == NULL) || ferror(f) || line[size-1] != '\xff' ) {
- rv = (line[size-1] != '\xff') ? ERANGE : ENOENT;
- line = 0;
- pw = 0;
- break;
- }
- line[strcspn(line, "\n")] = 0;
-
- s = line;
- pw->pw_name = s++;
- if (!(s = strchr(s, ':'))) continue;
-
- *s++ = 0; pw->pw_passwd = s;
- if (!(s = strchr(s, ':'))) continue;
-
- *s++ = 0; pw->pw_uid = atou(&s);
- if (*s != ':') continue;
-
- *s++ = 0; pw->pw_gid = atou(&s);
- if (*s != ':') continue;
-
- *s++ = 0; pw->pw_gecos = s;
- if (!(s = strchr(s, ':'))) continue;
-
- *s++ = 0; pw->pw_dir = s;
- if (!(s = strchr(s, ':'))) continue;
-
- *s++ = 0; pw->pw_shell = s;
- break;
- }
- *res = pw;
- if (rv) errno = rv;
- return rv;
-}
-
-#endif // ifdef BSD
diff --git a/packages/google-compute-engine-oslogin/src/nss/nss_cache_oslogin.c b/packages/google-compute-engine-oslogin/src/nss/nss_cache_oslogin.c
deleted file mode 100644
index 55bb78d..0000000
--- a/packages/google-compute-engine-oslogin/src/nss/nss_cache_oslogin.c
+++ /dev/null
@@ -1,274 +0,0 @@
-// Copyright 2018 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-// An NSS module which adds supports for file /etc/oslogin_passwd.cache
-
-#include <nss_cache_oslogin.h>
-#include <compat.h>
-
-#include <sys/mman.h>
-
-// Locking implementation: use pthreads.
-#include <pthread.h>
-static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
-#define NSS_CACHE_OSLOGIN_LOCK() \
- do { \
- pthread_mutex_lock(&mutex); \
- } while (0)
-#define NSS_CACHE_OSLOGIN_UNLOCK() \
- do { \
- pthread_mutex_unlock(&mutex); \
- } while (0)
-
-static FILE *p_file = NULL;
-static char p_filename[NSS_CACHE_OSLOGIN_PATH_LENGTH] = NSS_CACHE_OSLOGIN_PATH;
-#ifdef BSD
-extern int fgetpwent_r(FILE *, struct passwd *, char *, size_t,
- struct passwd **);
-#endif // ifdef BSD
-
-/* 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_oslogin_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;
-}
-
-//
-// Routines for passwd map defined below here
-//
-
-// _nss_cache_oslogin_pwuid_wrap()
-// Internal wrapper for binary searches, using uid-specific calls.
-
-static enum nss_cache_oslogin_match _nss_cache_oslogin_pwuid_wrap(
- FILE *file, struct nss_cache_oslogin_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_OSLOGIN_EXACT;
- }
- DEBUG("Failed match at uid %d\n", result->pw_uid);
- if (result->pw_uid > *uid) {
- return NSS_CACHE_OSLOGIN_HIGH;
- } else {
- return NSS_CACHE_OSLOGIN_LOW;
- }
- }
-
- return NSS_CACHE_OSLOGIN_ERROR;
-}
-
-// _nss_cache_oslogin_pwnam_wrap()
-// Internal wrapper for binary searches, using username-specific calls.
-
-static enum nss_cache_oslogin_match _nss_cache_oslogin_pwnam_wrap(
- FILE *file, struct nss_cache_oslogin_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_OSLOGIN_EXACT;
- }
- DEBUG("Failed match at name %s\n", result->pw_name);
- if (ret > 0) {
- return NSS_CACHE_OSLOGIN_HIGH;
- } else {
- return NSS_CACHE_OSLOGIN_LOW;
- }
- }
-
- return NSS_CACHE_OSLOGIN_ERROR;
-}
-
-// _nss_cache_oslogin_setpwent_locked()
-// Internal setup routine
-
-static enum nss_status _nss_cache_oslogin_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_oslogin_setpwent()
-// Called by NSS to open the passwd file
-// 'stayopen' parameter is ignored.
-
-enum nss_status _nss_cache_oslogin_setpwent(int stayopen) {
- enum nss_status ret;
- NSS_CACHE_OSLOGIN_LOCK();
- ret = _nss_cache_oslogin_setpwent_locked();
- NSS_CACHE_OSLOGIN_UNLOCK();
- return ret;
-}
-
-// _nss_cache_oslogin_endpwent_locked()
-// Internal close routine
-
-static enum nss_status _nss_cache_oslogin_endpwent_locked(void) {
- DEBUG("Closing passwd.cache\n");
- if (p_file) {
- fclose(p_file);
- p_file = NULL;
- }
- return NSS_STATUS_SUCCESS;
-}
-
-// _nss_cache_oslogin_endpwent()
-// Called by NSS to close the passwd file
-
-enum nss_status _nss_cache_oslogin_endpwent(void) {
- enum nss_status ret;
- NSS_CACHE_OSLOGIN_LOCK();
- ret = _nss_cache_oslogin_endpwent_locked();
- NSS_CACHE_OSLOGIN_UNLOCK();
- return ret;
-}
-
-// _nss_cache_oslogin_getpwent_r_locked()
-// Called internally to return the next entry from the passwd file
-
-static enum nss_status _nss_cache_oslogin_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_oslogin_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 {
- if (errno == ENOENT) {
- errno = 0;
- }
- *errnop = errno;
- ret = _nss_cache_oslogin_ent_bad_return_code(*errnop);
- }
- }
-
- return ret;
-}
-
-// _nss_cache_oslogin_getpwent_r()
-// Called by NSS to look up next entry in passwd file
-
-enum nss_status _nss_cache_oslogin_getpwent_r(struct passwd *result,
- char *buffer, size_t buflen,
- int *errnop) {
- enum nss_status ret;
- NSS_CACHE_OSLOGIN_LOCK();
- ret = _nss_cache_oslogin_getpwent_r_locked(result, buffer, buflen, errnop);
- NSS_CACHE_OSLOGIN_UNLOCK();
- return ret;
-}
-
-// _nss_cache_oslogin_getpwuid_r()
-// Find a user account by uid
-
-enum nss_status _nss_cache_oslogin_getpwuid_r(uid_t uid, struct passwd *result,
- char *buffer, size_t buflen,
- int *errnop) {
- enum nss_status ret;
-
- NSS_CACHE_OSLOGIN_LOCK();
- ret = _nss_cache_oslogin_setpwent_locked();
-
- if (ret == NSS_STATUS_SUCCESS) {
- while ((ret = _nss_cache_oslogin_getpwent_r_locked(
- result, buffer, buflen, errnop)) == NSS_STATUS_SUCCESS) {
- if (result->pw_uid == uid) break;
- }
- }
-
- _nss_cache_oslogin_endpwent_locked();
- NSS_CACHE_OSLOGIN_UNLOCK();
-
- return ret;
-}
-
-// _nss_cache_oslogin_getpwnam_r()
-// Find a user account by name
-
-enum nss_status _nss_cache_oslogin_getpwnam_r(const char *name,
- struct passwd *result,
- char *buffer, size_t buflen,
- int *errnop) {
- enum nss_status ret;
-
- NSS_CACHE_OSLOGIN_LOCK();
- ret = _nss_cache_oslogin_setpwent_locked();
-
- if (ret == NSS_STATUS_SUCCESS) {
- while ((ret = _nss_cache_oslogin_getpwent_r_locked(
- result, buffer, buflen, errnop)) == NSS_STATUS_SUCCESS) {
- if (!strcmp(result->pw_name, name)) break;
- }
- }
-
- _nss_cache_oslogin_endpwent_locked();
- NSS_CACHE_OSLOGIN_UNLOCK();
-
- return ret;
-}
-
-NSS_METHOD_PROTOTYPE(__nss_compat_getpwnam_r);
-NSS_METHOD_PROTOTYPE(__nss_compat_getpwuid_r);
-NSS_METHOD_PROTOTYPE(__nss_compat_getpwent_r);
-NSS_METHOD_PROTOTYPE(__nss_compat_setpwent);
-NSS_METHOD_PROTOTYPE(__nss_compat_endpwent);
-
-DECLARE_NSS_METHOD_TABLE(methods,
- { NSDB_PASSWD, "getpwnam_r", __nss_compat_getpwnam_r,
- (void*)_nss_cache_oslogin_getpwnam_r },
- { NSDB_PASSWD, "getpwuid_r", __nss_compat_getpwuid_r,
- (void*)_nss_cache_oslogin_getpwuid_r },
- { NSDB_PASSWD, "getpwent_r", __nss_compat_getpwent_r,
- (void*)_nss_cache_oslogin_getpwent_r },
- { NSDB_PASSWD, "endpwent", __nss_compat_endpwent,
- (void*)_nss_cache_oslogin_endpwent },
- { NSDB_PASSWD, "setpwent", __nss_compat_setpwent,
- (void*)_nss_cache_oslogin_setpwent },
-)
-
-NSS_REGISTER_METHODS(methods)
diff --git a/packages/google-compute-engine-oslogin/src/nss/nss_oslogin.cc b/packages/google-compute-engine-oslogin/src/nss/nss_oslogin.cc
deleted file mode 100644
index 2a34c83..0000000
--- a/packages/google-compute-engine-oslogin/src/nss/nss_oslogin.cc
+++ /dev/null
@@ -1,209 +0,0 @@
-// Copyright 2017 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-#include <compat.h>
-#include <curl/curl.h>
-#include <errno.h>
-#include <grp.h>
-#include <nss.h>
-#include <oslogin_utils.h>
-#include <pthread.h>
-#include <pwd.h>
-#include <string.h>
-#include <sys/param.h>
-#include <sys/types.h>
-#include <syslog.h>
-#include <unistd.h>
-#include <stdlib.h>
-
-#include <iostream>
-#include <sstream>
-#include <string>
-
-using std::string;
-
-using oslogin_utils::AddUsersToGroup;
-using oslogin_utils::BufferManager;
-using oslogin_utils::FindGroup;
-using oslogin_utils::GetGroupsForUser;
-using oslogin_utils::GetUsersForGroup;
-using oslogin_utils::Group;
-using oslogin_utils::HttpGet;
-using oslogin_utils::kMetadataServerUrl;
-using oslogin_utils::MutexLock;
-using oslogin_utils::NssCache;
-using oslogin_utils::ParseJsonToPasswd;
-using oslogin_utils::UrlEncode;
-
-// Size of the NssCache. This also determines how many users will be requested
-// per HTTP call.
-static const uint64_t kNssCacheSize = 2048;
-
-// NssCache for storing passwd entries.
-static NssCache nss_cache(kNssCacheSize);
-
-// Protects access to nss_cache.
-static pthread_mutex_t cache_mutex = PTHREAD_MUTEX_INITIALIZER;
-
-extern "C" {
-
-// Get a passwd entry by id.
-enum nss_status _nss_oslogin_getpwuid_r(uid_t uid, struct passwd *result,
- char *buffer, size_t buflen,
- int *errnop) {
- BufferManager buffer_manager(buffer, buflen);
- std::stringstream url;
- url << kMetadataServerUrl << "users?uid=" << uid;
- string response;
- long http_code = 0;
- if (!HttpGet(url.str(), &response, &http_code) || http_code != 200 ||
- response.empty()) {
- *errnop = ENOENT;
- return NSS_STATUS_NOTFOUND;
- }
- if (!ParseJsonToPasswd(response, result, &buffer_manager, errnop)) {
- if (*errnop == EINVAL) {
- openlog("nss_oslogin", LOG_PID, LOG_USER);
- syslog(LOG_ERR, "Received malformed response from server: %s",
- response.c_str());
- closelog();
- }
- return *errnop == ERANGE ? NSS_STATUS_TRYAGAIN : NSS_STATUS_NOTFOUND;
- }
- return NSS_STATUS_SUCCESS;
-}
-
-// Get a passwd entry by name.
-enum nss_status _nss_oslogin_getpwnam_r(const char *name, struct passwd *result,
- char *buffer, size_t buflen,
- int *errnop) {
- BufferManager buffer_manager(buffer, buflen);
- std::stringstream url;
- url << kMetadataServerUrl << "users?username=" << UrlEncode(name);
- string response;
- long http_code = 0;
- if (!HttpGet(url.str(), &response, &http_code) || http_code != 200 ||
- response.empty()) {
- *errnop = ENOENT;
- return NSS_STATUS_NOTFOUND;
- }
- if (!ParseJsonToPasswd(response, result, &buffer_manager, errnop)) {
- if (*errnop == EINVAL) {
- openlog("nss_oslogin", LOG_PID, LOG_USER);
- syslog(LOG_ERR, "Received malformed response from server: %s",
- response.c_str());
- closelog();
- }
- return *errnop == ERANGE ? NSS_STATUS_TRYAGAIN : NSS_STATUS_NOTFOUND;
- }
- return NSS_STATUS_SUCCESS;
-}
-
-enum nss_status _nss_oslogin_getgrby(struct group *grp, char *buf,
- size_t buflen, int *errnop) {
- BufferManager buffer_manager(buf, buflen);
- if (!FindGroup(grp, &buffer_manager, errnop))
- return *errnop == ERANGE ? NSS_STATUS_TRYAGAIN : NSS_STATUS_NOTFOUND;
-
- std::vector<string> users;
- if (!GetUsersForGroup(grp->gr_name, &users, errnop))
- return *errnop == ERANGE ? NSS_STATUS_TRYAGAIN : NSS_STATUS_NOTFOUND;
-
- if (!AddUsersToGroup(users, grp, &buffer_manager, errnop))
- return *errnop == ERANGE ? NSS_STATUS_TRYAGAIN : NSS_STATUS_NOTFOUND;
-
- return NSS_STATUS_SUCCESS;
-}
-
-enum nss_status _nss_oslogin_getgrgid_r(gid_t gid, struct group *grp, char *buf,
- size_t buflen, int *errnop) {
- grp->gr_gid = gid;
- return _nss_oslogin_getgrby(grp, buf, buflen, errnop);
-}
-
-enum nss_status _nss_oslogin_getgrnam_r(const char *name, struct group *grp,
- char *buf, size_t buflen, int *errnop) {
- grp->gr_name = (char *)name;
- return _nss_oslogin_getgrby(grp, buf, buflen, errnop);
-}
-
-enum nss_status _nss_oslogin_initgroups_dyn(const char *user, gid_t skipgroup,
- long int *start, long int *size,
- gid_t **groupsp, long int limit,
- int *errnop) {
- std::vector<Group> grouplist;
- if (!GetGroupsForUser(string(user), &grouplist, errnop)) {
- return NSS_STATUS_NOTFOUND;
- }
-
- gid_t *groups = *groupsp;
- for (int i = 0; i < (int) grouplist.size(); i++) {
- // Resize the buffer if needed.
- if (*start == *size) {
- gid_t *newgroups;
- long int newsize = 2 * *size;
- // Stop at limit if provided.
- if (limit > 0) {
- if (*size >= limit) {
- *errnop = ERANGE;
- return NSS_STATUS_TRYAGAIN;
- }
- newsize = MIN(limit, newsize);
- }
- newgroups = (gid_t *)realloc(groups, newsize * sizeof(gid_t *));
- if (newgroups == NULL) {
- *errnop = EAGAIN;
- return NSS_STATUS_TRYAGAIN;
- }
- *groupsp = groups = newgroups;
- *size = newsize;
- }
- groups[(*start)++] = grouplist[i].gid;
- }
- return NSS_STATUS_SUCCESS;
-}
-
-// nss_getpwent_r() is intentionally left unimplemented. This functionality is
-// now covered by the nss_cache binary and nss_cache module.
-
-nss_status _nss_oslogin_getpwent_r() { return NSS_STATUS_NOTFOUND; }
-nss_status _nss_oslogin_endpwent() { return NSS_STATUS_SUCCESS; }
-nss_status _nss_oslogin_setpwent() { return NSS_STATUS_SUCCESS; }
-
-NSS_METHOD_PROTOTYPE(__nss_compat_getpwnam_r);
-NSS_METHOD_PROTOTYPE(__nss_compat_getpwuid_r);
-NSS_METHOD_PROTOTYPE(__nss_compat_getpwent_r);
-NSS_METHOD_PROTOTYPE(__nss_compat_setpwent);
-NSS_METHOD_PROTOTYPE(__nss_compat_endpwent);
-NSS_METHOD_PROTOTYPE(__nss_compat_getgrnam_r);
-NSS_METHOD_PROTOTYPE(__nss_compat_getgrgid_r);
-
-DECLARE_NSS_METHOD_TABLE(methods,
- {NSDB_PASSWD, "getpwnam_r", __nss_compat_getpwnam_r,
- (void *)_nss_oslogin_getpwnam_r},
- {NSDB_PASSWD, "getpwuid_r", __nss_compat_getpwuid_r,
- (void *)_nss_oslogin_getpwuid_r},
- {NSDB_PASSWD, "getpwent_r", __nss_compat_getpwent_r,
- (void *)_nss_oslogin_getpwent_r},
- {NSDB_PASSWD, "endpwent", __nss_compat_endpwent,
- (void *)_nss_oslogin_endpwent},
- {NSDB_PASSWD, "setpwent", __nss_compat_setpwent,
- (void *)_nss_oslogin_setpwent},
- {NSDB_GROUP, "getgrnam_r", __nss_compat_getgrnam_r,
- (void *)_nss_oslogin_getgrnam_r},
- {NSDB_GROUP, "getgrgid_r", __nss_compat_getgrgid_r,
- (void *)_nss_oslogin_getgrgid_r}, )
-
-NSS_REGISTER_METHODS(methods)
-} // extern "C"