"Fossies" - the Fresh Open Source Software Archive

Member "netxms-3.1.300/src/server/core/acl.cpp" (7 Jan 2020, 4151 Bytes) of package /linux/misc/netxms-3.1.300.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "acl.cpp" see the Fossies "Dox" file reference documentation.

    1 /* 
    2 ** NetXMS - Network Management System
    3 ** Copyright (C) 2003-2017 Victor Kirhenshtein
    4 **
    5 ** This program is free software; you can redistribute it and/or modify
    6 ** it under the terms of the GNU General Public License as published by
    7 ** the Free Software Foundation; either version 2 of the License, or
    8 ** (at your option) any later version.
    9 **
   10 ** This program is distributed in the hope that it will be useful,
   11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13 ** GNU General Public License for more details.
   14 **
   15 ** You should have received a copy of the GNU General Public License
   16 ** along with this program; if not, write to the Free Software
   17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   18 **
   19 ** File: acl.cpp
   20 **
   21 **/
   22 
   23 #include "nxcore.h"
   24 
   25 /**
   26  * Access list constructor
   27  */
   28 AccessList::AccessList()
   29 {
   30    m_size = 0;
   31    m_allocated = 0;
   32    m_elements = NULL;
   33 }
   34 
   35 /**
   36  * Destructor
   37  */
   38 AccessList::~AccessList()
   39 {
   40    free(m_elements);
   41 }
   42 
   43 /**
   44  * Add element to list
   45  */
   46 void AccessList::addElement(UINT32 dwUserId, UINT32 dwAccessRights)
   47 {
   48    int i;
   49 
   50    for(i = 0; i < m_size; i++)
   51       if (m_elements[i].dwUserId == dwUserId)    // Object already exist in list
   52       {
   53          m_elements[i].dwAccessRights = dwAccessRights;
   54          break;
   55       }
   56 
   57    if (i == m_size)
   58    {
   59       if (m_size == m_allocated)
   60       {
   61          m_allocated += 16;
   62          m_elements = (ACL_ELEMENT *)realloc(m_elements, sizeof(ACL_ELEMENT) * m_allocated);
   63       }
   64       m_elements[m_size].dwUserId = dwUserId;
   65       m_elements[m_size].dwAccessRights = dwAccessRights;
   66       m_size++;
   67    }
   68 }
   69 
   70 /**
   71  * Delete element from list
   72  */
   73 bool AccessList::deleteElement(UINT32 dwUserId)
   74 {
   75    bool deleted = false;
   76    for(int i = 0; i < m_size; i++)
   77       if (m_elements[i].dwUserId == dwUserId)
   78       {
   79          m_size--;
   80          memmove(&m_elements[i], &m_elements[i + 1], sizeof(ACL_ELEMENT) * (m_size - i));
   81          deleted = true;
   82          break;
   83       }
   84    return deleted;
   85 }
   86 
   87 /**
   88  * Retrieve access rights for specific user object
   89  * Returns true on success and stores access rights to specific location
   90  * If user doesn't have explicit rights or via group, returns false
   91  */
   92 bool AccessList::getUserRights(UINT32 dwUserId, UINT32 *pdwAccessRights)
   93 {
   94    int i;
   95    bool found = false;
   96 
   97    // Check for explicit rights
   98    for(i = 0; i < m_size; i++)
   99       if (m_elements[i].dwUserId == dwUserId)
  100       {
  101          *pdwAccessRights = m_elements[i].dwAccessRights;
  102          found = true;
  103          break;
  104       }
  105 
  106    if (!found)
  107    {
  108       *pdwAccessRights = 0;   // Default: no access
  109       for(i = 0; i < m_size; i++)
  110          if (m_elements[i].dwUserId & GROUP_FLAG)
  111          {
  112             if (CheckUserMembership(dwUserId, m_elements[i].dwUserId))
  113             {
  114                *pdwAccessRights |= m_elements[i].dwAccessRights;
  115                found = true;
  116             }
  117          }
  118    }
  119 
  120    return found;
  121 }
  122 
  123 /**
  124  * Enumerate all elements
  125  */
  126 void AccessList::enumerateElements(void (* pHandler)(UINT32, UINT32, void *), void *pArg)
  127 {
  128    for(int i = 0; i < m_size; i++)
  129       pHandler(m_elements[i].dwUserId, m_elements[i].dwAccessRights, pArg);
  130 }
  131 
  132 /**
  133  * Fill NXCP message with ACL's data
  134  */
  135 void AccessList::fillMessage(NXCPMessage *pMsg)
  136 {
  137    pMsg->setField(VID_ACL_SIZE, m_size);
  138 
  139    UINT32 dwId1, dwId2;
  140    int i;
  141    for(i = 0, dwId1 = VID_ACL_USER_BASE, dwId2 = VID_ACL_RIGHTS_BASE; i < m_size; i++, dwId1++, dwId2++)
  142    {
  143       pMsg->setField(dwId1, m_elements[i].dwUserId);
  144       pMsg->setField(dwId2, m_elements[i].dwAccessRights);
  145    }
  146 }
  147 
  148 /**
  149  * Serialize as JSON
  150  */
  151 json_t *AccessList::toJson()
  152 {
  153    json_t *root = json_array();
  154    for(int i = 0; i < m_size; i++)
  155    {
  156       json_t *e = json_object();
  157       json_object_set_new(e, "userId", json_integer(m_elements[i].dwUserId));
  158       json_object_set_new(e, "access", json_integer(m_elements[i].dwAccessRights));
  159       json_array_append_new(root, e);
  160    }
  161    return root;
  162 }
  163 
  164 /**
  165  * Delete all elements
  166  */
  167 void AccessList::deleteAll()
  168 {
  169    m_size = 0;
  170    m_allocated = 0;
  171    free(m_elements);
  172    m_elements = NULL;
  173 }