Commit 7c074ba Move sized_output_name() and sized_domain_name() into responder common code

5 files Authored and Committed by jhrozek 4 days ago
Move sized_output_name() and sized_domain_name() into responder common code

These functions are used to format a name into a format that the user
configured for output, including case sensitiveness, replacing
whitespace and qualified format. They were used only in the NSS
responder, which typically returns strings to the NSS client library and
then the user.

But it makes sense to just reuse the same code in the IFP responder as
well, since it does essentially the same job.

The patch also renames sized_member_name to sized_domain_name.
Previously, the function was only used to format a group member, the IFP
responder would use the same function to format a group the user is a
member of.

Related to:
    https://pagure.io/SSSD/sssd/issue/3268

Reviewed-by: Pavel Březina <pbrezina@redhat.com>

    
 1 @@ -393,4 +393,25 @@
 2   
 3   errno_t sss_resp_populate_cr_domains(struct resp_ctx *rctx);
 4   
 5 + /**
 6 +  * Helper functions to format output names
 7 +  */
 8 + 
 9 + /* Format orig_name into a sized_string in output format as prescribed
10 +  * by the name_dom domain
11 +  */
12 + int sized_output_name(TALLOC_CTX *mem_ctx,
13 +                       struct resp_ctx *rctx,
14 +                       const char *orig_name,
15 +                       struct sss_domain_info *name_dom,
16 +                       struct sized_string **_name);
17 + 
18 + /* Format orig_name into a sized_string in output format as prescribed
19 +  * by the domain read from the fully qualified name.
20 +  */
21 + int sized_domain_name(TALLOC_CTX *mem_ctx,
22 +                       struct resp_ctx *rctx,
23 +                       const char *member_name,
24 +                       struct sized_string **_name);
25 + 
26   #endif /* __SSS_RESPONDER_H__ */
 1 @@ -1651,3 +1651,93 @@
 2   
 3       return ret;
 4   }
 5 + 
 6 + /**
 7 +  * Helper functions to format output names
 8 +  */
 9 + int sized_output_name(TALLOC_CTX *mem_ctx,
10 +                       struct resp_ctx *rctx,
11 +                       const char *orig_name,
12 +                       struct sss_domain_info *name_dom,
13 +                       struct sized_string **_name)
14 + {
15 +     TALLOC_CTX *tmp_ctx = NULL;
16 +     errno_t ret;
17 +     char *username;
18 +     struct sized_string *name;
19 + 
20 +     tmp_ctx = talloc_new(NULL);
21 +     if (tmp_ctx == NULL) {
22 +         return ENOMEM;
23 +     }
24 + 
25 +     username = sss_output_name(tmp_ctx, orig_name, name_dom->case_preserve,
26 +                                rctx->override_space);
27 +     if (username == NULL) {
28 +         ret = EIO;
29 +         goto done;
30 +     }
31 + 
32 +     if (name_dom->fqnames) {
33 +         username = sss_tc_fqname(tmp_ctx, name_dom->names, name_dom, username);
34 +         if (username == NULL) {
35 +             DEBUG(SSSDBG_CRIT_FAILURE, "sss_replace_space failed\n");
36 +             ret = EIO;
37 +             goto done;
38 +         }
39 +     }
40 + 
41 +     name = talloc_zero(tmp_ctx, struct sized_string);
42 +     if (name == NULL) {
43 +         ret = ENOMEM;
44 +         goto done;
45 +     }
46 + 
47 +     to_sized_string(name, username);
48 +     name->str = talloc_steal(name, username);
49 +     *_name = talloc_steal(mem_ctx, name);
50 +     ret = EOK;
51 + done:
52 +     talloc_zfree(tmp_ctx);
53 +     return ret;
54 + }
55 + 
56 + int sized_domain_name(TALLOC_CTX *mem_ctx,
57 +                       struct resp_ctx *rctx,
58 +                       const char *member_name,
59 +                       struct sized_string **_name)
60 + {
61 +     TALLOC_CTX *tmp_ctx = NULL;
62 +     errno_t ret;
63 +     char *domname;
64 +     struct sss_domain_info *member_dom;
65 + 
66 +     tmp_ctx = talloc_new(NULL);
67 +     if (tmp_ctx == NULL) {
68 +         return ENOMEM;
69 +     }
70 + 
71 +     ret = sss_parse_internal_fqname(tmp_ctx, member_name, NULL, &domname);
72 +     if (ret != EOK) {
73 +         DEBUG(SSSDBG_CRIT_FAILURE, "sss_parse_internal_fqname failed\n");
74 +         goto done;
75 +     }
76 + 
77 +     if (domname == NULL) {
78 +         ret = ERR_WRONG_NAME_FORMAT;
79 +         goto done;
80 +     }
81 + 
82 +     member_dom = find_domain_by_name(get_domains_head(rctx->domains),
83 +                                      domname, true);
84 +     if (member_dom == NULL) {
85 +         ret = ERR_DOMAIN_NOT_FOUND;
86 +         goto done;
87 +     }
88 + 
89 +     ret = sized_output_name(mem_ctx, rctx, member_name,
90 +                             member_dom, _name);
91 + done:
92 +     talloc_free(tmp_ctx);
93 +     return ret;
94 + }
 1 @@ -140,17 +140,6 @@
 2   nss_get_name_from_msg(struct sss_domain_info *domain,
 3                         struct ldb_message *msg);
 4   
 5 - int sized_output_name(TALLOC_CTX *mem_ctx,
 6 -                       struct resp_ctx *rctx,
 7 -                       const char *orig_name,
 8 -                       struct sss_domain_info *name_dom,
 9 -                       struct sized_string **_name);
