Ignore:
Timestamp:
Nov 24, 2016, 1:14:11 PM (9 years ago)
Author:
Silvan Scherrer
Message:

Samba Server: update vendor to version 4.4.3

File:
1 edited

Legend:

Unmodified
Added
Removed
  • vendor/current/nsswitch/wins.c

    r740 r988  
    2121#include "includes.h"
    2222#include "nsswitch/winbind_nss.h"
     23#include "nsswitch/libwbclient/wbclient.h"
    2324
    2425#ifdef HAVE_NS_API_H
     
    3839#define INADDRSZ 4
    3940#endif
    40 
    41 static int initialised;
    4241
    4342NSS_STATUS _nss_wins_gethostbyname_r(const char *hostname, struct hostent *he,
     
    4645                           char *buffer, size_t buflen, int *h_errnop);
    4746
    48 static void nss_wins_init(void)
    49 {
    50         initialised = 1;
    51         load_case_tables_library();
    52         lp_set_cmdline("log level", "0");
    53 
    54         TimeInit();
    55         setup_logging("nss_wins",False);
    56         lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
    57         load_interfaces();
    58 }
    59 
    60 static struct in_addr *lookup_byname_backend(const char *name, int *count)
    61 {
    62         struct ip_service *address = NULL;
    63         struct in_addr *ret = NULL;
    64         int j;
    65 
    66         if (!initialised) {
    67                 nss_wins_init();
    68         }
    69 
    70         *count = 0;
    71 
    72         /* always try with wins first */
    73         if (NT_STATUS_IS_OK(resolve_wins(name,0x00,&address,count))) {
    74                 if ( (ret = SMB_MALLOC_P(struct in_addr)) == NULL ) {
    75                         free( address );
    76                         return NULL;
    77                 }
    78                 if (address[0].ss.ss_family != AF_INET) {
    79                         free(address);
    80                         free(ret);
    81                         return NULL;
    82                 }
    83                 *ret = ((struct sockaddr_in *)(void *)&address[0].ss)
    84                         ->sin_addr;
    85                 free( address );
    86                 return ret;
    87         }
    88 
    89         /* uggh, we have to broadcast to each interface in turn */
    90         for (j=iface_count() - 1;j >= 0;j--) {
    91                 const struct in_addr *bcast = iface_n_bcast_v4(j);
    92                 struct sockaddr_storage ss;
    93                 struct sockaddr_storage *pss;
    94                 NTSTATUS status;
    95 
    96                 if (!bcast) {
    97                         continue;
    98                 }
    99                 in_addr_to_sockaddr_storage(&ss, *bcast);
    100                 status = name_query(name, 0x00, True, True, &ss,
    101                                     NULL, &pss, count, NULL);
    102                 if (NT_STATUS_IS_OK(status) && (*count > 0)) {
    103                         if ((ret = SMB_MALLOC_P(struct in_addr)) == NULL) {
    104                                 return NULL;
    105                         }
    106                         *ret = ((struct sockaddr_in *)pss)->sin_addr;
    107                         TALLOC_FREE(pss);
    108                         break;
    109                 }
    110         }
    111 
    112         return ret;
     47static char *lookup_byname_backend(const char *name)
     48{
     49        const char *p;
     50        char *ip, *ipp;
     51        size_t nbt_len;
     52        wbcErr result;
     53
     54        nbt_len = strlen(name);
     55        if (nbt_len > MAX_NETBIOSNAME_LEN - 1) {
     56                return NULL;
     57        }
     58        p = strchr(name, '.');
     59        if (p != NULL) {
     60                return NULL;
     61        }
     62
     63        result = wbcResolveWinsByName(name, &ip);
     64        if (result != WBC_ERR_SUCCESS) {
     65                return NULL;
     66        }
     67
     68        ipp = strchr(ip, '\t');
     69        if (ipp != NULL) {
     70                *ipp = '\0';
     71        }
     72
     73        return ip;
    11374}
    11475
    11576#ifdef HAVE_NS_API_H
    11677
    117 static struct node_status *lookup_byaddr_backend(char *addr, int *count)
    118 {
    119         struct sockaddr_storage ss;
    120         struct nmb_name nname;
    121         struct node_status *result;
    122         NTSTATUS status;
    123 
    124         if (!initialised) {
    125                 nss_wins_init();
    126         }
    127 
    128         make_nmb_name(&nname, "*", 0);
    129         if (!interpret_string_addr(&ss, addr, AI_NUMERICHOST)) {
    130                 return NULL;
    131         }
    132         status = node_status_query(NULL, &nname, &ss, &result, count, NULL);
    133         if (!NT_STATUS_IS_OK(status)) {
    134                 return NULL;
    135         }
    136 
    137         return result;
     78static char *lookup_byaddr_backend(const char *ip)
     79{
     80        wbcErr result;
     81        char *name = NULL;
     82
     83        result = wbcResolveWinsByIP(ip, &name);
     84        if (result != WBC_ERR_SUCCESS) {
     85                return NULL;
     86        }
     87
     88        return name;
    13889}
    13990
     
    14293int init(void)
    14394{
     95        bool ok;
     96
    14497        nsd_logprintf(NSD_LOG_MIN, "entering init (wins)\n");
    145         nss_wins_init();
     98
     99        ok = nss_wins_init();
     100        if (!ok) {
     101                return NSD_ERROR;
     102        }
     103
    146104        return NSD_OK;
    147105}
     
    152110        char *key;
    153111        char *addr;
    154         struct in_addr *ip_list;
    155         struct node_status *status;
    156112        int i, count, len, size;
    157113        char response[1024];
     
    181137         * ip_address[ ip_address]*\tname[ alias]*
    182138         */
    183         if (StrCaseCmp(map,"hosts.byaddr") == 0) {
    184                 if ( status = lookup_byaddr_backend(key, &count)) {
    185                     size = strlen(key) + 1;
    186                     if (size > len) {
    187                         talloc_free(status);
    188                         return NSD_ERROR;
    189                     }
    190                     len -= size;
    191                     strncat(response,key,size);
    192                     strncat(response,"\t",1);
    193                     for (i = 0; i < count; i++) {
    194                         /* ignore group names */
    195                         if (status[i].flags & 0x80) continue;
    196                         if (status[i].type == 0x20) {
    197                                 size = sizeof(status[i].name) + 1;
    198                                 if (size > len) {
    199                                     talloc_free(status);
    200                                     return NSD_ERROR;
    201                                 }
    202                                 len -= size;
    203                                 strncat(response, status[i].name, size);
    204                                 strncat(response, " ", 1);
    205                                 found = True;
     139        if (strcasecmp_m(map,"hosts.byaddr") == 0) {
     140                char *name;
     141
     142                name = lookup_byaddr_backend(key);
     143                if (name != NULL) {
     144                        size = strlen(key) + 1;
     145                        if (size > len) {
     146                                return NSD_ERROR;
    206147                        }
    207                     }
    208                     response[strlen(response)-1] = '\n';
    209                     talloc_free(status);
     148                        len -= size;
     149                        strncat(response,key,size);
     150                        strncat(response,"\t",1);
     151
     152                        size = strlen(name) + 1;
     153                        if (size > len) {
     154                                return NSD_ERROR;
     155                        }
     156                        len -= size;
     157                        strncat(response, name, size);
     158                        strncat(response, " ", 1);
     159                        found = True;
    210160                }
    211         } else if (StrCaseCmp(map,"hosts.byname") == 0) {
    212             if (ip_list = lookup_byname_backend(key, &count)) {
    213                 for (i = count; i ; i--) {
    214                     addr = inet_ntoa(ip_list[i-1]);
    215                     size = strlen(addr) + 1;
    216                     if (size > len) {
    217                         free(ip_list);
    218                         return NSD_ERROR;
    219                     }
    220                     len -= size;
    221                     if (i != 0)
    222                         response[strlen(response)-1] = ' ';
    223                     strncat(response,addr,size);
    224                     strncat(response,"\t",1);
     161                response[strlen(response)-1] = '\n';
     162        } else if (strcasecmp_m(map,"hosts.byname") == 0) {
     163                char *ip;
     164
     165                ip = lookup_byname_backend(key);
     166                if (ip != NULL) {
     167                        size = strlen(ip) + 1;
     168                        if (size > len) {
     169                                wbcFreeMemory(ip);
     170                                return NSD_ERROR;
     171                        }
     172                        len -= size;
     173                        strncat(response,ip,size);
     174                        strncat(response,"\t",1);
     175                        size = strlen(key) + 1;
     176                        wbcFreeMemory(ip);
     177                        if (size > len) {
     178                                return NSD_ERROR;
     179                        }
     180                        strncat(response,key,size);
     181                        strncat(response,"\n",1);
     182
     183                        found = True;
    225184                }
    226                 size = strlen(key) + 1;
    227                 if (size > len) {
    228                     free(ip_list);
    229                     return NSD_ERROR;
    230                 }
    231                 strncat(response,key,size);
    232                 strncat(response,"\n",1);
    233                 found = True;
    234                 free(ip_list);
    235             }
    236185        }
    237186
     
    252201   functions. */
    253202
    254 static char *get_static(char **buffer, size_t *buflen, int len)
     203static char *get_static(char **buffer, size_t *buflen, size_t len)
    255204{
    256205        char *result;
     
    281230{
    282231        NSS_STATUS nss_status = NSS_STATUS_SUCCESS;
    283         struct in_addr *ip_list;
    284         int i, count;
     232        char *ip;
     233        struct in_addr in;
     234        int i;
    285235        fstring name;
    286236        size_t namelen;
    287         TALLOC_CTX *frame;
     237        int rc;
    288238
    289239#if HAVE_PTHREAD
     
    291241#endif
    292242
    293         frame = talloc_stackframe();
    294 
    295243        memset(he, '\0', sizeof(*he));
    296244        fstrcpy(name, hostname);
     
    298246        /* Do lookup */
    299247
    300         ip_list = lookup_byname_backend(name, &count);
    301 
    302         if (!ip_list) {
     248        ip = lookup_byname_backend(name);
     249        if (ip == NULL) {
    303250                nss_status = NSS_STATUS_NOTFOUND;
    304251                goto out;
    305252        }
    306253
     254        rc = inet_pton(AF_INET, ip, &in);
     255        wbcFreeMemory(ip);
     256        if (rc == 0) {
     257                nss_status = NSS_STATUS_TRYAGAIN;
     258                goto out;
     259        }
     260
    307261        /* Copy h_name */
    308262
     
    310264
    311265        if ((he->h_name = get_static(&buffer, &buflen, namelen)) == NULL) {
    312                 free(ip_list);
    313266                nss_status = NSS_STATUS_TRYAGAIN;
    314267                goto out;
     
    323276
    324277        if (get_static(&buffer, &buflen, i) == NULL) {
    325                 free(ip_list);
    326278                nss_status = NSS_STATUS_TRYAGAIN;
    327279                goto out;
     
    329281
    330282        if ((he->h_addr_list = (char **)get_static(
    331                      &buffer, &buflen, (count + 1) * sizeof(char *))) == NULL) {
    332                 free(ip_list);
    333                 nss_status = NSS_STATUS_TRYAGAIN;
    334                 goto out;
    335         }
    336 
    337         for (i = 0; i < count; i++) {
    338                 if ((he->h_addr_list[i] = get_static(&buffer, &buflen,
    339                                                      INADDRSZ)) == NULL) {
    340                         free(ip_list);
    341                         nss_status = NSS_STATUS_TRYAGAIN;
    342                         goto out;
    343                 }
    344                 memcpy(he->h_addr_list[i], &ip_list[i], INADDRSZ);
    345         }
    346 
    347         he->h_addr_list[count] = NULL;
    348 
    349         free(ip_list);
     283                     &buffer, &buflen, 2 * sizeof(char *))) == NULL) {
     284                nss_status = NSS_STATUS_TRYAGAIN;
     285                goto out;
     286        }
     287
     288        if ((he->h_addr_list[0] = get_static(&buffer, &buflen,
     289                                             INADDRSZ)) == NULL) {
     290                nss_status = NSS_STATUS_TRYAGAIN;
     291                goto out;
     292        }
     293
     294        memcpy(he->h_addr_list[0], &in, INADDRSZ);
     295
     296        he->h_addr_list[1] = NULL;
    350297
    351298        /* Set h_addr_type and h_length */
     
    375322
    376323  out:
    377 
    378         TALLOC_FREE(frame);
    379324
    380325#if HAVE_PTHREAD
Note: See TracChangeset for help on using the changeset viewer.