From: Thibault Godouet Date: Thu, 16 Jan 2014 21:28:52 +0000 (+0000) Subject: Fixed compilation on Solaris 10 (missing strndup()) and Solaris credentials extractio... X-Git-Tag: ver3_1_3~18 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6898e4e8210af9d9af978db73b0bc42f42b6cf39;p=thirdparty%2Ffcron.git Fixed compilation on Solaris 10 (missing strndup()) and Solaris credentials extraction from socket --- diff --git a/mem.c b/mem.c index 8293bb3..59b001d 100644 --- a/mem.c +++ b/mem.c @@ -24,6 +24,28 @@ #include "global.h" #include "mem.h" +#if defined(__sun) +/* Solaris 10 has no strndup() */ +char * +strndup (const char *s, size_t n) + /* Written by Kaveh R. Ghazi */ +{ + char *result; + size_t len = strlen (s); + + if (n < len) + len = n; + + result = (char *) malloc (len + 1); + if (!result) + return 0; + + memcpy (result, s, len); + result[len] = '\0'; + return(result); +} +#endif + char * strdup2(const char *str) { diff --git a/mem.h b/mem.h index 1f7a656..5b98dc8 100644 --- a/mem.h +++ b/mem.h @@ -53,6 +53,9 @@ /* functions prototypes */ +#if defined(__sun) +extern char *strndup (const char *s, size_t n); /* Solaris 10 has no strndup() */ +#endif extern char *strdup2(const char *); extern char *strndup2(const char *, size_t n); extern void *alloc_safe(size_t len, const char *desc); diff --git a/socket.c b/socket.c index e52c957..8832761 100644 --- a/socket.c +++ b/socket.c @@ -214,12 +214,46 @@ init_socket(void) } -#if defined(HAVE_GETPEERUCRED) || defined(HAVE_GETPEEREID) +#if defined(HAVE_GETPEERUCRED) +void +auth_client_getpeer(struct fcrondyn_cl *client) + /* check client identity by reading its credentials from the socket + * using getpeerucred() (Solaris 10 onward). + * Sets client->fcl_user on success, don't do anything on failure + * so that the client stays unauthenticated */ +{ + struct passwd *p_entry = NULL; + ucred_t *ucred = NULL; + uid_t uid = -1; + + if (getpeerucred(client->fcl_sock_fd, &ucred) < 0) { + error_e("Could not get client credentials using getpeerucred()"); + return; + } + uid = ucred_getruid(ucred); + if (uid == -1) { + error_e("Could not get client uid from ucred_t"); + return; + } + p_entry = getpwuid(uid); + if (p_entry == NULL) { + error_e("Could not find password entry for uid %d", cred.uid); + return; + } + /* Successfully identified user: */ + client->fcl_user = strdup2(p_entry->pw_name); + + explain("Client's pid=%d, uid=%d, username=%s\n", + ucred_getpid(ucred), uid, client->fcl_user); + +} +#endif /* HAVE_GETPEERUCRED */ + +#if defined(HAVE_GETPEEREID) /* * WARNING: UNTESTED CODE !!! */ - void auth_client_getpeer(struct fcrondyn_cl *client) /* check client identity by reading its credentials from the socket @@ -228,41 +262,27 @@ auth_client_getpeer(struct fcrondyn_cl *client) * so that the client stays unauthenticated */ { struct passwd *p_entry = NULL; -#ifdef GETPEERUCRED - ucred_t *ucred = NULL; -#elif defined(HAVE_GETPEEREID) uid_t euid = -1; gid_t egid = -1; -#endif -#ifdef GETPEERUCRED - if (getpeerucred(client->fcl_sock_fd, &ucred) < 0) { - error_e("Could not get client credentials using getpeerucred()"); - return; - } -#elif defined(HAVE_GETPEEREID) if (getpeereid(client->fcl_sock_fd, &euid, &egid) < 0) { error_e("Could not get client credentials using getpeereid()"); return; } -#else -#error "No authentication method in auth_client_getpeer()!" -#endif - - p_entry = getpwuid(cred.uid); + p_entry = getpwuid(euid); if (p_entry == NULL) { - error_e("Could not find password entry for uid %d", cred.uid); + error_e("Could not find password entry for uid %d", euid); return; } /* Successfully identified user: */ client->fcl_user = strdup2(p_entry->pw_name); - explain("Client's pid=%d, uid=%d, gid=%d username=%s\n", cred.pid, cred.uid, - cred.gid, client->fcl_user); + explain("Client's uid=%d, gid=%d username=%s\n", euid, egid, + client->fcl_user); } -#endif /* HAVE_GETPEERUCRED || HAVE_GETPEEREID */ +#endif /* HAVE_GETPEEREID */