PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/source3/torture/nsstest.c

https://github.com/endisd/samba
C | 494 lines | 408 code | 65 blank | 21 comment | 86 complexity | ebed0bd4a9e3da62f778a7d49028a3b1 MD5 | raw file
Possible License(s): GPL-3.0
  1. /*
  2. Unix SMB/CIFS implementation.
  3. nss tester for winbindd
  4. Copyright (C) Andrew Tridgell 2001
  5. Copyright (C) Tim Potter 2003
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program 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
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "includes.h"
  18. #ifdef malloc
  19. #undef malloc
  20. #endif
  21. #ifdef realloc
  22. #undef realloc
  23. #endif
  24. static const char *so_path = "/lib/libnss_winbind.so";
  25. static const char *nss_name = "winbind";
  26. static int nss_errno;
  27. static NSS_STATUS last_error;
  28. static int total_errors;
  29. static void *find_fn(const char *name)
  30. {
  31. char *s;
  32. static void *h;
  33. void *res;
  34. if (asprintf(&s, "_nss_%s_%s", nss_name, name) < 0) {
  35. exit(1);
  36. }
  37. if (!h) {
  38. h = dlopen(so_path, RTLD_LAZY);
  39. }
  40. if (!h) {
  41. printf("Can't open shared library %s\n", so_path);
  42. exit(1);
  43. }
  44. res = dlsym(h, s);
  45. if (!res) {
  46. printf("Can't find function %s\n", s);
  47. total_errors++;
  48. SAFE_FREE(s);
  49. return NULL;
  50. }
  51. SAFE_FREE(s);
  52. return res;
  53. }
  54. static void report_nss_error(const char *who, NSS_STATUS status)
  55. {
  56. last_error = status;
  57. total_errors++;
  58. printf("ERROR %s: NSS_STATUS=%d %d (nss_errno=%d)\n",
  59. who, status, NSS_STATUS_SUCCESS, nss_errno);
  60. }
  61. static struct passwd *nss_getpwent(void)
  62. {
  63. NSS_STATUS (*_nss_getpwent_r)(struct passwd *, char *,
  64. size_t , int *) =
  65. (NSS_STATUS (*)(struct passwd *, char *,
  66. size_t, int *))find_fn("getpwent_r");
  67. static struct passwd pwd;
  68. static char buf[1000];
  69. NSS_STATUS status;
  70. if (!_nss_getpwent_r)
  71. return NULL;
  72. status = _nss_getpwent_r(&pwd, buf, sizeof(buf), &nss_errno);
  73. if (status == NSS_STATUS_NOTFOUND) {
  74. return NULL;
  75. }
  76. if (status != NSS_STATUS_SUCCESS) {
  77. report_nss_error("getpwent", status);
  78. return NULL;
  79. }
  80. return &pwd;
  81. }
  82. static struct passwd *nss_getpwnam(const char *name)
  83. {
  84. NSS_STATUS (*_nss_getpwnam_r)(const char *, struct passwd *, char *,
  85. size_t , int *) =
  86. (NSS_STATUS (*)(const char *, struct passwd *, char *,
  87. size_t, int *))find_fn("getpwnam_r");
  88. static struct passwd pwd;
  89. static char buf[1000];
  90. NSS_STATUS status;
  91. if (!_nss_getpwnam_r)
  92. return NULL;
  93. status = _nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &nss_errno);
  94. if (status == NSS_STATUS_NOTFOUND) {
  95. return NULL;
  96. }
  97. if (status != NSS_STATUS_SUCCESS) {
  98. report_nss_error("getpwnam", status);
  99. return NULL;
  100. }
  101. return &pwd;
  102. }
  103. static struct passwd *nss_getpwuid(uid_t uid)
  104. {
  105. NSS_STATUS (*_nss_getpwuid_r)(uid_t , struct passwd *, char *,
  106. size_t , int *) =
  107. (NSS_STATUS (*)(uid_t, struct passwd *, char *,
  108. size_t, int *))find_fn("getpwuid_r");
  109. static struct passwd pwd;
  110. static char buf[1000];
  111. NSS_STATUS status;
  112. if (!_nss_getpwuid_r)
  113. return NULL;
  114. status = _nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &nss_errno);
  115. if (status == NSS_STATUS_NOTFOUND) {
  116. return NULL;
  117. }
  118. if (status != NSS_STATUS_SUCCESS) {
  119. report_nss_error("getpwuid", status);
  120. return NULL;
  121. }
  122. return &pwd;
  123. }
  124. static void nss_setpwent(void)
  125. {
  126. NSS_STATUS (*_nss_setpwent)(void) =
  127. (NSS_STATUS(*)(void))find_fn("setpwent");
  128. NSS_STATUS status;
  129. if (!_nss_setpwent)
  130. return;
  131. status = _nss_setpwent();
  132. if (status != NSS_STATUS_SUCCESS) {
  133. report_nss_error("setpwent", status);
  134. }
  135. }
  136. static void nss_endpwent(void)
  137. {
  138. NSS_STATUS (*_nss_endpwent)(void) =
  139. (NSS_STATUS (*)(void))find_fn("endpwent");
  140. NSS_STATUS status;
  141. if (!_nss_endpwent)
  142. return;
  143. status = _nss_endpwent();
  144. if (status != NSS_STATUS_SUCCESS) {
  145. report_nss_error("endpwent", status);
  146. }
  147. }
  148. static struct group *nss_getgrent(void)
  149. {
  150. NSS_STATUS (*_nss_getgrent_r)(struct group *, char *,
  151. size_t , int *) =
  152. (NSS_STATUS (*)(struct group *, char *,
  153. size_t, int *))find_fn("getgrent_r");
  154. static struct group grp;
  155. static char *buf;
  156. static int buflen = 1024;
  157. NSS_STATUS status;
  158. if (!_nss_getgrent_r)
  159. return NULL;
  160. if (!buf)
  161. buf = (char *)malloc(buflen);
  162. again:
  163. status = _nss_getgrent_r(&grp, buf, buflen, &nss_errno);
  164. if (status == NSS_STATUS_TRYAGAIN) {
  165. buflen *= 2;
  166. buf = (char *)realloc(buf, buflen);
  167. if (!buf) {
  168. return NULL;
  169. }
  170. goto again;
  171. }
  172. if (status == NSS_STATUS_NOTFOUND) {
  173. SAFE_FREE(buf);
  174. return NULL;
  175. }
  176. if (status != NSS_STATUS_SUCCESS) {
  177. report_nss_error("getgrent", status);
  178. SAFE_FREE(buf);
  179. return NULL;
  180. }
  181. return &grp;
  182. }
  183. static struct group *nss_getgrnam(const char *name)
  184. {
  185. NSS_STATUS (*_nss_getgrnam_r)(const char *, struct group *, char *,
  186. size_t , int *) =
  187. (NSS_STATUS (*)(const char *, struct group *, char *,
  188. size_t, int *))find_fn("getgrnam_r");
  189. static struct group grp;
  190. static char *buf;
  191. static int buflen = 1000;
  192. NSS_STATUS status;
  193. if (!_nss_getgrnam_r)
  194. return NULL;
  195. if (!buf)
  196. buf = (char *)malloc(buflen);
  197. again:
  198. status = _nss_getgrnam_r(name, &grp, buf, buflen, &nss_errno);
  199. if (status == NSS_STATUS_TRYAGAIN) {
  200. buflen *= 2;
  201. buf = (char *)realloc(buf, buflen);
  202. if (!buf) {
  203. return NULL;
  204. }
  205. goto again;
  206. }
  207. if (status == NSS_STATUS_NOTFOUND) {
  208. SAFE_FREE(buf);
  209. return NULL;
  210. }
  211. if (status != NSS_STATUS_SUCCESS) {
  212. report_nss_error("getgrnam", status);
  213. SAFE_FREE(buf);
  214. return NULL;
  215. }
  216. return &grp;
  217. }
  218. static struct group *nss_getgrgid(gid_t gid)
  219. {
  220. NSS_STATUS (*_nss_getgrgid_r)(gid_t , struct group *, char *,
  221. size_t , int *) =
  222. (NSS_STATUS (*)(gid_t, struct group *, char *,
  223. size_t, int *))find_fn("getgrgid_r");
  224. static struct group grp;
  225. static char *buf;
  226. static int buflen = 1000;
  227. NSS_STATUS status;
  228. if (!_nss_getgrgid_r)
  229. return NULL;
  230. if (!buf)
  231. buf = (char *)malloc(buflen);
  232. again:
  233. status = _nss_getgrgid_r(gid, &grp, buf, buflen, &nss_errno);
  234. if (status == NSS_STATUS_TRYAGAIN) {
  235. buflen *= 2;
  236. buf = (char *)realloc(buf, buflen);
  237. if (!buf) {
  238. return NULL;
  239. }
  240. goto again;
  241. }
  242. if (status == NSS_STATUS_NOTFOUND) {
  243. SAFE_FREE(buf);
  244. return NULL;
  245. }
  246. if (status != NSS_STATUS_SUCCESS) {
  247. report_nss_error("getgrgid", status);
  248. SAFE_FREE(buf);
  249. return NULL;
  250. }
  251. return &grp;
  252. }
  253. static void nss_setgrent(void)
  254. {
  255. NSS_STATUS (*_nss_setgrent)(void) =
  256. (NSS_STATUS (*)(void))find_fn("setgrent");
  257. NSS_STATUS status;
  258. if (!_nss_setgrent)
  259. return;
  260. status = _nss_setgrent();
  261. if (status != NSS_STATUS_SUCCESS) {
  262. report_nss_error("setgrent", status);
  263. }
  264. }
  265. static void nss_endgrent(void)
  266. {
  267. NSS_STATUS (*_nss_endgrent)(void) =
  268. (NSS_STATUS (*)(void))find_fn("endgrent");
  269. NSS_STATUS status;
  270. if (!_nss_endgrent)
  271. return;
  272. status = _nss_endgrent();
  273. if (status != NSS_STATUS_SUCCESS) {
  274. report_nss_error("endgrent", status);
  275. }
  276. }
  277. static int nss_initgroups(char *user, gid_t group, gid_t **groups, long int *start, long int *size)
  278. {
  279. NSS_STATUS (*_nss_initgroups)(char *, gid_t , long int *,
  280. long int *, gid_t **, long int , int *) =
  281. (NSS_STATUS (*)(char *, gid_t, long int *,
  282. long int *, gid_t **,
  283. long int, int *))find_fn("initgroups_dyn");
  284. NSS_STATUS status;
  285. if (!_nss_initgroups)
  286. return NSS_STATUS_UNAVAIL;
  287. status = _nss_initgroups(user, group, start, size, groups, 0, &nss_errno);
  288. if (status != NSS_STATUS_SUCCESS) {
  289. report_nss_error("initgroups", status);
  290. }
  291. return status;
  292. }
  293. static void print_passwd(struct passwd *pwd)
  294. {
  295. printf("%s:%s:%lu:%lu:%s:%s:%s\n",
  296. pwd->pw_name,
  297. pwd->pw_passwd,
  298. (unsigned long)pwd->pw_uid,
  299. (unsigned long)pwd->pw_gid,
  300. pwd->pw_gecos,
  301. pwd->pw_dir,
  302. pwd->pw_shell);
  303. }
  304. static void print_group(struct group *grp)
  305. {
  306. int i;
  307. printf("%s:%s:%lu:",
  308. grp->gr_name,
  309. grp->gr_passwd,
  310. (unsigned long)grp->gr_gid);
  311. if (!grp->gr_mem[0]) {
  312. printf("\n");
  313. return;
  314. }
  315. for (i=0; grp->gr_mem[i+1]; i++) {
  316. printf("%s,", grp->gr_mem[i]);
  317. }
  318. printf("%s\n", grp->gr_mem[i]);
  319. }
  320. static void nss_test_initgroups(char *name, gid_t gid)
  321. {
  322. long int size = 16;
  323. long int start = 1;
  324. gid_t *groups = NULL;
  325. int i;
  326. NSS_STATUS status;
  327. groups = (gid_t *)malloc(size);
  328. groups[0] = gid;
  329. status = nss_initgroups(name, gid, &groups, &start, &size);
  330. if (status == NSS_STATUS_UNAVAIL) {
  331. printf("No initgroups fn\n");
  332. return;
  333. }
  334. for (i=0; i<start-1; i++) {
  335. printf("%lu, ", (unsigned long)groups[i]);
  336. }
  337. printf("%lu\n", (unsigned long)groups[i]);
  338. }
  339. static void nss_test_users(void)
  340. {
  341. struct passwd *pwd;
  342. nss_setpwent();
  343. /* loop over all users */
  344. while ((pwd = nss_getpwent())) {
  345. printf("Testing user %s\n", pwd->pw_name);
  346. printf("getpwent: "); print_passwd(pwd);
  347. pwd = nss_getpwuid(pwd->pw_uid);
  348. if (!pwd) {
  349. total_errors++;
  350. printf("ERROR: can't getpwuid\n");
  351. continue;
  352. }
  353. printf("getpwuid: "); print_passwd(pwd);
  354. pwd = nss_getpwnam(pwd->pw_name);
  355. if (!pwd) {
  356. total_errors++;
  357. printf("ERROR: can't getpwnam\n");
  358. continue;
  359. }
  360. printf("getpwnam: "); print_passwd(pwd);
  361. printf("initgroups: "); nss_test_initgroups(pwd->pw_name, pwd->pw_gid);
  362. printf("\n");
  363. }
  364. nss_endpwent();
  365. }
  366. static void nss_test_groups(void)
  367. {
  368. struct group *grp;
  369. nss_setgrent();
  370. /* loop over all groups */
  371. while ((grp = nss_getgrent())) {
  372. printf("Testing group %s\n", grp->gr_name);
  373. printf("getgrent: "); print_group(grp);
  374. grp = nss_getgrnam(grp->gr_name);
  375. if (!grp) {
  376. total_errors++;
  377. printf("ERROR: can't getgrnam\n");
  378. continue;
  379. }
  380. printf("getgrnam: "); print_group(grp);
  381. grp = nss_getgrgid(grp->gr_gid);
  382. if (!grp) {
  383. total_errors++;
  384. printf("ERROR: can't getgrgid\n");
  385. continue;
  386. }
  387. printf("getgrgid: "); print_group(grp);
  388. printf("\n");
  389. }
  390. nss_endgrent();
  391. }
  392. static void nss_test_errors(void)
  393. {
  394. struct passwd *pwd;
  395. struct group *grp;
  396. pwd = getpwnam("nosuchname");
  397. if (pwd || last_error != NSS_STATUS_NOTFOUND) {
  398. total_errors++;
  399. printf("ERROR Non existant user gave error %d\n", last_error);
  400. }
  401. pwd = getpwuid(0xFFF0);
  402. if (pwd || last_error != NSS_STATUS_NOTFOUND) {
  403. total_errors++;
  404. printf("ERROR Non existant uid gave error %d\n", last_error);
  405. }
  406. grp = getgrnam("nosuchgroup");
  407. if (grp || last_error != NSS_STATUS_NOTFOUND) {
  408. total_errors++;
  409. printf("ERROR Non existant group gave error %d\n", last_error);
  410. }
  411. grp = getgrgid(0xFFF0);
  412. if (grp || last_error != NSS_STATUS_NOTFOUND) {
  413. total_errors++;
  414. printf("ERROR Non existant gid gave error %d\n", last_error);
  415. }
  416. }
  417. int main(int argc, char *argv[])
  418. {
  419. if (argc > 1) so_path = argv[1];
  420. if (argc > 2) nss_name = argv[2];
  421. nss_test_users();
  422. nss_test_groups();
  423. nss_test_errors();
  424. printf("total_errors=%d\n", total_errors);
  425. return total_errors;
  426. }