LCOV - code coverage report
Current view: top level - nsswitch - nsstest.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 173 270 64.1 %
Date: 2021-09-23 10:06:22 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        5972 : static void *find_fn(const char *name)
      33             : {
      34             :         char *s;
      35             :         static void *h;
      36             :         void *res;
      37             : 
      38        5972 :         if (asprintf(&s, "_nss_%s_%s", nss_name, name) < 0) {
      39           0 :                 exit(1);
      40             :         }
      41             : 
      42        5972 :         if (!h) {
      43          10 :                 h = dlopen(so_path, RTLD_LAZY);
      44             :         }
      45        5972 :         if (!h) {
      46           0 :                 printf("Can't open shared library %s\n", so_path);
      47           0 :                 exit(1);
      48             :         }
      49        5972 :         res = dlsym(h, s);
      50        5972 :         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        5972 :         SAFE_FREE(s);
      57        5972 :         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         702 : static struct passwd *nss_getpwent(void)
      69             : {
      70         702 :         NSS_STATUS (*_nss_getpwent_r)(struct passwd *, char *,
      71             :                                       size_t , int *) =
      72             :                 (NSS_STATUS (*)(struct passwd *, char *,
      73         702 :                                 size_t, int *))find_fn("getpwent_r");
      74             :         static struct passwd pwd;
      75             :         static char buf[1000];
      76             :         NSS_STATUS status;
      77             : 
      78         702 :         if (!_nss_getpwent_r)
      79           0 :                 return NULL;
      80             : 
      81         702 :         status = _nss_getpwent_r(&pwd, buf, sizeof(buf), &nss_errno);
      82         702 :         if (status == NSS_STATUS_NOTFOUND) {
      83          10 :                 return NULL;
      84             :         }
      85         692 :         if (status != NSS_STATUS_SUCCESS) {
      86           0 :                 report_nss_error("getpwent", status);
      87           0 :                 return NULL;
      88             :         }
      89         692 :         return &pwd;
      90             : }
      91             : 
      92         692 : static struct passwd *nss_getpwnam(const char *name)
      93             : {
      94         692 :         NSS_STATUS (*_nss_getpwnam_r)(const char *, struct passwd *, char *,
      95             :                                       size_t , int *) =
      96             :                 (NSS_STATUS (*)(const char *, struct passwd *, char *,
      97         692 :                                 size_t, int *))find_fn("getpwnam_r");
      98             :         static struct passwd pwd;
      99             :         static char buf[1000];
     100             :         NSS_STATUS status;
     101             : 
     102         692 :         if (!_nss_getpwnam_r)
     103           0 :                 return NULL;
     104             : 
     105         692 :         status = _nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &nss_errno);
     106         692 :         if (status == NSS_STATUS_NOTFOUND) {
     107           0 :                 return NULL;
     108             :         }
     109         692 :         if (status != NSS_STATUS_SUCCESS) {
     110           0 :                 report_nss_error("getpwnam", status);
     111           0 :                 return NULL;
     112             :         }
     113         692 :         return &pwd;
     114             : }
     115             : 
     116         692 : static struct passwd *nss_getpwuid(uid_t uid)
     117             : {
     118         692 :         NSS_STATUS (*_nss_getpwuid_r)(uid_t , struct passwd *, char *,
     119             :                                       size_t , int *) =
     120             :                 (NSS_STATUS (*)(uid_t, struct passwd *, char *,
     121         692 :                                 size_t, int *))find_fn("getpwuid_r");
     122             :         static struct passwd pwd;
     123             :         static char buf[1000];
     124             :         NSS_STATUS status;
     125             : 
     126         692 :         if (!_nss_getpwuid_r)
     127           0 :                 return NULL;
     128             : 
     129         692 :         status = _nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &nss_errno);
     130         692 :         if (status == NSS_STATUS_NOTFOUND) {
     131           0 :                 return NULL;
     132             :         }
     133         692 :         if (status != NSS_STATUS_SUCCESS) {
     134           0 :                 report_nss_error("getpwuid", status);
     135           0 :                 return NULL;
     136             :         }
     137         692 :         return &pwd;
     138             : }
     139             : 
     140          10 : static void samba_nss_setpwent(void)
     141             : {
     142          10 :         NSS_STATUS (*_nss_setpwent)(void) =
     143          10 :                 (NSS_STATUS(*)(void))find_fn("setpwent");
     144             :         NSS_STATUS status;
     145             : 
     146          10 :         if (!_nss_setpwent)
     147           0 :                 return;
     148             : 
     149          10 :         status = _nss_setpwent();
     150          10 :         if (status != NSS_STATUS_SUCCESS) {
     151           0 :                 report_nss_error("setpwent", status);
     152             :         }
     153             : }
     154             : 
     155          10 : static void samba_nss_endpwent(void)
     156             : {
     157          10 :         NSS_STATUS (*_nss_endpwent)(void) =
     158          10 :                 (NSS_STATUS (*)(void))find_fn("endpwent");
     159             :         NSS_STATUS status;
     160             : 
     161          10 :         if (!_nss_endpwent)
     162           0 :                 return;
     163             : 
     164          10 :         status = _nss_endpwent();
     165          10 :         if (status != NSS_STATUS_SUCCESS) {
     166           0 :                 report_nss_error("endpwent", status);
     167             :         }
     168             : }
     169             : 
     170             : 
     171        1058 : static struct group *nss_getgrent(void)
     172             : {
     173        1058 :         NSS_STATUS (*_nss_getgrent_r)(struct group *, char *,
     174             :                                       size_t , int *) =
     175             :                 (NSS_STATUS (*)(struct group *, char *,
     176        1058 :                                 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        1058 :         if (!_nss_getgrent_r)
     183           0 :                 return NULL;
     184             : 
     185        1058 :         if (!buf)
     186          10 :                 buf = (char *)malloc(buflen);
     187             : 
     188        1731 : again:
     189        1058 :         status = _nss_getgrent_r(&grp, buf, buflen, &nss_errno);
     190        1058 :         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        1058 :         if (status == NSS_STATUS_NOTFOUND) {
     201          10 :                 SAFE_FREE(buf);
     202          10 :                 return NULL;
     203             :         }
     204        1048 :         if (status != NSS_STATUS_SUCCESS) {
     205           0 :                 report_nss_error("getgrent", status);
     206           0 :                 SAFE_FREE(buf);
     207           0 :                 return NULL;
     208             :         }
     209        1048 :         return &grp;
     210             : }
     211             : 
     212        1048 : static struct group *nss_getgrnam(const char *name)
     213             : {
     214        1048 :         NSS_STATUS (*_nss_getgrnam_r)(const char *, struct group *, char *,
     215             :                                       size_t , int *) =
     216             :                 (NSS_STATUS (*)(const char *, struct group *, char *,
     217        1048 :                                 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        1048 :         if (!_nss_getgrnam_r)
     224           0 :                 return NULL;
     225             : 
     226        1048 :         if (!buf)
     227           8 :                 buf = (char *)malloc(buflen);
     228        1716 : again:
     229        1048 :         status = _nss_getgrnam_r(name, &grp, buf, buflen, &nss_errno);
     230        1048 :         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        1048 :         if (status == NSS_STATUS_NOTFOUND) {
     241           0 :                 SAFE_FREE(buf);
     242           0 :                 return NULL;
     243             :         }
     244        1048 :         if (status != NSS_STATUS_SUCCESS) {
     245           0 :                 report_nss_error("getgrnam", status);
     246           0 :                 SAFE_FREE(buf);
     247           0 :                 return NULL;
     248             :         }
     249        1048 :         return &grp;
     250             : }
     251             : 
     252        1048 : static struct group *nss_getgrgid(gid_t gid)
     253             : {
     254        1048 :         NSS_STATUS (*_nss_getgrgid_r)(gid_t , struct group *, char *,
     255             :                                       size_t , int *) =
     256             :                 (NSS_STATUS (*)(gid_t, struct group *, char *,
     257        1048 :                                 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        1048 :         if (!_nss_getgrgid_r)
     264           0 :                 return NULL;
     265             : 
     266        1048 :         if (!buf)
     267           8 :                 buf = (char *)malloc(buflen);
     268             : 
     269        1716 : again:
     270        1048 :         status = _nss_getgrgid_r(gid, &grp, buf, buflen, &nss_errno);
     271        1048 :         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        1048 :         if (status == NSS_STATUS_NOTFOUND) {
     282           0 :                 SAFE_FREE(buf);
     283           0 :                 return NULL;
     284             :         }
     285        1048 :         if (status != NSS_STATUS_SUCCESS) {
     286           0 :                 report_nss_error("getgrgid", status);
     287           0 :                 SAFE_FREE(buf);
     288           0 :                 return NULL;
     289             :         }
     290        1048 :         return &grp;
     291             : }
     292             : 
     293          10 : static void samba_nss_setgrent(void)
     294             : {
     295          10 :         NSS_STATUS (*_nss_setgrent)(void) =
     296          10 :                 (NSS_STATUS (*)(void))find_fn("setgrent");
     297             :         NSS_STATUS status;
     298             : 
     299          10 :         if (!_nss_setgrent)
     300           0 :                 return;
     301             : 
     302          10 :         status = _nss_setgrent();
     303          10 :         if (status != NSS_STATUS_SUCCESS) {
     304           0 :                 report_nss_error("setgrent", status);
     305             :         }
     306             : }
     307             : 
     308          10 : static void samba_nss_endgrent(void)
     309             : {
     310          10 :         NSS_STATUS (*_nss_endgrent)(void) =
     311          10 :                 (NSS_STATUS (*)(void))find_fn("endgrent");
     312             :         NSS_STATUS status;
     313             : 
     314          10 :         if (!_nss_endgrent)
     315           0 :                 return;
     316             : 
     317          10 :         status = _nss_endgrent();
     318          10 :         if (status != NSS_STATUS_SUCCESS) {
     319           0 :                 report_nss_error("endgrent", status);
     320             :         }
     321             : }
     322             : 
     323         692 : static int nss_initgroups(char *user, gid_t group, gid_t **groups, long int *start, long int *size)
     324             : {
     325         692 :         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         692 :                                 long int, int *))find_fn("initgroups_dyn");
     330             :         NSS_STATUS status;
     331             : 
     332         692 :         if (!_nss_initgroups)
     333           0 :                 return NSS_STATUS_UNAVAIL;
     334             : 
     335         692 :         status = _nss_initgroups(user, group, start, size, groups, 0, &nss_errno);
     336         692 :         if (status != NSS_STATUS_SUCCESS) {
     337           0 :                 report_nss_error("initgroups", status);
     338             :         }
     339         692 :         return status;
     340             : }
     341             : 
     342        2076 : static void print_passwd(struct passwd *pwd)
     343             : {
     344        5106 :         printf("%s:%s:%lu:%lu:%s:%s:%s\n",
     345             :                pwd->pw_name,
     346             :                pwd->pw_passwd,
     347        2076 :                (unsigned long)pwd->pw_uid,
     348        2076 :                (unsigned long)pwd->pw_gid,
     349             :                pwd->pw_gecos,
     350             :                pwd->pw_dir,
     351             :                pwd->pw_shell);
     352        2076 : }
     353             : 
     354        3144 : static void print_group(struct group *grp)
     355             : {
     356             :         int i;
     357        3144 :         printf("%s:%s:%lu:",
     358             :                grp->gr_name,
     359             :                grp->gr_passwd,
     360        3144 :                (unsigned long)grp->gr_gid);
     361             : 
     362        3144 :         if (!grp->gr_mem[0]) {
     363        3144 :                 printf("\n");
     364        3144 :                 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         692 : static void nss_test_initgroups(char *name, gid_t gid)
     374             : {
     375         692 :         long int size = 16;
     376         692 :         long int start = 1;
     377         692 :         gid_t *groups = NULL;
     378             :         int i;
     379             :         NSS_STATUS status;
     380             : 
     381         692 :         groups = (gid_t *)malloc(sizeof(gid_t) * size);
     382         692 :         if (groups == NULL) {
     383           0 :                 printf("Unable to allocate memory for groups\n");
     384           0 :                 return;
     385             :         }
     386         692 :         groups[0] = gid;
     387             : 
     388         692 :         status = nss_initgroups(name, gid, &groups, &start, &size);
     389         692 :         if (status == NSS_STATUS_UNAVAIL) {
     390           0 :                 printf("No initgroups fn\n");
     391           0 :                 return;
     392             :         }
     393             : 
     394        1768 :         for (i=0; i<start-1; i++) {
     395        1076 :                 printf("%lu, ", (unsigned long)groups[i]);
     396             :         }
     397         692 :         printf("%lu\n", (unsigned long)groups[i]);
     398             : }
     399             : 
     400             : 
     401          10 : static void nss_test_users(void)
     402             : {
     403             :         struct passwd *pwd;
     404             : 
     405          10 :         samba_nss_setpwent();
     406             :         /* loop over all users */
     407         709 :         while ((pwd = nss_getpwent())) {
     408         692 :                 printf("Testing user %s\n", pwd->pw_name);
     409         692 :                 printf("getpwent:   "); print_passwd(pwd);
     410         692 :                 pwd = nss_getpwuid(pwd->pw_uid);
     411         692 :                 if (!pwd) {
     412           0 :                         total_errors++;
     413           0 :                         printf("ERROR: can't getpwuid\n");
     414           0 :                         continue;
     415             :                 }
     416         692 :                 printf("getpwuid:   "); print_passwd(pwd);
     417         692 :                 pwd = nss_getpwnam(pwd->pw_name);
     418         692 :                 if (!pwd) {
     419           0 :                         total_errors++;
     420           0 :                         printf("ERROR: can't getpwnam\n");
     421           0 :                         continue;
     422             :                 }
     423         692 :                 printf("getpwnam:   "); print_passwd(pwd);
     424         692 :                 printf("initgroups: "); nss_test_initgroups(pwd->pw_name, pwd->pw_gid);
     425         692 :                 printf("\n");
     426             :         }
     427          10 :         samba_nss_endpwent();
     428          10 : }
     429             : 
     430          10 : static void nss_test_groups(void)
     431             : {
     432             :         struct group *grp;
     433             : 
     434          10 :         samba_nss_setgrent();
     435             :         /* loop over all groups */
     436        1065 :         while ((grp = nss_getgrent())) {
     437        1048 :                 printf("Testing group %s\n", grp->gr_name);
     438        1048 :                 printf("getgrent: "); print_group(grp);
     439        1048 :                 grp = nss_getgrnam(grp->gr_name);
     440        1048 :                 if (!grp) {
     441           0 :                         total_errors++;
     442           0 :                         printf("ERROR: can't getgrnam\n");
     443           0 :                         continue;
     444             :                 }
     445        1048 :                 printf("getgrnam: "); print_group(grp);
     446        1048 :                 grp = nss_getgrgid(grp->gr_gid);
     447        1048 :                 if (!grp) {
     448           0 :                         total_errors++;
     449           0 :                         printf("ERROR: can't getgrgid\n");
     450           0 :                         continue;
     451             :                 }
     452        1048 :                 printf("getgrgid: "); print_group(grp);
     453        1048 :                 printf("\n");
     454             :         }
     455          10 :         samba_nss_endgrent();
     456          10 : }
     457             : 
     458          10 : static void nss_test_errors(void)
     459             : {
     460             :         struct passwd *pwd;
     461             :         struct group *grp;
     462             : 
     463          10 :         pwd = getpwnam("nosuchname");
     464          10 :         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          10 :         pwd = getpwuid(0xFFF0);
     470          10 :         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          10 :         grp = getgrnam("nosuchgroup");
     476          10 :         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          10 :         grp = getgrgid(0xFFF0);
     482          10 :         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          10 : }
     487             : 
     488          10 :  int main(int argc, char *argv[])
     489             : {
     490          10 :         if (argc > 1) so_path = argv[1];
     491          10 :         if (argc > 2) nss_name = argv[2];
     492             : 
     493          10 :         nss_test_users();
     494          10 :         nss_test_groups();
     495          10 :         nss_test_errors();
     496             : 
     497          10 :         printf("total_errors=%d\n", total_errors);
     498             : 
     499          10 :         return total_errors;
     500             : }

Generated by: LCOV version 1.13