~zisis00/drizzle/ldap_policy

« back to all changes in this revision

Viewing changes to plugin/ldap_policy/ldap_policy.cc

  • Committer: Zisis Sialveras
  • Date: 2012-08-09 18:18:22 UTC
  • mfrom: (2553.1.25 workspace)
  • Revision ID: zisis00@gmail.com-20120809181822-9wobhdfdx411tu5w
Some changes in ldap_policy

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2012 Zisis Sialveras
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; version 2 of the License.
 
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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
1
20
#include "ldap_policy.hpp"
2
21
#include <drizzled/module/option_map.h>
3
22
 
26
45
  context.registerVariable(new sys_var_const_string_val("schema-dir", schemas_dir));
27
46
  context.registerVariable(new sys_var_const_string_val("groups-dir", groups_dir));
28
47
  context.registerVariable(new sys_var_const_string_val("tables-dir", tables_dir));
29
 
  ///XXX: na kanw register to cache_timeout
 
48
  ///Remember to register the Variable Cache-timeout
30
49
 
31
50
  context.add(ldap_policy);
32
51
  return 0;
53
72
  errmsg_printf(error::ERROR, _(error.c_str()));
54
73
}
55
74
 
 
75
/**
 
76
 * restrictProcess dictates if user A is allowed to see/kill user B connection.
 
77
 *
 
78
 * In the beginning, restrictProcess checks if user A and B has the same gidNumber
 
79
 * attribute in their LDAP records. If they have not, restrictProcess checks
 
80
 * if user's B group is one of the user's A supplementaries groups. This is done
 
81
 * by searching in user's B group LDAP record and checking if its attributes is uid
 
82
 * of user A.
 
83
 */
 
84
 
56
85
bool LDAP_policy::restrictProcess(const drizzled::identifier::User &user_ctx,
57
86
                                  const drizzled::identifier::User &session_ctx)
58
87
{
137
166
    ldap_msgfree(response);
138
167
    ldap_value_free(gid);
139
168
    ldap_value_free(uid);
140
 
    //XXX: isws na eprepe na tsekarw mpas kai einai null
 
169
 
141
170
    return false;
142
171
  }
143
172
  else if (return_code != LDAP_COMPARE_FALSE)
325
354
  return retval;
326
355
}
327
356
 
 
357
 
 
358
/**
 
359
 * check_if_allowed returns TRUE if user is NOT authorized in target table. Otherwise, it returns FALSE.
 
360
 * check_if_allowed function, checks if uidNumber attribute of user's LDAP record matches with
 
361
 * allowedUsers attribute of tables's LDAP record or gidNumber matches with allowedGroups.
 
362
 * If neither of them matches, check_if_allowed search for the supplementaries groups of user
 
363
 * and returns true if none of them is allowed. Otherwise it returns false.
 
364
 */
 
365
 
328
366
bool LDAP_policy::check_if_allowed(LDAPMessage *u_rep, const drizzled::identifier::User &user, const drizzled::identifier::Table &table)
329
367
{
330
368
  LDAPMessage *entry= ldap_first_entry(ldap, u_rep);
610
648
        return true;
611
649
}
612
650
 
 
651
 
 
652
/**
 
653
 * check_if_allowed returns TRUE if user is NOT authorized in target schema. Otherwise, it returns FALSE.
 
654
 * check_if_allowed function, checks if uidNumber attribute of user's LDAP record matches with
 
655
 * allowedUsers attribute of schema's LDAP record or gidNumber matches with allowedGroups.
 
656
 * If neither of them matches, check_if_allowed search for the supplementaries groups of user
 
657
 * and returns true if none of them is allowed. Otherwise it returns false.
 
658
 */
 
659
 
613
660
bool LDAP_policy::check_if_allowed(LDAPMessage *u_rep, const drizzled::identifier::User &user, const drizzled::identifier::Schema& schema)
614
661
{
615
662
  LDAPMessage *entry= ldap_first_entry(ldap, u_rep);
639
686
      bval.bv_val= gid[0];
640
687
      bval.bv_len= strlen(gid[0]);
641
688
 
642
 
      rc= ldap_compare_ext_s(   ldap,
 
689
      rc= ldap_compare_ext_s( ldap,
643
690
                              schema_dn.c_str(),
644
691
                              "allowedGroups",
645
692
                              &bval,
664
711
      }
665
712
      else if(rc != LDAP_COMPARE_FALSE)
666
713
      {
667
 
 
668
 
        //XXX: otan vgazei no such object mporw na to vazw stin cache sto denied.
 
714
        //Maybe when return code is "No such object" I could add in cache.
669
715
 
670
716
        error= "ldap_compare_ext_s failed: ";
671
717
        error+= ldap_err2string(rc);
688
734
    else if (not strcmp(uattr, "uidNumber"))
689
735
    {
690
736
      uid= ldap_get_values(ldap, entry, uattr);
691
 
      user_id= uid[0]; // XXX: ti fash ?
692
737
 
693
738
      struct berval bval;
694
739
      bval.bv_val= uid[0];
836
881
  {
837
882
    return false;
838
883
  }
 
884
 
839
885
  LDAPMessage *user_response;
840
886
 
841
887
  const char *uattrs[]= { "uidNumber",
842
888
                          "gidNumber",
843
889
                          NULL
844
890
                          };
845
 
  string filter= "(cn=" + user_ctx.username() + ")";
846
891
 
 
892
  string filter= "(&(cn=" + user_ctx.username() + ")(BelongInSchema=" + table.getSchemaName() + "))";
 
893
  /* Schema attribute allows to determine from which schema, is the table we are looking for.
 
894
           Still I need to implement the BelongInSchema schema */
847
895
 
848
896
  CacheResponse cache_res= user_cache.is_authorized(user_ctx, table);
849
897
 
867
915
                                      NULL,
868
916
                                      1,
869
917
                                      &user_response);
 
918
 
870
919
  if(return_code != LDAP_SUCCESS)
871
920
  {
872
921
    error= "ldap_search_ext_s failed:";
873
922
    error+= ldap_err2string(return_code);
874
923
    printerror();
 
924
 
875
925
    return true;
876
926
  }
877
927
 
886
936
                                 const drizzled::identifier::Schema& schema)
887
937
{
888
938
 
889
 
  cerr << "Enters restrictSchema()" << endl;
890
 
 
891
939
  if(schema.getSchemaName() == "information_schema" || schema.getSchemaName() == "INFORMATION_SCHEMA")
892
940
  {
893
941
    return false;
980
1028
 
981
1029
inline bool CacheItem::is_valid() const
982
1030
{
983
 
        struct timeval tv_tmp;
984
 
        gettimeofday(&tv_tmp, NULL);
 
1031
        time_t temp_timer;
 
1032
        temp_timer = time(NULL);
985
1033
 
986
 
        if(tv_tmp.tv_sec - tv.tv_sec > cache_timeout)
 
1034
        if(temp_timer - timer > cache_timeout)
987
1035
        {
988
1036
                return false;
989
1037
        }