~ubuntu-branches/ubuntu/breezy/koffice/breezy-security

« back to all changes in this revision

Viewing changes to lib/kofficeui/koTemplates.cc

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2005-10-11 14:49:50 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051011144950-lwpngbifzp8nk0ds
Tags: 1:1.4.1-0ubuntu7
* SECURITY UPDATE: fix heap based buffer overflow in the RTF importer of KWord
* Opening specially crafted RTF files in KWord can cause
  execution of abitrary code.
* Add kubuntu_01_rtfimport_heap_overflow.diff
* References:
  CAN-2005-2971
  CESA-2005-005
  http://www.koffice.org/security/advisory-20051011-1.txt

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
#include <kdebug.h>
28
28
#include <kdeversion.h>
29
29
#include <kinstance.h>
 
30
#include <ksavefile.h>
30
31
#include <kstandarddirs.h>
 
32
#include <kiconloader.h>
31
33
#include <kio/netaccess.h>
32
34
 
33
35
#include <stdlib.h>
34
36
 
35
37
 
36
38
KoTemplate::KoTemplate(const QString &name, const QString &description, const QString &file,
37
 
                       const QString &picture, bool hidden,
 
39
                       const QString &picture, const QString &fileName, bool hidden,
38
40
                       bool touched) :
39
 
    m_name(name), m_descr(description), m_file(file), m_picture(picture), m_hidden(hidden),
