LCOV - code coverage report
Current view: top level - util - sss_nss.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 73 86 84.9 %
Date: 2016-06-29 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*
       2             :    SSSD
       3             : 
       4             :    Utility functions related to ID information
       5             : 
       6             :    Copyright (C) Jan Zeleny <jzeleny@redhat.com> 2012
       7             : 
       8             :    This program is free software; you can redistribute it and/or modify
       9             :    it under the terms of the GNU General Public License as published by
      10             :    the Free Software Foundation; either version 3 of the License, or
      11             :    (at your option) any later version.
      12             : 
      13             :    This program is distributed in the hope that it will be useful,
      14             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :    GNU General Public License for more details.
      17             : 
      18             :    You should have received a copy of the GNU General Public License
      19             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      20             : */
      21             : 
      22             : #include "util/util.h"
      23             : #include "util/sss_nss.h"
      24             : 
      25          60 : char *expand_homedir_template(TALLOC_CTX *mem_ctx, const char *template,
      26             :                               struct sss_nss_homedir_ctx *homedir_ctx)
      27             : {
      28             :     char *copy;
      29             :     char *p;
      30             :     char *n;
      31          60 :     char *result = NULL;
      32          60 :     char *res = NULL;
      33          60 :     TALLOC_CTX *tmp_ctx = NULL;
      34          60 :     const char *orig = NULL;
      35             : 
      36          60 :     if (template == NULL) {
      37           1 :         DEBUG(SSSDBG_CRIT_FAILURE, "Missing template.\n");
      38           1 :         return NULL;
      39             :     }
      40             : 
      41          59 :     if (homedir_ctx == NULL) {
      42           1 :         DEBUG(SSSDBG_CRIT_FAILURE, "Missing home directory data.\n");
      43           1 :         return NULL;
      44             :     }
      45             : 
      46          58 :     tmp_ctx = talloc_new(NULL);
      47          58 :     if (!tmp_ctx) return NULL;
      48             : 
      49          58 :     copy = talloc_strdup(tmp_ctx, template);
      50          58 :     if (copy == NULL) {
      51           0 :         DEBUG(SSSDBG_CRIT_FAILURE, "talloc_strdup failed.\n");
      52           0 :         goto done;
      53             :     }
      54             : 
      55          58 :     result = talloc_strdup(tmp_ctx, "");
      56          58 :     if (result == NULL) {
      57           0 :         DEBUG(SSSDBG_CRIT_FAILURE, "talloc_strdup failed.\n");
      58           0 :         goto done;
      59             :     }
      60             : 
      61          58 :     p = copy;
      62         158 :     while ( (n = strchr(p, '%')) != NULL) {
      63          52 :         *n = '\0';
      64          52 :         n++;
      65          52 :         if ( *n == '\0' ) {
      66           1 :             DEBUG(SSSDBG_CRIT_FAILURE, "format error, single %% at the end of "
      67             :                                         "the template.\n");
      68           1 :             goto done;
      69             :         }
      70          51 :         switch( *n ) {
      71             :             case 'u':
      72           6 :                 if (homedir_ctx->username == NULL) {
      73           1 :                     DEBUG(SSSDBG_CRIT_FAILURE,
      74             :                           "Cannot expand user name template because user name "
      75             :                           "is empty.\n");
      76           1 :                     goto done;
      77             :                 }
      78           5 :                 result = talloc_asprintf_append(result, "%s%s", p,
      79             :                                                 homedir_ctx->username);
      80           5 :                 break;
      81             : 
      82             :             case 'U':
      83           6 :                 if (homedir_ctx->uid == 0) {
      84           1 :                     DEBUG(SSSDBG_CRIT_FAILURE, "Cannot expand uid template "
      85             :                                                 "because uid is invalid.\n");
      86           1 :                     goto done;
      87             :                 }
      88           5 :                 result = talloc_asprintf_append(result, "%s%d", p,
      89             :                                                 homedir_ctx->uid);
      90           5 :                 break;
      91             : 
      92             :             case 'd':
      93           6 :                 if (homedir_ctx->domain == NULL) {
      94           1 :                     DEBUG(SSSDBG_CRIT_FAILURE, "Cannot expand domain name "
      95             :                                                 "template because domain name "
      96             :                                                 "is empty.\n");
      97           1 :                     goto done;
      98             :                 }
      99           5 :                 result = talloc_asprintf_append(result, "%s%s", p,
     100             :                                                 homedir_ctx->domain);
     101           5 :                 break;
     102             : 
     103             :             case 'f':
     104           8 :                 if (homedir_ctx->domain == NULL
     105           6 :                         || homedir_ctx->username == NULL) {
     106           3 :                     DEBUG(SSSDBG_CRIT_FAILURE, "Cannot expand fully qualified "
     107             :                                                 "name template because domain "
     108             :                                                 "or user name is empty.\n");
     109           3 :                     goto done;
     110             :                 }
     111           5 :                 result = talloc_asprintf_append(result, "%s%s@%s", p,
     112             :                                                 homedir_ctx->username,
     113             :                                                 homedir_ctx->domain);
     114           5 :                 break;
     115             : 
     116             :             case 'o':
     117           6 :                 if (homedir_ctx->original == NULL) {
     118           1 :                     DEBUG(SSSDBG_CRIT_FAILURE,
     119             :                           "Original home directory for %s is not available, "
     120             :                            "using empty string\n", homedir_ctx->username);
     121           1 :                     orig = "";
     122             :                 } else {
     123           5 :                     orig = homedir_ctx->original;
     124             :                 }
     125           6 :                 result = talloc_asprintf_append(result, "%s%s", p, orig);
     126           6 :                 break;
     127             : 
     128             :             case 'F':
     129           6 :                 if (homedir_ctx->flatname == NULL) {
     130           1 :                     DEBUG(SSSDBG_CRIT_FAILURE, "Cannot expand domain name "
     131             :                                                 "template because domain flat "
     132             :                                                 "name is empty.\n");
     133           1 :                     goto done;
     134             :                 }
     135           5 :                 result = talloc_asprintf_append(result, "%s%s", p,
     136             :                                                 homedir_ctx->flatname);
     137           5 :                 break;
     138             : 
     139             :             case 'H':
     140           6 :                 if (homedir_ctx->config_homedir_substr == NULL) {
     141           1 :                     DEBUG(SSSDBG_CRIT_FAILURE,
     142             :                           "Cannot expand home directory substring template "
     143             :                           "substring is empty.\n");
     144           1 :                     goto done;
     145             :                 }
     146           5 :                 result = talloc_asprintf_append(result, "%s%s", p,
     147             :                                            homedir_ctx->config_homedir_substr);
     148           5 :                 break;
     149             : 
     150             :             case 'P':
     151           0 :                 if (homedir_ctx->upn == NULL) {
     152           0 :                     DEBUG(SSSDBG_CRIT_FAILURE,
     153             :                           "Cannot expand user principal name template "
     154             :                           "string is empty.\n");
     155           0 :                     goto done;
     156             :                 }
     157           0 :                 result = talloc_asprintf_append(result, "%s%s", p,
     158             :                                                 homedir_ctx->upn);
     159           0 :                 break;
     160             : 
     161             :             case '%':
     162           6 :                 result = talloc_asprintf_append(result, "%s%%", p);
     163           6 :                 break;
     164             : 
     165             :             default:
     166           1 :                 DEBUG(SSSDBG_CRIT_FAILURE, "format error, unknown template "
     167             :                                             "[%%%c].\n", *n);
     168           1 :                 goto done;
     169             :         }
     170             : 
     171          42 :         if (result == NULL) {
     172           0 :             DEBUG(SSSDBG_CRIT_FAILURE, "talloc_asprintf_append failed.\n");
     173           0 :             goto done;
     174             :         }
     175             : 
     176          42 :         p = n + 1;
     177             :     }
     178             : 
     179          48 :     result = talloc_asprintf_append(result, "%s", p);
     180          48 :     if (result == NULL) {
     181           0 :         DEBUG(SSSDBG_CRIT_FAILURE, "talloc_asprintf_append failed.\n");
     182           0 :         goto done;
     183             :     }
     184             : 
     185          48 :     res = talloc_move(mem_ctx, &result);
     186             : done:
     187          58 :     talloc_zfree(tmp_ctx);
     188          58 :     return res;
     189             : }

Generated by: LCOV version 1.10