1 /*
2  * dselect - Debian package maintenance user interface
3  * methparse.cc - access method list parsing
4  *
5  * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6  * Copyright © 2008-2011, 2013-2015 Guillem Jover <guillem@debian.org>
7  *
8  * This is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #include <config.h>
23 #include <compat.h>
24 
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/wait.h>
28 
29 #include <errno.h>
30 #include <limits.h>
31 #include <string.h>
32 #include <dirent.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 
37 #include <dpkg/i18n.h>
38 #include <dpkg/c-ctype.h>
39 #include <dpkg/dpkg.h>
40 #include <dpkg/dpkg-db.h>
41 
42 #include "dselect.h"
43 #include "bindings.h"
44 #include "method.h"
45 
46 int noptions=0;
47 struct dselect_option *options = nullptr, *coption = nullptr;
48 struct method *methods = nullptr;
49 
50 static void DPKG_ATTR_NORET
badmethod(const char * pathname,const char * why)51 badmethod(const char *pathname, const char *why)
52 {
53   ohshit(_("syntax error in method options file '%.250s' -- %s"), pathname, why);
54 }
55 
56 static void DPKG_ATTR_NORET
eofmethod(const char * pathname,FILE * f,const char * why)57 eofmethod(const char *pathname, FILE *f, const char *why)
58 {
59   if (ferror(f))
60     ohshite(_("error reading options file '%.250s'"), pathname);
61   badmethod(pathname,why);
62 }
63 
readmethods(const char * pathbase,dselect_option ** optionspp,int * nread)64 void readmethods(const char *pathbase, dselect_option **optionspp, int *nread) {
65   static const char *const methodprograms[]= {
66     METHODSETUPSCRIPT,
67     METHODUPDATESCRIPT,
68     METHODINSTALLSCRIPT,
69     nullptr
70   };
71   const char *const *ccpp;
72   int methodlen, c, baselen;
73   char *pathinmeth, *pathbuf, *pathmeth;
74   DIR *dir;
75   FILE *names, *descfile;
76   struct dirent *dent;
77   struct varbuf vb;
78   method *meth;
79   dselect_option *opt;
80   struct stat stab;
81 
82   baselen= strlen(pathbase);
83   pathbuf= new char[baselen+IMETHODMAXLEN+IOPTIONMAXLEN+sizeof(OPTIONSDESCPFX)+10];
84   strcpy(pathbuf,pathbase);
85   strcpy(pathbuf+baselen,"/");
86   pathmeth= pathbuf+baselen+1;
87 
88   dir= opendir(pathbuf);
89   if (!dir) {
90     if (errno == ENOENT) {
91       delete[] pathbuf;
92       return;
93     }
94     ohshite(_("unable to read '%.250s' directory for reading methods"),
95             pathbuf);
96   }
97 
98   debug(dbg_general, "readmethods('%s',...) directory open", pathbase);
99 
100   while ((dent = readdir(dir)) != nullptr) {
101     c= dent->d_name[0];
102     debug(dbg_general, "readmethods('%s',...) considering '%s' ...",
103           pathbase, dent->d_name);
104     if (c != '_' && !c_isalpha(c))
105       continue;
106     char *p = dent->d_name + 1;
107     while ((c = *p) != 0 && c_isalnum(c) && c != '_')
108       p++;
109     if (c) continue;
110     methodlen= strlen(dent->d_name);
111     if (methodlen > IMETHODMAXLEN)
112       ohshit(_("method '%.250s' has name that is too long (%d > %d characters)"),
113              dent->d_name, methodlen, IMETHODMAXLEN);
114     /* Check if there is a localized version of this method */
115 
116     strcpy(pathmeth, dent->d_name);
117     strcpy(pathmeth+methodlen, "/");
118     pathinmeth= pathmeth+methodlen+1;
119 
120     for (ccpp= methodprograms; *ccpp; ccpp++) {
121       strcpy(pathinmeth,*ccpp);
122       if (access(pathbuf,R_OK|X_OK))
123         ohshite(_("unable to access method script '%.250s'"), pathbuf);
124     }
125     debug(dbg_general, " readmethods('%s',...) scripts ok", pathbase);
126 
127     strcpy(pathinmeth,METHODOPTIONSFILE);
128     names= fopen(pathbuf,"r");
129     if (!names)
130       ohshite(_("unable to read method options file '%.250s'"), pathbuf);
131 
132     meth= new method;
133     meth->name= new char[strlen(dent->d_name)+1];
134     strcpy(meth->name,dent->d_name);
135     meth->path= new char[baselen+1+methodlen+2+50];
136     strncpy(meth->path,pathbuf,baselen+1+methodlen);
137     strcpy(meth->path+baselen+1+methodlen,"/");
138     meth->pathinmeth= meth->path+baselen+1+methodlen+1;
139     meth->next= methods;
140     meth->prev = nullptr;
141     if (methods)
142       methods->prev = meth;
143     methods= meth;
144     debug(dbg_general, " readmethods('%s',...) new method"
145                        " name='%s' path='%s' pathinmeth='%s'",
146           pathbase, meth->name, meth->path, meth->pathinmeth);
147 
148     while ((c= fgetc(names)) != EOF) {
149       if (c_isspace(c))
150         continue;
151       opt= new dselect_option;
152       opt->meth= meth;
153       vb.reset();
154       do {
155         if (!c_isdigit(c))
156           badmethod(pathbuf, _("non-digit where digit wanted"));
157         vb(c);
158         c= fgetc(names);
159         if (c == EOF)
160           eofmethod(pathbuf, names, _("end of file in index string"));
161       } while (!c_isspace(c));
162       if (strlen(vb.string()) > OPTIONINDEXMAXLEN)
163         badmethod(pathbuf,_("index string too long"));
164       strcpy(opt->index,vb.string());
165       do {
166         if (c == '\n') badmethod(pathbuf,_("newline before option name start"));
167         c= fgetc(names);
168         if (c == EOF)
169           eofmethod(pathbuf, names, _("end of file before option name start"));
170       } while (c_isspace(c));
171       vb.reset();
172       if (!c_isalpha(c) && c != '_')
173         badmethod(pathbuf,_("nonalpha where option name start wanted"));
174       do {
175         if (!c_isalnum(c) && c != '_')
176           badmethod(pathbuf, _("non-alphanum in option name"));
177         vb(c);
178         c= fgetc(names);
179         if (c == EOF)
180           eofmethod(pathbuf, names, _("end of file in option name"));
181       } while (!c_isspace(c));
182       opt->name= new char[strlen(vb.string())+1];
183       strcpy(opt->name,vb.string());
184       do {
185         if (c == '\n') badmethod(pathbuf,_("newline before summary"));
186         c= fgetc(names);
187         if (c == EOF)
188           eofmethod(pathbuf, names, _("end of file before summary"));
189       } while (c_isspace(c));
190       vb.reset();
191       do {
192         vb(c);
193         c= fgetc(names);
194         if (c == EOF)
195           eofmethod(pathbuf, names, _("end of file in summary - missing newline"));
196       } while (c != '\n');
197       opt->summary= new char[strlen(vb.string())+1];
198       strcpy(opt->summary,vb.string());
199 
200       strcpy(pathinmeth,OPTIONSDESCPFX);
201       strcpy(pathinmeth+sizeof(OPTIONSDESCPFX)-1,opt->name);
202       descfile= fopen(pathbuf,"r");
203       if (!descfile) {
204         if (errno != ENOENT)
205           ohshite(_("unable to open option description file '%.250s'"), pathbuf);
206         opt->description = nullptr;
207       } else { /* descfile != 0 */
208         if (fstat(fileno(descfile),&stab))
209           ohshite(_("unable to stat option description file '%.250s'"), pathbuf);
210         opt->description= new char[stab.st_size+1];  errno=0;
211         size_t filelen = stab.st_size;
212         if (fread(opt->description,1,stab.st_size+1,descfile) != filelen)
213           ohshite(_("failed to read option description file '%.250s'"), pathbuf);
214         opt->description[stab.st_size]= 0;
215         if (ferror(descfile))
216           ohshite(_("error during read of option description file '%.250s'"), pathbuf);
217         fclose(descfile);
218       }
219       strcpy(pathinmeth,METHODOPTIONSFILE);
220 
221       debug(dbg_general,
222             " readmethods('%s',...) new option index='%s' name='%s'"
223             " summary='%.20s' strlen(description=%s)=%zu method name='%s'"
224             " path='%s' pathinmeth='%s'",
225             pathbase,
226             opt->index, opt->name, opt->summary,
227             opt->description ? "'...'" : "null",
228             opt->description ? strlen(opt->description) : -1,
229             opt->meth->name, opt->meth->path, opt->meth->pathinmeth);
230 
231       dselect_option **optinsert = optionspp;
232       while (*optinsert && strcmp(opt->index, (*optinsert)->index) > 0)
233         optinsert = &(*optinsert)->next;
234       opt->next= *optinsert;
235       *optinsert= opt;
236       (*nread)++;
237     }
238     if (ferror(names))
239       ohshite(_("error during read of method options file '%.250s'"), pathbuf);
240     fclose(names);
241   }
242   closedir(dir);
243   debug(dbg_general, "readmethods('%s',...) done", pathbase);
244   delete[] pathbuf;
245 }
246 
247 static char *methoptfile = nullptr;
248 
getcurrentopt()249 void getcurrentopt() {
250   char methoptbuf[IMETHODMAXLEN+1+IOPTIONMAXLEN+2];
251   FILE *cmo;
252   int l;
253   char *p;
254 
255   if (methoptfile == nullptr)
256     methoptfile = dpkg_db_get_path(CMETHOPTFILE);
257 
258   coption = nullptr;
259   cmo= fopen(methoptfile,"r");
260   if (!cmo) {
261     if (errno == ENOENT) return;
262     ohshite(_("unable to open current option file '%.250s'"), methoptfile);
263   }
264   debug(dbg_general, "getcurrentopt() cmethopt open");
265   if (!fgets(methoptbuf,sizeof(methoptbuf),cmo)) { fclose(cmo); return; }
266   if (fgetc(cmo) != EOF) { fclose(cmo); return; }
267   if (!feof(cmo)) { fclose(cmo); return; }
268   debug(dbg_general, "getcurrentopt() cmethopt eof");
269   fclose(cmo);
270   debug(dbg_general, "getcurrentopt() cmethopt read");
271   l= strlen(methoptbuf);  if (!l || methoptbuf[l-1] != '\n') return;
272   methoptbuf[--l]= 0;
273   debug(dbg_general, "getcurrentopt() cmethopt len and newline");
274   p= strchr(methoptbuf,' ');  if (!p) return;
275   debug(dbg_general, "getcurrentopt() cmethopt space");
276   *p++= 0;
277   debug(dbg_general, "getcurrentopt() cmethopt meth name '%s'", methoptbuf);
278   method *meth = methods;
279   while (meth && strcmp(methoptbuf, meth->name))
280     meth = meth->next;
281   if (!meth) return;
282   debug(dbg_general, "getcurrentopt() cmethopt meth found; opt '%s'", p);
283   dselect_option *opt = options;
284   while (opt && (opt->meth != meth || strcmp(p, opt->name)))
285     opt = opt->next;
286   if (!opt) return;
287   debug(dbg_general, "getcurrentopt() cmethopt opt found");
288   coption= opt;
289 }
290 
writecurrentopt()291 void writecurrentopt() {
292   struct atomic_file *file;
293 
294   if (methoptfile == NULL)
295     internerr("method options filename is NULL");
296 
297   file = atomic_file_new(methoptfile, (enum atomic_file_flags)0);
298   atomic_file_open(file);
299   if (fprintf(file->fp, "%s %s\n", coption->meth->name, coption->name) == EOF)
300     ohshite(_("unable to write new option to '%.250s'"), file->name_new);
301   atomic_file_close(file);
302   atomic_file_commit(file);
303   atomic_file_free(file);
304 }
305