]> diplodocus.org Git - nmh/blobdiff - uip/aliasbr.c
Replaced snprintf() to convert an int with m_name().
[nmh] / uip / aliasbr.c
index 039344165e22ed57f15d3bbc378b5bb8c711f380..489b707bdb43f1eb244da9b27fa3ee0daff333ec 100644 (file)
@@ -1,6 +1,4 @@
-
-/*
- * aliasbr.c -- new aliasing mechanism
+/* aliasbr.c -- new aliasing mechanism
  *
  * This code is Copyright (c) 2002, by the authors of nmh.  See the
  * COPYRIGHT file in the root directory of the nmh distribution for
@@ -11,7 +9,6 @@
 #include <h/aliasbr.h>
 #include <h/addrsbr.h>
 #include <h/utils.h>
-#include <grp.h>
 #include <pwd.h>
 
 static int akvis;
@@ -20,15 +17,11 @@ static char *akerrst;
 struct aka *akahead = NULL;
 struct aka *akatail = NULL;
 
-struct home *homehead = NULL;
-struct home *hometail = NULL;
-
 /*
  * prototypes
  */
 int alias (char *);
 int akvisible (void);
-void init_pw (void);
 char *akresult (struct aka *);
 char *akvalue (char *);
 char *akerror (int);
@@ -39,12 +32,9 @@ static char *scanp (char *);
 static char *getp (char *);
 static char *seekp (char *, char *, char **);
 static int addfile (struct aka *, char *);
-static int addgroup (struct aka *, char *);
-static int addmember (struct aka *, char *);
 static char *getalias (char *);
 static void add_aka (struct aka *, char *);
 static struct aka *akalloc (char *);
-static struct home *hmalloc (struct passwd *);
 
 
 /* Do mh alias substitution on 's' and return the results. */
@@ -112,7 +102,9 @@ akval (struct aka *ak, char *s)
     for (; ak; ak = ak->ak_next) {
        if (aleq (s, ak->ak_name)) {
            return akresult (ak);
-       } else if (strchr (s, ':')) {
+       }
+
+        if (strchr (s, ':')) {
            /* The first address in a blind list will contain the
               alias name, so try to match, but just with just the
               address (not including the list name).  If there's a
@@ -130,6 +122,8 @@ akval (struct aka *ak, char *s)
                if (mp  &&  mp->m_ingrp) {
                    char *gname = add (mp->m_gname, NULL);
 
+                    /* FIXME: gname must be true;  add() never returns NULL.
+                    * Is some other test required? */
                    if (gname  &&  aleq (name, ak->ak_name)) {
                        /* Will leak cp. */
                        cp = concat (gname, akresult (ak), NULL);
@@ -158,14 +152,13 @@ aleq (char *string, char *aliasent)
 {
     char c;
 
-    while ((c = *string++))
+    while ((c = *string++)) {
        if (*aliasent == '*')
            return 1;
-       else
-           if ((c | 040) != (*aliasent | 040))
-               return 0;
-           else
-               aliasent++;
+        if (tolower((unsigned char)c) != tolower((unsigned char)*aliasent))
+            return 0;
+        aliasent++;
+    }
 
     return (*aliasent == 0 || *aliasent == '*');
 }
@@ -181,7 +174,7 @@ alias (char *file)
     FILE *fp;
 
     if (*file != '/'
-           && (strncmp (file, "./", 2) && strncmp (file, "../", 3)))
+            && !has_prefix(file, "./") && !has_prefix(file, "../"))
        file = etcpath (file);
     if ((fp = fopen (file, "r")) == NULL) {
        akerrst = file;
@@ -201,7 +194,7 @@ alias (char *file)
                    fclose (fp);
                    return i;
                }
-
+               continue;
            case ':':           /* comment */
            case ';': 
            case '#':
@@ -248,28 +241,6 @@ alias (char *file)
                }
                break;
 
-           case '=':           /* UNIX group */
-               if (!*(cp = getp (pp + 1))) {
-                   fclose (fp);
-                   return AK_ERROR;
-               }
-               if (!addgroup (ak, cp)) {
-                   fclose (fp);
-                   return AK_NOGROUP;
-               }
-               break;
-
-           case '+':           /* UNIX group members */
-               if (!*(cp = getp (pp + 1))) {
-                   fclose (fp);
-                   return AK_ERROR;
-               }
-               if (!addmember (ak, cp)) {
-                   fclose (fp);
-                   return AK_NOGROUP;
-               }
-               break;
-
            default:            /* list */
                while ((cp = getalias (pp)))
                    add_aka (ak, cp);
@@ -300,10 +271,6 @@ akerror (int i)
            snprintf (buffer, sizeof(buffer), "out of memory while on '%s'", akerrst);
            break;
 
-       case AK_NOGROUP: 
-           snprintf (buffer, sizeof(buffer), "no such group as '%s'", akerrst);
-           break;
-
        default: 
            snprintf (buffer, sizeof(buffer), "unknown error (%d)", i);
            break;
@@ -373,67 +340,6 @@ addfile (struct aka *ak, char *file)
 }
 
 
-static int
-addgroup (struct aka *ak, char *grp)
-{
-    char *gp;
-    struct group *gr = getgrnam (grp);
-    struct home *hm = NULL;
-
-    if (!gr)
-       gr = getgrgid (atoi (grp));
-    if (!gr) {
-       akerrst = grp;
-       return 0;
-    }
-
-    while ((gp = *gr->gr_mem++))
-    {
-       struct passwd *pw;
-       for (hm = homehead; hm; hm = hm->h_next)
-           if (!strcmp (hm->h_name, gp)) {
-               add_aka (ak, hm->h_name);
-               break;
-           }
-        if ((pw = getpwnam(gp)))
-       {
-               hmalloc(pw);
-               add_aka (ak, gp);
-       }
-    }
-
-    return 1;
-}
-
-
-static int
-addmember (struct aka *ak, char *grp)
-{
-    gid_t gid;
-    struct group *gr = getgrnam (grp);
-    struct home *hm = NULL;
-
-    if (gr)
-       gid = gr->gr_gid;
-    else {
-       gid = atoi (grp);
-       gr = getgrgid (gid);
-    }
-    if (!gr) {
-       akerrst = grp;
-       return 0;
-    }
-
-    init_pw ();
-
-    for (hm = homehead; hm; hm = hm->h_next)
-       if (hm->h_gid == gid)
-           add_aka (ak, hm->h_name);
-
-    return 1;
-}
-
-
 static char *
 getalias (char *addrs)
 {
@@ -489,31 +395,6 @@ add_aka (struct aka *ak, char *pp)
 }
 
 
-void
-init_pw (void)
-{
-    struct passwd  *pw;
-    static int init;
-  
-    if (!init)
-    {
-       /* if the list has yet to be initialized */
-       /* zap the list, and rebuild from scratch */
-       homehead=NULL;
-       hometail=NULL;
-       init++;
-
-       setpwent ();
-
-       while ((pw = getpwent ()))
-           if (!hmalloc (pw))
-               break;
-
-       endpwent ();
-    }
-}
-
-
 static struct aka *
 akalloc (char *id)
 {
@@ -532,26 +413,3 @@ akalloc (char *id)
 
     return p;
 }
-
-
-static struct home *
-hmalloc (struct passwd *pw)
-{
-    struct home *p;
-
-    NEW(p);
-    p->h_name = getcpy (pw->pw_name);
-    p->h_uid = pw->pw_uid;
-    p->h_gid = pw->pw_gid;
-    p->h_home = getcpy (pw->pw_dir);
-    p->h_shell = getcpy (pw->pw_shell);
-    p->h_ngrps = 0;
-    p->h_next = NULL;
-    if (hometail != NULL)
-       hometail->h_next = p;
-    if (homehead == NULL)
-       homehead = p;
-    hometail = p;
-
-    return p;
-}