| 1 | /* Copyright (C) 1997-2016 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <nss.h> |
| 20 | #include <errno.h> |
| 21 | #include <shadow.h> |
| 22 | #include <string.h> |
| 23 | #include <libc-lock.h> |
| 24 | #include <rpcsvc/nis.h> |
| 25 | |
| 26 | #include "nss-nisplus.h" |
| 27 | #include "nisplus-parser.h" |
| 28 | |
| 29 | __libc_lock_define_initialized (static, lock) |
| 30 | |
| 31 | static nis_result *result; |
| 32 | |
| 33 | /* Defined in nisplus-pwd.c. */ |
| 34 | extern nis_name pwd_tablename_val attribute_hidden; |
| 35 | extern size_t pwd_tablename_len attribute_hidden; |
| 36 | extern enum nss_status _nss_pwd_create_tablename (int *errnop); |
| 37 | |
| 38 | |
| 39 | enum nss_status |
| 40 | _nss_nisplus_setspent (int stayopen) |
| 41 | { |
| 42 | enum nss_status status = NSS_STATUS_SUCCESS; |
| 43 | int err; |
| 44 | |
| 45 | __libc_lock_lock (lock); |
| 46 | |
| 47 | if (result != NULL) |
| 48 | { |
| 49 | nis_freeresult (result); |
| 50 | result = NULL; |
| 51 | } |
| 52 | |
| 53 | if (pwd_tablename_val == NULL) |
| 54 | status = _nss_pwd_create_tablename (&err); |
| 55 | |
| 56 | __libc_lock_unlock (lock); |
| 57 | |
| 58 | return status; |
| 59 | } |
| 60 | |
| 61 | enum nss_status |
| 62 | _nss_nisplus_endspent (void) |
| 63 | { |
| 64 | __libc_lock_lock (lock); |
| 65 | |
| 66 | if (result != NULL) |
| 67 | { |
| 68 | nis_freeresult (result); |
| 69 | result = NULL; |
| 70 | } |
| 71 | |
| 72 | __libc_lock_unlock (lock); |
| 73 | |
| 74 | return NSS_STATUS_SUCCESS; |
| 75 | } |
| 76 | |
| 77 | static enum nss_status |
| 78 | internal_nisplus_getspent_r (struct spwd *sp, char *buffer, size_t buflen, |
| 79 | int *errnop) |
| 80 | { |
| 81 | int parse_res; |
| 82 | |
| 83 | /* Get the next entry until we found a correct one. */ |
| 84 | do |
| 85 | { |
| 86 | nis_result *saved_res; |
| 87 | |
| 88 | if (result == NULL) |
| 89 | { |
| 90 | saved_res = NULL; |
| 91 | |
| 92 | if (pwd_tablename_val == NULL) |
| 93 | { |
| 94 | enum nss_status status = _nss_pwd_create_tablename (errnop); |
| 95 | |
| 96 | if (status != NSS_STATUS_SUCCESS) |
| 97 | return status; |
| 98 | } |
| 99 | |
| 100 | result = nis_first_entry (pwd_tablename_val); |
| 101 | if (result == NULL) |
| 102 | { |
| 103 | *errnop = errno; |
| 104 | return NSS_STATUS_TRYAGAIN; |
| 105 | } |
| 106 | if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) |
| 107 | return niserr2nss (result->status); |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | saved_res = result; |
| 112 | result = nis_next_entry (pwd_tablename_val, &result->cookie); |
| 113 | if (result == NULL) |
| 114 | { |
| 115 | *errnop = errno; |
| 116 | return NSS_STATUS_TRYAGAIN; |
| 117 | } |
| 118 | if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) |
| 119 | { |
| 120 | nis_freeresult (saved_res); |
| 121 | return niserr2nss (result->status); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | parse_res = _nss_nisplus_parse_spent (result, sp, buffer, |
| 126 | buflen, errnop); |
| 127 | if (__glibc_unlikely (parse_res == -1)) |
| 128 | { |
| 129 | nis_freeresult (result); |
| 130 | result = saved_res; |
| 131 | *errnop = ERANGE; |
| 132 | return NSS_STATUS_TRYAGAIN; |
| 133 | } |
| 134 | |
| 135 | if (saved_res != NULL) |
| 136 | nis_freeresult (saved_res); |
| 137 | } |
| 138 | while (!parse_res); |
| 139 | |
| 140 | return NSS_STATUS_SUCCESS; |
| 141 | } |
| 142 | |
| 143 | enum nss_status |
| 144 | _nss_nisplus_getspent_r (struct spwd *result, char *buffer, size_t buflen, |
| 145 | int *errnop) |
| 146 | { |
| 147 | int status; |
| 148 | |
| 149 | __libc_lock_lock (lock); |
| 150 | |
| 151 | status = internal_nisplus_getspent_r (result, buffer, buflen, errnop); |
| 152 | |
| 153 | __libc_lock_unlock (lock); |
| 154 | |
| 155 | return status; |
| 156 | } |
| 157 | |
| 158 | enum nss_status |
| 159 | _nss_nisplus_getspnam_r (const char *name, struct spwd *sp, |
| 160 | char *buffer, size_t buflen, int *errnop) |
| 161 | { |
| 162 | int parse_res; |
| 163 | |
| 164 | if (pwd_tablename_val == NULL) |
| 165 | { |
| 166 | enum nss_status status = _nss_pwd_create_tablename (errnop); |
| 167 | |
| 168 | if (status != NSS_STATUS_SUCCESS) |
| 169 | return status; |
| 170 | } |
| 171 | |
| 172 | if (name == NULL) |
| 173 | { |
| 174 | *errnop = EINVAL; |
| 175 | return NSS_STATUS_NOTFOUND; |
| 176 | } |
| 177 | |
| 178 | nis_result *result; |
| 179 | char buf[strlen (name) + 9 + pwd_tablename_len]; |
| 180 | int olderr = errno; |
| 181 | |
| 182 | snprintf (buf, sizeof (buf), "[name=%s],%s" , name, pwd_tablename_val); |
| 183 | |
| 184 | result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS | USE_DGRAM, NULL, NULL); |
| 185 | |
| 186 | if (result == NULL) |
| 187 | { |
| 188 | *errnop = ENOMEM; |
| 189 | return NSS_STATUS_TRYAGAIN; |
| 190 | } |
| 191 | |
| 192 | if (__glibc_unlikely (niserr2nss (result->status) != NSS_STATUS_SUCCESS)) |
| 193 | { |
| 194 | enum nss_status status = niserr2nss (result->status); |
| 195 | |
| 196 | __set_errno (olderr); |
| 197 | |
| 198 | nis_freeresult (result); |
| 199 | return status; |
| 200 | } |
| 201 | |
| 202 | parse_res = _nss_nisplus_parse_spent (result, sp, buffer, buflen, errnop); |
| 203 | nis_freeresult (result); |
| 204 | |
| 205 | if (__glibc_unlikely (parse_res < 1)) |
| 206 | { |
| 207 | if (parse_res == -1) |
| 208 | { |
| 209 | *errnop = ERANGE; |
| 210 | return NSS_STATUS_TRYAGAIN; |
| 211 | } |
| 212 | else |
| 213 | { |
| 214 | __set_errno (olderr); |
| 215 | return NSS_STATUS_NOTFOUND; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | return NSS_STATUS_SUCCESS; |
| 220 | } |
| 221 | |