]> git.ipfire.org Git - people/ms/dma.git/commitdiff
Add a new patch to guess the username better when getlogin(3) fails.
authorPeter Pentchev <roam@ringlet.net>
Thu, 12 Mar 2009 12:46:47 +0000 (12:46 +0000)
committerPeter Pentchev <roam@ringlet.net>
Thu, 12 Mar 2009 12:46:47 +0000 (12:46 +0000)
changelog
patches/18-guess-username.patch [new file with mode: 0644]
patches/series

index e39a4cfb79aa26cf71ba5aff318ff327520ba7ea..0c593ad99441cd7db26c2261b68434c3024aeafe 100644 (file)
--- a/changelog
+++ b/changelog
@@ -1,3 +1,9 @@
+dma (0.0.2009.02.11-1~3) unstable; urgency=low
+
+  * Add a new patch to guess the username better when getlogin(3) fails.
+
+ -- Peter Pentchev <roam@ringlet.net>  Thu, 12 Mar 2009 14:43:14 +0200
+
 dma (0.0.2009.02.11-1~2) unstable; urgency=low
 
   * Add four new patches:
diff --git a/patches/18-guess-username.patch b/patches/18-guess-username.patch
new file mode 100644 (file)
index 0000000..f20c63d
--- /dev/null
@@ -0,0 +1,96 @@
+Apply better logic to guess the username when getlogin(3) is not
+available for some reason, e.g. no login session at all, no access
+to utmp/wtmp, etc.
+
+--- a/dma.c
++++ b/dma.c
+@@ -75,6 +75,8 @@
+ struct authusers authusers = LIST_HEAD_INITIALIZER(authusers);
+ static int daemonize = 1;
+ struct config *config;
++static const char *username;
++static uid_t uid;
+ static int open_locked(const char *, int);
+@@ -117,6 +119,45 @@
+       return name;
+ }
++static const char *
++check_username(const char *username, uid_t uid)
++{
++      struct passwd *pwd;
++
++      if (username == NULL)
++              return (NULL);
++      pwd = getpwnam(username);
++      if (pwd == NULL || pwd->pw_uid != uid)
++              return (NULL);
++      return (username);
++}
++
++static void
++set_username(void)
++{
++      struct passwd *pwd;
++      char *u;
++
++      uid = getuid();
++      username = check_username(getlogin(), uid);
++      if (username == NULL)
++              username = check_username(getenv("LOGNAME"), uid);
++      if (username == NULL)
++              username = check_username(getenv("USER"), uid);
++      if (username == NULL) {
++              pwd = getpwuid(uid);
++              if (pwd != NULL && pwd->pw_name != NULL &&
++                  pwd->pw_name[0] != '\0')
++                      username = check_username(strdup(pwd->pw_name), uid);
++      }
++      if (username == NULL) {
++              asprintf(&u, "%ld", (long)uid);
++              username = u;
++      }
++      if (username == NULL)
++              username = "unknown-or-invalid-username";
++}
++
+ static char *
+ set_from(const char *osender)
+ {
+@@ -125,7 +166,7 @@
+       if ((config->features & VIRTUAL) != 0) {
+               SLIST_FOREACH(v, &virtusers, next) {
+-                      if (strcmp(v->login, getlogin()) == 0) {
++                      if (strcmp(v->login, username) == 0) {
+                               sender = strdup(v->address);
+                               if (sender == NULL)
+                                       return(NULL);
+@@ -139,7 +180,7 @@
+               if (sender == NULL)
+                       return (NULL);
+       } else {
+-              if (asprintf(&sender, "%s@%s", getlogin(), hostname()) <= 0)
++              if (asprintf(&sender, "%s@%s", username, hostname()) <= 0)
+                       return (NULL);
+       }
+@@ -367,7 +408,7 @@
+ \tid %"PRIxMAX"\n\
+ \tby %s (%s)\n\
+ \t%s\n",
+-              getlogin(), getuid(),
++              username, uid,
+               sender,
+               queue->id,
+               hostname(), VERSION,
+@@ -1121,6 +1162,7 @@
+       opterr = 1;
+       openlog(tag, LOG_PID | LOG_PERROR, LOG_MAIL);
++      set_username();
+       config = malloc(sizeof(struct config));
+       if (config == NULL)
index 56668cec6df41e054d79636c3a068f1482bb9f77..7134a0c3a1dc2ddf6dfbba83595372202acd21b2 100644 (file)
@@ -15,3 +15,4 @@
 15-bounce-message.patch
 16-bounce-full.patch
 17-mailname.patch
+18-guess-username.patch