-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkdatabase.py.orig
1118 lines (866 loc) · 29.6 KB
/
kdatabase.py.orig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<<<<<<< /home/fdgonthier/tmp/kdatabase.py~base.9bG6qK
=======
# -*- mode: python; python-indent: 4; tab-width: 4; indent-tabs-mode: t -*-
>>>>>>> /home/fdgonthier/tmp/kdatabase.py~other.6jzd-D
#####
##### DATABASE STUFF
#####
#import pgdb
import pgdb
import os
<<<<<<< /home/fdgonthier/tmp/kdatabase.py~base.9bG6qK
=======
import time
>>>>>>> /home/fdgonthier/tmp/kdatabase.py~other.6jzd-D
from kparams import *
from kiniconfig import *
from ktbxsosdconfig import *
from kreadline import *
from kout import *
from kexcept import *
class NoRow(Exception):
def __init__(self, err_msg):
self.err_msg=err_msg
def __str__(self):
return repr(self.err_msg)
# identifiers for database infos and handlers
# don't change those ids... they match tbxsosd config tags...
DB_PROFILES="user"
DB_LOGIN="login"
DB_PUBKEYS="pkey"
DB_PRIVKEYS="skey"
<<<<<<< /home/fdgonthier/tmp/kdatabase.py~base.9bG6qK
=======
DB_EVENTS="event"
>>>>>>> /home/fdgonthier/tmp/kdatabase.py~other.6jzd-D
# will contain database handlers
dbs = {}
dbs_info = {}
# init settings for all databases
def db_init():
global dbs
global dbs_info
dbs[DB_PROFILES] = None
dbs[DB_LOGIN] = None
dbs[DB_PUBKEYS] = None
dbs[DB_PRIVKEYS] = None
<<<<<<< /home/fdgonthier/tmp/kdatabase.py~base.9bG6qK
=======
dbs[DB_EVENTS] = None
>>>>>>> /home/fdgonthier/tmp/kdatabase.py~other.6jzd-D
# get databases connection info
# first try ini files...
# then try tbxsosd files
<<<<<<< /home/repos/kctl/kdatabase.py.orig.380629861
<<<<<<< /home/fdgonthier/tmp/kdatabase.py~base.9bG6qK
try:
=======
try:
>>>>>>> /home/fdgonthier/tmp/kdatabase.py~other.6jzd-D
||||||| /home/fdgonthier/tmp/kdatabase.py~base.D-bP8W
try:
=======
try:
>>>>>>> /home/fdgonthier/tmp/kdatabase.py~other.Lsa77p
config = KIniConfig()
for key in dbs.keys():
dbs_info[key] = {}
dbs_info[key]["name"] = config.get("databases", "db_" + key + ".name")
dbs_info[key]["host"] = config.get("databases", "db_" + key + ".host")
dbs_info[key]["port"] = config.get("databases", "db_" + key + ".port")
<<<<<<< /home/repos/kctl/kdatabase.py.orig.380629861
<<<<<<< /home/fdgonthier/tmp/kdatabase.py~base.9bG6qK
dbs_info[key]["username"] = config.get("databases", "db_" + key + ".admin_username")
=======
dbs_info[key]["username"] = config.get("databases", "db_" + key + ".admin_username")
>>>>>>> /home/fdgonthier/tmp/kdatabase.py~other.6jzd-D
||||||| /home/fdgonthier/tmp/kdatabase.py~base.D-bP8W
dbs_info[key]["username"] = config.get("databases", "db_" + key + ".admin_username")
=======
dbs_info[key]["username"] = config.get("databases", "db_" + key + ".admin_username")
>>>>>>> /home/fdgonthier/tmp/kdatabase.py~other.Lsa77p
dbs_info[key]["password"] = config.get("databases", "db_" + key + ".admin_password")
except:
config = KTbxsosdConfig()
# This tells if kctl should try to login with the current username.
<<<<<<< /home/fdgonthier/tmp/kdatabase.py~base.9bG6qK
kctl_curr_creds = int(config.get("kctl.curr_creds"))
=======
kctl_curr_creds = config.get("kctl.curr_creds")
>>>>>>> /home/fdgonthier/tmp/kdatabase.py~other.6jzd-D
for key in dbs.keys():
dbs_info[key] = {}
dbs_info[key]["name"] = config.get("db_" + key + ".name")
dbs_info[key]["host"] = config.get("db_" + key + ".host")
dbs_info[key]["port"] = config.get("db_" + key + ".port")
<<<<<<< /home/fdgonthier/tmp/kdatabase.py~base.9bG6qK
if kctl_curr_creds == 1:
=======
if str(kctl_curr_creds) == str(1):
>>>>>>> /home/fdgonthier/tmp/kdatabase.py~other.6jzd-D
import pwd, os
dbs_info[key]["username"] = pwd.getpwuid(os.getuid())[0]
<<<<<<< /home/fdgonthier/tmp/kdatabase.py~base.9bG6qK
=======
dbs_info[key]["password"] = config.get("db_" + key + ".admin_password")
>>>>>>> /home/fdgonthier/tmp/kdatabase.py~other.6jzd-D
else:
dbs_info[key]["username"] = config.get("db_" + key + ".admin_username")
dbs_info[key]["password"] = config.get("db_" + key + ".admin_password")
<<<<<<< /home/repos/kctl/kdatabase.py.orig.380629861
# connect to databases
for id in dbs.keys():
<<<<<<< /home/fdgonthier/tmp/kdatabase.py~base.9bG6qK
=======
||||||| /home/fdgonthier/tmp/kdatabase.py~base.D-bP8W
# connect to databases
for id in dbs.keys():
=======
# what can be None (connect to all databases) or a list of ids of databases to connect to
def db_conn(dbi):
global dbs
global dbs_info
id = dbi
if dbs[id] == None:
>>>>>>> /home/fdgonthier/tmp/kdatabase.py~other.Lsa77p
debug("Connecting to database '%s' (params %s)" % ( dbs_info[id]["name"], str(dbs_info[id]) ))
>>>>>>> /home/fdgonthier/tmp/kdatabase.py~other.6jzd-D
host = dbs_info[id]["host"]
if dbs_info[id]["port"] != None and dbs_info[id]["port"] != "":
host = host + ":" + str(dbs_info[id]["port"])
<<<<<<< /home/repos/kctl/kdatabase.py.orig.1718025403
dbs[id] = pgdb.connect(host = host, database = dbs_info[id]["name"],
user = dbs_info[id]["username"], password = dbs_info[id]["password"])
debug("Connection successful to database '%s'" % ( dbs_info[id]["name"] ))
||||||| /home/fdgonthier/tmp/kdatabase.py~base.9bG6qK
dbs[id] = pgdb.connect(host = host, database = dbs_info[id]["name"],
user = dbs_info[id]["username"], password = dbs_info[id]["password"])
=======
dbs[id] = pgdb.connect(host = host, database = dbs_info[id]["name"],
user = dbs_info[id]["username"], password = dbs_info[id]["password"])
debug("Connection successful to database '%s'" % ( dbs_info[id]["name"] ))
<<<<<<< /home/repos/kctl/kdatabase.py.orig.380629861
>>>>>>> /home/fdgonthier/tmp/kdatabase.py~other.6jzd-D
||||||| /home/fdgonthier/tmp/kdatabase.py~base.D-bP8W
=======
# connect to all dbs (to use only for selftest
def db_conn_all():
for id in dbs:
db_conn(id)
>>>>>>> /home/fdgonthier/tmp/kdatabase.py~other.Lsa77p
def db_commit():
global dbs
if kparams_get("commit") == True:
for id in dbs.keys():
if dbs[id] != None:
dbs[id].commit()
debug("commited changes.")
else:
for id in dbs.keys():
if dbs[id] != None:
dbs[id].rollback()
out("NOT COMMITING DATABASE CHANGES!!!")
# quote and escape sql argument
# WARNING: uses pgdb._quote() which is an internal function
def db_safestr(s):
return str(pgdb._quote(s))
# makes query available in debugging (debugging can be on or off - see kout.py)
def db_debug_query(query):
if kparams_get("db_debug_query") == True:
debug("query: '" + query + "'", True) # force debugging
else:
debug("query: '" + query + "'")
# exec query - raise exception if error
def db_exec(dbi, query):
db_conn(dbi)
dbh = dbs[dbi]
cur = dbh.cursor()
try:
cur.execute(query)
except pgdb.DatabaseError, e:
db_conn(dbi)
dbh.rollback()
raise ExError(str(e))
return cur.rowcount
# returns the first element of the first row - raise NoRow if no row available
def db_get_first_element(dbi, query):
db_conn(dbi)
dbh = dbs[dbi]
cur = dbh.cursor()
try:
cur.execute(query)
except pgdb.DatabaseError, e:
dbh.rollback()
raise ExError(str(e))
# check row count
if cur.rowcount == None or cur.rowcount < 1:
raise NoRow("no row returned")
# get first row
row = cur.fetchone()
return row[0]
<<<<<<< /home/fdgonthier/tmp/kdatabase.py~base.9bG6qK
=======
# returns the first row - raise NoRow if no row available
def db_get_first_row(dbi, query):
db_conn(dbi)
dbh = dbs[dbi]
cur = dbh.cursor()
try:
cur.execute(query)
except pgdb.DatabaseError, e:
dbh.rollback()
raise ExError(str(e))
# check row count
if cur.rowcount == None or cur.rowcount < 1:
raise NoRow("no row returned")
# get first row
row = cur.fetchone()
return row
>>>>>>> /home/fdgonthier/tmp/kdatabase.py~other.6jzd-D
# returns True if any row found - false otherwise
def db_has_record(dbi, query):
db_conn(dbi)
dbh = dbs[dbi]
cur = dbh.cursor()
try:
cur.execute(query)
except pgdb.DatabaseError, e:
dbh.rollback()
raise ExError(str(e))
# check row count
<<<<<<< /home/fdgonthier/tmp/kdatabase.py~base.9bG6qK
if cur.rowcount == None or cur.rowcount < 1:
=======
# don't rely on rowcount since it's not working will all databases (sqlite)
if cur.fetchone() == None:
>>>>>>> /home/fdgonthier/tmp/kdatabase.py~other.6jzd-D
return False
return True
# returns list [desc, data] from the query
# desc is a list of field infos
# data if a list of rows
# ie:
# desc: TODO
# data: [ [1, 'Gaga'], [2, 'gogo'] ]
# returns [ None, None ] if no data
def db_find_list_desc_data(dbi, query):
db_conn(dbi)
dbh = dbs[dbi]
cur = dbh.cursor()
try:
cur.execute(query)
except pgdb.DatabaseError, e:
dbh.rollback()
raise ExError(str(e))
if cur.rowcount == None or cur.rowcount < 1:
return [ None, None ]
field_descs = []
for row in cur.description:
field_descs.append([row[0]])
#field_descs = cur.description
res = cur.fetchall()
return [ field_descs, res ]
# returns list of hashes
# i.e. [ { 'col1' => value1, 'col2' => value2 }, { 'col1' => value3, 'col2' => value4 } ]
# returns empty list if no data
def db_find_hash(dbi, query):
db_conn(dbi)
dbh = dbs[dbi]
cur = dbh.cursor()
try:
cur.execute(query)
except pgdb.DatabaseError, e:
dbh.rollback()
raise ExError(str(e))
if cur.rowcount == None:
return None
res = []
desc = cur.description
while 1:
row = cur.fetchone()
if row == None: break
new_row = {}
i = 0
for val in row:
fieldname = desc[i][0]
new_row[fieldname] = val
i += 1
res.append(new_row)
return res
# returns all rows from table (as an hash)
def db_find_all_from_table_hash(dbi, table):
return db_find_hash(dbi, "SELECT * FROM " + table)
# returns the first row of the table specified where the value of
# the column specified matches the value specified.
def db_find_id_hash(dbi, table, column, value):
query = "SELECT * FROM " + table + " WHERE " + column + " = %s" % ( db_safestr(value) )
res = db_find_hash(dbi, query)
if res == None or len(res) < 1:
return None
return res[0]
# returns the first row of the table specified where the value of
# the column specified matches the value specified.
def db_has_record_id(dbi, table, column, value):
query = "SELECT * FROM " + table + " WHERE " + column + " = %s" % ( db_safestr(value) )
db_debug_query(query)
return db_has_record(dbi, query)
# Variant of the function above. The rows having a value that match the value
# specified are deleted. The function returns the number of rows deleted.
def db_delete_id(dbi, table, column, value):
query = "DELETE FROM " + table + " WHERE " + column + " = %s" % ( db_safestr(value) )
db_exec(dbi, query)
###
### Specialised database functions. ###
###
def sdb_logins_find():
return db_find_all_from_table_hash(DB_LOGIN, "user_login")
def sdb_login_find(id):
return db_find_id_hash(DB_LOGIN, "user_login", "prof_id", id)
def sdb_login_find_by_login(login):
return db_find_id_hash(DB_LOGIN, "user_login", "user_name", login)
def sdb_login_delete(id):
return db_delete_id(DB_LOGIN, "user_login", "prof_id", id)
def sdb_profiles_find():
return db_find_hash(DB_PROFILES, "SELECT * FROM profiles")
def sdb_profile_find(id):
return db_find_id_hash(DB_PROFILES, "profiles", "prof_id", id)
def sdb_user_profile_valid(id):
return db_has_record_id(DB_PROFILES, "user_profiles", "user_id", id)
def sdb_group_profile_valid(id):
return db_has_record_id(DB_PROFILES, "group_profiles", "group_id", id)
def sdb_ldap_group_find(id):
return db_has_record_id(DB_PROFILES, "ldap_groups", "group_id", id)
def sdb_email_part_find(id):
return db_has_record_id(DB_PROFILES, "email_parts", "group_id", id)
def sdb_email_primary_find(id):
return db_find_hash(DB_PROFILES,
"SELECT * FROM emails WHERE user_id=%s AND is_primary = TRUE" % ( db_safestr(id) ))
def sdb_email_find(id):
return db_find_id_hash(DB_PROFILES, "emails", "user_id", id)
# check org existance
def sdb_org_exist(id):
global dbs
query = "select * from organization where org_id=%s" % ( db_safestr(id) )
return db_has_record(DB_PROFILES, query)
def sdb_org_check(id):
if sdb_org_exist(id) != True:
raise ExError("Organization id %s does not exist" % ( id ))
# check profile existance
def sdb_prof_exist(id):
global dbs
query = "select * from profiles where prof_id=%s" % ( db_safestr(id) )
return db_has_record(DB_PROFILES, query)
def sdb_prof_check(id):
if sdb_prof_exist(id) != True:
raise ExError("Profile id %s does not exist" % ( id ))
def sdb_getorgname(org_id):
global dbs
query = "select name from organization where org_id = %s" % ( db_safestr(org_id) )
db_debug_query(query)
return db_get_first_element(DB_PROFILES, query)
<<<<<<< /home/fdgonthier/tmp/kdatabase.py~base.9bG6qK
=======
def sdb_org_id_by_kdn(kdn):
global dbs
query = "select org_id from organization where name=%s" % ( db_safestr(kdn) )
db_debug_query(query)
return db_get_first_element(DB_PROFILES, query)
def sdb_kdn_by_org_id(org_id):
global dbs
query = "select name from organization where org_id=%s" % ( db_safestr(org_id) )
db_debug_query(query)
return db_get_first_element(DB_PROFILES, query)
def sdb_org_keys(org_id):
global dbs
query = "SELECT key_id FROM profiles WHERE org_id=%s" % ( db_safestr(org_id) )
db_debug_query(query)
return db_find_list_desc_data(DB_PROFILES, query)
>>>>>>> /home/fdgonthier/tmp/kdatabase.py~other.6jzd-D
def sdb_getprofilegroupid(prof_id):
global dbs
query = "select group_id from profiles where prof_id = %s" % ( db_safestr(prof_id) )
db_debug_query(query)
return db_get_first_element(DB_PROFILES, query)
def sdb_getgroupprofileid(group_id):
global dbs
query = "select prof_id from profiles where group_id = %s" % ( db_safestr(group_id) )
db_debug_query(query)
return db_get_first_element(DB_PROFILES, query)
def sdb_getorgprofiles(org_id):
global dbs
query = "select prof_id from profiles where org_id = %s" % ( db_safestr(org_id) )
db_debug_query(query)
return db_find_list_desc_data(DB_PROFILES, query)
def sdb_getprofileorg(prof_id):
global dbs
query = "select org_id from profiles where prof_id = %s" % ( db_safestr(prof_id) )
db_debug_query(query)
return db_get_first_element(DB_PROFILES, query)
# get profile status
def sdb_getprofstatus(id):
global dbs
sdb_prof_check(id)
query = "select * from get_prof_status(%s);" % (db_safestr(id))
db_debug_query(query)
return db_get_first_element(DB_PROFILES, query)
# get profile name
def sdb_get_org_name(id):
global dbs
sdb_org_check(id)
query = "select name from organization WHERE org_id = %s;" % (db_safestr(id))
db_debug_query(query)
return db_get_first_element(DB_PROFILES, query)
# get profile name
def sdb_get_prof_name(id):
global dbs
sdb_prof_check(id)
query = "select * from get_profile_name(%s);" % (db_safestr(id))
db_debug_query(query)
return db_get_first_element(DB_PROFILES, query)
# return True / False
def sdb_key_valid(key_id):
global dbs
query = "select * from pkey_view where key_id = %s;" % ( db_safestr(key_id) )
db_debug_query(query)
return db_has_record(DB_PUBKEYS, query)
# get key status
def sdb_getkeystatus(id):
global dbs
query = "select * from get_key_status(%s);" % (db_safestr(id))
db_debug_query(query)
return db_get_first_element(DB_PROFILES, query)
# set key status
def sdb_setkeystatus(id, status):
global dbs
query = "select set_key_status(%s, %s);" % (db_safestr(id), db_safestr(status))
db_debug_query(query)
db_exec(DB_PROFILES, query)
<<<<<<< /home/fdgonthier/tmp/kdatabase.py~base.9bG6qK
=======
# check if priv key exists
def sdb_privkeyexists(id):
global dbs
query = "select key_id from private_key where key_id=%s;" % ( id )
db_debug_query(query)
return db_has_record(DB_PRIVKEYS, query)
# check if pub key exists
def sdb_pubkeyexists(id):
global dbs
query = "select key_id from public_key where key_id=%s;" % ( id )
db_debug_query(query)
return db_has_record(DB_PUBKEYS, query)
>>>>>>> /home/fdgonthier/tmp/kdatabase.py~other.6jzd-D
# delete private keys
def sdb_delprivkeys(id):
global dbs
query = "select del_key(%s);" % ( db_safestr(id) )
db_debug_query(query)
db_exec(DB_PRIVKEYS, query)
# delete public keys
def sdb_delpubkeys(id):
global dbs
query = "select del_key(%s);" % ( db_safestr(id) )
db_debug_query(query)
db_exec(DB_PUBKEYS, query)
# adds a primary email to a user
def sdb_addpemail(prof_id, email):
global dbs
query = "select add_primary_email(%s, %s);" % (db_safestr(prof_id), db_safestr(email))
db_debug_query(query)
db_exec(DB_PROFILES, query)
# adds an email or email part
def sdb_addemail(prof_id, email):
global dbs
query = "select add_email(%s, %s);" % (db_safestr(prof_id), db_safestr(email))
db_debug_query(query)
db_exec(DB_PROFILES, query)
# delete an email or email part
def sdb_rmemail(prof_id, email):
global dbs
query = "select del_email(%s, %s);" % (db_safestr(prof_id), db_safestr(email))
db_debug_query(query)
db_exec(DB_PROFILES, query)
# List all email address for a profile.
def sdb_lsemail(prof_id):
global dbs
query = "select * from email_ls(%s);" % ( db_safestr(prof_id) )
db_debug_query(query)
return db_find_list_desc_data(DB_PROFILES, query)
def sdb_addgroup(org_id, group_name):
global dbs
query = "select add_group_profile(%s, %s);" % ( db_safestr(org_id), db_safestr(group_name) )
db_debug_query(query)
return db_get_first_element(DB_PROFILES, query)
def sdb_addldapgroup(group_id, group_dn):
global dbs
query = "select add_ldap_group(%s, %s);" % ( db_safestr(group_id), db_safestr(group_dn) )
db_debug_query(query)
return db_get_first_element(DB_PROFILES, query)
def sdb_lsldapgroups(group_id):
global dbs
query = "select * from ldap_groups where group_id = %s;" % ( db_safestr(group_id) )
db_debug_query(query)
return db_find_list_desc_data(DB_PROFILES, query)
def sdb_rmldapgroup(ldap_group_id):
global dbs
query = "select del_ldap_group(%s);" % ( db_safestr(ldap_group_id) )
db_debug_query(query)
db_exec(DB_PROFILES, query)
<<<<<<< /home/fdgonthier/tmp/kdatabase.py~base.9bG6qK
=======
def sdb_set_org_license(kdn, lic):
<<<<<<< /home/repos/kctl/kdatabase.py.orig.380629861
global dbs
query = "select set_org_license(%s, %s);" % ( db_safestr(kdn), db_safestr(lic) )
db_debug_query(query)
db_exec(dbs[DB_PROFILES], query)
>>>>>>> /home/fdgonthier/tmp/kdatabase.py~other.6jzd-D
||||||| /home/fdgonthier/tmp/kdatabase.py~base.D-bP8W
global dbs
query = "select set_org_license(%s, %s);" % ( db_safestr(kdn), db_safestr(lic) )
db_debug_query(query)
db_exec(dbs[DB_PROFILES], query)
=======
global dbs
query = "select set_org_license(%s, %s);" % ( db_safestr(kdn), db_safestr(lic) )
db_debug_query(query)
db_exec(DB_PROFILES, query)
>>>>>>> /home/fdgonthier/tmp/kdatabase.py~other.Lsa77p
def sdb_addorg(org_name):
global dbs
query = "select add_org(%s);" % ( db_safestr(org_name) )
db_debug_query(query)
return db_get_first_element(DB_PROFILES, query)
def sdb_rmorg(org_id):
global dbs
query = "select del_org(%s);" % ( db_safestr(org_id) )
db_debug_query(query)
db_exec(DB_PROFILES, query)
def sdb_lsorg():
global dbs
query = "select * from organization_view;"
db_debug_query(query)
return db_find_list_desc_data(DB_PROFILES, query)
def sdb_setorgforwardto(org_id, forward_to):
global dbs
query = "select set_org_forward_to(%s, %s);" % ( db_safestr(org_id), db_safestr(forward_to) )
db_debug_query(query)
db_exec(DB_PROFILES, query)
def sdb_adduser(org_id, first_name, last_name):
global dbs
query = "select add_user_profile(%s, %s, %s);" % ( db_safestr(org_id),
db_safestr(first_name), db_safestr(last_name) )
db_debug_query(query)
<<<<<<< /home/repos/kctl/kdatabase.py.orig.380629861
<<<<<<< /home/fdgonthier/tmp/kdatabase.py~base.9bG6qK
db_exec(dbs[DB_PROFILES], query)
=======
return db_get_first_element(dbs[DB_PROFILES], query)
>>>>>>> /home/fdgonthier/tmp/kdatabase.py~other.6jzd-D
||||||| /home/fdgonthier/tmp/kdatabase.py~base.D-bP8W
return db_get_first_element(dbs[DB_PROFILES], query)
=======
return db_get_first_element(DB_PROFILES, query)
>>>>>>> /home/fdgonthier/tmp/kdatabase.py~other.Lsa77p
def sdb_rmprofile(prof_id):
global dbs
query = "select del_profile(%s);" % ( db_safestr(prof_id) )
db_debug_query(query)
db_exec(DB_PROFILES, query)
<<<<<<< /home/fdgonthier/tmp/kdatabase.py~base.9bG6qK
=======
>>>>>>> /home/fdgonthier/tmp/kdatabase.py~other.6jzd-D
def sdb_lsprofiles():
global dbs
query = "select * from profiles_view;"
db_debug_query(query)
return db_find_list_desc_data(DB_PROFILES, query)
def sdb_addlogin(org_id, prof_id, username, password):
global dbs
query = "select add_login(%s, %s, %s, %s);" % ( db_safestr(username), db_safestr(password), db_safestr(prof_id), db_safestr(org_id) )
db_debug_query(query)
db_exec(DB_LOGIN, query)
<<<<<<< /home/fdgonthier/tmp/kdatabase.py~base.9bG6qK
=======
>>>>>>> /home/fdgonthier/tmp/kdatabase.py~other.6jzd-D
def sdb_rmlogin(login):
global dbs
query = "select del_login(%s);" % ( db_safestr(login) )
db_debug_query(query)
db_exec(DB_LOGIN, query)
<<<<<<< /home/fdgonthier/tmp/kdatabase.py~base.9bG6qK
=======
>>>>>>> /home/fdgonthier/tmp/kdatabase.py~other.6jzd-D
def sdb_lslogin():
global dbs
query = "select * from login_view;"
db_debug_query(query)
return db_find_list_desc_data(DB_LOGIN, query)
def sdb_setkey(prof_id, key_id):
global dbs
query = "select set_key(%s, %s);" % ( db_safestr(prof_id), db_safestr(key_id) )
db_debug_query(query)
db_exec(DB_PROFILES, query)
def sdb_disownkey(prof_id):
global dbs
query = "select unset_key(%s);" % ( db_safestr(prof_id) )
db_debug_query(query)
db_exec(DB_PROFILES, query)
def sdb_lskeys():
global dbs
query = "select * from pkey_view;"
db_debug_query(query)
return db_find_list_desc_data(DB_PUBKEYS, query)
def sdb_reseller_allocated_seats(org_id):
global dbs
query = "select sum(num) from login_seats_allocation where parent_org_id = %s" % ( db_safestr(org_id) )
db_debug_query(query)
return db_get_first_element(DB_LOGIN, query)
<<<<<<< /home/repos/kctl/kdatabase.py.orig.380629861
<<<<<<< /home/fdgonthier/tmp/kdatabase.py~base.9bG6qK
=======
def sdb_set_reseller_seats(counttotal, best_before):
||||||| /home/fdgonthier/tmp/kdatabase.py~base.D-bP8W
def sdb_set_reseller_seats(counttotal, best_before):
=======
def sdb_allocated_seats(org_id):
>>>>>>> /home/fdgonthier/tmp/kdatabase.py~other.Lsa77p
global dbs
query = "select num from login_seats_allocation where org_id = %s" % ( db_safestr(org_id) )
db_debug_query(query)
return db_get_first_element(DB_LOGIN, query)
def sdb_setseatsallocation(p_org_id, org_id, number):
global dbs
query = "select set_seats_allocation(%s, %s, %s);" % ( db_safestr(p_org_id), db_safestr(org_id), db_safestr(number) )
db_debug_query(query)
db_exec(DB_LOGIN, query)
def sdb_lsseatsallocation():
global dbs
query = "select * from login_seats_allocation"
db_debug_query(query)
return db_find_list_desc_data(DB_LOGIN, query)
def sdb_lsseatsorg(org_id):
global dbs
query = "select user_name from login_seats where org_id=%s" % ( db_safestr(org_id) )
db_debug_query(query)
return db_find_list_desc_data(DB_LOGIN, query)
<<<<<<< /home/repos/kctl/kdatabase.py.orig.380629861
>>>>>>> /home/fdgonthier/tmp/kdatabase.py~other.6jzd-D
def sdb_lslicenses():
||||||| /home/fdgonthier/tmp/kdatabase.py~base.D-bP8W
def sdb_lslicenses():
=======
def sdb_freeloginseat(org_id, username):
>>>>>>> /home/fdgonthier/tmp/kdatabase.py~other.Lsa77p
global dbs
query = "select free_login_seat(%s, %s);" % ( db_safestr(org_id), db_safestr(username) )
db_debug_query(query)
db_exec(DB_LOGIN, query)
<<<<<<< /home/fdgonthier/tmp/kdatabase.py~base.9bG6qK
=======
def sdb_importpubkey(text, id, owner, data):
global dbs
query = "select import_key(%s, %s, %s, %s);" % ( db_safestr(text), db_safestr(id), db_safestr(owner), db_safestr(data) )
db_debug_query(query)
return db_exec(DB_PUBKEYS, query)
def sdb_importprivkey(text, id, owner, data):
global dbs
query = "select import_key(%s, %s, %s, %s);" % ( db_safestr(text), db_safestr(id), db_safestr(owner), db_safestr(data) )
db_debug_query(query)
return db_exec(DB_PRIVKEYS, query)
def sdb_getpubkey(text, id):
global dbs
query = "select key_data from %s_key where key_id=%s;" % ( text, db_safestr(id) )
db_debug_query(query)
return db_get_first_element(DB_PUBKEYS, query)
def sdb_getprivkey(text, id):
global dbs
query = "select key_data from %s_key where key_id=%s;" % ( text, db_safestr(id) )
db_debug_query(query)
return db_get_first_element(DB_PRIVKEYS, query)
# returns a list: [nb of packagings in the specified (inclusive) interval, total time as h:m:s]
# expects start and stop as UTC seconds from epoch
def sdb_getstatspackagings(start, stop, type=None):
global dbs
# timestamps are stored as "Y-m-d h:m:s" (local time)
start = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(start))
stop = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(stop))
query = "select count(*), sum(disconntime - conntime)"
query = query + " from packagings"
query = query + " where conntime>='%s' and disconntime<='%s'" % ( start, stop )
if type != None:
query = query + " and pkg_type='%d'" % ( type )
db_debug_query(query)
return db_get_first_row(DB_EVENTS, query)
# returns a list: [nb of processings in the specified (inclusive) interval, total time as h:m:s]
# expects start and stop as UTC seconds from epoch
def sdb_getstatsprocessings(start, stop, type=None):
global dbs
# timestamps are stored as "Y-m-d h:m:s" (local time)
start = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(start))
stop = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(stop))
query = "select count(*), sum(disconntime - conntime)"
query = query + " from processings"
query = query + " where conntime>='%s' and disconntime<='%s'" % ( start, stop )
if type != None:
query = query + " and pkg_type='%d'" % ( type )
db_debug_query(query)
return db_get_first_row(DB_EVENTS, query)
<<<<<<< local
>>>>>>> /home/fdgonthier/tmp/kdatabase.py~other.6jzd-D
=======
>>>>>>> other
### BEGIN_TESTS ###
def sdb_test_org_all():
print "org_all()"
org_id = sdb_addorg("lalala")
print sdb_lsorg()
sdb_setorgforwardto(org_id, "lalalala@lilililili")
print sdb_lsorg()
sdb_setorgforwardto(org_id, "")
print sdb_lsorg()
sdb_rmorg(org_id)
try:
out("SHOULD FAIL:")
sdb_rmorg(org_id)
except:
out("FAILED")
print "OK"
# FOR LDAP
def sdb_test_group_all():
print "group_all()"
org_id = sdb_addorg("lalala")
prof_id = sdb_addgroup(org_id, "group1")
sdb_rmprofile(prof_id)
try:
out("SHOULD FAIL:")
sdb_rmprofile(prof_id)
except:
out("FAILED")
print "OK"
# FOR LOCAL USERS
def sdb_test_user_all():
print "user_all()"
try:
out("SHOULD FAIL:")
prof_id = sdb_adduser("99112045945", "Mathieu", "Martin")
except:
out("FAILED")
org_id = sdb_addorg("lalala")
prof_id = sdb_adduser(org_id, "Mathieu", "Martin")
sdb_rmprofile(prof_id)
try:
out("SHOULD FAIL:")
sdb_rmprofile(prof_id)
except:
out("FAILED")
print "OK"
def sdb_test_sdb_email_all():
print "email_all()"
try:
out("SHOULD FAIL:")
sdb_addemail("99112045945", "mathieu@mathieu")
except:
out("FAILED")
try: