-
-/*
- * 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
* complete copyright information.
*/
-#include <h/mh.h>
-#include <h/aliasbr.h>
-#include <h/addrsbr.h>
-#include <h/utils.h>
-#include <grp.h>
+#include "h/mh.h"
+#include "sbr/concat.h"
+#include "sbr/vfgets.h"
+#include "sbr/getcpy.h"
+#include "h/aliasbr.h"
+#include "h/addrsbr.h"
+#include "h/utils.h"
#include <pwd.h>
static int akvis;
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);
-
static char *akval (struct aka *, char *);
-static int aleq (char *, char *);
-static char *scanp (char *);
+static bool aleq (char *, char *) PURE;
+static char *scanp (char *) PURE;
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. */
struct mailname *mp = getm (name, NULL, 0, NULL, 0);
if (mp && mp->m_ingrp) {
- char *gname = add (mp->m_gname, NULL);
+ char *gname = mh_xstrdup(FENDNULL(mp->m_gname));
+ /* 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);
}
-static int
+static bool
aleq (char *string, char *aliasent)
{
char c;
while ((c = *string++)) {
if (*aliasent == '*')
- return 1;
- if ((c | 040) != (*aliasent | 040))
- return 0;
+ return true;
+ if (tolower((unsigned char)c) != tolower((unsigned char)*aliasent))
+ return false;
aliasent++;
}
- return (*aliasent == 0 || *aliasent == '*');
+ return *aliasent == 0 || *aliasent == '*';
}
FILE *fp;
if (*file != '/'
- && !HasPrefix(file, "./") && !HasPrefix(file, "../"))
+ && !has_prefix(file, "./") && !has_prefix(file, "../"))
file = etcpath (file);
if ((fp = fopen (file, "r")) == NULL) {
akerrst = file;
fclose (fp);
return i;
}
-
+ continue;
case ':': /* comment */
case ';':
case '#':
}
switch (lc) {
case ':':
- ak->ak_visible = 0;
+ ak->ak_visible = false;
break;
case ';':
- ak->ak_visible = 1;
+ ak->ak_visible = true;
break;
default:
}
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);
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;
}
-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)
{
if (cp == NULL)
cp = addrs;
- else
- if (*cp == 0)
- return (cp = NULL);
+ else if (*cp == 0)
+ return cp = NULL;
/* Remove leading any space from the address. */
for (pp = cp; isspace ((unsigned char) *pp); pp++)
continue;
if (*pp == 0)
- return (cp = NULL);
+ return cp = NULL;
/* Find the end of the address. */
for (qp = pp; *qp != 0 && *qp != ','; qp++)
continue;
}
-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)
{
NEW(p);
p->ak_name = getcpy (id);
- p->ak_visible = 0;
+ p->ak_visible = false;
p->ak_addr = NULL;
p->ak_next = NULL;
if (akatail != NULL)
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;
-}