Skip to content

Commit

Permalink
Use clicon_err() consistently and drop perror()
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Prindeville committed Aug 26, 2023
1 parent 1698c67 commit 4a6dcb0
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 54 deletions.
6 changes: 3 additions & 3 deletions apps/backend/backend_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1029,13 +1029,13 @@ main(int argc,
daemonized errors OK. Before this stage, errors are logged on stderr
also */
if (foreground==0){
clicon_log_init(__PROGRAM__, dbg?LOG_DEBUG:LOG_INFO,
logdst==CLICON_LOG_FILE?CLICON_LOG_FILE:CLICON_LOG_SYSLOG);
/* Call plugin callbacks just before fork/daemonization */
if (clixon_plugin_pre_daemon_all(h) < 0)
goto done;
clicon_log_init(__PROGRAM__, dbg?LOG_DEBUG:LOG_INFO,
logdst==CLICON_LOG_FILE?CLICON_LOG_FILE:CLICON_LOG_SYSLOG);
if (daemon(0, 0) < 0){
fprintf(stderr, "config: daemon");
clicon_err(OE_UNIX, errno, "daemon");
exit(-1);
}
}
Expand Down
2 changes: 1 addition & 1 deletion apps/restconf/restconf_main_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ restconf_clixon_backend(clicon_handle h,
while (1){
if (clicon_hello_req(h, "cl:restconf", NULL, &id) < 0){
if (errno == ENOENT){
fprintf(stderr, "waiting");
clicon_err(OE_UNIX, errno, "waiting");
sleep(1);
continue;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/src/clixon_proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,18 +214,18 @@ clixon_proc_socket(char **argv,
close(sp[0]);
close(0);
if (dup2(sp[1], STDIN_FILENO) < 0){
perror("dup2");
clicon_err(OE_UNIX, errno, "dup2(STDIN)");
return -1;
}
close(1);
if (dup2(sp[1], STDOUT_FILENO) < 0){
perror("dup2");
clicon_err(OE_UNIX, errno, "dup2(STDOUT)");
return -1;
}
close(sp[1]);

if (execvp(argv[0], argv) < 0){
perror("execvp");
clicon_err(OE_UNIX, errno, "execvp '%s'", argv[0]);
return -1;
}
exit(-1); /* Shouldnt reach here */
Expand Down
6 changes: 6 additions & 0 deletions lib/src/clixon_uid.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ drop_priv_temp(uid_t new_uid)
#ifdef HAVE_GETRESUID
int retval = -1;

clicon_debug(4, "%s uid:%u", __FUNCTION__, new_uid);

/* XXX: implicit declaration of function 'setresuid' on travis */
if (setresuid(-1, new_uid, geteuid()) < 0){
clicon_err(OE_UNIX, errno, "setresuid");
Expand Down Expand Up @@ -203,6 +205,8 @@ drop_priv_perm(uid_t new_uid)
uid_t euid;
uid_t suid;

clicon_debug(4, "%s uid:%u", __FUNCTION__, new_uid);

if (setresuid(new_uid, new_uid, new_uid) < 0){
clicon_err(OE_UNIX, errno, "setresuid");
goto done;
Expand Down Expand Up @@ -236,6 +240,8 @@ restore_priv(void)
uid_t euid;
uid_t suid;

clicon_debug(4, "%s", __FUNCTION__);

if (getresuid(&ruid, &euid, &suid) < 0){
clicon_err(OE_UNIX, errno, "setresuid");
goto done;
Expand Down
6 changes: 6 additions & 0 deletions util/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ clixon_util_validate: clixon_util_validate.c $(BELIBDEPS) $(LIBDEPS)
clixon_util_dispatcher: clixon_util_dispatcher.c $(BELIBDEPS) $(LIBDEPS)
$(CC) $(INCLUDES) $(CPPFLAGS) -D__PROGRAM__=\"$@\" $(CFLAGS) $(LDFLAGS) $^ -l clixon_backend -o $@ $(LIBS) $(BELIBS)

clixon_netconf_ssh_callhome: clixon_netconf_ssh_callhome.c $(LIBDEPS)
$(CC) $(INCLUDES) $(CPPFLAGS) -D__PROGRAM__=\"$@\" $(CFLAGS) $(LDFLAGS) $^ $(LIBS) -o $@

clixon_netconf_ssh_callhome_client: clixon_netconf_ssh_callhome_client.c $(LIBDEPS)
$(CC) $(INCLUDES) $(CPPFLAGS) -D__PROGRAM__=\"$@\" $(CFLAGS) $(LDFLAGS) $^ $(LIBS) -o $@

ifdef with_restconf
clixon_util_stream: clixon_util_stream.c $(LIBDEPS)
$(CC) $(INCLUDES) $(CPPFLAGS) -D__PROGRAM__=\"$@\" $(CFLAGS) $(LDFLAGS) $^ $(LIBS) -lcurl -o $@
Expand Down
26 changes: 14 additions & 12 deletions util/clixon_netconf_ssh_callhome.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ sudo clixon_netconf_ssh_callhome -a 127.0.0.1 -c /var/tmp/./test_netconf_ssh_cal
#include <arpa/inet.h>
#include <sys/socket.h>

#include <cligen/cligen_buf.h>
#include <clixon/clixon_err.h>

#define NETCONF_CH_SSH 4334
#define SSHDBIN_DEFAULT "/usr/sbin/sshd"
#define UTIL_OPTS "hD:f:a:p:s:c:C:"
Expand All @@ -91,11 +94,11 @@ callhome_connect(struct sockaddr *sa,
int s;

if ((s = socket(sa->sa_family, SOCK_STREAM, 0)) < 0) {
perror("socket");
clicon_err(OE_UNIX, errno, "socket");
goto done;
}
if (connect(s, sa, sa_len) < 0){
perror("connect");
clicon_err(OE_UNIX, errno, "connect");
close(s);
goto done;
}
Expand Down Expand Up @@ -155,28 +158,27 @@ ssh_server_exec(int s,

if (s < 0){
errno = EINVAL;
perror("socket s");
clicon_err(OE_UNIX, errno, "socket s");
goto done;
}
if (sshdbin == NULL){
errno = EINVAL;
perror("sshdbin");
clicon_err(OE_UNIX, errno, "sshdbin");
goto done;
}
if (sshdconfigfile == NULL){
errno = EINVAL;
perror("sshdconfigfile");
goto done;
}
if (clixonconfigfile == NULL){
errno = EINVAL;
perror("clixonconfigfile");
clicon_err(OE_UNIX, errno, "clixonconfigfile");
goto done;
}
/* Construct subsystem string */
len = strlen(formatstr)+strlen(clixonconfigfile)+1;
if ((optstr = malloc(len)) == NULL){
perror("malloc");
clicon_err(OE_UNIX, errno, "malloc");
goto done;
}
snprintf(optstr, len, formatstr, clixonconfigfile);
Expand All @@ -185,7 +187,7 @@ ssh_server_exec(int s,
if (dbg)
nr++;
if ((argv = calloc(nr, sizeof(char *))) == NULL){
perror("calloc");
clicon_err(OE_UNIX, errno, "calloc");
goto done;
}

Expand All @@ -204,21 +206,21 @@ ssh_server_exec(int s,
argv[i++] = NULL;
assert(i==nr);
if (setreuid(0, 0) < 0){
perror("setreuid");
clicon_err(OE_UNIX, errno, "setreuid");
goto done;
}
close(0);
close(1);
if (dup2(s, STDIN_FILENO) < 0){
perror("dup2");
clicon_err(OE_UNIX, errno, "dup2(STDIN)");
return -1;
}
if (dup2(s, STDOUT_FILENO) < 0){
perror("dup2");
clicon_err(OE_UNIX, errno, "dup2(STDOUT)");
return -1;
}
if (execv(argv[0], argv) < 0) {
perror("execv");
clicon_err(OE_UNIX, errno, "execv %s", argv[0]);
exit(1);
}
/* Should reach here */
Expand Down
29 changes: 16 additions & 13 deletions util/clixon_netconf_ssh_callhome_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ Example sshd-config (-c option):n
#include <arpa/inet.h>
#include <sys/socket.h>

#include <cligen/cligen_buf.h>
#include <clixon/clixon_err.h>

#define NETCONF_CH_SSH 4334
#define UTIL_OPTS "hD:f:a:p:"

Expand Down Expand Up @@ -103,7 +106,8 @@ fdpass(int nfd)

/* Avoid obvious stupidity */
if (isatty(STDOUT_FILENO)){
perror("Cannot pass file descriptor to tty");
errno = EINVAL;
clicon_err(OE_UNIX, errno, "isatty");
return -1;
}
memset(&mh, 0, sizeof(mh));
Expand Down Expand Up @@ -131,15 +135,15 @@ fdpass(int nfd)
if (r == -1) {
if (errno == EAGAIN || errno == EINTR) {
if (poll(&pfd, 1, -1) == -1){
perror("poll");
clicon_err(OE_UNIX, errno, "poll");
return -1;
}
continue;
}
perror("sendmsg");
clicon_err(OE_UNIX, errno, "sendmsg");
return -1;
} else if (r != 1){
perror("sendmsg: unexpected return value");
clicon_err(OE_UNIX, errno, "sendmsg: unexpected value");
return -1;
}
else
Expand Down Expand Up @@ -167,34 +171,34 @@ callhome_bind(struct sockaddr *sa,

if (sock == NULL){
errno = EINVAL;
perror("sock");
clicon_err(OE_UNIX, errno, "sock");
goto done;
}
/* create inet socket */
if ((s = socket(sa->sa_family, SOCK_STREAM, 0)) < 0) {
perror("socket");
clicon_err(OE_UNIX, errno, "socket");
goto done;
}
if (setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof(on)) == -1) {
perror("setsockopt SO_KEEPALIVE");
clicon_err(OE_UNIX, errno, "setsockopt SO_KEEPALIVE");
goto done;
}
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)) == -1) {
perror("setsockopt SO_REUSEADDR");
clicon_err(OE_UNIX, errno, "setsockopt SO_REUSEADDR");
goto done;
}
/* only bind ipv6, otherwise it may bind to ipv4 as well which is strange but seems default */
if (sa->sa_family == AF_INET6 &&
setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) == -1) {
perror("setsockopt IPPROTO_IPV6");
clicon_err(OE_UNIX, errno, "setsockopt IPPROTO_IPV6");
goto done;
}
if (bind(s, sa, sin_len) == -1) {
perror("bind");
clicon_err(OE_UNIX, errno, "bind");
goto done;
}
if (listen(s, backlog) < 0){
perror("listen");
clicon_err(OE_UNIX, errno, "listen");
goto done;
}
if (sock)
Expand Down Expand Up @@ -293,7 +297,7 @@ main(int argc,
/* Wait until connect */
len = sizeof(from);
if ((s = accept(ss, &from, &len)) < 0){
perror("accept");
clicon_err(OE_UNIX, errno, "accept");
goto done;
}
/* s Pass the first connected socket using sendmsg(2) to stdout and exit. */
Expand All @@ -304,4 +308,3 @@ main(int argc,
return retval;
}


24 changes: 11 additions & 13 deletions util/clixon_restconf_callhome_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,34 +153,34 @@ callhome_bind(struct sockaddr *sa,

if (sock == NULL){
errno = EINVAL;
perror("sock");
clicon_err(OE_UNIX, errno, "sock");
goto done;
}
/* create inet socket */
if ((s = socket(sa->sa_family, SOCK_STREAM, 0)) < 0) {
perror("socket");
clicon_err(OE_UNIX, errno, "socket");
goto done;
}
if (setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof(on)) == -1) {
perror("setsockopt SO_KEEPALIVE");
clicon_err(OE_UNIX, errno, "setsockopt SO_KEEPALIVE");
goto done;
}
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)) == -1) {
perror("setsockopt SO_REUSEADDR");
clicon_err(OE_UNIX, errno, "setsockopt SO_REUSEADDR");
goto done;
}
/* only bind ipv6, otherwise it may bind to ipv4 as well which is strange but seems default */
if (sa->sa_family == AF_INET6 &&
setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) == -1) {
perror("setsockopt IPPROTO_IPV6");
clicon_err(OE_UNIX, errno, "setsockopt IPPROTO_IPV6");
goto done;
}
if (bind(s, sa, sin_len) == -1) {
perror("bind");
clicon_err(OE_UNIX, errno, "bind");
goto done;
}
if (listen(s, backlog) < 0){
perror("listen");
clicon_err(OE_UNIX, errno, "listen");
goto done;
}
if (sock)
Expand Down Expand Up @@ -363,7 +363,7 @@ tls_ssl_init_connect(SSL_CTX *ctx,

/* create new SSL connection state */
if ((ssl = SSL_new(ctx)) == NULL){
clicon_err(OE_SSL, 0, "SSL_new.");
clicon_err(OE_SSL, 0, "SSL_new");
goto done;
}
SSL_set_fd(ssl, s); /* attach the socket descriptor */
Expand All @@ -372,7 +372,7 @@ tls_ssl_init_connect(SSL_CTX *ctx,
protos[0] = 8;
strncpy((char*)&protos[1], "http/1.1", 9);
if ((retval = SSL_set_alpn_protos(ssl, protos, 9)) != 0){
clicon_err(OE_SSL, retval, "SSL_set_alpn_protos.");
clicon_err(OE_SSL, retval, "SSL_set_alpn_protos");
goto done;
}
#if 0
Expand All @@ -394,11 +394,9 @@ tls_ssl_init_connect(SSL_CTX *ctx,
switch (sslerr){
case SSL_ERROR_SSL: /* 1 */
goto done;
break;
default:
clicon_err(OE_XML, errno, "SSL_connect");
goto done;
break;
}
}
/* check certificate verification result */
Expand All @@ -407,7 +405,7 @@ tls_ssl_init_connect(SSL_CTX *ctx,
case X509_V_OK:
break;
default:
clicon_err(OE_SSL, errno, "verify problems: %d", verify);
clicon_err(OE_SSL, errno, "SSL_get_verify_result: %d", verify);
goto done;
}
*sslp = ssl;
Expand Down Expand Up @@ -468,7 +466,7 @@ tls_server_accept_cb(int ss,
clicon_debug(1, "%s", __FUNCTION__);
len = sizeof(from);
if ((s = accept(ss, &from, &len)) < 0){
perror("accept");
clicon_err(OE_UNIX, errno, "accept");
goto done;
}
clicon_debug(1, "accepted");
Expand Down
Loading

0 comments on commit 4a6dcb0

Please sign in to comment.