40
 
    m_touched(touched), m_cached(false) {
 
41
    m_name(name), m_descr(description), m_file(file), m_picture(picture), m_fileName(fileName),
 
42
    m_hidden(hidden), m_touched(touched), m_cached(false) {
41
43
}
42
44
 
43
 
const QPixmap &KoTemplate::loadPicture() {
 
45
const QPixmap &KoTemplate::loadPicture( KInstance* instance ) {
44
46
 
45
47
    if(m_cached)
46
48
        return m_pixmap;
47
 
    QImage img( m_picture );
48
 
    if (img.isNull()) {
49
 
        kdWarning() << "Couldn't find icon " << m_picture << endl;
50
 
        m_pixmap=QPixmap();
51
 
        return m_pixmap;
52
 
    }
53
 
    const int maxHeightWidth = 64;
54
 
    if (img.width() > maxHeightWidth || img.height() > maxHeightWidth) {
55
 
        img = img.smoothScale( maxHeightWidth, maxHeightWidth, QImage::ScaleMax );
56
 
    }
57
 
    m_pixmap.convertFromImage(img, QPixmap::Color);
58
49
    m_cached=true;
59
 
    return m_pixmap;
 
50
    if ( m_picture[ 0 ] == '/' )
 
51
    {
 
52
        // ### TODO: use the class KoPicture instead of QImage to support non-image pictures
 
53
        QImage img( m_picture );
 
54
        if (img.isNull()) {
 
55
            kdWarning() << "Couldn't find icon " << m_picture << endl;
 
56
            m_pixmap=QPixmap();
 
57
            return m_pixmap;
 
58
        }
 
59
        const int maxHeightWidth = 64; // ### TODO: some people would surely like to have 128x128
 
60
        if (img.width() > maxHeightWidth || img.height() > maxHeightWidth) {
 
61
            img = img.smoothScale( maxHeightWidth, maxHeightWidth, QImage::ScaleMax );
 
62
        }
 
63
        m_pixmap.convertFromImage(img);
 
64
        return m_pixmap;
 
65
    } else { // relative path
 
66
        m_pixmap = instance->iconLoader()->loadIcon( m_picture, KIcon::Desktop, KIcon::SizeLarge /*48*/ );
 
67
        return m_pixmap;
 
68
    }
60
69
}
61
70
 
62
71
 
95
104
        return true;
96
105
    }
97
106
    else if(myTemplate && force) {
 
107
        //kdDebug() << "removing :" << myTemplate->fileName() << endl;
 
108
        QFile::remove( myTemplate->fileName()  );
 
109
        QFile::remove( myTemplate->picture() );
 
110
        QFile::remove( myTemplate->file() );
98
111
        m_templates.removeRef(myTemplate);
99
112
        m_templates.append(t);
100
113
        m_touched=touch;
114
127
 
115
128
KoTemplateTree::KoTemplateTree(const QCString &templateType,
116
129
                               KInstance *instance, bool readTree) :
117
 
    m_templateType(templateType), m_instance(instance), m_defaultGroup(0L) {
 
130
    m_templateType(templateType), m_instance(instance), m_defaultGroup(0L),
 
131
    m_defaultTemplate(0L) {
118
132
 
119
133
    m_groups.setAutoDelete(true);
120
134
    if(readTree)
128
142
}
129
143
 
130
144
void KoTemplateTree::writeTemplateTree() {
131
 
 
132
145
    QString localDir=m_instance->dirs()->saveLocation(m_templateType);
133
146
 
134
147
    for(KoTemplateGroup *group=m_groups.first(); group!=0L; group=m_groups.next()) {
149
162
                //kdDebug() << "hidden" << endl;
150
163
                if(group->dirs().count()==1 && !group->dirs().grep(localDir).isEmpty()) {
151
164
                    //kdDebug() << "local only" << endl;
152
 
                    KIO::NetAccess::del(group->dirs().first());
 
165
                    KIO::NetAccess::del(group->dirs().first(), 0);
153
166
                    //kdDebug() << "removing: " << group->dirs().first() << endl;
154
167
                }
155
168
                else {
163
176
                //kdDebug() << "++template: " << t->name() << endl;
164
177
                writeTemplate(t, group, localDir);
165
178
            }
166
 
            if(t->isHidden() && t->touched() && t->file().contains(localDir)) {
 
179
            if(t->isHidden() && t->touched() ) {
167
180
                //kdDebug() << "+++ delete local template ##############" << endl;
168
181
                writeTemplate(t, group, localDir);
169
182
                QFile::remove(t->file());
237
250
                //kdDebug() << "filePath: " << filePath << endl;
238
251
                QString icon;
239
252
                QString text;
240
 
                QString description;
 
253
                QString description;
241
254
                QString hidden_str;
 
255
                QString fileName;
242
256
                bool hidden=false;
 
257
                bool defaultTemplate = false;
243
258
                QString templatePath;
244
259
                // If a desktop file, then read the name from it.
245
260
                // Otherwise (or if no name in it?) use file name
248
263
                    config.setDesktopGroup();
249
264
                    if (config.readEntry("Type")=="Link") {
250
265
                        text=config.readEntry("Name");
 
266
                        hidden=config.readBoolEntry("X-KDE-Hidden", false);
 
267
                        fileName=filePath;
251
268
                        description=config.readEntry("Comment");
252
 
                        //kdDebug() << "name: " << text << endl;
 
269
                        //kdDebug() << "name: " << text << endl;
253
270
                        icon=config.readEntry("Icon");
254
 
                        //kdDebug() << "icon1: " << icon << endl;
255
 
                        if(icon[0]!='/') // allow absolute paths for icons
256
 
                            icon=*it + icon;
 
271
                        if(icon[0]!='/' && // allow absolute paths for icons
 
272
                           QFile::exists(*it+icon)) // allow icons from icontheme
 
273
                            icon=*it+icon;
257
274
                        //kdDebug() << "icon2: " << icon << endl;
258
 
                        hidden_str=config.readEntry("X-KDE-Hidden");
259
 
                        if(hidden_str.lower()=="true")
260
 
                            hidden=true;
 
275
                        hidden=config.readBoolEntry("X-KDE-Hidden", false);
 
276
                        defaultTemplate = config.readBoolEntry("X-KDE-DefaultTemplate", false);
261
277
                        //kdDebug() << "hidden: " << hidden_str << endl;
262
278
                        templatePath=config.readPathEntry("URL");
263
279
                        //kdDebug() << "Link to : " << templatePath << endl;
284
300
                    templatePath = filePath; // Note that we store the .png file as the template !
285
301
                    // That's the way it's always been done. Then the app replaces the extension...
286
302
                }
287
 
                KoTemplate *t=new KoTemplate(text, description, templatePath, icon, hidden);
 
303
                KoTemplate *t=new KoTemplate(text, description, templatePath, icon, fileName, hidden);
288
304
                groupIt.current()->add(t, false, false); // false -> we aren't a "user", false -> don't
289
305
                                                         // "touch" the group to avoid useless
290
306
                                                         // creation of dirs in .kde/blah/...
 
307
                if ( defaultTemplate )
 
308
                    m_defaultTemplate = t;
291
309
            }
292
310
        }
293
311
    }
295
313
 
296
314
void KoTemplateTree::writeTemplate(KoTemplate *t, KoTemplateGroup *group,
297
315
                                   const QString &localDir) {
 
316
    QString fileName;
 
317
    if ( t->isHidden() )
 
318
    {
 
319
        fileName = t->fileName();
 
320
        // try to remove the file
 
321
        if ( QFile::remove(fileName) || !QFile::exists(fileName) )
 
322
        {
 
323
            QFile::remove( t->name() );
 
324
            QFile::remove( t->picture() );
 
325
            return;
 
326
        }
 
327
    }
 
328
    // be sure that the template's file name is unique so we don't overwrite an other
 
329
    QString const path = localDir + group->name() + '/';
 
330
    QString const name = KoTemplates::stripWhiteSpace( t->name() );
 
331
    fileName = path + name + ".desktop";
 
332
    if ( t->isHidden() && QFile::exists(fileName) )
 
333
        return;
 
334
    QString fill;
 
335
    while ( KIO::NetAccess::exists( fileName, true, 0 ) )
 
336
    {
 
337
        fill += '_';
 
338
        fileName = path + fill + name + ".desktop";
 
339
    }
298
340
 
299
 
    KSimpleConfig config(KoTemplates::stripWhiteSpace(localDir+group->name()+'/'+t->name()+".desktop"));
 
341
    KSimpleConfig config( fileName );
300
342
    config.setDesktopGroup();
301
343
    config.writeEntry("Type", "Link");
302
 
#if KDE_IS_VERSION(3,1,3)
303
344
    config.writePathEntry("URL", t->file());
304
 
#else
305
 
    config.writeEntry("URL", t->file());
306
 
#endif
307
345
    config.writeEntry("Name", t->name());
308
346
    config.writeEntry("Icon", t->picture());
309
347
    config.writeEntry("X-KDE-Hidden", t->isHidden());