Skip to content

Commit

Permalink
* Autoconf-like build and feature test tools implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
dave committed Aug 19, 2007
1 parent b8eac1b commit 23fdff3
Show file tree
Hide file tree
Showing 18 changed files with 138 additions and 86 deletions.
15 changes: 5 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,21 @@ NAME=cntlm
VER=`cat VERSION`
DIR=`pwd`

$(NAME): endian $(OBJS)
$(NAME): .config-stamp $(OBJS)
@echo "Linking $@"
@$(CC) $(CFLAGS) -o $@ $(OBJS) $(LDFLAGS)

endian: endian.c
@printf "Checking arch: "
@$(CC) $(CFLAGS) -o $@ endian.c
@if [ x`./endian NTLM_BIG_ENDIAN` != x ]; then echo "big endian"; else echo "little endian"; fi

proxy.o: proxy.c
@echo "Compiling $<"
@if [ -z "$(SYSCONFDIR)" ]; then \
$(CC) $(CFLAGS) `./endian NTLM_BIG_ENDIAN` -c proxy.c -o $@; \
$(CC) $(CFLAGS) -c proxy.c -o $@; \
else \
$(CC) $(CFLAGS) `./endian NTLM_BIG_ENDIAN` -DSYSCONFDIR=\"$(SYSCONFDIR)\" -c proxy.c -o $@; \
$(CC) $(CFLAGS) -DSYSCONFDIR=\"$(SYSCONFDIR)\" -c proxy.c -o $@; \
fi

.c.o:
@echo "Compiling $<"
@$(CC) $(CFLAGS) `./endian NTLM_BIG_ENDIAN` -c -o $@ $<
@$(CC) $(CFLAGS) -c -o $@ $<

install: $(NAME)
# AIX?
Expand Down Expand Up @@ -79,7 +74,7 @@ uninstall:
rm -f $(BINDIR)/$(NAME) $(MANDIR)/man1/$(NAME).1 2>/dev/null || true

clean:
@rm -f *.o tags cntlm endian pid massif* callgrind* 2>/dev/null
@rm -f *.o tags cntlm endian pid .config-stamp 2>/dev/null

cleanp:
@rm -f *.deb *.tgz *.tar.gz *.rpm *.o tags cntlm pid massif* callgrind* 2>/dev/null
Expand Down
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ Traditional installation
~~~~~~~~~~~~~~~~~~~~~~~~
First, you have to compile cntlm. Using the Makefile, this should be very easy:

$ ./configure
$ make

