1 #include "utils.h"
2 #include "string.h"
3 #include <dlfcn.h>
4 #include <sys/stat.h>
5 #include <errno.h>
6 #include <stdlib.h>
7 
8 extern int errno;
9 
IsChromeOs(void)10 unsigned char IsChromeOs(void)
11 {
12   int file_size=0,i=0;
13   unsigned char ret_stat = 0;
14   char *buf;
15   char *ptr;
16   char os_name[30]={0,};
17 
18 
19   FILE *fptr = fopen("/etc/os-release","r");
20   if(fptr == NULL)
21     return ret_stat;
22   fseek(fptr,0,SEEK_END);
23   file_size = ftell(fptr);
24   fseek(fptr,0,SEEK_SET);
25 
26   buf = (char *)malloc(file_size);
27   fread(buf,file_size,1,fptr);
28 
29   ptr=strstr(buf,"NAME");
30   if(ptr != NULL)
31   {
32     ptr = ptr + 5;
33     while(*ptr!='\n'&&*ptr!='\0')
34     {
35       os_name[i]=*ptr;
36       ptr++;i++;
37     }
38     if(strcasestr(os_name,"chrome os")!=NULL)
39       ret_stat = 1;
40     else
41       ret_stat = 0;
42 
43   }
44 
45   fclose(fptr);
46   free(buf);
47 
48   return ret_stat;
49 
50 
51 }
52 
GetPair(char * buf,int buf_len,char * key,char * value,char ** tail)53 static int GetPair(char *buf, int buf_len, char *key, char *value, char **tail)
54 {
55    int i=0, j;
56 
57    key[0] = 0;
58    value[0] = 0;
59 
60    if (buf[i] == '#')
61    {
62       for (; buf[i] != '\n' && i < buf_len; i++);  /* eat comment line */
63       if (buf[i] == '\n')
64          i++;   /* bump past '\n' */
65    }
66 
67    j = 0;
68    while ((buf[i] != '=') && (i < buf_len) && (j < UTILS_LINE_SIZE))
69       key[j++] = buf[i++];
70    for (j--; key[j] == ' ' && j > 0; j--);  /* eat white space before = */
71    key[++j] = 0;
72 
73    if (buf[i] == '=')
74       for (i++; buf[i] == ' ' && i < buf_len; i++);  /* eat white space after = */
75 
76    j = 0;
77    while ((buf[i] != '\n') && (i < buf_len) && (j < UTILS_LINE_SIZE))
78       value[j++] = buf[i++];
79    for (j--; value[j] == ' ' && j > 0; j--);  /* eat white space before \n */
80    value[++j] = 0;
81 
82    if (buf[i] == '\n')
83      i++;   /* bump past '\n' */
84 
85    if (tail != NULL)
86       *tail = buf + i;  /* tail points to next line */
87 
88    return i;
89 }
90 
91 
92 /* Get value for specified section and key from hplip.conf. */
get_conf(const char * section,const char * key,char * value,int value_size)93 enum UTILS_CONF_RESULT get_conf(const char *section, const char *key, char *value, int value_size)
94 {
95    return get_key_value(CONFDIR "/hplip.conf", section, key, value, value_size);
96 }
97 
98 /* Get value for specified section and key from specified file. */
get_key_value(const char * file,const char * section,const char * key,char * value,int value_size)99 enum UTILS_CONF_RESULT get_key_value(const char *file, const char *section, const char *key, char *value, int value_size)
100 {
101    char new_key[UTILS_LINE_SIZE];
102    char new_value[UTILS_LINE_SIZE];
103    char rcbuf[255];
104    char new_section[32];
105    char *tail;
106    FILE *inFile;
107    enum UTILS_CONF_RESULT stat = UTILS_CONF_DATFILE_ERROR;
108    int i,j;
109 
110    if((inFile = fopen(file, "r")) == NULL)
111    {
112       BUG("unable to open %s: %m\n", file);
113       goto bugout;
114    }
115 
116    new_section[0] = 0;
117 
118    /* Read the config file */
119    while ((fgets(rcbuf, sizeof(rcbuf), inFile) != NULL))
120    {
121       if (rcbuf[0] == '[')
122       {
123          i = j = 0;
124          while ((rcbuf[i] != ']') && (j < (sizeof(new_section)-2)))
125             new_section[j++] = rcbuf[i++];
126          new_section[j++] = rcbuf[i++];   /* ']' */
127          new_section[j] = 0;        /* zero terminate */
128          continue;
129       }
130 
131       GetPair(rcbuf, strlen(rcbuf), new_key, new_value, &tail);
132 
133       if ((strcasecmp(new_section, section) == 0) && (strcasecmp(new_key, key) == 0))
134       {
135          strncpy(value, new_value, value_size);
136          stat = UTILS_CONF_OK;
137          break;  /* done */
138       }
139    }
140 
141    if (stat != UTILS_CONF_OK)
142       BUG("unable to find %s %s in %s\n", section, key, file);
143 
144 bugout:
145    if (inFile != NULL)
146       fclose(inFile);
147 
148    return stat;
149 }
150 
151 
validate_plugin_version()152 enum UTILS_PLUGIN_STATUS validate_plugin_version()
153 {
154     char hplip_version[128];
155     char plugin_version[128];
156 
157     if (get_conf("[hplip]", "version", hplip_version, sizeof(hplip_version)) != UTILS_CONF_OK)
158       return UTILS_PLUGIN_STATUS_NOT_INSTALLED;
159 
160     if (get_key_value(HPLIP_PLUGIN_STATE,"[plugin]" , "version", plugin_version, sizeof(plugin_version)) != UTILS_CONF_OK )
161     {
162         BUG("validate_plugin_version() Failed to get Plugin version from [%s]\n", "/var/lib/hp/hplip.state");
163         return UTILS_PLUGIN_STATUS_NOT_INSTALLED;
164     }
165 
166 
167     if (strcmp(hplip_version, plugin_version) == 0)
168     {
169         return UTILS_PLUGIN_STATUS_OK;
170     }
171     else
172     {
173         BUG("validate_plugin_version() Plugin version[%s] mismatch with HPLIP version[%s]\n",plugin_version, hplip_version);
174         return UTILS_PLUGIN_STATUS_MISMATCH;
175     }
176     return UTILS_PLUGIN_STATUS_NOT_INSTALLED;
177 }
178 
179 
load_plugin_library(enum UTILS_PLUGIN_LIBRARY_TYPE eLibraryType,const char * szPluginName)180 void *load_plugin_library (enum UTILS_PLUGIN_LIBRARY_TYPE eLibraryType, const char *szPluginName)
181 {
182     void *pHandler = NULL;
183     char szHome[256];
184     char szLibraryFile[256];
185     unsigned char isChrome = 0;
186 
187     isChrome = IsChromeOs();
188 
189     if (szPluginName == NULL || szPluginName[0] == '\0')
190     {
191         BUG("Invalid Library name\n");
192         return pHandler;
193     }
194 
195 
196     if(!isChrome){
197       if (get_conf("[dirs]", "home", szHome, sizeof(szHome)) != UTILS_CONF_OK)
198       {
199           BUG("Failed to find the home directory from hplip.conf file\n");
200           return pHandler;
201       }
202 
203 
204       if (validate_plugin_version() != UTILS_PLUGIN_STATUS_OK )
205       {
206           BUG("Plugin version is not matching \n");
207           return pHandler;
208       }
209     }
210 
211     if(isChrome)
212       snprintf(szHome,sizeof(szHome),"/usr/libexec/cups");
213 
214     if (eLibraryType == UTILS_PRINT_PLUGIN_LIBRARY){
215         if(!isChrome)
216           snprintf(szLibraryFile, sizeof(szLibraryFile), "%s/prnt/plugins/%s", szHome, szPluginName);
217         else
218           snprintf(szLibraryFile, sizeof(szLibraryFile), "%s/filter/%s", szHome, szPluginName);
219     }
220     else if (eLibraryType == UTILS_SCAN_PLUGIN_LIBRARY)
221         snprintf(szLibraryFile, sizeof(szLibraryFile), "%s/scan/plugins/%s", szHome, szPluginName);
222     else if (eLibraryType == UTILS_FAX_PLUGIN_LIBRARY)
223         snprintf(szLibraryFile, sizeof(szLibraryFile), "%s/fax/plugins/%s", szHome, szPluginName);
224     else
225     {
226         BUG("Invalid Library Type =%d \n",eLibraryType);
227         return pHandler;
228     }
229 
230     return load_library (szLibraryFile);
231 
232 }
233 
load_library(const char * szLibraryFile)234 void *load_library (const char *szLibraryFile)
235 {
236     void *pHandler = NULL;
237 
238     if (szLibraryFile == NULL || szLibraryFile[0] == '\0')
239     {
240         BUG("Invalid Library name\n");
241         return pHandler;
242     }
243 
244     if ((pHandler = dlopen(szLibraryFile, RTLD_NOW|RTLD_GLOBAL)) == NULL)
245         BUG("unable to load library %s: %s\n", szLibraryFile, dlerror());
246 
247     return pHandler;
248 }
249 
get_library_symbol(void * pLibHandler,const char * szSymbol)250 void *get_library_symbol(void *pLibHandler, const char *szSymbol)
251 {
252     void *pSymHandler = NULL;
253     if (pLibHandler == NULL)
254     {
255         BUG("Invalid Library hanlder\n");
256         return NULL;
257     }
258 
259     if (szSymbol == NULL || szSymbol[0] == '\0')
260     {
261         BUG("Invalid Library symbol\n");
262         return NULL;
263     }
264 
265     pSymHandler = dlsym(pLibHandler, szSymbol);
266     if (pSymHandler == NULL)
267         BUG("Can't find %s symbol in Library:%s\n",szSymbol,dlerror());
268 
269     return pSymHandler;
270 }
271 
unload_library(void * pLibHandler)272 void unload_library(void *pLibHandler)
273 {
274     if (pLibHandler)
275         dlclose(pLibHandler);
276     else
277         BUG("Invalid Library hanlder pLibHandler = NULL.\n");
278 }
279 
createTempFile(char * szFileName,FILE ** pFilePtr)280 int createTempFile(char* szFileName, FILE** pFilePtr)
281 {
282     int iFD = -1;
283 
284     if (szFileName == NULL || szFileName[0] == '\0' || pFilePtr == NULL)
285     {
286         BUG("Invalid Filename/ pointer\n");
287         return 0;
288     }
289 
290     if (strstr(szFileName,"XXXXXX") == NULL)
291         strcat(szFileName,"_XXXXXX");
292 
293     iFD = mkstemp(szFileName);
294     if(-1 == iFD)
295     {
296         BUG("Failed to create the temp file Name[%s] errno[%d : %s]\n",szFileName,errno,strerror(errno));
297         return 0;
298     }
299     else
300     {
301         *pFilePtr = fdopen(iFD,"w+");
302     }
303 
304     return iFD;
305 }
306 
getHPLogLevel()307 int getHPLogLevel()
308 {
309     FILE    *fp;
310     char    str[258];
311     char    *p;
312     int iLogLevel = 0;
313 
314     fp = fopen ("/etc/cups/cupsd.conf", "r");
315     if (fp == NULL)
316         return 0;
317     while (!feof (fp))
318     {
319         if (!fgets (str, 256, fp))
320         {
321             break;
322         }
323         if ((p = strstr (str, "hpLogLevel")))
324         {
325             p += strlen ("hpLogLevel") + 1;
326             iLogLevel = atoi (p);
327             break;
328         }
329     }
330     fclose (fp);
331     return iLogLevel;
332 }
333 
334 
335