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