From 67d005ee653f32a815bc0363ffdafef39d09b4da Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ond=C5=99ej=20Kuzn=C3=ADk?= Date: Fri, 25 Sep 2020 12:29:14 +0100 Subject: [PATCH] ITS#9348 Stop using plain strerror() --- contrib/slapd-modules/dsaschema/dsaschema.c | 4 +- .../nssov/nss-pam-ldapd/nslcd-prot.h | 18 ++- contrib/slapd-modules/nssov/nssov.c | 112 +++++++++++++----- servers/slapd/ad.c | 4 +- servers/slapd/back-mdb/config.c | 4 +- servers/slapd/config.c | 6 +- servers/slapd/main.c | 6 +- servers/slapd/slaptest.c | 5 +- 8 files changed, 116 insertions(+), 43 deletions(-) diff --git a/contrib/slapd-modules/dsaschema/dsaschema.c b/contrib/slapd-modules/dsaschema/dsaschema.c index bef7b21f84..5c787b33c3 100644 --- a/contrib/slapd-modules/dsaschema/dsaschema.c +++ b/contrib/slapd-modules/dsaschema/dsaschema.c @@ -168,8 +168,10 @@ static int dsaschema_read_config(const char *fname, int depth) fp = fopen(fname, "r"); if (fp == NULL) { + char ebuf[128]; + int saved_errno = errno; fprintf(stderr, "could not open config file \"%s\": %s (%d)\n", - fname, strerror(errno), errno); + fname, AC_STRERROR_R(saved_errno, ebuf, sizeof(ebuf)), saved_errno); return 1; } fp_getline_init(&lineno); diff --git a/contrib/slapd-modules/nssov/nss-pam-ldapd/nslcd-prot.h b/contrib/slapd-modules/nssov/nss-pam-ldapd/nslcd-prot.h index 06d8959fd4..c68ffa20a3 100644 --- a/contrib/slapd-modules/nssov/nss-pam-ldapd/nslcd-prot.h +++ b/contrib/slapd-modules/nssov/nss-pam-ldapd/nslcd-prot.h @@ -86,8 +86,10 @@ static void debug_dump(const void *ptr, size_t size) DEBUG_DUMP(ptr, size); \ if (tio_write(fp, ptr, (size_t)size)) \ { \ + char ebuf[128]; \ + int saved_errno = errno; \ DEBUG_PRINT("WRITE : var="__STRING(ptr)" error: %s", \ - strerror(errno)); \ + AC_STRERROR_R(saved_errno, ebuf, sizeof(ebuf))); \ ERROR_OUT_WRITEERROR(fp); \ } @@ -161,8 +163,10 @@ static void debug_dump(const void *ptr, size_t size) #define READ(fp, ptr, size) \ if (tio_read(fp, ptr, (size_t)size)) \ { \ + char ebuf[128]; \ + int saved_errno = errno; \ DEBUG_PRINT("READ : var="__STRING(ptr)" error: %s", \ - strerror(errno)); \ + AC_STRERROR_R(saved_errno, ebuf, sizeof(ebuf))); \ ERROR_OUT_READERROR(fp); \ } \ DEBUG_PRINT("READ : var="__STRING(ptr)" size=%d", (int)(size)); \ @@ -301,7 +305,10 @@ static void debug_dump(const void *ptr, size_t size) /* read (skip) the specified number of bytes */ \ if (tio_skip(fp, sz)) \ { \ - DEBUG_PRINT("READ : skip error: %s", strerror(errno)); \ + char ebuf[128]; \ + int saved_errno = errno; \ + DEBUG_PRINT("READ : skip error: %s", \ + AC_STRERROR_R(saved_errno, ebuf, sizeof(ebuf))); \ ERROR_OUT_READERROR(fp); \ } @@ -350,7 +357,10 @@ TFILE *nslcd_client_open(void) /* flush the stream */ \ if (tio_flush(fp) < 0) \ { \ - DEBUG_PRINT("WRITE_FLUSH : error: %s", strerror(errno)); \ + char ebuf[128]; \ + int saved_errno = errno; \ + DEBUG_PRINT("WRITE_FLUSH : error: %s", \ + AC_STRERROR_R(saved_errno, ebuf, sizeof(ebuf))); \ ERROR_OUT_WRITEERROR(fp); \ } \ /* read and check response version number */ \ diff --git a/contrib/slapd-modules/nssov/nssov.c b/contrib/slapd-modules/nssov/nssov.c index c48be67ae5..a21f2477cf 100644 --- a/contrib/slapd-modules/nssov/nssov.c +++ b/contrib/slapd-modules/nssov/nssov.c @@ -298,11 +298,15 @@ static void handleconnection(nssov_info *ni,int sock,Operation *op) struct berval peerbv = { sizeof(peerbuf), peerbuf }; /* log connection */ - if (LUTIL_GETPEEREID(sock,&uid,&gid,&peerbv)) - Debug( LDAP_DEBUG_TRACE,"nssov: connection from unknown client: %s\n",strerror(errno) ); - else + if (LUTIL_GETPEEREID(sock,&uid,&gid,&peerbv)) { + char ebuf[128]; + int saved_errno = errno; + Debug( LDAP_DEBUG_TRACE,"nssov: connection from unknown client: %s\n", + AC_STRERROR_R(saved_errno, ebuf, sizeof(ebuf)) ); + } else { Debug( LDAP_DEBUG_TRACE,"nssov: connection from uid=%d gid=%d\n", (int)uid,(int)gid ); + } /* Should do authid mapping too */ op->o_dn.bv_len = sprintf(authid,"gidNumber=%d+uidNumber=%d,cn=peercred,cn=external,cn=auth", @@ -322,7 +326,10 @@ static void handleconnection(nssov_info *ni,int sock,Operation *op) READBUFFER_MINSIZE,READBUFFER_MAXSIZE, WRITEBUFFER_MINSIZE,WRITEBUFFER_MAXSIZE))==NULL) { - Debug( LDAP_DEBUG_ANY,"nssov: cannot create stream for writing: %s",strerror(errno) ); + char ebuf[128]; + int saved_errno = errno; + Debug( LDAP_DEBUG_ANY,"nssov: cannot create stream for writing: %s", + AC_STRERROR_R(saved_errno, ebuf, sizeof(ebuf)) ); (void)close(sock); return; } @@ -403,27 +410,43 @@ static void *acceptconn(void *ctx, void *arg) connection_client_enable(ni->ni_conn); if (csock<0) { + char ebuf[128]; + int saved_errno = errno; if ((errno==EINTR)||(errno==EAGAIN)||(errno==EWOULDBLOCK)) { - Debug( LDAP_DEBUG_TRACE,"nssov: accept() failed (ignored): %s",strerror(errno) ); + Debug( LDAP_DEBUG_TRACE,"nssov: accept() failed (ignored): %s", + AC_STRERROR_R(saved_errno, ebuf, sizeof(ebuf)) ); return NULL; } - Debug( LDAP_DEBUG_ANY,"nssov: accept() failed: %s",strerror(errno) ); + Debug( LDAP_DEBUG_ANY,"nssov: accept() failed: %s", + AC_STRERROR_R(saved_errno, ebuf, sizeof(ebuf)) ); return NULL; } /* make sure O_NONBLOCK is not inherited */ if ((j=fcntl(csock,F_GETFL,0))<0) { - Debug( LDAP_DEBUG_ANY,"nssov: fcntl(F_GETFL) failed: %s",strerror(errno) ); - if (close(csock)) - Debug( LDAP_DEBUG_ANY,"nssov: problem closing socket: %s",strerror(errno) ); + char ebuf[128]; + int saved_errno = errno; + Debug( LDAP_DEBUG_ANY,"nssov: fcntl(F_GETFL) failed: %s", + AC_STRERROR_R(saved_errno, ebuf, sizeof(ebuf)) ); + if (close(csock)) { + saved_errno = errno; + Debug( LDAP_DEBUG_ANY,"nssov: problem closing socket: %s", + AC_STRERROR_R(saved_errno, ebuf, sizeof(ebuf)) ); + } return NULL; } if (fcntl(csock,F_SETFL,j&~O_NONBLOCK)<0) { - Debug( LDAP_DEBUG_ANY,"nssov: fcntl(F_SETFL,~O_NONBLOCK) failed: %s",strerror(errno) ); - if (close(csock)) - Debug( LDAP_DEBUG_ANY,"nssov: problem closing socket: %s",strerror(errno) ); + char ebuf[128]; + int saved_errno = errno; + Debug( LDAP_DEBUG_ANY,"nssov: fcntl(F_SETFL,~O_NONBLOCK) failed: %s", + AC_STRERROR_R(saved_errno, ebuf, sizeof(ebuf)) ); + if (close(csock)) { + saved_errno = errno; + Debug( LDAP_DEBUG_ANY,"nssov: problem closing socket: %s", + AC_STRERROR_R(saved_errno, ebuf, sizeof(ebuf)) ); + } return NULL; } } @@ -868,10 +891,12 @@ nssov_db_open( } } if ( slapMode & SLAP_SERVER_MODE ) { + char ebuf[128]; /* make sure /var/run/nslcd exists */ if (mkdir(NSLCD_PATH, (mode_t) 0555)) { + int saved_errno = errno; Debug(LDAP_DEBUG_TRACE,"nssov: mkdir(%s) failed (ignored): %s\n", - NSLCD_PATH,strerror(errno) ); + NSLCD_PATH, AC_STRERROR_R(saved_errno, ebuf, sizeof(ebuf)) ); } else { Debug(LDAP_DEBUG_TRACE,"nssov: created %s\n",NSLCD_PATH ); } @@ -879,14 +904,17 @@ nssov_db_open( /* create a socket */ if ( (sock=socket(PF_UNIX,SOCK_STREAM,0))<0 ) { - Debug(LDAP_DEBUG_ANY,"nssov: cannot create socket: %s\n",strerror(errno) ); + int saved_errno = errno; + Debug(LDAP_DEBUG_ANY,"nssov: cannot create socket: %s\n", + AC_STRERROR_R(saved_errno, ebuf, sizeof(ebuf)) ); return -1; } /* remove existing named socket */ if (unlink(NSLCD_SOCKET)<0) { + int saved_errno = errno; Debug( LDAP_DEBUG_TRACE,"nssov: unlink() of "NSLCD_SOCKET" failed (ignored): %s\n", - strerror(errno) ); + AC_STRERROR_R(saved_errno, ebuf, sizeof(ebuf)) ); } /* create socket address structure */ memset(&addr,0,sizeof(struct sockaddr_un)); @@ -896,18 +924,27 @@ nssov_db_open( /* bind to the named socket */ if (bind(sock,(struct sockaddr *)&addr,sizeof(struct sockaddr_un))) { + int saved_errno = errno; Debug( LDAP_DEBUG_ANY,"nssov: bind() to "NSLCD_SOCKET" failed: %s", - strerror(errno) ); - if (close(sock)) - Debug( LDAP_DEBUG_ANY,"nssov: problem closing socket: %s",strerror(errno) ); + AC_STRERROR_R(saved_errno, ebuf, sizeof(ebuf)) ); + if (close(sock)) { + saved_errno = errno + Debug( LDAP_DEBUG_ANY,"nssov: problem closing socket: %s", + AC_STRERROR_R(saved_errno, ebuf, sizeof(ebuf)) ); + } return -1; } /* close the file descriptor on exit */ if (fcntl(sock,F_SETFD,FD_CLOEXEC)<0) { - Debug( LDAP_DEBUG_ANY,"nssov: fcntl(F_SETFL,O_NONBLOCK) failed: %s",strerror(errno) ); - if (close(sock)) - Debug( LDAP_DEBUG_ANY,"nssov: problem closing socket: %s",strerror(errno) ); + int saved_errno = errno; + Debug( LDAP_DEBUG_ANY,"nssov: fcntl(F_SETFL,O_NONBLOCK) failed: %s", + AC_STRERROR_R(saved_errno, ebuf, sizeof(ebuf)) ); + if (close(sock)) { + saved_errno = errno + Debug( LDAP_DEBUG_ANY,"nssov: problem closing socket: %s", + AC_STRERROR_R(saved_errno, ebuf, sizeof(ebuf)) ); + } return -1; } /* set permissions of socket so anybody can do requests */ @@ -917,17 +954,27 @@ nssov_db_open( http://lkml.org/lkml/2005/5/16/11 */ if (chmod(NSLCD_SOCKET,(mode_t)0666)) { - Debug( LDAP_DEBUG_ANY,"nssov: chmod(0666) failed: %s",strerror(errno) ); - if (close(sock)) - Debug( LDAP_DEBUG_ANY,"nssov: problem closing socket: %s",strerror(errno) ); + int saved_errno = errno; + Debug( LDAP_DEBUG_ANY,"nssov: chmod(0666) failed: %s", + AC_STRERROR_R(saved_errno, ebuf, sizeof(ebuf)) ); + if (close(sock)) { + saved_errno = errno + Debug( LDAP_DEBUG_ANY,"nssov: problem closing socket: %s", + AC_STRERROR_R(saved_errno, ebuf, sizeof(ebuf)) ); + } return -1; } /* start listening for connections */ if (listen(sock,SOMAXCONN)<0) { - Debug( LDAP_DEBUG_ANY,"nssov: listen() failed: %s",strerror(errno) ); - if (close(sock)) - Debug( LDAP_DEBUG_ANY,"nssov: problem closing socket: %s",strerror(errno) ); + int saved_errno = errno; + Debug( LDAP_DEBUG_ANY,"nssov: listen() failed: %s", + AC_STRERROR_R(saved_errno, ebuf, sizeof(ebuf)) ); + if (close(sock)) { + saved_errno = errno + Debug( LDAP_DEBUG_ANY,"nssov: problem closing socket: %s", + AC_STRERROR_R(saved_errno, ebuf, sizeof(ebuf)) ); + } return -1; } ni->ni_socket = sock; @@ -946,18 +993,23 @@ nssov_db_close( nssov_info *ni = on->on_bi.bi_private; if ( slapMode & SLAP_SERVER_MODE ) { + char ebuf[128]; /* close socket if it's still in use */ if (ni->ni_socket >= 0) { - if (close(ni->ni_socket)) - Debug( LDAP_DEBUG_ANY,"problem closing server socket (ignored): %s",strerror(errno) ); + if (close(ni->ni_socket)) { + int saved_errno = errno; + Debug( LDAP_DEBUG_ANY,"problem closing server socket (ignored): %s", + AC_STRERROR_R(saved_errno, ebuf, sizeof(ebuf)) ); + } ni->ni_socket = -1; } /* remove existing named socket */ if (unlink(NSLCD_SOCKET)<0) { + int saved_errno = errno; Debug( LDAP_DEBUG_TRACE,"unlink() of "NSLCD_SOCKET" failed (ignored): %s", - strerror(errno) ); + AC_STRERROR_R(saved_errno, ebuf, sizeof(ebuf)) ); } } return 0; diff --git a/servers/slapd/ad.c b/servers/slapd/ad.c index 7d1e5fd93e..fa198b011d 100644 --- a/servers/slapd/ad.c +++ b/servers/slapd/ad.c @@ -1143,9 +1143,11 @@ file2anlist( AttributeName *an, const char *fname, const char *brkstr ) fp = fopen( fname, "r" ); if ( fp == NULL ) { + char ebuf[128]; + int saved_errno = errno; Debug( LDAP_DEBUG_ANY, "get_attrs_from_file: failed to open attribute list file " - "\"%s\": %s\n", fname, strerror(errno) ); + "\"%s\": %s\n", fname, AC_STRERROR_R( saved_errno, ebuf, sizeof(ebuf) ) ); return NULL; } diff --git a/servers/slapd/back-mdb/config.c b/servers/slapd/back-mdb/config.c index 160445aa75..06af74ef37 100644 --- a/servers/slapd/back-mdb/config.c +++ b/servers/slapd/back-mdb/config.c @@ -700,8 +700,10 @@ mdb_cf_gen( ConfigArgs *c ) } ch_free( testpath ); if ( !f ) { + char ebuf[128]; + int saved_errno = errno; snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s: invalid path: %s", - c->log, strerror( errno )); + c->log, AC_STRERROR_R( saved_errno, ebuf, sizeof(ebuf) ) ); Debug( LDAP_DEBUG_ANY, "%s\n", c->cr_msg ); return -1; } diff --git a/servers/slapd/config.c b/servers/slapd/config.c index c6fac264d2..9ea7d39d90 100644 --- a/servers/slapd/config.c +++ b/servers/slapd/config.c @@ -787,11 +787,12 @@ read_config_file(const char *fname, int depth, ConfigArgs *cf, ConfigTable *cft) init_config_argv( c ); if ( stat( fname, &s ) != 0 ) { + char ebuf[128]; int saved_errno = errno; ldap_syslog = 1; Debug(LDAP_DEBUG_ANY, "could not stat config file \"%s\": %s (%d)\n", - fname, strerror(saved_errno), saved_errno); + fname, AC_STRERROR_R( saved_errno, ebuf, sizeof(ebuf) ), saved_errno); ch_free( c->argv ); ch_free( c ); return(1); @@ -809,11 +810,12 @@ read_config_file(const char *fname, int depth, ConfigArgs *cf, ConfigTable *cft) fp = fopen( fname, "r" ); if ( fp == NULL ) { + char ebuf[128]; int saved_errno = errno; ldap_syslog = 1; Debug(LDAP_DEBUG_ANY, "could not open config file \"%s\": %s (%d)\n", - fname, strerror(saved_errno), saved_errno); + fname, AC_STRERROR_R( saved_errno, ebuf, sizeof(ebuf) ), saved_errno); ch_free( c->argv ); ch_free( c ); return(1); diff --git a/servers/slapd/main.c b/servers/slapd/main.c index 42894f2b24..739c32e6d9 100644 --- a/servers/slapd/main.c +++ b/servers/slapd/main.c @@ -945,12 +945,13 @@ unhandled_option:; FILE *fp = fopen( slapd_pid_file, "w" ); if ( fp == NULL ) { + char ebuf[128]; int save_errno = errno; Debug( LDAP_DEBUG_ANY, "unable to open pid file " "\"%s\": %d (%s)\n", slapd_pid_file, - save_errno, strerror( save_errno ) ); + save_errno, AC_STRERROR_R( save_errno, ebuf, sizeof(ebuf) ) ); free( slapd_pid_file ); slapd_pid_file = NULL; @@ -967,12 +968,13 @@ unhandled_option:; FILE *fp = fopen( slapd_args_file, "w" ); if ( fp == NULL ) { + char ebuf[128]; int save_errno = errno; Debug( LDAP_DEBUG_ANY, "unable to open args file " "\"%s\": %d (%s)\n", slapd_args_file, - save_errno, strerror( save_errno ) ); + save_errno, AC_STRERROR_R( save_errno, ebuf, sizeof(ebuf) ) ); free( slapd_args_file ); slapd_args_file = NULL; diff --git a/servers/slapd/slaptest.c b/servers/slapd/slaptest.c index 8736d4bf8f..44c1dd0e46 100644 --- a/servers/slapd/slaptest.c +++ b/servers/slapd/slaptest.c @@ -46,6 +46,7 @@ static int test_file( const char *fname, const char *ftype ) { struct stat st; + char ebuf[128]; int save_errno; switch ( stat( fname, &st ) ) { @@ -70,7 +71,7 @@ test_file( const char *fname, const char *ftype ) Debug( LDAP_DEBUG_ANY, "unable to open file " "\"%s\": %d (%s)\n", fname, - save_errno, strerror( save_errno ) ); + save_errno, AC_STRERROR_R( save_errno, ebuf, sizeof(ebuf) ) ); return -1; } @@ -82,7 +83,7 @@ test_file( const char *fname, const char *ftype ) Debug( LDAP_DEBUG_ANY, "unable to stat file " "\"%s\": %d (%s)\n", slapd_pid_file, - save_errno, strerror( save_errno ) ); + save_errno, AC_STRERROR_R( save_errno, ebuf, sizeof(ebuf) ) ); return -1; } -- 2.47.3