LCOV - code coverage report
Current view: top level - src/libs/elektra - owner.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 30 30 100.0 %
Date: 2019-09-12 12:28:41 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief Obsolete owner methods.
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  */
       8             : 
       9             : #include <kdb.h>
      10             : #include <kdbconfig.h>
      11             : #include <kdbmeta.h>
      12             : #include <kdbprivate.h>
      13             : 
      14             : #ifdef HAVE_STDIO_H
      15             : #include <stdio.h>
      16             : #endif
      17             : 
      18             : #ifdef HAVE_STDARG_H
      19             : #include <stdarg.h>
      20             : #endif
      21             : 
      22             : #ifdef HAVE_STRING_H
      23             : #include <string.h>
      24             : #endif
      25             : 
      26             : #ifdef HAVE_STDLIB_H
      27             : #include <stdlib.h>
      28             : #endif
      29             : 
      30             : #ifdef HAVE_ERRNO_H
      31             : #include <errno.h>
      32             : #endif
      33             : 
      34             : 
      35             : /**
      36             :  * Return a pointer to the real internal @p key owner.
      37             :  *
      38             :  * This is a much more efficient version of keyGetOwner() and you
      39             :  * should use it if you are responsible enough to not mess up things.
      40             :  * You are not allowed to modify the returned string in any way.
      41             :  * If you need a copy of the string, consider to use keyGetOwner() instead.
      42             :  *
      43             :  * keyOwner() returns "" when there is no keyOwner. The reason is
      44             :  * @code
      45             : key=keyNew(0);
      46             : keySetOwner(key,"");
      47             : keyOwner(key); // you would expect "" here
      48             : keySetOwner(key,"system");
      49             : keyOwner(key); // you would expect "" here
      50             :  * @endcode
      51             :  *
      52             :  * @note Note that the Key structure keeps its own size field that is calculated
      53             :  * by library internal calls, so to avoid inconsistencies, you
      54             :  * must never use the pointer returned by keyOwner() method to set a new
      55             :  * value. Use keySetOwner() instead.
      56             :  *
      57             :  * @param key the key object to work with
      58             :  * @return a pointer to internal owner
      59             :  * @retval "" when there is no (an empty) owner
      60             :  * @retval 0 iff key is a NULL pointer
      61             :  * @see keyGetOwnerSize() for the size of the string with concluding 0
      62             :  * @see keyGetOwner(), keySetOwner()
      63             :  * @see keyName() for name without owner
      64             :  * @see keyGetFullName() for name with owner
      65             :  */
      66         684 : const char * keyOwner (const Key * key)
      67             : {
      68             :         const char * owner;
      69             : 
      70         684 :         if (!key) return 0;
      71         682 :         owner = keyValue (keyGetMeta (key, "owner"));
      72             : 
      73         682 :         if (!owner)
      74             :         {
      75             :                 return "";
      76             :         }
      77             : 
      78         582 :         return owner;
      79             : }
      80             : 
      81             : 
      82             : /**
      83             :  * Return the size of the owner of the Key with concluding 0.
      84             :  *
      85             :  * The returned number can be used to allocate a string.
      86             :  * 1 will returned on an empty owner to store the concluding 0
      87             :  * on using keyGetOwner().
      88             :  *
      89             :  * @code
      90             : char * buffer;
      91             : buffer = elektraMalloc (keyGetOwnerSize (key));
      92             : // use buffer and keyGetOwnerSize (key) for maxSize
      93             :  * @endcode
      94             :  *
      95             :  * @note that -1 might be returned on null pointer, so when you
      96             :  * directly allocate afterwards its best to check if you will pass
      97             :  * a null pointer before.
      98             :  *
      99             :  * @param key the key object to work with
     100             :  * @return number of bytes
     101             :  * @retval 1 if there is no owner
     102             :  * @retval -1 on NULL pointer
     103             :  * @see keyGetOwner()
     104             :  */
     105        2626 : ssize_t keyGetOwnerSize (const Key * key)
     106             : {
     107             :         ssize_t size;
     108        2626 :         if (!key) return -1;
     109             : 
     110        2626 :         size = keyGetValueSize (keyGetMeta (key, "owner"));
     111             : 
     112        2626 :         if (!size || size == -1)
     113             :         {
     114             :                 /*errno=KDB_ERR_NODESC;*/
     115             :                 return 1;
     116             :         }
     117             : 
     118        2620 :         return size;
     119             : }
     120             : 
     121             : 
     122             : /**
     123             :  * Return the owner of the key.
     124             :  * - Given @p user:someuser/..... return @p someuser
     125             :  * - Given @p user:some.user/.... return @p some.user
     126             :  * - Given @p user/.... return the current user
     127             :  *
     128             :  * Only @p user/... keys have an owner.
     129             :  * For @p system/... keys (that doesn't have a key owner) an empty
     130             :  * string ("") is returned.
     131             :  *
     132             :  * Although usually the same, the owner of a key is not related to its
     133             :  * UID. Owner are related to WHERE the key is stored on disk, while
     134             :  * UIDs are related to mode controls of a key.
     135             :  *
     136             :  * @param key the object to work with
     137             :  * @param returnedOwner a pre-allocated space to store the owner
     138             :  * @param maxSize maximum number of bytes that fit returned
     139             :  * @return number of bytes written to buffer
     140             :  * @retval 1 if there is no owner
     141             :  * @retval -1 on NULL pointers
     142             :  * @retval -1 when maxSize is 0, larger than SSIZE_MAX or too small for ownername
     143             :  * @see keySetName(), keySetOwner(), keyOwner(), keyGetFullName()
     144             :  */
     145          40 : ssize_t keyGetOwner (const Key * key, char * returnedOwner, size_t maxSize)
     146             : {
     147             :         const char * owner;
     148             :         size_t ownerSize;
     149          40 :         if (!key) return -1;
     150             : 
     151          38 :         if (!maxSize) return -1;
     152          32 :         if (!returnedOwner) return -1;
     153          30 :         if (maxSize > SSIZE_MAX) return -1;
     154             : 
     155          28 :         owner = keyValue (keyGetMeta (key, "owner"));
     156          28 :         ownerSize = keyGetValueSize (keyGetMeta (key, "owner"));
     157             : 
     158          28 :         if (!owner)
     159             :         {
     160             :                 /*errno=KDB_ERR_NODESC;*/
     161           8 :                 returnedOwner[0] = 0;
     162           8 :                 return 1;
     163             :         }
     164             : 
     165          20 :         strncpy (returnedOwner, owner, maxSize);
     166          20 :         if (maxSize < ownerSize)
     167             :         {
     168             :                 /*errno=KDB_ERR_TRUNC;*/
     169             :                 return -1;
     170             :         }
     171          14 :         return ownerSize;
     172             : }
     173             : 
     174             : 
     175             : /**
     176             :  * Set the owner of a key.
     177             :  *
     178             :  * an owner is a name of a system user related to a UID.
     179             :  * The owner decides on which location on the disc the key
     180             :  * goes.
     181             :  *
     182             :  * A private copy is stored, so the passed parameter can be freed after
     183             :  * the call.
     184             :  *
     185             :  * @param key the key object to work with
     186             :  * @param newOwner the string which describes the owner of the key
     187             :  * @return the number of bytes actually saved including final NULL
     188             :  * @retval 1 when owner is freed (by setting 0 or "")
     189             :  * @retval -1 on null pointer or memory problems
     190             :  * @see keySetName(), keyGetOwner(), keyGetFullName()
     191             :  */
     192     3037068 : ssize_t keySetOwner (Key * key, const char * newOwner)
     193             : {
     194     3037068 :         if (!key) return -1;
     195     3037066 :         if (!newOwner || *newOwner == 0)
     196             :         {
     197     3034543 :                 keySetMeta (key, "owner", 0);
     198     3034543 :                 return 1;
     199             :         }
     200             : 
     201        2523 :         keySetMeta (key, "owner", newOwner);
     202        2523 :         return keyGetOwnerSize (key);
     203             : }

Generated by: LCOV version 1.13