10 - 
11 - int sized_member_name(TALLOC_CTX *mem_ctx,
12 -                       struct resp_ctx *rctx,
13 -                       const char *member_name,
14 -                       struct sized_string **_name);
15 - 
16   const char *
17   nss_get_pwfield(struct nss_ctx *nctx,
18                   struct sss_domain_info *dom);
1 @@ -163,7 +163,7 @@
2                   }
3               }
4   
5 -             ret = sized_member_name(tmp_ctx, rctx, member_name, &name);
6 +             ret = sized_domain_name(tmp_ctx, rctx, member_name, &name);
7               if (ret != EOK) {
8                   DEBUG(SSSDBG_OP_FAILURE, "Unable to get sized name [%d]: %s\n",
9                         ret, sss_strerror(ret));
 1 @@ -53,93 +53,6 @@
 2       return ldb_msg_find_attr_as_string(msg, SYSDB_NAME, NULL);
 3   }
 4   
 5 - int sized_output_name(TALLOC_CTX *mem_ctx,
 6 -                       struct resp_ctx *rctx,
 7 -                       const char *orig_name,
 8 -                       struct sss_domain_info *name_dom,
 9 -                       struct sized_string **_name)
10 - {
11 -     TALLOC_CTX *tmp_ctx = NULL;
12 -     errno_t ret;
13 -     char *username;
14 -     struct sized_string *name;
15 - 
16 -     tmp_ctx = talloc_new(NULL);
17 -     if (tmp_ctx == NULL) {
18 -         return ENOMEM;
19 -     }
20 - 
21 -     username = sss_output_name(tmp_ctx, orig_name, name_dom->case_preserve,
22 -                                rctx->override_space);
23 -     if (username == NULL) {
24 -         ret = EIO;
25 -         goto done;
26 -     }
27 - 
28 -     if (name_dom->fqnames) {
29 -         username = sss_tc_fqname(tmp_ctx, name_dom->names, name_dom, username);
30 -         if (username == NULL) {
31 -             DEBUG(SSSDBG_CRIT_FAILURE, "sss_replace_space failed\n");
32 -             ret = EIO;
33 -             goto done;
34 -         }
35 -     }
36 - 
37 -     name = talloc_zero(tmp_ctx, struct sized_string);
38 -     if (name == NULL) {
39 -         ret = ENOMEM;
40 -         goto done;
41 -     }
42 - 
43 -     to_sized_string(name, username);
44 -     name->str = talloc_steal(name, username);
45 -     *_name = talloc_steal(mem_ctx, name);
46 -     ret = EOK;
47 - done:
48 -     talloc_zfree(tmp_ctx);
49 -     return ret;
50 - }
51 - 
52 - int sized_member_name(TALLOC_CTX *mem_ctx,
53 -                       struct resp_ctx *rctx,
54 -                       const char *member_name,
55 -                       struct sized_string **_name)
56 - {
57 -     TALLOC_CTX *tmp_ctx = NULL;
58 -     errno_t ret;
59 -     char *domname;
60 -     struct sss_domain_info *member_dom;
61 - 
62 -     tmp_ctx = talloc_new(NULL);
63 -     if (tmp_ctx == NULL) {
64 -         return ENOMEM;
65 -     }
66 - 
67 -     ret = sss_parse_internal_fqname(tmp_ctx, member_name, NULL, &domname);
68 -     if (ret != EOK) {
69 -         DEBUG(SSSDBG_CRIT_FAILURE, "sss_parse_internal_fqname failed\n");
70 -         goto done;
71 -     }
72 - 
73 -     if (domname == NULL) {
74 -         ret = ERR_WRONG_NAME_FORMAT;
75 -         goto done;
76 -     }
77 - 
78 -     member_dom = find_domain_by_name(get_domains_head(rctx->domains),
79 -                                      domname, true);
80 -     if (member_dom == NULL) {
81 -         ret = ERR_DOMAIN_NOT_FOUND;
82 -         goto done;
83 -     }
84 - 
85 -     ret = sized_output_name(mem_ctx, rctx, member_name,
86 -                             member_dom, _name);
87 - done:
88 -     talloc_free(tmp_ctx);
89 -     return ret;
90 - }
91 - 
92   const char *
93   nss_get_pwfield(struct nss_ctx *nctx,
94                  struct sss_domain_info *dom)