]> git.sur5r.net Git - openldap/blob - servers/slapd/backend.c
Check for HAVE_LONG_LONG in STRTOL macro
[openldap] / servers / slapd / backend.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /* backend.c - routines for dealing with back-end databases */
7
8
9 #include "portable.h"
10
11 #include <stdio.h>
12
13 #include <ac/string.h>
14 #include <ac/socket.h>
15 #include <sys/stat.h>
16
17 #include "slap.h"
18 #include "lutil.h"
19 #include "lber_pvt.h"
20
21 #include "ldap_rq.h"
22
23 #ifdef LDAP_SLAPI
24 #include "slapi.h"
25 #endif
26
27 /*
28  * If a module is configured as dynamic, its header should not
29  * get included into slapd. While this is a general rule and does
30  * not have much of an effect in UNIX, this rule should be adhered
31  * to for Windows, where dynamic object code should not be implicitly
32  * imported into slapd without appropriate __declspec(dllimport) directives.
33  */
34
35 #if defined(SLAPD_BDB) && !defined(SLAPD_BDB_DYNAMIC)
36 #include "back-bdb/external.h"
37 #endif
38 #if defined(SLAPD_DNSSRV) && !defined(SLAPD_DNSSRV_DYNAMIC)
39 #include "back-dnssrv/external.h"
40 #endif
41 #if defined(SLAPD_HDB) && !defined(SLAPD_HDB_DYNAMIC)
42 #include "back-hdb/external.h"
43 #endif
44 #if defined(SLAPD_LDAP) && !defined(SLAPD_LDAP_DYNAMIC)
45 #include "back-ldap/external.h"
46 #endif
47 #if defined(SLAPD_LDBM) && !defined(SLAPD_LDBM_DYNAMIC)
48 #include "back-ldbm/external.h"
49 #endif
50 #if defined(SLAPD_META) && !defined(SLAPD_META_DYNAMIC)
51 #include "back-meta/external.h"
52 #endif
53 #if defined(SLAPD_MONITOR) && !defined(SLAPD_MONITOR_DYNAMIC)
54 #include "back-monitor/external.h"
55 #endif
56 #if defined(SLAPD_NULL) && !defined(SLAPD_NULL_DYNAMIC)
57 #include "back-null/external.h"
58 #endif
59 #if defined(SLAPD_PASSWD) && !defined(SLAPD_PASSWD_DYNAMIC)
60 #include "back-passwd/external.h"
61 #endif
62 #if defined(SLAPD_PERL) && !defined(SLAPD_PERL_DYNAMIC)
63 #include "back-perl/external.h"
64 #endif
65 #if defined(SLAPD_SHELL) && !defined(SLAPD_SHELL_DYNAMIC)
66 #include "back-shell/external.h"
67 #endif
68 #if defined(SLAPD_TCL) && !defined(SLAPD_TCL_DYNAMIC)
69 #include "back-tcl/external.h"
70 #endif
71 #if defined(SLAPD_SQL) && !defined(SLAPD_SQL_DYNAMIC)
72 #include "back-sql/external.h"
73 #endif
74 #if defined(SLAPD_PRIVATE) && !defined(SLAPD_PRIVATE_DYNAMIC)
75 #include "private/external.h"
76 #endif
77
78 static BackendInfo binfo[] = {
79 #if defined(SLAPD_BDB) && !defined(SLAPD_BDB_DYNAMIC)
80         {"bdb", bdb_initialize},
81 #endif
82 #if defined(SLAPD_DNSSRV) && !defined(SLAPD_DNSSRV_DYNAMIC)
83         {"dnssrv",      dnssrv_back_initialize},
84 #endif
85 #if defined(SLAPD_HDB) && !defined(SLAPD_HDB_DYNAMIC)
86         {"hdb", hdb_initialize},
87 #endif
88 #if defined(SLAPD_LDAP) && !defined(SLAPD_LDAP_DYNAMIC)
89         {"ldap",        ldap_back_initialize},
90 #endif
91 #if defined(SLAPD_LDBM) && !defined(SLAPD_LDBM_DYNAMIC)
92         {"ldbm",        ldbm_back_initialize},
93 #endif
94 #if defined(SLAPD_META) && !defined(SLAPD_META_DYNAMIC)
95         {"meta",        meta_back_initialize},
96 #endif
97 #if defined(SLAPD_MONITOR) && !defined(SLAPD_MONITOR_DYNAMIC)
98         {"monitor",     monitor_back_initialize},
99 #endif
100 #if defined(SLAPD_NULL) && !defined(SLAPD_NULL_DYNAMIC)
101         {"null",        null_back_initialize},
102 #endif
103 #if defined(SLAPD_PASSWD) && !defined(SLAPD_PASSWD_DYNAMIC)
104         {"passwd",      passwd_back_initialize},
105 #endif
106 #if defined(SLAPD_PERL) && !defined(SLAPD_PERL_DYNAMIC)
107         {"perl",        perl_back_initialize},
108 #endif
109 #if defined(SLAPD_SHELL) && !defined(SLAPD_SHELL_DYNAMIC)
110         {"shell",       shell_back_initialize},
111 #endif
112 #if defined(SLAPD_TCL) && !defined(SLAPD_TCL_DYNAMIC)
113         {"tcl",         tcl_back_initialize},
114 #endif
115 #if defined(SLAPD_SQL) && !defined(SLAPD_SQL_DYNAMIC)
116         {"sql",         sql_back_initialize},
117 #endif
118         /* for any private backend */
119 #if defined(SLAPD_PRIVATE) && !defined(SLAPD_PRIVATE_DYNAMIC)
120         {"private",     private_back_initialize},
121 #endif
122         {NULL}
123 };
124
125 int                     nBackendInfo = 0;
126 BackendInfo     *backendInfo = NULL;
127
128 int                     nBackendDB = 0; 
129 BackendDB       *backendDB = NULL;
130
131 ldap_pvt_thread_pool_t  syncrepl_pool;
132 int                     syncrepl_pool_max = SLAP_MAX_SYNCREPL_THREADS;
133
134 int backend_init(void)
135 {
136         int rc = -1;
137
138         ldap_pvt_thread_pool_init( &syncrepl_pool, syncrepl_pool_max, 0 );
139
140         if((nBackendInfo != 0) || (backendInfo != NULL)) {
141                 /* already initialized */
142 #ifdef NEW_LOGGING
143                 LDAP_LOG( BACKEND, ERR, 
144                         "backend_init:  backend already initialized\n", 0, 0, 0 );
145 #else
146                 Debug( LDAP_DEBUG_ANY,
147                         "backend_init: already initialized.\n", 0, 0, 0 );
148 #endif
149                 return -1;
150         }
151
152         for( ;
153                 binfo[nBackendInfo].bi_type != NULL;
154                 nBackendInfo++ )
155         {
156                 rc = binfo[nBackendInfo].bi_init( &binfo[nBackendInfo] );
157
158                 if(rc != 0) {
159 #ifdef NEW_LOGGING
160                         LDAP_LOG( BACKEND, INFO, 
161                                 "backend_init:  initialized for type \"%s\"\n",
162                                 binfo[nBackendInfo].bi_type, 0, 0 );
163 #else
164                         Debug( LDAP_DEBUG_ANY,
165                                 "backend_init: initialized for type \"%s\"\n",
166                                 binfo[nBackendInfo].bi_type, 0, 0 );
167 #endif
168                         /* destroy those we've already inited */
169                         for( nBackendInfo--;
170                                 nBackendInfo >= 0 ;
171                                 nBackendInfo-- )
172                         { 
173                                 if ( binfo[nBackendInfo].bi_destroy ) {
174                                         binfo[nBackendInfo].bi_destroy(
175                                                 &binfo[nBackendInfo] );
176                                 }
177                         }
178                         return rc;
179                 }
180         }
181
182         if ( nBackendInfo > 0) {
183                 backendInfo = binfo;
184                 return 0;
185         }
186
187 #ifdef SLAPD_MODULES    
188         return 0;
189 #else
190
191 #ifdef NEW_LOGGING
192         LDAP_LOG( BACKEND, ERR, "backend_init: failed\n", 0, 0, 0 );
193 #else
194         Debug( LDAP_DEBUG_ANY,
195                 "backend_init: failed\n",
196                 0, 0, 0 );
197 #endif
198
199         return rc;
200 #endif /* SLAPD_MODULES */
201 }
202
203 int backend_add(BackendInfo *aBackendInfo)
204 {
205    int rc = 0;
206
207    if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
208 #ifdef NEW_LOGGING
209         LDAP_LOG( BACKEND, ERR, 
210                   "backend_add:  initialization for type \"%s\" failed\n",
211                   aBackendInfo->bi_type, 0, 0 );
212 #else
213       Debug( LDAP_DEBUG_ANY,
214              "backend_add: initialization for type \"%s\" failed\n",
215              aBackendInfo->bi_type, 0, 0 );
216 #endif
217       return rc;
218    }
219
220    /* now add the backend type to the Backend Info List */
221    {
222       BackendInfo *newBackendInfo = 0;
223
224       /* if backendInfo == binfo no deallocation of old backendInfo */
225       if (backendInfo == binfo) {
226          newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
227          AC_MEMCPY(newBackendInfo, backendInfo, sizeof(BackendInfo) * 
228                 nBackendInfo);
229       } else {
230          newBackendInfo = ch_realloc(backendInfo, sizeof(BackendInfo) * 
231                                      (nBackendInfo + 1));
232       }
233       AC_MEMCPY(&newBackendInfo[nBackendInfo], aBackendInfo, 
234              sizeof(BackendInfo));
235       backendInfo = newBackendInfo;
236       nBackendInfo++;
237
238       return 0;
239    }        
240 }
241
242 int backend_startup(Backend *be)
243 {
244         int i;
245         int rc = 0;
246
247         init_syncrepl();
248
249         if( ! ( nBackendDB > 0 ) ) {
250                 /* no databases */
251 #ifdef NEW_LOGGING
252                 LDAP_LOG( BACKEND, INFO, 
253                         "backend_startup: %d databases to startup. \n", nBackendDB, 0, 0 );
254 #else
255                 Debug( LDAP_DEBUG_ANY,
256                         "backend_startup: %d databases to startup.\n",
257                         nBackendDB, 0, 0 );
258 #endif
259                 return 1;
260         }
261
262         if(be != NULL) {
263                 /* startup a specific backend database */
264
265                 LDAP_TAILQ_INIT( &be->be_pending_csn_list );
266
267 #ifdef NEW_LOGGING
268                 LDAP_LOG( BACKEND, DETAIL1, "backend_startup:  starting \"%s\"\n",
269                            be->be_suffix[0].bv_val, 0, 0 );
270 #else
271                 Debug( LDAP_DEBUG_TRACE,
272                         "backend_startup: starting \"%s\"\n",
273                         be->be_suffix[0].bv_val, 0, 0 );
274 #endif
275
276                 if ( be->bd_info->bi_open ) {
277                         rc = be->bd_info->bi_open( be->bd_info );
278                         if ( rc != 0 ) {
279 #ifdef NEW_LOGGING
280                                 LDAP_LOG( BACKEND, CRIT, "backend_startup: bi_open failed!\n", 0, 0, 0 );
281 #else
282                                 Debug( LDAP_DEBUG_ANY,
283                                         "backend_startup: bi_open failed!\n",
284                                         0, 0, 0 );
285 #endif
286
287                                 return rc;
288                         }
289                 }
290
291                 if ( be->bd_info->bi_db_open ) {
292                         rc = be->bd_info->bi_db_open( be );
293                         if ( rc != 0 ) {
294 #ifdef NEW_LOGGING
295                                 LDAP_LOG( BACKEND, CRIT, 
296                                         "backend_startup: bi_db_open failed! (%d)\n", rc, 0, 0 );
297 #else
298                                 Debug( LDAP_DEBUG_ANY,
299                                         "backend_startup: bi_db_open failed! (%d)\n",
300                                         rc, 0, 0 );
301 #endif
302                                 return rc;
303                         }
304                 }
305
306                 return rc;
307         }
308
309         /* open each backend type */
310         for( i = 0; i < nBackendInfo; i++ ) {
311                 if( backendInfo[i].bi_nDB == 0) {
312                         /* no database of this type, don't open */
313                         continue;
314                 }
315
316                 if( backendInfo[i].bi_open ) {
317                         rc = backendInfo[i].bi_open(
318                                 &backendInfo[i] );
319                         if ( rc != 0 ) {
320 #ifdef NEW_LOGGING
321                                 LDAP_LOG( BACKEND, CRIT, 
322                                         "backend_startup: bi_open %d failed!\n", i, 0, 0 );
323 #else
324                                 Debug( LDAP_DEBUG_ANY,
325                                         "backend_startup: bi_open %d failed!\n",
326                                         i, 0, 0 );
327 #endif
328                                 return rc;
329                         }
330                 }
331         }
332
333         ldap_pvt_thread_mutex_init( &syncrepl_rq.rq_mutex );
334         LDAP_STAILQ_INIT( &syncrepl_rq.task_list );
335         LDAP_STAILQ_INIT( &syncrepl_rq.run_list );
336
337         /* open each backend database */
338         for( i = 0; i < nBackendDB; i++ ) {
339                 /* append global access controls */
340                 acl_append( &backendDB[i].be_acl, global_acl );
341
342                 LDAP_TAILQ_INIT( &backendDB[i].be_pending_csn_list );
343
344                 if ( backendDB[i].bd_info->bi_db_open ) {
345                         rc = backendDB[i].bd_info->bi_db_open(
346                                 &backendDB[i] );
347                         if ( rc != 0 ) {
348 #ifdef NEW_LOGGING
349                                 LDAP_LOG( BACKEND, CRIT, 
350                                         "backend_startup: bi_db_open(%d) failed! (%d)\n", i, rc, 0 );
351 #else
352                                 Debug( LDAP_DEBUG_ANY,
353                                         "backend_startup: bi_db_open(%d) failed! (%d)\n",
354                                         i, rc, 0 );
355 #endif
356                                 return rc;
357                         }
358                 }
359
360                 if ( backendDB[i].syncinfo != NULL ) {
361                         syncinfo_t *si = ( syncinfo_t * ) backendDB[i].syncinfo;
362                         si->be = &backendDB[i];
363                         ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
364                         ldap_pvt_runqueue_insert( &syncrepl_rq, si->interval,
365                                                         do_syncrepl, (void *) backendDB[i].syncinfo );
366                         ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
367                 }
368         }
369
370         return rc;
371 }
372
373 int backend_num( Backend *be )
374 {
375         int i;
376
377         if( be == NULL ) return -1;
378
379         for( i = 0; i < nBackendDB; i++ ) {
380                 if( be == &backendDB[i] ) return i;
381         }
382         return -1;
383 }
384
385 int backend_shutdown( Backend *be )
386 {
387         int i;
388         int rc = 0;
389
390         if( be != NULL ) {
391                 /* shutdown a specific backend database */
392
393                 if ( be->bd_info->bi_nDB == 0 ) {
394                         /* no database of this type, we never opened it */
395                         return 0;
396                 }
397
398                 if ( be->bd_info->bi_db_close ) {
399                         be->bd_info->bi_db_close( be );
400                 }
401
402                 if( be->bd_info->bi_close ) {
403                         be->bd_info->bi_close( be->bd_info );
404                 }
405
406                 return 0;
407         }
408
409         /* close each backend database */
410         for( i = 0; i < nBackendDB; i++ ) {
411                 if ( backendDB[i].bd_info->bi_db_close ) {
412                         backendDB[i].bd_info->bi_db_close(
413                                 &backendDB[i] );
414                 }
415
416                 if(rc != 0) {
417 #ifdef NEW_LOGGING
418                         LDAP_LOG( BACKEND, NOTICE, 
419                                 "backend_shutdown: bi_close %s failed!\n",
420                                 backendDB[i].be_type, 0, 0 );
421 #else
422                         Debug( LDAP_DEBUG_ANY,
423                                 "backend_close: bi_close %s failed!\n",
424                                 backendDB[i].be_type, 0, 0 );
425 #endif
426                 }
427         }
428
429         /* close each backend type */
430         for( i = 0; i < nBackendInfo; i++ ) {
431                 if( backendInfo[i].bi_nDB == 0 ) {
432                         /* no database of this type */
433                         continue;
434                 }
435
436                 if( backendInfo[i].bi_close ) {
437                         backendInfo[i].bi_close(
438                                 &backendInfo[i] );
439                 }
440         }
441
442         return 0;
443 }
444
445 int backend_destroy(void)
446 {
447         int i;
448         BackendDB *bd;
449
450         ldap_pvt_thread_pool_destroy( &syncrepl_pool, 1 );
451
452         /* destroy each backend database */
453         for( i = 0, bd = backendDB; i < nBackendDB; i++, bd++ ) {
454                 if ( bd->bd_info->bi_db_destroy ) {
455                         bd->bd_info->bi_db_destroy( bd );
456                 }
457                 ber_bvarray_free( bd->be_suffix );
458                 ber_bvarray_free( bd->be_nsuffix );
459                 if ( bd->be_rootdn.bv_val ) free( bd->be_rootdn.bv_val );
460                 if ( bd->be_rootndn.bv_val ) free( bd->be_rootndn.bv_val );
461                 if ( bd->be_rootpw.bv_val ) free( bd->be_rootpw.bv_val );
462                 acl_destroy( bd->be_acl, global_acl );
463         }
464         free( backendDB );
465
466         /* destroy each backend type */
467         for( i = 0; i < nBackendInfo; i++ ) {
468                 if( backendInfo[i].bi_destroy ) {
469                         backendInfo[i].bi_destroy(
470                                 &backendInfo[i] );
471                 }
472         }
473
474 #ifdef SLAPD_MODULES
475         if (backendInfo != binfo) {
476            free(backendInfo);
477         }
478 #endif /* SLAPD_MODULES */
479
480         nBackendInfo = 0;
481         backendInfo = NULL;
482
483         return 0;
484 }
485
486 BackendInfo* backend_info(const char *type)
487 {
488         int i;
489
490         /* search for the backend type */
491         for( i = 0; i < nBackendInfo; i++ ) {
492                 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
493                         return &backendInfo[i];
494                 }
495         }
496
497         return NULL;
498 }
499
500
501 BackendDB *
502 backend_db_init(
503     const char  *type
504 )
505 {
506         Backend *be;
507         BackendInfo *bi = backend_info(type);
508         int     rc = 0;
509
510         if( bi == NULL ) {
511                 fprintf( stderr, "Unrecognized database type (%s)\n", type );
512                 return NULL;
513         }
514
515         backendDB = (BackendDB *) ch_realloc(
516                         (char *) backendDB,
517                     (nBackendDB + 1) * sizeof(Backend) );
518
519         memset( &backendDB[nbackends], '\0', sizeof(Backend) );
520
521         be = &backends[nbackends++];
522
523         be->bd_info = bi;
524         be->be_def_limit = deflimit;
525         be->be_dfltaccess = global_default_access;
526
527         be->be_restrictops = global_restrictops;
528         be->be_requires = global_requires;
529         be->be_ssf_set = global_ssf_set;
530
531         be->be_context_csn.bv_len = 0;
532         be->be_context_csn.bv_val = NULL;
533         ldap_pvt_thread_mutex_init( &be->be_pcl_mutex );
534         ldap_pvt_thread_mutex_init( &be->be_context_csn_mutex );
535
536         be->syncinfo = NULL;
537
538         /* assign a default depth limit for alias deref */
539         be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH; 
540
541         if(bi->bi_db_init) {
542                 rc = bi->bi_db_init( be );
543         }
544
545         if(rc != 0) {
546                 fprintf( stderr, "database init failed (%s)\n", type );
547                 nbackends--;
548                 return NULL;
549         }
550
551         bi->bi_nDB++;
552         return( be );
553 }
554
555 void
556 be_db_close( void )
557 {
558         int     i;
559
560         for ( i = 0; i < nbackends; i++ ) {
561                 if ( backends[i].bd_info->bi_db_close ) {
562                         (*backends[i].bd_info->bi_db_close)( &backends[i] );
563                 }
564         }
565 }
566
567 Backend *
568 select_backend(
569         struct berval * dn,
570         int manageDSAit,
571         int noSubs )
572 {
573         int     i, j;
574         ber_len_t len, dnlen = dn->bv_len;
575         Backend *be = NULL;
576
577         for ( i = 0; i < nbackends; i++ ) {
578                 for ( j = 0; backends[i].be_nsuffix != NULL &&
579                     backends[i].be_nsuffix[j].bv_val != NULL; j++ )
580                 {
581                         if ( ( SLAP_GLUE_SUBORDINATE( &backends[i] ) )
582                                 && noSubs )
583                         {
584                                 continue;
585                         }
586
587                         len = backends[i].be_nsuffix[j].bv_len;
588
589                         if ( len > dnlen ) {
590                                 /* suffix is longer than DN */
591                                 continue;
592                         }
593                         
594                         /*
595                          * input DN is normalized, so the separator check
596                          * need not look at escaping
597                          */
598                         if ( len && len < dnlen &&
599                                 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
600                         {
601                                 continue;
602                         }
603
604                         if ( strcmp( backends[i].be_nsuffix[j].bv_val,
605                                 &dn->bv_val[dnlen-len] ) == 0 )
606                         {
607                                 if( be == NULL ) {
608                                         be = &backends[i];
609
610                                         if( manageDSAit && len == dnlen ) {
611                                                 continue;
612                                         }
613                                 } else {
614                                         be = &backends[i];
615                                 }
616                                 return be;
617                         }
618                 }
619         }
620
621         return be;
622 }
623
624 int
625 be_issuffix(
626     Backend     *be,
627     struct berval       *bvsuffix
628 )
629 {
630         int     i;
631
632         for ( i = 0; be->be_nsuffix != NULL && be->be_nsuffix[i].bv_val != NULL; i++ ) {
633                 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
634                         return( 1 );
635                 }
636         }
637
638         return( 0 );
639 }
640
641 int
642 be_isroot( Backend *be, struct berval *ndn )
643 {
644         if ( !ndn->bv_len ) {
645                 return( 0 );
646         }
647
648         if ( !be->be_rootndn.bv_len ) {
649                 return( 0 );
650         }
651
652         return dn_match( &be->be_rootndn, ndn );
653 }
654
655 int
656 be_isupdate( Backend *be, struct berval *ndn )
657 {
658         if ( !ndn->bv_len ) {
659                 return( 0 );
660         }
661
662         if ( !be->be_update_ndn.bv_len ) {
663                 return( 0 );
664         }
665
666         return dn_match( &be->be_update_ndn, ndn );
667 }
668
669 struct berval *
670 be_root_dn( Backend *be )
671 {
672         return &be->be_rootdn;
673 }
674
675 int
676 be_isroot_pw( Operation *op )
677 {
678         int result;
679         char *errmsg;
680
681         if ( ! be_isroot( op->o_bd, &op->o_req_ndn ) ) {
682                 return 0;
683         }
684
685         if( op->o_bd->be_rootpw.bv_len == 0 ) {
686                 return 0;
687         }
688
689 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
690         ldap_pvt_thread_mutex_lock( &passwd_mutex );
691 #ifdef SLAPD_SPASSWD
692         lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
693 #endif
694 #endif
695
696         result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
697
698 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
699 #ifdef SLAPD_SPASSWD
700         lutil_passwd_sasl_conn = NULL;
701 #endif
702         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
703 #endif
704
705         return result == 0;
706 }
707
708 int
709 be_entry_release_rw(
710         Operation *op,
711         Entry *e,
712         int rw )
713 {
714         if ( op->o_bd->be_release ) {
715                 /* free and release entry from backend */
716                 return op->o_bd->be_release( op, e, rw );
717         } else {
718                 /* free entry */
719                 entry_free( e );
720                 return 0;
721         }
722 }
723
724 int
725 backend_unbind( Operation *op, SlapReply *rs )
726 {
727         int             i;
728 #if defined( LDAP_SLAPI )
729         Slapi_PBlock *pb = op->o_pb;
730
731         int     rc;
732         slapi_x_pblock_set_operation( pb, op );
733 #endif /* defined( LDAP_SLAPI ) */
734
735         for ( i = 0; i < nbackends; i++ ) {
736 #if defined( LDAP_SLAPI )
737                 slapi_pblock_set( pb, SLAPI_BACKEND, (void *)&backends[i] );
738                 rc = doPluginFNs( &backends[i], SLAPI_PLUGIN_PRE_UNBIND_FN,
739                                 (Slapi_PBlock *)pb );
740                 if ( rc < 0 ) {
741                         /*
742                          * A preoperation plugin failure will abort the
743                          * entire operation.
744                          */
745 #ifdef NEW_LOGGING
746                         LDAP_LOG( OPERATION, INFO, "do_bind: Unbind preoperation plugin "
747                                         "failed\n", 0, 0, 0);
748 #else
749                         Debug(LDAP_DEBUG_TRACE, "do_bind: Unbind preoperation plugin "
750                                         "failed.\n", 0, 0, 0);
751 #endif
752                         return 0;
753                 }
754 #endif /* defined( LDAP_SLAPI ) */
755
756                 if ( backends[i].be_unbind ) {
757                         op->o_bd = &backends[i];
758                         (*backends[i].be_unbind)( op, rs );
759                 }
760
761 #if defined( LDAP_SLAPI )
762                 if ( doPluginFNs( &backends[i], SLAPI_PLUGIN_POST_UNBIND_FN,
763                                 (Slapi_PBlock *)pb ) < 0 ) {
764 #ifdef NEW_LOGGING
765                         LDAP_LOG( OPERATION, INFO, "do_unbind: Unbind postoperation plugins "
766                                         "failed\n", 0, 0, 0);
767 #else
768                         Debug(LDAP_DEBUG_TRACE, "do_unbind: Unbind postoperation plugins "
769                                         "failed.\n", 0, 0, 0);
770 #endif
771                 }
772 #endif /* defined( LDAP_SLAPI ) */
773         }
774
775         return 0;
776 }
777
778 int
779 backend_connection_init(
780         Connection   *conn
781 )
782 {
783         int     i;
784
785         for ( i = 0; i < nbackends; i++ ) {
786                 if ( backends[i].be_connection_init ) {
787                         (*backends[i].be_connection_init)( &backends[i], conn);
788                 }
789         }
790
791         return 0;
792 }
793
794 int
795 backend_connection_destroy(
796         Connection   *conn
797 )
798 {
799         int     i;
800
801         for ( i = 0; i < nbackends; i++ ) {
802                 if ( backends[i].be_connection_destroy ) {
803                         (*backends[i].be_connection_destroy)( &backends[i], conn);
804                 }
805         }
806
807         return 0;
808 }
809
810 static int
811 backend_check_controls(
812         Operation *op,
813         SlapReply *rs )
814 {
815         LDAPControl **ctrls = op->o_ctrls;
816         rs->sr_err = LDAP_SUCCESS;
817
818         if( ctrls ) {
819                 for( ; *ctrls != NULL ; ctrls++ ) {
820                         if( (*ctrls)->ldctl_iscritical && !ldap_charray_inlist(
821                                 op->o_bd->be_controls, (*ctrls)->ldctl_oid ) )
822                         {
823                                 rs->sr_text = "control unavailable in context";
824                                 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
825                                 break;
826                         }
827                 }
828         }
829
830         return rs->sr_err;
831 }
832
833 int
834 backend_check_restrictions(
835         Operation *op,
836         SlapReply *rs,
837         struct berval *opdata )
838 {
839         slap_mask_t restrictops;
840         slap_mask_t requires;
841         slap_mask_t opflag;
842         slap_ssf_set_t *ssf;
843         int updateop = 0;
844         int starttls = 0;
845         int session = 0;
846
847         if( op->o_bd ) {
848                 if ( backend_check_controls( op, rs ) != LDAP_SUCCESS ) {
849                         return rs->sr_err;
850                 }
851
852                 restrictops = op->o_bd->be_restrictops;
853                 requires = op->o_bd->be_requires;
854                 ssf = &op->o_bd->be_ssf_set;
855
856         } else {
857                 restrictops = global_restrictops;
858                 requires = global_requires;
859                 ssf = &global_ssf_set;
860         }
861
862         switch( op->o_tag ) {
863         case LDAP_REQ_ADD:
864                 opflag = SLAP_RESTRICT_OP_ADD;
865                 updateop++;
866                 break;
867         case LDAP_REQ_BIND:
868                 opflag = SLAP_RESTRICT_OP_BIND;
869                 session++;
870                 break;
871         case LDAP_REQ_COMPARE:
872                 opflag = SLAP_RESTRICT_OP_COMPARE;
873                 break;
874         case LDAP_REQ_DELETE:
875                 updateop++;
876                 opflag = SLAP_RESTRICT_OP_DELETE;
877                 break;
878         case LDAP_REQ_EXTENDED:
879                 opflag = SLAP_RESTRICT_OP_EXTENDED;
880
881                 if( !opdata ) {
882                         /* treat unspecified as a modify */
883                         opflag = SLAP_RESTRICT_OP_MODIFY;
884                         updateop++;
885                         break;
886                 }
887
888                 {
889                         if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
890                                 session++;
891                                 starttls++;
892                                 break;
893                         }
894                 }
895
896                 {
897                         if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
898                                 break;
899                         }
900                 }
901
902 #ifdef LDAP_EXOP_X_CANCEL
903                 {
904                         if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
905                                 break;
906                         }
907                 }
908 #endif
909
910                 /* treat everything else as a modify */
911                 opflag = SLAP_RESTRICT_OP_MODIFY;
912                 updateop++;
913                 break;
914
915         case LDAP_REQ_MODIFY:
916                 updateop++;
917                 opflag = SLAP_RESTRICT_OP_MODIFY;
918                 break;
919         case LDAP_REQ_RENAME:
920                 updateop++;
921                 opflag = SLAP_RESTRICT_OP_RENAME;
922                 break;
923         case LDAP_REQ_SEARCH:
924                 opflag = SLAP_RESTRICT_OP_SEARCH;
925                 break;
926         case LDAP_REQ_UNBIND:
927                 session++;
928                 opflag = 0;
929                 break;
930         default:
931                 rs->sr_text = "restrict operations internal error";
932                 rs->sr_err = LDAP_OTHER;
933                 return rs->sr_err;
934         }
935
936         if ( !starttls ) {
937                 /* these checks don't apply to StartTLS */
938
939                 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
940                 if( op->o_transport_ssf < ssf->sss_transport ) {
941                         rs->sr_text = "transport confidentiality required";
942                         return rs->sr_err;
943                 }
944
945                 if( op->o_tls_ssf < ssf->sss_tls ) {
946                         rs->sr_text = "TLS confidentiality required";
947                         return rs->sr_err;
948                 }
949
950
951                 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
952                         /* simple bind specific check */
953                         if( op->o_ssf < ssf->sss_simple_bind ) {
954                                 rs->sr_text = "confidentiality required";
955                                 return rs->sr_err;
956                         }
957                 }
958
959                 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
960                         /* these checks don't apply to SASL bind */
961
962                         if( op->o_sasl_ssf < ssf->sss_sasl ) {
963                                 rs->sr_text = "SASL confidentiality required";
964                                 return rs->sr_err;
965                         }
966
967                         if( op->o_ssf < ssf->sss_ssf ) {
968                                 rs->sr_text = "confidentiality required";
969                                 return rs->sr_err;
970                         }
971                 }
972
973                 if( updateop ) {
974                         if( op->o_transport_ssf < ssf->sss_update_transport ) {
975                                 rs->sr_text = "transport update confidentiality required";
976                                 return rs->sr_err;
977                         }
978
979                         if( op->o_tls_ssf < ssf->sss_update_tls ) {
980                                 rs->sr_text = "TLS update confidentiality required";
981                                 return rs->sr_err;
982                         }
983
984                         if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
985                                 rs->sr_text = "SASL update confidentiality required";
986                                 return rs->sr_err;
987                         }
988
989                         if( op->o_ssf < ssf->sss_update_ssf ) {
990                                 rs->sr_text = "update confidentiality required";
991                                 return rs->sr_err;
992                         }
993
994                         if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
995                                 op->o_ndn.bv_len == 0 )
996                         {
997                                 rs->sr_text = "modifications require authentication";
998                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
999                                 return rs->sr_err;
1000                         }
1001
1002 #ifdef SLAP_X_LISTENER_MOD
1003                         if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( op->o_ndn.bv_len > 0 ? S_IWUSR : S_IWOTH ) ) ) {
1004                                 /* no "w" mode means readonly */
1005                                 rs->sr_text = "modifications not allowed on this listener";
1006                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1007                                 return rs->sr_err;
1008                         }
1009 #endif /* SLAP_X_LISTENER_MOD */
1010                 }
1011         }
1012
1013         if ( !session ) {
1014                 /* these checks don't apply to Bind, StartTLS, or Unbind */
1015
1016                 if( requires & SLAP_REQUIRE_STRONG ) {
1017                         /* should check mechanism */
1018                         if( ( op->o_transport_ssf < ssf->sss_transport
1019                                 && op->o_authmech.bv_len == 0 ) || op->o_dn.bv_len == 0 )
1020                         {
1021                                 rs->sr_text = "strong authentication required";
1022                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1023                                 return rs->sr_err;
1024                         }
1025                 }
1026
1027                 if( requires & SLAP_REQUIRE_SASL ) {
1028                         if( op->o_authmech.bv_len == 0 || op->o_dn.bv_len == 0 ) {
1029                                 rs->sr_text = "SASL authentication required";
1030                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1031                                 return rs->sr_err;
1032                         }
1033                 }
1034                         
1035                 if( requires & SLAP_REQUIRE_AUTHC ) {
1036                         if( op->o_dn.bv_len == 0 ) {
1037                                 rs->sr_text = "authentication required";
1038                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1039                                 return rs->sr_err;
1040                         }
1041                 }
1042
1043                 if( requires & SLAP_REQUIRE_BIND ) {
1044                         int version;
1045                         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1046                         version = op->o_conn->c_protocol;
1047                         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1048
1049                         if( !version ) {
1050                                 /* no bind has occurred */
1051                                 rs->sr_text = "BIND required";
1052                                 rs->sr_err = LDAP_OPERATIONS_ERROR;
1053                                 return rs->sr_err;
1054                         }
1055                 }
1056
1057                 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1058                         if( op->o_protocol < LDAP_VERSION3 ) {
1059                                 /* no bind has occurred */
1060                                 rs->sr_text = "operation restricted to LDAPv3 clients";
1061                                 rs->sr_err = LDAP_OPERATIONS_ERROR;
1062                                 return rs->sr_err;
1063                         }
1064                 }
1065
1066 #ifdef SLAP_X_LISTENER_MOD
1067                 if ( !starttls && op->o_dn.bv_len == 0 ) {
1068                         if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & S_IXOTH ) ) {
1069                                 /* no "x" mode means bind required */
1070                                 rs->sr_text = "bind required on this listener";
1071                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1072                                 return rs->sr_err;
1073                         }
1074                 }
1075
1076                 if ( !starttls && !updateop ) {
1077                         if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( op->o_dn.bv_len > 0 ? S_IRUSR : S_IROTH ) ) ) {
1078                                 /* no "r" mode means no read */
1079                                 rs->sr_text = "read not allowed on this listener";
1080                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1081                                 return rs->sr_err;
1082                         }
1083                 }
1084 #endif /* SLAP_X_LISTENER_MOD */
1085
1086         }
1087
1088         if( restrictops & opflag ) {
1089                 if( restrictops == SLAP_RESTRICT_OP_READS ) {
1090                         rs->sr_text = "read operations restricted";
1091                 } else {
1092                         rs->sr_text = "operation restricted";
1093                 }
1094                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1095                 return rs->sr_err;
1096         }
1097
1098         rs->sr_err = LDAP_SUCCESS;
1099         return rs->sr_err;
1100 }
1101
1102 int backend_check_referrals( Operation *op, SlapReply *rs )
1103 {
1104         rs->sr_err = LDAP_SUCCESS;
1105
1106         if( op->o_bd->be_chk_referrals ) {
1107                 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1108
1109                 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1110                         send_ldap_result( op, rs );
1111                 }
1112         }
1113
1114         return rs->sr_err;
1115 }
1116
1117 int
1118 be_entry_get_rw(
1119         Operation *op,
1120         struct berval *ndn,
1121         ObjectClass *oc,
1122         AttributeDescription *at,
1123         int rw,
1124         Entry **e )
1125 {
1126         int rc;
1127
1128         *e = NULL;
1129
1130         if (op->o_bd == NULL) {
1131                 rc = LDAP_NO_SUCH_OBJECT;
1132         } else if ( op->o_bd->be_fetch ) {
1133                 rc = ( op->o_bd->be_fetch )( op, ndn,
1134                         oc, at, rw, e );
1135         } else {
1136                 rc = LDAP_UNWILLING_TO_PERFORM;
1137         }
1138         return rc;
1139 }
1140
1141 int 
1142 backend_group(
1143         Operation *op,
1144         Entry   *target,
1145         struct berval *gr_ndn,
1146         struct berval *op_ndn,
1147         ObjectClass *group_oc,
1148         AttributeDescription *group_at
1149 )
1150 {
1151         Entry *e;
1152         Attribute *a;
1153         int rc;
1154         GroupAssertion *g;
1155         Backend *be = op->o_bd;
1156
1157         if ( op->o_abandon ) return SLAPD_ABANDON;
1158
1159         op->o_bd = select_backend( gr_ndn, 0, 0 );
1160
1161         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1162
1163         for (g = op->o_conn->c_groups; g; g=g->ga_next) {
1164                 if (g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1165                         g->ga_at != group_at || g->ga_len != gr_ndn->bv_len)
1166                         continue;
1167                 if (strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0)
1168                         break;
1169         }
1170
1171         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1172
1173         if (g) {
1174                 rc = g->ga_res;
1175                 goto done;
1176         }
1177
1178         if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1179                 e = target;
1180         } else {
1181                 rc = be_entry_get_rw(op, gr_ndn, group_oc, group_at, 0, &e );
1182         }
1183         if ( e ) {
1184                 a = attr_find( e->e_attrs, group_at );
1185                 if ( a ) {
1186                         /* If the attribute is a subtype of labeledURI, treat this as
1187                          * a dynamic group ala groupOfURLs
1188                          */
1189                         if (is_at_subtype( group_at->ad_type, slap_schema.si_ad_labeledURI->ad_type ) ) {
1190                                 int i;
1191                                 LDAPURLDesc *ludp;
1192                                 struct berval bv, nbase;
1193                                 Filter *filter;
1194                                 Entry *user;
1195                                 Backend *b2 = op->o_bd;
1196
1197                                 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1198                                         user = target;
1199                                 } else {
1200                                         op->o_bd = select_backend( op_ndn, 0, 0 );
1201                                         rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1202                                 }
1203                                 
1204                                 if ( rc == 0 ) {
1205                                         rc = 1;
1206                                         for (i=0; a->a_vals[i].bv_val; i++) {
1207                                                 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) != LDAP_SUCCESS )
1208                                                         continue;
1209                                                 nbase.bv_val = NULL;
1210                                                 /* host part must be empty */
1211                                                 /* attrs and extensions parts must be empty */
1212                                                 if (( ludp->lud_host && *ludp->lud_host )
1213                                                         || ludp->lud_attrs || ludp->lud_exts )
1214                                                         goto loopit;
1215                                                 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1216                                                 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase, op->o_tmpmemctx ) != LDAP_SUCCESS )
1217                                                         goto loopit;
1218                                                 switch(ludp->lud_scope) {
1219                                                 case LDAP_SCOPE_BASE:
1220                                                         if ( !dn_match(&nbase, op_ndn)) goto loopit;
1221                                                         break;
1222                                                 case LDAP_SCOPE_ONELEVEL:
1223                                                         dnParent(op_ndn, &bv );
1224                                                         if ( !dn_match(&nbase, &bv)) goto loopit;
1225                                                         break;
1226                                                 case LDAP_SCOPE_SUBTREE:
1227                                                         if ( !dnIsSuffix(op_ndn, &nbase)) goto loopit;
1228                                                         break;
1229                                                 }
1230                                                 filter = str2filter_x( op, ludp->lud_filter );
1231                                                 if ( filter ) {
1232                                                         if ( test_filter( NULL, user, filter ) == LDAP_COMPARE_TRUE )
1233                                                         {
1234                                                                 rc = 0;
1235                                                         }
1236                                                         filter_free_x( op, filter );
1237                                                 }
1238         loopit:
1239                                                 ldap_free_urldesc( ludp );
1240                                                 if ( nbase.bv_val ) {
1241                                                         op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1242                                                 }
1243                                                 if ( rc == 0 ) break;
1244                                         }
1245                                         if ( user != target ) {
1246                                                 be_entry_release_r( op, user );
1247                                         }
1248                                 }
1249                                 op->o_bd = b2;
1250                         } else {
1251                                 rc = value_find_ex( group_at,
1252                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1253                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1254                                 a->a_nvals, op_ndn, op->o_tmpmemctx );
1255                         }
1256                 } else {
1257                         rc = LDAP_NO_SUCH_ATTRIBUTE;
1258                 }
1259                 if (e != target ) {
1260                         be_entry_release_r( op, e );
1261                 }
1262         } else {
1263                 rc = LDAP_NO_SUCH_OBJECT;
1264         }
1265
1266         if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1267                 g = ch_malloc(sizeof(GroupAssertion) + gr_ndn->bv_len);
1268                 g->ga_be = op->o_bd;
1269                 g->ga_oc = group_oc;
1270                 g->ga_at = group_at;
1271                 g->ga_res = rc;
1272                 g->ga_len = gr_ndn->bv_len;
1273                 strcpy(g->ga_ndn, gr_ndn->bv_val);
1274                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1275                 g->ga_next = op->o_conn->c_groups;
1276                 op->o_conn->c_groups = g;
1277                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1278         }
1279 done:
1280         op->o_bd = be;
1281         return rc;
1282 }
1283
1284 int 
1285 backend_attribute(
1286         Operation *op,
1287         Entry   *target,
1288         struct berval   *edn,
1289         AttributeDescription *entry_at,
1290         BerVarray *vals
1291 )
1292 {
1293         Entry *e;
1294         Attribute *a;
1295         int i, j, rc = LDAP_SUCCESS;
1296         AccessControlState acl_state = ACL_STATE_INIT;
1297         Backend *be = op->o_bd;
1298
1299         op->o_bd = select_backend( edn, 0, 0 );
1300
1301         if ( target && dn_match( &target->e_nname, edn ) ) {
1302                 e = target;
1303         } else {
1304                 rc = be_entry_get_rw(op, edn, NULL, entry_at, 0, &e );
1305         } 
1306
1307         if ( e ) {
1308                 a = attr_find( e->e_attrs, entry_at );
1309                 if ( a ) {
1310                         BerVarray v;
1311
1312                         if ( op->o_conn && access_allowed( op,
1313                                 e, entry_at, NULL, ACL_AUTH,
1314                                 &acl_state ) == 0 ) {
1315                                 rc = LDAP_INSUFFICIENT_ACCESS;
1316                                 goto freeit;
1317                         }
1318
1319                         for ( i=0; a->a_vals[i].bv_val; i++ ) ;
1320                         
1321                         v = op->o_tmpalloc( sizeof(struct berval) * (i+1), op->o_tmpmemctx );
1322                         for ( i=0,j=0; a->a_vals[i].bv_val; i++ ) {
1323                                 if ( op->o_conn && access_allowed( op,
1324                                         e, entry_at,
1325                                         &a->a_nvals[i],
1326                                         ACL_AUTH, &acl_state ) == 0 ) {
1327                                         continue;
1328                                 }
1329                                 ber_dupbv_x( &v[j],
1330                                         &a->a_nvals[i], op->o_tmpmemctx );
1331                                 if (v[j].bv_val ) j++;
1332                         }
1333                         if (j == 0) {
1334                                 op->o_tmpfree( v, op->o_tmpmemctx );
1335                                 *vals = NULL;
1336                                 rc = LDAP_INSUFFICIENT_ACCESS;
1337                         } else {
1338                                 v[j].bv_val = NULL;
1339                                 v[j].bv_len = 0;
1340                                 *vals = v;
1341                                 rc = LDAP_SUCCESS;
1342                         }
1343                 }
1344 freeit:         if (e != target ) {
1345                         be_entry_release_r( op, e );
1346                 }
1347         }
1348
1349         op->o_bd = be;
1350         return rc;
1351 }
1352
1353 Attribute *backend_operational(
1354         Operation *op,
1355         SlapReply *rs,
1356         int opattrs     )
1357 {
1358         Attribute *a = NULL, **ap = &a;
1359
1360         /*
1361          * If operational attributes (allegedly) are required, 
1362          * and the backend supports specific operational attributes, 
1363          * add them to the attribute list
1364          */
1365         if ( opattrs || ( op->ors_attrs &&
1366                 ad_inlist( slap_schema.si_ad_subschemaSubentry, op->ors_attrs )) ) {
1367                 *ap = slap_operational_subschemaSubentry( op->o_bd );
1368                 ap = &(*ap)->a_next;
1369         }
1370
1371         if ( ( opattrs || op->ors_attrs ) && op->o_bd && op->o_bd->be_operational != NULL ) {
1372                 ( void )op->o_bd->be_operational( op, rs, opattrs, ap );
1373         }
1374
1375         return a;
1376 }
1377