Cntlm does not require any dynamic libraries and there are no dependencies you
Expand Down
2 changes: 1 addition & 1 deletion acl.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ int acl_add(plist_t *rules, char *spec, enum acl_t acl) {
if (rules == NULL)
return 0;

spec = strdupl(spec);
spec = strdup(spec);
aux = (network_t *)new(sizeof(network_t));
i = strcspn(spec, "/");
if (i < strlen(spec)) {
Expand Down
2 changes: 1 addition & 1 deletion config.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ char *config_pop(config_t cf, const char *option) {

tmp = hlist_get(cf->options, option);
if (tmp) {
tmp = strdupl(tmp);
tmp = strdup(tmp);
cf->options = hlist_del(cf->options, option);
}

Expand Down
3 changes: 3 additions & 0 deletions config/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define config_endian 1
#define config_strdup 1
#define config_gethostname 1
16 changes: 16 additions & 0 deletions config/endian.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <stdio.h>
#include <stdint.h>

uint8_t num[] = { 0xEF, 0xBE };

/*
* RC: 1 = LE, 0 = BE
*/
int main(int argc, char **argv) {
int rc;

rc = (*((uint16_t *)num) == 0xBEEF);
printf("%s\n", rc ? "little endian" : "big endian");

return rc;
}
13 changes: 13 additions & 0 deletions config/gethostname.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <unistd.h>

int main(int argc, char **argv) {
char *tmp[300];

memset(tmp, 0, sizeof(tmp));
gethostname(tmp, sizeof(tmp)-1);
if (strlen(tmp))
printf("%s", tmp);

return !(!strlen(tmp));
}

5 changes: 5 additions & 0 deletions config/strdup.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <string.h>

int main(int argc, char **argv) {
return !strcmp("hello", strdup("hello"));
}
29 changes: 29 additions & 0 deletions configure
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/sh

CC=gcc
STAMP=.config-stamp
CONFIG=config/config.h
TESTS="endian strdup gethostname"

#[ -f $STAMP ] && exit 0
touch $STAMP

rm -f $CONFIG
for i in $TESTS; do
printf "Checking $i... "
printf "#define config_$i " >> $CONFIG
OUT=`gcc -Wall -pedantic -O3 -D_POSIX_C_SOURCE=199506L -D_ISOC99_SOURCE -D_REENTRANT -o config/$i config/$i.c 2>&1`
rc=$?

if [ $rc -ne 0 ]; then # -o -n "$OUT" ]; then
rc=0
RET=no
else
RET=`./config/$i`
rc=$?
[ -z "$RET" ] && if [ $rc -eq 0 ]; then RET="no"; else RET=yes; fi
fi

echo $rc >> $CONFIG
echo $RET
done
10 changes: 8 additions & 2 deletions doc/cntlm.1
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,10 @@ the whole output to the bug report.

.TP
.B \-w <workstation>
Proxy account workstation NetBIOS name. Do not use full domain name (FQDN)
here. Just the first part. Some proxies require this information.
Workstation NetBIOS name. Do not use full domain name (FQDN) here. Just the
first part. If not specified, \fBcntlm\fP tries to get the system hostname and
if that fails, uses "cntlm" - it's because some proxies require this field
non-empty.

.SH CONFIGURATION
Configuration file has the same syntax as OpenSSH ssh_config. It comprises of
Expand Down Expand Up @@ -439,6 +441,10 @@ Tunnel specification. See \fB-L\fP for more.
Proxy account name, without the possibility to include domain name ('at' sign
is interpreted literally).

.TP
.B Workstation <hostname>
The hostname of your workstation.

.SH FILES
The optional location of the configuration file is defined in the Makefile,
with the default for 1) deb/rpm package, 2) traditional "make; make install"
Expand Down
5 changes: 4 additions & 1 deletion doc/files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
./acl.h
./config.c
./config.h
./endian.c
./config/endian.c
./config/gethostname.c
./config/strdup.c
./configure
./ntlm.c
./ntlm.h
./proxy.c
Expand Down
37 changes: 0 additions & 37 deletions endian.c

This file was deleted.

10 changes: 5 additions & 5 deletions ntlm.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ int ntlm_request(char **dst, char *hostname, char *domain, int nt, int lm, uint3
VAL(buf, uint32_t, 28) = U32LE(32);

if (!nt) {
tmp = uppercase(strdupl(hostname));
tmp = uppercase(strdup(hostname));
memcpy(buf+32, tmp, hlen);
free(tmp);

tmp = uppercase(strdupl(domain));
tmp = uppercase(strdup(domain));
memcpy(buf+32+hlen, tmp, dlen);
free(tmp);
} else {
Expand Down Expand Up @@ -200,9 +200,9 @@ int ntlm_response(char **dst, char *challenge, char *username, char *password, c
ulen = unicode(&uuser, username);
hlen = unicode(&uhost, hostname);
} else {
udomain = uppercase(strdupl(domain));
uuser = uppercase(strdupl(username));
uhost = uppercase(strdupl(hostname));
udomain = uppercase(strdup(domain));
uuser = uppercase(strdup(username));
uhost = uppercase(strdup(hostname));

dlen = strlen(domain);
ulen = strlen(username);
Expand Down
40 changes: 24 additions & 16 deletions proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
* Some helping routines like linked list manipulation substr(), memory
* allocation, NTLM authentication routines, etc.
*/
#include "config/config.h"
#include "socket.h"
#include "utils.h"
#include "ntlm.h"
Expand Down Expand Up @@ -213,7 +214,7 @@ int parent_proxy(char *proxy, int port) {
/*
* Check format and parse it.
*/
proxy = strdupl(proxy);
proxy = strdup(proxy);
len = strlen(proxy);
i = strcspn(proxy, ": ");
if (i != len) {
Expand Down Expand Up @@ -295,12 +296,12 @@ int headers_recv(int fd, rr_data_t data) {

tok = strtok_r(NULL, " ", &s3);
if (tok)
ccode = strdupl(tok);
ccode = strdup(tok);

while (s3 < buf+len && *s3 == ' ')
s3++;
if (strlen(s3))
data->msg = strdupl(s3);
data->msg = strdup(s3);

if (!ccode || strlen(ccode) != 3 || (data->code = atoi(ccode)) == 0 || !data->http || !data->msg) {
i = -1;
Expand All @@ -312,11 +313,11 @@ int headers_recv(int fd, rr_data_t data) {
data->url = NULL;
data->http = NULL;

data->method = strdupl(tok);
data->method = strdup(tok);

tok = strtok_r(NULL, " ", &s3);
if (tok)
data->url = strdupl(tok);
data->url = strdup(tok);

tok = strtok_r(NULL, " ", &s3);
if (tok)
Expand Down Expand Up @@ -679,7 +680,7 @@ int authenticate(int sd, rr_data_t data, char *user, char *password, char *domai
*/
if (!CONNECT(data)) {
free(auth->method);
auth->method = strdupl("HEAD");
auth->method = strdup("HEAD");
}
auth->headers = hlist_mod(auth->headers, "Proxy-Authorization", buf, 1);
auth->headers = hlist_del(auth->headers, "Content-Length");
Expand Down Expand Up @@ -814,10 +815,10 @@ int scanner_hook(rr_data_t *request, rr_data_t *response, int *cd, int *sd, long

tmp = hlist_get((*request)->headers, "User-Agent");
if (tmp) {
tmp = lowercase(strdupl(tmp));
tmp = lowercase(strdup(tmp));
list = scanner_agent_list;
while (list) {
pat = lowercase(strdupl(list->aux));
pat = lowercase(strdup(list->aux));
if (debug)
printf("scanner_hook: matching U-A header (%s) to %s\n", tmp, pat);
if (!fnmatch(pat, tmp, 0)) {
Expand Down Expand Up @@ -936,7 +937,7 @@ int scanner_hook(rr_data_t *request, rr_data_t *response, int *cd, int *sd, long
newreq = dup_rr_data(*request);

free(newreq->method);
newreq->method = strdupl("POST");
newreq->method = strdup("POST");
hlist_mod(newreq->headers, "Referer", (*request)->url, 1);
hlist_mod(newreq->headers, "Content-Type", "application/x-www-form-urlencoded", 1);
hlist_mod(newreq->headers, "Content-Length", tmp, 1);
Expand Down Expand Up @@ -1459,9 +1460,9 @@ void *autotunnel(void *client) {
data2 = new_rr_data();

data1->req = 1;
data1->method = strdupl("CONNECT");
data1->url = strdupl(thost);
data1->http = strdupl("0");
data1->method = strdup("CONNECT");
data1->url = strdup(thost);
data1->http = strdup("0");

if (debug)
printf("Starting authentication...\n");
Expand Down Expand Up @@ -1563,7 +1564,7 @@ void tunnel_add(plist_t *list, char *spec, int gateway) {
char *field[4];
char *tmp;

spec = strdupl(spec);
spec = strdup(spec);
len = strlen(spec);
field[0] = spec;
for (count = 1, i = 0; i < len; ++i)
Expand Down Expand Up @@ -1644,7 +1645,7 @@ int main(int argc, char **argv) {

openlog("cntlm", LOG_CONS, LOG_DAEMON);

#ifdef NTLM_BIG_ENDIAN
#if config_endian == 0
syslog(LOG_INFO, "Starting cntlm version " VERSION " for BIG endian\n");
#else
syslog(LOG_INFO, "Starting cntlm version " VERSION " for LITTLE endian\n");
Expand Down Expand Up @@ -2063,8 +2064,15 @@ int main(int argc, char **argv) {
/*
* Set default values.
*/
if (!strlen(workstation))
strlcpy(workstation, "cntlm", AUTHSIZE);
if (!strlen(workstation)) {
#if config_gethostname == 1
gethostname(workstation, AUTHSIZE);
#endif
if (!strlen(workstation))
strlcpy(workstation, "cntlm", AUTHSIZE);

syslog(LOG_INFO, "Workstation name used: %s\n", workstation);
}

/*
* Ok, we are ready to rock. If daemon mode was requested,
Expand Down
1 change: 1 addition & 0 deletions redhat/cntlm.spec
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ contains detailed information.
%setup -q -n %{name}-%{version}

%build
./configure
make SYSCONFDIR=%{_sysconfdir} \
BINDIR=%{_sbindir} \
MANDIR=%{_mandir}
Expand Down
3 changes: 2 additions & 1 deletion swap.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#define _SWAP_H

#include <stdint.h>
#include "config/config.h"

#define swap16(x) \
((uint16_t)( \
Expand All @@ -36,7 +37,7 @@
(((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) | \
(((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24) ))

#ifdef NTLM_BIG_ENDIAN
#if config_endian == 0
# define U16LE(x) swap16(x)
# define U32LE(x) swap32(x)
# define U16BE(x) (x)
Expand Down
Loading

0 comments on commit 23fdff3

Please sign in to comment.