Include <iostream> without the .h on modern C++
[oweals/cde.git] / cde / programs / dtsearchpath / dtappg / dtappgather.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*******************************************************************
24 **  (c) Copyright Hewlett-Packard Company, 1990, 1991, 1992, 1993.
25 **  All rights are reserved.  Copying or other reproduction of this
26 **  program except for archival purposes is prohibited without prior
27 **  written consent of Hewlett-Packard Company.
28 ********************************************************************
29 ****************************<+>*************************************/
30 /*******************************************************************
31  *
32  *  File:              dtappgather.C
33  *
34  *  Purpose:           Create the Application Manager
35  *                     the desktop components.
36  *
37  *  Description:       dtappgather is invoked by the Xsession file at
38  *                     user login.  It collects the application groups
39  *                     and subdirectories that are to appear in the
40  *                     user's Application Manager view.
41  *
42  *                     It is also invoked by the "ReloadApps" action.
43  *
44  *  Product:           @(#)Common Desktop Environment 1.0          
45  *
46  *  Revision:          $TOG: dtappgather.C /main/9 1998/12/14 17:38:54 mgreess $
47  *
48  ********************************************************************/
49
50 /********************************************************************
51  *
52  *  Environment variables used by this utility
53  *
54  *  DTMOUNTPOINT
55  *     Default may be vendor specific:  /nfs
56  *
57  *  DTAPPSEARCHPATH
58  *     Default:  $HOME/.dt,/etc/dt/appconfig,/usr/dt/appconfig
59  *     Set at login by the dtsearchpath utility.
60  *
61  *  HOME
62  *     No default.
63  *     Set by the dtlogin program.
64  *
65  *  DISPLAY
66  *     No default.
67  *     Set by the dtlogin program.
68  *
69  *  TARGET_APPMAN_DIR
70  *     Default: /var/dt/appconfig/appmanager/$DTUSERSESSION
71  *     Set within this utility.
72  *
73  ****************************************************************************/
74 #include "Options.h"
75 #include "dtappgather.h"
76 #include "DirIterator.h"
77 #include <stdlib.h>
78 #if defined(linux)
79 #include <iostream>
80 #else
81 #include <iostream.h>
82 #endif
83
84 Options * options;
85
86 void LegacyCleanUp
87         (
88         CDEEnvironment * user
89         )
90 {
91
92     // For now, remove the old ApplicationManager subdirectory
93     // so that we don't leave old CDE users with extra baggage
94     // under $HOME.
95
96     CString targetAppmanDir (user->HOME() + ".dt/ApplicationManager");
97
98     if (user->OS()->isDirectory(targetAppmanDir) &&
99         0 == user->OS()->isLink(targetAppmanDir)) {
100
101         user->OS()->changePermissions(targetAppmanDir,0777);
102         user->OS()->removeDirectory(targetAppmanDir);
103     }
104 }
105
106 AppManagerDirectory::AppManagerDirectory
107         (
108         CDEEnvironment * user,
109         const CString &  app
110         ) : user_(user),
111             langVersionFound(0),
112             appsp_(app)
113 {
114     // Set the users Application Manager subdirectory
115
116     CString userhostdir = user->UserHostDir();
117
118     if (userhostdir.isNull())
119         userhostdir = "generic-display-0";
120
121     // else if dir is non-NULL and there is a / in DTUSERSESSION, 
122     // dtappgather creates /var/dt/appconfig/appmanager/DTUSERSESSION.
123     // This is a possible security hole, so
124     // prevent creation of directories of the form
125     // /var/dt/appconfig/appmanager/directory1/directory2 
126     //
127
128     else if ((char *)strstr(userhostdir.data(),"/"))
129         userhostdir = "generic-display-0";
130
131     dirname_ = "/var/dt/appconfig/appmanager/";
132     dirname_ += userhostdir;
133
134     if (user->OS()->isDirectory(dirname_) &&
135         0 == user->OS()->isLink(dirname_)) {
136
137         user->OS()->changeOwnerGroup(dirname_,"","");
138         user->OS()->changePermissions(dirname_,0755);
139 #ifdef sun
140         user->OS()->removeFiles(dirname_, ".~*");
141 #else
142         user->OS()->removeFiles(dirname_, "[.]~*");
143 #endif
144         user->OS()->removeDeadLinks(dirname_);
145         if (!options->Retain())
146 #ifdef sun
147             user->OS()->removeFiles(dirname_,"*");
148 #else
149             user->OS()->removeFiles(dirname_,"[.]*");
150 #endif
151     }
152     else {
153
154         // Make /var/dt/appconfig/appmanager directories if not present
155         // 
156         // Make user session subdirectory under /var/dt/appconfig/appmanager
157
158         CString dir(dirname_);
159         dir.replace("/" + userhostdir,"");
160         if (!user->OS()->isDirectory(dir)) {    // does appmanager exist?
161             dir.replace("/appmanager","");
162             if (!user->OS()->isDirectory(dir)) {   // does appconfig exist?
163                 dir.replace("/appconfig","");
164                 if (!user->OS()->isDirectory(dir)) {   // does dt exist?
165                     dir.replace("/dt","");
166                     if (!user->OS()->isDirectory(dir)) {  // does /var exist?
167                         user->OS()->MakeDirectory(dir,0775);
168                         user->OS()->changeOwnerGroup(dir,"root","other");
169                         user->OS()->changePermissions(dir,0775);
170                     }
171                     dir += "/dt";
172                     user->OS()->MakeDirectory(dir,0755);
173                     user->OS()->changeOwnerGroup(dir,"root","other");
174                     user->OS()->changePermissions(dir,0755);
175                 }
176                 dir += "/appconfig";
177                 user->OS()->MakeDirectory(dir,0755);
178                 user->OS()->changeOwnerGroup(dir,"bin","bin");
179                 user->OS()->changePermissions(dir,0755);
180             }
181             dir += "/appmanager";
182             user->OS()->MakeDirectory(dir,0755);
183             user->OS()->changeOwnerGroup(dir,"bin","bin");
184             user->OS()->changePermissions(dir,0755);
185         }
186         user->OS()->MakeDirectory(dirname_,0755);
187         user->OS()->changeOwnerGroup(dirname_,"","");
188     }
189
190     // Make /var/dt/tmp directory if not present
191
192     CString tmp("/var/dt/tmp/");
193     if (!user->OS()->isDirectory(tmp)) {  // does tmp exist?
194         user->OS()->MakeDirectory(tmp,0755);
195         user->OS()->changeOwnerGroup(tmp,"root","other");
196         user->OS()->changePermissions(tmp,0755);
197     }
198
199     tmp += userhostdir;
200     if (!user->OS()->isDirectory(tmp)) {  // does tmp/$DTUSERSESSION exist?
201         user->OS()->MakeDirectory(tmp, 0755);
202         user->OS()->changeOwnerGroup(tmp,"","");
203         user->OS()->changePermissions(tmp,0755);
204     }
205 }
206
207
208 /********************************************************************
209  * 
210  *  TraversePath()
211  *
212  *      Parse a given search path, using comma (,) and colon (:) as
213  *      delimiters.  Pass each path element to another function.
214  ********************************************************************/
215
216 void AppManagerDirectory::TraversePath()
217 {
218     if (!appsp_.isNull()) {
219         CTokenizedString subpath(appsp_,":");
220         CString dir = subpath.next();
221         while (!dir.isNull()) {
222             GatherAppsFromASearchElement (dir);
223             dir = subpath.next();
224             if (langVersionFound && dir == "/usr/dt/appconfig/appmanager/C")
225                 dir = subpath.next();
226         }
227     }
228 }
229
230 /********************************************************************
231  * 
232  *  goodFile()
233  *
234  *      Ignore '.' and '..' and make sure file doesn't exist.
235  *
236  ********************************************************************/
237
238 int AppManagerDirectory::goodFile
239         (
240         const CString & path,
241         const CString & fname
242         ) const
243 {
244     if (fname == "." || fname == "..")
245         return 0;
246
247     CString filnam(path + "/" + fname);
248
249     if (user_->OS()->FileExists(filnam))
250         return 0;
251
252     return 1;
253 }
254
255
256 /*********************************************************************
257  *
258  *  GatherAppsFromASearchElement()
259  *
260  *      Given search path element (host name or path name), construct
261  *      the appropriate path for the various desktop subsystems.
262  *
263  *      A path is constructed so that each element is appended to
264  *      the tail.
265  *********************************************************************/
266 void AppManagerDirectory::GatherAppsFromASearchElement
267         (
268         const CString & path
269         ) 
270 {
271     if (path.length()) {
272         CString source(path);
273         if (source.contains("/%L")) {
274             if (user_->OS()->LANG() != "C") {
275                 source.replace("%L",user_->OS()->LANG());
276                 if (!user_->OS()->isDirectory(source))
277                      return;
278                 if (source.contains("/usr/dt/appconfig/appmanager/"))
279                     langVersionFound = 1;
280             }
281             else
282                 return;
283         }
284
285         else if (!user_->OS()->isDirectory(source))
286             return;
287
288         DirectoryIterator iter (source);
289         struct dirent * direntry;
290
291         user_->OS()->setUserId();
292
293         while (direntry = iter()) {
294             CString dname(direntry->d_name);
295             if (user_->OS()->isDirectory(source + "/" + dname)
296              || user_->OS()->isFile(source + "/" + dname))
297                 if (goodFile(dirname_, dname))
298                     user_->OS()->symbolicLink (source + "/" + dname, 
299                                                dirname_ + "/" + dname);
300         }
301
302         user_->OS()->setUserId();
303     }
304 }
305
306 int main (int argc, char **argv)
307 {
308     options = new Options(argc, argv);
309
310     UnixEnvironment * os = new UnixEnvironment;
311
312     CDEEnvironment * user = new CDEEnvironment(0, os);
313
314     LegacyCleanUp(user);
315
316     CString app(getenv("DTAPPSEARCHPATH"));
317
318     AppManagerDirectory * appman = new AppManagerDirectory(user, app);
319     appman->TraversePath();
320
321     user->OS()->changePermissions((*appman)(),0555);
322
323     return 0;
324 }