s3: Fix Coverity ID 2222: RESOURCE_LEAK
[samba.git] / nsswitch / wbinfo.c
index 3d14d21ad62b6df3158e70edf58dd54064a41f19..9efd40b56a0842a8976b9ce908e86c1c8a54be6e 100644 (file)
@@ -357,16 +357,9 @@ static bool wbinfo_get_usersids(const char *user_sid_str)
        }
 
        for (i = 0; i < num_sids; i++) {
-               char *str = NULL;
-               wbc_status = wbcSidToString(&sids[i], &str);
-               if (!WBC_ERROR_IS_OK(wbc_status)) {
-                       d_fprintf(stderr, "failed to call wbcSidToString: "
-                                 "%s\n", wbcErrorString(wbc_status));
-                       wbcFreeMemory(sids);
-                       return false;
-               }
+               char str[WBC_SID_STRING_BUFLEN];
+               wbcSidToStringBuf(&sids[i], str, sizeof(str));
                d_printf("%s\n", str);
-               wbcFreeMemory(str);
        }
 
        wbcFreeMemory(sids);
@@ -385,7 +378,7 @@ static bool wbinfo_get_userdomgroups(const char *user_sid_str)
 
        wbc_status = wbcStringToSid(user_sid_str, &user_sid);
        if (!WBC_ERROR_IS_OK(wbc_status)) {
-               d_fprintf(stderr, "failed to call wbcSidToString: %s\n",
+               d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
                          wbcErrorString(wbc_status));
                return false;
        }
@@ -398,16 +391,9 @@ static bool wbinfo_get_userdomgroups(const char *user_sid_str)
        }
 
        for (i = 0; i < num_sids; i++) {
-               char *str = NULL;
-               wbc_status = wbcSidToString(&sids[i], &str);
-               if (!WBC_ERROR_IS_OK(wbc_status)) {
-                       d_fprintf(stderr, "failed to call wbcSidToString: "
-                                 "%s\n", wbcErrorString(wbc_status));
-                       wbcFreeMemory(sids);
-                       return false;
-               }
+               char str[WBC_SID_STRING_BUFLEN];
+               wbcSidToStringBuf(&sids[i], str, sizeof(str));
                d_printf("%s\n", str);
-               wbcFreeMemory(str);
        }
 
        wbcFreeMemory(sids);
@@ -424,7 +410,7 @@ static bool wbinfo_get_sidaliases(const char *domain,
        struct wbcDomainSid user_sid;
        uint32_t *alias_rids = NULL;
        uint32_t num_alias_rids;
-       char *domain_sid_str = NULL;
+       char domain_sid_str[WBC_SID_STRING_BUFLEN];
 
        /* Send request */
        if ((domain == NULL) || (strequal(domain, ".")) ||
@@ -451,10 +437,7 @@ static bool wbinfo_get_sidaliases(const char *domain,
                goto done;
        }
 
-       wbc_status = wbcSidToString(&dinfo->sid, &domain_sid_str);
-       if (!WBC_ERROR_IS_OK(wbc_status)) {
-               goto done;
-       }
+       wbcSidToStringBuf(&dinfo->sid, domain_sid_str, sizeof(domain_sid_str));
 
        for (i = 0; i < num_alias_rids; i++) {
                d_printf("%s-%d\n", domain_sid_str, alias_rids[i]);
@@ -463,12 +446,7 @@ static bool wbinfo_get_sidaliases(const char *domain,
        wbcFreeMemory(alias_rids);
 
 done:
-       if (domain_sid_str) {
-               wbcFreeMemory(domain_sid_str);
-       }
-       if (dinfo) {
-               wbcFreeMemory(dinfo);
-       }
+       wbcFreeMemory(dinfo);
        return (WBC_ERR_SUCCESS == wbc_status);
 }
 
@@ -589,6 +567,8 @@ static bool wbinfo_list_domains(bool list_all_domains, bool verbose)
                d_printf("\n");
        }
 
+       wbcFreeMemory(domain_list);
+
        return true;
 }
 
@@ -641,6 +621,8 @@ static bool wbinfo_show_onlinestatus(const char *domain)
                         is_offline ? "offline" : "online" );
        }
 
+       wbcFreeMemory(domain_list);
+
        return true;
 }
 
