aboutsummaryrefslogtreecommitdiff
path: root/src/mklist.c
blob: 6da5cfa2be0e5d8bef72aebfb9a008d35e68adee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* duser - Manage MajorDomo lists
* Copyright (C) 2011 Joseph Hunkeler <jhunkeler@gmail.com, jhunk@stsci.edu>
*
* This file is part of duser.
*
* duser is free software: you can redifilenameibute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* duser is difilenameibuted in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with duser. If not, see <http://www.gnu.org/licenses/>.
**/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <errno.h>
#include <libgen.h>
#include <pcre.h>
#include "duser.h"

char* str_replace(char* str, char* old, char* new)
{
	static char buffer[BUFSIZ];
	char *p;
	if(!(p = strstr(str, old)))
		return NULL;

	strncpy(buffer, str, p-str);
	buffer[p-str] = 0;
	sprintf(buffer+(p-str), "%s%s", new, p+strlen(old));
	
	return buffer;
}

char* getfile(const char* filename)
{
	int bytes = 0;
	static char buffer[BUFSIZ];
	FILE* fp = NULL;
	if((fp = fopen(filename, "r")) == NULL)
	{
		fprintf(stderr, "%s: fopen: %s\n", SELF, strerror(errno));
		return NULL;
	}

	memset(buffer, 0, strlen(buffer));
	if((bytes = fread(buffer, BUFSIZ, 1, fp)) < 0)
	{
		fprintf(stderr, "%s: fread: %s\n", SELF, strerror(errno));
		exit(1);
	}
	buffer[strlen(buffer)] = 0;
	fclose(fp);

	return buffer;
}

static char* mailfmt = "mail -s '%s' -r %s %s"; 
int mailer(const char* subj, const char* from, const char* to, const char* mesg)
{
	char mail[BUFSIZ];
	if((snprintf(mail, BUFSIZ, mailfmt, subj, from, to)) < 1 )
	{
		fprintf(stderr, "%s: Failed to generate mail string.\n", SELF);
		return -1;
	}

	FILE *pipe;
	if((pipe = popen(mail, "w")) == NULL)
	{
		fprintf(stderr, "%s: Unable to open pipe.\n", SELF);
		return -1;
	}
	fputs(mesg, pipe);
	pclose(pipe);
	return 0;
}

int mklist(char* filename)
{
	uid_t uid;
	struct passwd *pwd;
	char tmpname[strlen(filename)+1];
//	char* list = basename(filename);
	int status = 0;
	uid = getuid();
	pwd = getpwuid(uid);

	if((status = access(filename, F_OK)) == 0)
	{
		COM(SELF, "FATAL: %s: '%s' already exists\n", basename(filename), strerror(errno));
		fprintf(stderr, "'%s' already exists\n", basename(filename));
		return -1;
	}

	strncpy(tmpname, filename, strlen(filename));
	strncat(tmpname, ".info", strlen(filename));
	if((status = touch(tmpname, 0644)) != 0)
	{
		COM(SELF, "FATAL: %s: %s: %s\n", tmpname, strerror(errno));
		fprintf(stderr, "FATAL: %s: %s: %s\n", SELF, tmpname, strerror(errno));
	}

	char* tmptpl;
//	char* tpl; 
	if((tmptpl = getfile("/usr/local/etc/duser/tpl/aliases")) == NULL)
	{
		fprintf(stderr, "%s: %s\n", SELF, strerror(errno));
		exit(1);
	}

	pcre *re;
	const int OVECOUNT = 30;
	const char* error;
	int rc;
	int erroffset;
	int ovector[OVECOUNT];

	if((re = pcre_compile("%s|list", 0, &error, &erroffset, NULL)) == NULL)
	{
		fprintf(stderr, "%s: PCRE compilation failed at offset %d: %s\n", SELF, erroffset, error);
		return -1;
	}

	if((rc = pcre_exec(re, NULL, tmptpl, strlen(tmptpl), 0, 0, ovector, OVECOUNT)) < 0)
	{
		fprintf(stderr, "%s: PCRE match failed '%d'\n", SELF, rc);
	}
	
	printf("%s\n", (tmptpl + ovector[1]));
	return 0;
}