LCOV - code coverage report
Current view: top level - nsswitch - nsstest.c (source / functions) Hit Total Coverage
Test: coverage report for v4-17-test 1498b464 Lines: 173 270 64.1 %
Date: 2024-06-13 04:01:37 Functions: 19 20 95.0 %

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

Generated by: LCOV version 1.13