summaryrefslogtreecommitdiffstats
path: root/src/config_perms.c
blob: 0d42af53d428fad5fd0423edd516992e2df134a9 (plain)
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
/* 	Asterisk Manager Proxy
	Copyright (c) 2005-2008 David C. Troy <dave@popvox.com>
	
	This program is free software, distributed under the terms of
	the GNU General Public License.

	config_perms.c
	routines to read and parse the astmanproxy.users file
*/

#include "astmanproxy.h"

extern pthread_mutex_t userslock;

void *free_userperm(struct proxy_user *pu) {
	struct proxy_user *next_pu;

	while( pu ) {
		next_pu = pu->next;
		free( pu );
		pu = next_pu;
	}
	return 0;
}

void *add_userperm(char* username, char *userspec, struct proxy_user **pu) {

	int ccount = 0;
	struct proxy_user *user;
	char *s;
	
	/* malloc ourselves a server credentials structure */
	user = malloc(sizeof(struct proxy_user));
	if ( !user ) {
		fprintf(stderr, "Failed to allocate user credentials: %s\n", strerror(errno));
		exit(1);
	}
	memset(user, 0, sizeof (struct proxy_user) );
	
	s = userspec;
	strncpy(user->username, username, sizeof(user->username)-1 );
	do {
		if ( *s == ',' ) {
			ccount++;
			continue;
		}
		switch(ccount) {
			case 0:
			 strncat(user->secret, s, 1);
			 break;
			case 1:
			 strncat(user->channel, s, 1);
			 break;
			case 2:
			 strncat(user->ocontext, s, 1);
			 break;
			case 3:
			 strncat(user->icontext, s, 1);
			 break;
		}
	} while (*(s++));

	user->next = *pu;
	*pu = user;

	return 0;
}

void *processperm(char *s, struct proxy_user **pu) {
	char name[80],value[80];
	int nvstate = 0;


	memset (name,0,sizeof name);
	memset (value,0,sizeof value);

	do {
		*s = tolower(*s);

		if ( *s == ' ' || *s == '\t')
			continue;
		if ( *s == ';' || *s == '#' || *s == '\r' || *s == '\n' )
			break;
		if ( *s == '=' ) {
			nvstate = 1;
			continue;
		}
		if (!nvstate)
			strncat(name, s, 1);
		else
			strncat(value, s, 1);
	} while (*(s++));

	if (debug)
		debugmsg("perm: %s, %s", name, value);

	add_userperm(name,value,pu);

	return 0;
}

int ReadPerms() {
	FILE *FP;
	char buf[1024];
	char cfn[80];
	struct proxy_user *pu;

	pu=0;
	sprintf(cfn, "%s/%s", PDIR, PFILE);
	FP = fopen( cfn, "r" );

	if ( !FP )
	{
		fprintf(stderr, "Unable to open permissions file: %s/%s!\n", PDIR, PFILE);
		exit( 1 );
	}

	if (debug)
		debugmsg("config: parsing configuration file: %s", cfn);

	while ( fgets( buf, sizeof buf, FP ) ) {
		if (*buf == ';' || *buf == '\r' || *buf == '\n' || *buf == '#') continue;
		processperm(buf,&pu);
	}

	fclose(FP);

	pthread_mutex_lock(&userslock);
	free_userperm(pc.userlist);
	pc.userlist=pu;
	pthread_mutex_unlock(&userslock);

	return 0;
}