@@ -651,7 +633,7 @@ static bool wbinfo_domain_info(const char *domain)
 {
        wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
        struct wbcDomainInfo *dinfo = NULL;
-       char *sid_str = NULL;
+       char sid_str[WBC_SID_STRING_BUFLEN];
 
        if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0')){
                domain = get_winbind_domain();
@@ -666,13 +648,7 @@ static bool wbinfo_domain_info(const char *domain)
                return false;
        }
 
-       wbc_status = wbcSidToString(&dinfo->sid, &sid_str);
-       if (!WBC_ERROR_IS_OK(wbc_status)) {
-               d_fprintf(stderr, "failed to call wbcSidToString: %s\n",
-                         wbcErrorString(wbc_status));
-               wbcFreeMemory(dinfo);
-               return false;
-       }
+       wbcSidToStringBuf(&dinfo->sid, sid_str, sizeof(sid_str));
 
        /* Display response */
 
@@ -691,7 +667,6 @@ static bool wbinfo_domain_info(const char *domain)
                 (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_PRIMARY) ?
                 "Yes" : "No");
 
-       wbcFreeMemory(sid_str);
        wbcFreeMemory(dinfo);
 
        return true;
@@ -878,7 +853,7 @@ static bool wbinfo_uid_to_sid(uid_t uid)
 {
        wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
        struct wbcDomainSid sid;
-       char *sid_str = NULL;
+       char sid_str[WBC_SID_STRING_BUFLEN];
 
        /* Send request */
 
@@ -889,19 +864,12 @@ static bool wbinfo_uid_to_sid(uid_t uid)
                return false;
        }
 
-       wbc_status = wbcSidToString(&sid, &sid_str);
-       if (!WBC_ERROR_IS_OK(wbc_status)) {
-               d_fprintf(stderr, "failed to call wbcSidToString: %s\n",
-                         wbcErrorString(wbc_status));
-               return false;
-       }
+       wbcSidToStringBuf(&sid, sid_str, sizeof(sid_str));
 
        /* Display response */
 
        d_printf("%s\n", sid_str);
 
-       wbcFreeMemory(sid_str);
-
        return true;
 }
 
@@ -911,7 +879,7 @@ static bool wbinfo_gid_to_sid(gid_t gid)
 {
        wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
        struct wbcDomainSid sid;
-       char *sid_str = NULL;
+       char sid_str[WBC_SID_STRING_BUFLEN];
 
        /* Send request */
 
@@ -922,19 +890,12 @@ static bool wbinfo_gid_to_sid(gid_t gid)
                return false;
        }
 
-       wbc_status = wbcSidToString(&sid, &sid_str);
-       if (!WBC_ERROR_IS_OK(wbc_status)) {
-               d_fprintf(stderr, "failed to call wbcSidToString: %s\n",
-                         wbcErrorString(wbc_status));
-               return false;
-       }
+       wbcSidToStringBuf(&sid, sid_str, sizeof(sid_str));
 
        /* Display response */
 
        d_printf("%s\n", sid_str);
 
-       wbcFreeMemory(sid_str);
-
        return true;
 }
 
@@ -1040,6 +1001,64 @@ static bool wbinfo_allocate_gid(void)
        return true;
 }
 
+static bool wbinfo_set_uid_mapping(uid_t uid, const char *sid_str)
+{
+       wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
+       struct wbcDomainSid sid;
+
+       /* Send request */
+
+       wbc_status = wbcStringToSid(sid_str, &sid);
+       if (!WBC_ERROR_IS_OK(wbc_status)) {
+               d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
+                         wbcErrorString(wbc_status));
+               return false;
+       }
+
+       wbc_status = wbcSetUidMapping(uid, &sid);
+       if (!WBC_ERROR_IS_OK(wbc_status)) {
+               d_fprintf(stderr, "failed to call wbcSetUidMapping: %s\n",
+                         wbcErrorString(wbc_status));
+               return false;
+       }
+
+       /* Display response */
+
+       d_printf("uid %u now mapped to sid %s\n",
+               (unsigned int)uid, sid_str);
+
+       return true;
+}
+
+static bool wbinfo_set_gid_mapping(gid_t gid, const char *sid_str)
+{
+       wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
+       struct wbcDomainSid sid;
+
+       /* Send request */
+
+       wbc_status = wbcStringToSid(sid_str, &sid);
+       if (!WBC_ERROR_IS_OK(wbc_status)) {
+               d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
+                         wbcErrorString(wbc_status));
+               return false;
+       }
+
+       wbc_status = wbcSetGidMapping(gid, &sid);
+       if (!WBC_ERROR_IS_OK(wbc_status)) {
+               d_fprintf(stderr, "failed to call wbcSetGidMapping: %s\n",
+                         wbcErrorString(wbc_status));
+               return false;
+       }
+
+       /* Display response */
+
+       d_printf("gid %u now mapped to sid %s\n",
+               (unsigned int)gid, sid_str);
+
+       return true;
+}
+
 static bool wbinfo_remove_uid_mapping(uid_t uid, const char *sid_str)
 {
        wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
@@ -1238,18 +1257,10 @@ static bool wbinfo_lookuprids(const char *domain, const char *arg)
 
        ret = true;
 done:
-       if (dinfo) {
-               wbcFreeMemory(dinfo);
-       }
-       if (domain_name) {
-               wbcFreeMemory(domain_name);
-       }
-       if (names) {
-               wbcFreeMemory(names);
-       }
-       if (types) {
-               wbcFreeMemory(types);
-       }
+       wbcFreeMemory(dinfo);
+       wbcFreeMemory(domain_name);
+       wbcFreeMemory(names);
+       wbcFreeMemory(types);
        TALLOC_FREE(mem_ctx);
        return ret;
 }
