1 /* $Id$ */
2 /* Copyright (c) 2015 Pierre Pronchery <khorben@defora.org> */
3 /* This file is part of DeforaOS Desktop Locker */
4 /* This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, version 3 of the License.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>. */
15 
16 
17 
18 #include <dirent.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <dlfcn.h>
23 #include "Locker.h"
24 
25 /* constants */
26 #define PROGNAME	"auth"
27 
28 
29 /* private */
30 /* prototypes */
31 static int _auth(void);
32 
33 static int _dlerror(char const * message, int ret);
34 static int _error(char const * message, char const * error, int ret);
35 static int _perror(char const * message, int ret);
36 
37 
38 /* functions */
39 /* applets */
_auth(void)40 static int _auth(void)
41 {
42 	int ret = 0;
43 	const char path[] = "../src/auth";
44 #ifdef __APPLE__
45 	const char ext[] = ".dylib";
46 #else
47 	const char ext[] = ".so";
48 #endif
49 	DIR * dir;
50 	struct dirent * de;
51 	size_t len;
52 	char * s;
53 	void * p;
54 	LockerAuthDefinition * lad;
55 
56 	if((dir = opendir(path)) == NULL)
57 		return -_perror(path, 1);
58 	while((de = readdir(dir)) != NULL)
59 	{
60 		if((len = strlen(de->d_name)) < sizeof(ext))
61 			continue;
62 		if(strcmp(&de->d_name[len - sizeof(ext) + 1], ext) != 0)
63 			continue;
64 		if((s = malloc(sizeof(path) + len + 1)) == NULL)
65 		{
66 			ret += _perror(de->d_name, 1);
67 			continue;
68 		}
69 		snprintf(s, sizeof(path) + len + 1, "%s/%s", path, de->d_name);
70 		if((p = dlopen(s, RTLD_LAZY)) == NULL)
71 		{
72 			ret += _dlerror(s, 1);
73 			free(s);
74 			continue;
75 		}
76 		if((lad = dlsym(p, "plugin")) == NULL)
77 			ret += _dlerror(s, 1);
78 		else
79 		{
80 			if(lad->icon == NULL)
81 				ret += _error(s, "No icon defined", 1);
82 			else
83 				printf("\n%s: %s (%s)\n%s\n", de->d_name,
84 						lad->name, lad->icon,
85 						(lad->description != NULL)
86 						? lad->description
87 						: "(no description)");
88 			if(lad->get_widget == NULL)
89 				ret += _error(s, "No widget available", 1);
90 			if(lad->action == NULL)
91 				ret += _error(s, "No action handler", 1);
92 		}
93 		free(s);
94 		dlclose(p);
95 	}
96 	closedir(dir);
97 	return ret;
98 }
99 
100 
101 /* dlerror */
_dlerror(char const * message,int ret)102 static int _dlerror(char const * message, int ret)
103 {
104 	fputs(PROGNAME ": ", stderr);
105 	fprintf(stderr, "%s: %s\n", message, dlerror());
106 	return ret;
107 }
108 
109 
110 /* error */
_error(char const * message,char const * error,int ret)111 static int _error(char const * message, char const * error, int ret)
112 {
113 	fputs(PROGNAME ": ", stderr);
114 	fprintf(stderr, "%s: %s\n", message, error);
115 	return ret;
116 }
117 
118 
119 /* perror */
_perror(char const * message,int ret)120 static int _perror(char const * message, int ret)
121 {
122 	fputs(PROGNAME ": ", stderr);
123 	perror(message);
124 	return ret;
125 }
126 
127 
128 /* public */
129 /* functions */
130 /* main */
main(void)131 int main(void)
132 {
133 	return (_auth() == 0) ? 0 : 2;
134 }
135