]> granicus.if.org Git - fcron/commitdiff
Fixed compilation on Solaris 10 (missing strndup()) and Solaris credentials extractio...
authorThibault Godouet <fcron@free.fr>
Thu, 16 Jan 2014 21:28:52 +0000 (21:28 +0000)
committerThibault Godouet <fcron@free.fr>
Thu, 16 Jan 2014 21:28:52 +0000 (21:28 +0000)
mem.c
mem.h
socket.c

diff --git a/mem.c b/mem.c
index 8293bb343f99aaa1e24ba8d65feb66bdde792f2b..59b001d6ec148a67696f8cb5014073a80593475d 100644 (file)
--- a/mem.c
+++ b/mem.c
 #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 <ghazi@caip.rutgers.edu> */
+{
+    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 1f7a656a82ab69ac53c9ac4651783a21e625190d..5b98dc8b98008cfb75ed6327efb57159fad509d1 100644 (file)
--- 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);
index e52c957a0261122ef82019f15cca9248ebb58f48..8832761f0ddfd85da3094184ff82f37b5cbd3fad 100644 (file)
--- 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 */