@@ -1260,7 +1271,7 @@ static bool wbinfo_lookupname(const char *full_name)
 {
        wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
        struct wbcDomainSid sid;
-       char *sid_str;
+       char sid_str[WBC_SID_STRING_BUFLEN];
        enum wbcSidType type;
        fstring domain_name;
        fstring account_name;
@@ -1278,19 +1289,12 @@ static bool wbinfo_lookupname(const char *full_name)
                return false;
        }
 
-       wbc_status = wbcSidToString(&sid, &sid_str);
-       if (!WBC_ERROR_IS_OK(wbc_status)) {
-               d_fprintf(stderr, "failed to call wbcSidToString: %s\n",
-                         wbcErrorString(wbc_status));
-               return false;
-       }
+       wbcSidToStringBuf(&sid, sid_str, sizeof(sid_str));
 
        /* Display response */
 
        d_printf("%s %s (%d)\n", sid_str, wbcSidTypeString(type), type);
 
-       wbcFreeMemory(sid_str);
-
        return true;
 }
 
@@ -1894,6 +1898,8 @@ enum {
        OPT_USERSIDS,
        OPT_ALLOCATE_UID,
        OPT_ALLOCATE_GID,
+       OPT_SET_UID_MAPPING,
+       OPT_SET_GID_MAPPING,
        OPT_REMOVE_UID_MAPPING,
        OPT_REMOVE_GID_MAPPING,
        OPT_SEPARATOR,
@@ -1956,6 +1962,8 @@ int main(int argc, char **argv, char **envp)
                  "Get a new UID out of idmap" },
                { "allocate-gid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_GID,
                  "Get a new GID out of idmap" },
+               { "set-uid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_SET_UID_MAPPING, "Create or modify uid to sid mapping in idmap", "UID,SID" },
+               { "set-gid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_SET_GID_MAPPING, "Create or modify gid to sid mapping in idmap", "GID,SID" },
                { "remove-uid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_REMOVE_UID_MAPPING, "Remove uid to sid mapping in idmap", "UID,SID" },
                { "remove-gid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_REMOVE_GID_MAPPING, "Remove gid to sid mapping in idmap", "GID,SID" },
                { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
@@ -2157,6 +2165,26 @@ int main(int argc, char **argv, char **envp)
                                goto done;
                        }
                        break;
+               case OPT_SET_UID_MAPPING:
+                       if (!parse_mapping_arg(string_arg, &int_subarg,
+                               &string_subarg) ||
+                           !wbinfo_set_uid_mapping(int_subarg, string_subarg))
+                       {
+                               d_fprintf(stderr, "Could not create or modify "
+                                         "uid to sid mapping\n");
+                               goto done;
+                       }
+                       break;
+               case OPT_SET_GID_MAPPING:
+                       if (!parse_mapping_arg(string_arg, &int_subarg,
+                               &string_subarg) ||
+                           !wbinfo_set_gid_mapping(int_subarg, string_subarg))
+                       {
+                               d_fprintf(stderr, "Could not create or modify "
+                                         "gid to sid mapping\n");
+                               goto done;
+                       }
+                       break;
                case OPT_REMOVE_UID_MAPPING:
                        if (!parse_mapping_arg(string_arg, &int_subarg,
                                &string_subarg) ||