]> git.sur5r.net Git - openldap/blob - servers/slurpd/reject.c
Rework unprotected simple bind checks
[openldap] / servers / slurpd / reject.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright (c) 1996 Regents of the University of Michigan.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms are permitted
11  * provided that this notice is preserved and that due credit is given
12  * to the University of Michigan at Ann Arbor. The name of the University
13  * may not be used to endorse or promote products derived from this
14  * software without specific prior written permission. This software
15  * is provided ``as is'' without express or implied warranty.
16  */
17
18
19 /*
20  * reject.c - routines to write replication records to reject files.
21  * An Re struct is writted to a reject file if it cannot be propagated
22  * to a replica LDAP server.
23  */
24
25 #include "portable.h"
26
27 #include <stdio.h>
28
29 #include <ac/stdlib.h>
30 #include <ac/errno.h>
31 #include <ac/unistd.h>
32
33 #include <sys/stat.h>
34 #include <fcntl.h>
35
36 #include "slurp.h"
37 #include "globals.h"
38
39 /*
40  * Write a replication record to a reject file.  The reject file has the
41  * same name as the replica's private copy of the file but with ".rej"
42  * appended (e.g. "/usr/tmp/<hostname>:<port>.rej")
43  *
44  * If errmsg is non-NULL, use that as the error message in the reject
45  * file.  Otherwise, use ldap_err2string( lderr ).
46  */
47 void
48 write_reject(
49     Ri          *ri,
50     Re          *re,
51     int         lderr,
52     char        *errmsg
53 )
54 {
55     char        rejfile[ MAXPATHLEN ];
56     FILE        *rfp, *lfp;
57     int         rc;
58
59     ldap_pvt_thread_mutex_lock( &sglob->rej_mutex );
60     snprintf( rejfile, sizeof rejfile, "%s" LDAP_DIRSEP "%s:%d.rej",
61                 sglob->slurpd_rdir, ri->ri_hostname, ri->ri_port );
62
63     if ( access( rejfile, F_OK ) < 0 ) {
64         /* Doesn't exist - try to create */
65         int rjfd;
66         if (( rjfd = open( rejfile, O_RDWR|O_APPEND|O_CREAT|O_EXCL,
67                 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP )) < 0 ) {
68 #ifdef NEW_LOGGING
69                 LDAP_LOG ( SLURPD, ERR, "write_reject: "
70                         "Error: Cannot create \"%s\":%s\n", 
71                         rejfile, sys_errlist[ errno ], 0 );
72 #else
73             Debug( LDAP_DEBUG_ANY,
74                 "Error: write_reject: Cannot create \"%s\": %s\n",
75                 rejfile, sys_errlist[ errno ], 0 );
76 #endif
77             ldap_pvt_thread_mutex_unlock( &sglob->rej_mutex );
78             return;
79         } else {
80             close( rjfd );
81         }
82     }
83     if (( rc = acquire_lock( rejfile, &rfp, &lfp )) < 0 ) {
84 #ifdef NEW_LOGGING
85         LDAP_LOG ( SLURPD, ERR, "write_reject: "
86                 "Error: Cannot open reject file \"%s\"\n", rejfile, 0, 0 );
87 #else
88         Debug( LDAP_DEBUG_ANY, "Error: cannot open reject file \"%s\"\n",
89                 rejfile, 0, 0 );
90 #endif
91     } else {
92         fseek( rfp, 0, 2 );
93         if ( errmsg != NULL ) {
94             fprintf( rfp, "%s: %s\n", ERROR_STR, errmsg );
95         } else {
96             fprintf( rfp, "%s: %s\n", ERROR_STR, ldap_err2string( lderr ));
97         }
98         if ((rc = re->re_write( ri, re, rfp )) < 0 ) {
99 #ifdef NEW_LOGGING
100                 LDAP_LOG ( SLURPD, ERR, "write_reject: "
101                         "Error: Cannot write reject file \"%s\"\n", rejfile, 0, 0 );
102 #else
103             Debug( LDAP_DEBUG_ANY,
104                     "Error: cannot write reject file \"%s\"\n",
105                     rejfile, 0, 0 );
106 #endif
107         }
108         (void) relinquish_lock( rejfile, rfp, lfp );
109 #ifdef NEW_LOGGING
110         LDAP_LOG ( SLURPD, ERR, "write_reject: "
111                 "Error: ldap operation failed, data written to \"%s\"\n", 
112                 rejfile, 0, 0 );
113 #else
114         Debug( LDAP_DEBUG_ANY,
115                 "Error: ldap operation failed, data written to \"%s\"\n",
116                 rejfile, 0, 0 );
117 #endif
118     }
119     ldap_pvt_thread_mutex_unlock( &sglob->rej_mutex );
120     return;
121 }
122