1 /*$Id: issub.c,v 1.4 1999/10/12 23:38:36 lindberg Exp $*/
2 /*$Name: ezmlm-idx-040 $*/
14 #include "subscribe.h"
17 static void die_nomem(fatal
)
20 strerr_die2x(111,fatal
,ERR_NOMEM
);
23 static stralloc addr
= {0};
24 static stralloc lcaddr
= {0};
25 static stralloc line
= {0};
26 static stralloc quoted
= {0};
27 static stralloc fn
= {0};
29 static char ssbuf
[512];
30 static char szh
[FMT_ULONG
];
32 char *issub(dbname
,userhost
,tab
,fatal
)
33 /* Returns (char *) to match if userhost is in the subscriber database */
34 /* dbname, 0 otherwise. dbname is a base directory for a list and may NOT */
36 /* NOTE: The returned pointer is NOT VALID after a subsequent call to issub!*/
38 char *dbname
; /* directory to basedir */
40 char *tab
; /* override table name */
51 if (!stralloc_copys(&addr
,"T")) die_nomem(fatal
);
52 if (!stralloc_cats(&addr
,userhost
)) die_nomem(fatal
);
54 j
= byte_rchr(addr
.s
,addr
.len
,'@');
55 if (j
== addr
.len
) return (char *) 0;
56 case_lowerb(addr
.s
+ j
+ 1,addr
.len
- j
- 1);
57 if (!stralloc_copy(&lcaddr
,&addr
)) die_nomem(fatal
);
58 case_lowerb(lcaddr
.s
+ 1,j
- 1); /* totally lc version of addr */
61 lch
= h
; /* make hash for both for backwards comp */
62 for (j
= 0;j
< addr
.len
;++j
) { /* (lcaddr.len == addr.len) */
63 h
= (h
+ (h
<< 5)) ^ (uint32
) (unsigned char) addr
.s
[j
];
64 lch
= (lch
+ (lch
<< 5)) ^ (uint32
) (unsigned char) lcaddr
.s
[j
];
67 lcch
= 64 + (lch
% 53);
69 if (!stralloc_0(&addr
)) die_nomem(fatal
);
70 if (!stralloc_0(&lcaddr
)) die_nomem(fatal
);
71 if (!stralloc_copys(&fn
,dbname
)) die_nomem(fatal
);
72 if (!stralloc_cats(&fn
,"/subscribers/")) die_nomem(fatal
);
73 if (!stralloc_catb(&fn
,&lcch
,1)) die_nomem(fatal
);
74 if (!stralloc_0(&fn
)) die_nomem(fatal
);
78 if (errno
!= error_noent
)
79 strerr_die4sys(111,fatal
,ERR_OPEN
,fn
.s
,": ");
81 substdio_fdbuf(&ss
,read
,fd
,ssbuf
,sizeof(ssbuf
));
84 if (getln(&ss
,&line
,&match
,'\0') == -1)
85 strerr_die4sys(111,fatal
,ERR_READ
,fn
.s
,": ");
87 if (line
.len
== lcaddr
.len
)
88 if (!case_diffb(line
.s
,line
.len
,lcaddr
.s
))
89 { close(fd
); return line
.s
+1; }
94 /* here if file not found or (file found && addr not there) */
96 if (ch
== lcch
) return (char *) 0;
98 /* try case sensitive hash for backwards compatibility */
99 fn
.s
[fn
.len
- 2] = ch
;
100 fd
= open_read(fn
.s
);
102 if (errno
!= error_noent
)
103 strerr_die4sys(111,fatal
,ERR_OPEN
,fn
.s
,": ");
106 substdio_fdbuf(&ss
,read
,fd
,ssbuf
,sizeof(ssbuf
));
109 if (getln(&ss
,&line
,&match
,'\0') == -1)
110 strerr_die4sys(111,fatal
,ERR_READ
,fn
.s
,": ");
112 if (line
.len
== addr
.len
)
113 if (!case_diffb(line
.s
,line
.len
,addr
.s
))
114 { close(fd
); return line
.s
+1; }