Ejemplo n.º 1
0
void KStandardDirs::createSpecialResource(const char *type)
{
    char hostname[256];
    hostname[0] = 0;
    gethostname(hostname, 255);
    QString dir = QString("%1%2-%3").arg(localkdedir()).arg(type).arg(hostname);
    char link[1024];
    link[1023] = 0;
    int result = readlink(QFile::encodeName(dir).data(), link, 1023);
    bool relink = (result == -1) && (errno == ENOENT);
    if(result > 0)
    {
        link[result] = 0;
        if(!QDir::isRelativePath(link))
        {
            KDE_struct_stat stat_buf;
            int res = KDE_lstat(link, &stat_buf);
            if((res == -1) && (errno == ENOENT))
            {
                relink = true;
            }
            else if((res == -1) || (!S_ISDIR(stat_buf.st_mode)))
            {
                fprintf(stderr, "Error: \"%s\" is not a directory.\n", link);
                relink = true;
            }
            else if(stat_buf.st_uid != getuid())
            {
                fprintf(stderr, "Error: \"%s\" is owned by uid %d instead of uid %d.\n", link, stat_buf.st_uid, getuid());
                relink = true;
            }
        }
    }

    if(relink)
    {
        QString srv = findExe(QString::fromLatin1("lnusertemp"), kfsstnd_defaultbindir());
        if(srv.isEmpty())
            srv = findExe(QString::fromLatin1("lnusertemp"));
        if(!srv.isEmpty())
        {
            system(QFile::encodeName(srv) + " " + type);
            result = readlink(QFile::encodeName(dir).data(), link, 1023);
        }
    }
    if(result > 0)
    {
        link[result] = 0;
        if(link[0] == '/')
            dir = QFile::decodeName(link);
        else
            dir = QDir::cleanDirPath(dir + QFile::decodeName(link));
    }

    addResourceDir(type, dir + '/');
}
Ejemplo n.º 2
0
void KFileShare::readShareList()
{
    KFileSharePrivate::self();
    if(!s_shareList)
        sdShareList.setObject(s_shareList, new QStringList);
    else
        s_shareList->clear();

    // /usr/sbin on Mandrake, $PATH allows flexibility for other distributions
    QString exe = findExe("filesharelist");
    if(exe.isEmpty())
    {
        s_authorization = ErrorNotFound;
        return;
    }
    KProcIO proc;
    proc << exe;
    if(!proc.start(KProcess::Block))
    {
        kdError() << "Can't run " << exe << endl;
        s_authorization = ErrorNotFound;
        return;
    }

    // Reading code shamelessly stolen from khostname.cpp ;)
    QString line;
    int length;
    do
    {
        length = proc.readln(line, true);
        if(length > 0)
        {
            if(line[length - 1] != '/')
                line += '/';
            s_shareList->append(line);
            kdDebug(7000) << "Shared dir:" << line << endl;
        }
    } while(length > -1);
}
Ejemplo n.º 3
0
int main(int argc, char **argv, char **envp)
{
    char *binary;
    char **arglist;
    char *label = NULL;
    char filelabel[SMACK_LONGLABEL];
    char mylabel[SMACK_LONGLABEL];
    int o;
    int rc;
    int lind = 0;
    int doexec = 0;
    //int binfd;

    static struct option lopts[] = {
        { "help",  no_argument,       NULL, 'h' },
        { "label", required_argument, NULL, 'l' },

        { NULL, 0, NULL, 0}
    };

    while ( (o = getopt_long(argc, argv, "+hxl:", lopts, &lind)) != -1) {
        switch (o) {
        case 'h':
            usage(argv[0], stdout, 0);
            break;
        case 'x':
            doexec = 1;
            break;
        case 'l':
            if (label) {
                free(label);
                usage(argv[0], stderr, 1);
            }
            label = strdup(optarg);
            break;
        default:
            if (label)
                free(label);
            usage(argv[0], stderr, 1);
            break;
        }
    }

    if ((rc = getsmack(mylabel, sizeof(mylabel)-1)) == -1) {
        fprintf(stderr, "%s: failed to get smack label\n", argv[0]);
        if (label) free(label);
        exit(1);
    }
    mylabel[rc] = 0;

    if (label) {
        if (strlen(label) >= SMACK_LONGLABEL-1) {
            fprintf(stderr, "%s: label '%s' exceeds length of %d\n",
                    argv[0], label, SMACK_LONGLABEL-1);
            exit(1);
        }
        if (!cantransition(argv[0], mylabel, label)) {
            free(label);
            exit(1);
        }
    }

    // Find the executable in $PATH:
    binary = findExe(argv[optind]);
    if (!binary) {
        fprintf(stderr, "%s: Cannot find %s\n", argv[0], argv[optind]);
        if (label) free(label);
        exit(1);
    }

    // Get the program's label
    if ((rc = getxattr(binary, XATTR_NAME_SMACK, filelabel, sizeof(filelabel)-1)) == -1) {
        perror("getxattr");
        if (label) free(label);
        free(binary);
        exit(1);
    }
    filelabel[rc] = 0;

    // We need to be able to execute the program,
    // and the label we execute the program AS must also be allowed to execute
    // it.
    if (!label) {
        if (!cantransition(argv[0], mylabel, filelabel)) {
            free(binary);
            exit(1);
        }
    } else {
        if (!smackaccess(label, filelabel, "x")) {
            fprintf(stderr, "%s: '%s' cannot execute '%s'\n", argv[0], label, filelabel);
            free(binary);
            exit(1);
        }

    }

    // Set smack label:
    if (label) {
        if (setsmack(label) == -1) {
            perror("setsmack");
            free(label);
            exit(1);
        }
        free(label);
    } else if (setsmack(filelabel) == -1) {
        perror("setsmack");
        exit(1);
    }

    if (!doexec)
    {
        pid_t pid = fork();
        if (pid) {
            int st;
            free(binary);
            while (waitpid(pid, &st, 0) != pid) {
                perror("wait");
            }
            exit(WEXITSTATUS(st));
        }
    }
    if (getuid() != geteuid())
        seteuid(getuid());


    // Prepare parameters:
    arglist = (char**)malloc(sizeof(arglist[0]) * (argc - optind + 1));
    //arglist[0] = binary;
    arglist[0] = argv[optind];
    for (o = optind + 1; o < argc; ++o) {
        arglist[o - optind] = argv[o];
    }
    arglist[o - optind] = NULL;

    execve(binary, arglist, envp);
    //fexecve(binfd, arglist, envp);
    perror("exec");
    exit(1);
    return 1;
}
Ejemplo n.º 4
0
int main(int argc, char **argv)
{
    QCoreApplication app(argc, argv);
    app.setApplicationVersion("1.0");

#ifdef Q_OS_WIN
    const QLatin1Char pathsep(';');
#else
    const QLatin1Char pathsep(':');
#endif

    QCommandLineParser parser;
    parser.setApplicationDescription(QCoreApplication::translate("qtpaths", "Command line client to QStandardPaths"));
    parser.addPositionalArgument(QCoreApplication::translate("qtpaths", "[name]"), QCoreApplication::tr("Name of file or directory"));
    parser.addHelpOption();
    parser.addVersionOption();

    //setting up options
    QCommandLineOption types(QStringLiteral("types"), QCoreApplication::translate("qtpaths", "Available location types."));
    parser.addOption(types);

    QCommandLineOption paths(QStringLiteral("paths"), QCoreApplication::translate("qtpaths", "Find paths for <type>."), QStringLiteral("type"));
    parser.addOption(paths);

    QCommandLineOption writablePath(QStringLiteral("writable-path"),
                                    QCoreApplication::translate("qtpaths", "Find writable path for <type>."), QStringLiteral("type"));
    parser.addOption(writablePath);

    QCommandLineOption locateDir(QStringList() << QStringLiteral("locate-dir") << QStringLiteral("locate-directory"),
                                 QCoreApplication::translate("qtpaths", "Locate directory [name] in <type>."), QStringLiteral("type"));
    parser.addOption(locateDir);

    QCommandLineOption locateDirs(QStringList() << QStringLiteral("locate-dirs") << QStringLiteral("locate-directories"),
                                  QCoreApplication::translate("qtpaths", "Locate directories [name] in all paths for <type>."), QStringLiteral("type"));
    parser.addOption(locateDirs);

    QCommandLineOption locateFile(QStringLiteral("locate-file"),
                                  QCoreApplication::translate("qtpaths", "Locate file [name] for <type>."), QStringLiteral("type"));
    parser.addOption(locateFile);

    QCommandLineOption locateFiles(QStringLiteral("locate-files"),
                                   QCoreApplication::translate("qtpaths", "Locate files [name] in all paths for <type>."), QStringLiteral("type"));
    parser.addOption(locateFiles);

    QCommandLineOption findExe(QStringList() << QStringLiteral("find-exe") << QStringLiteral("find-executable"),
                               QCoreApplication::translate("qtpaths", "Find executable with [name]."));
    parser.addOption(findExe);

    QCommandLineOption display(QStringList() << QStringLiteral("display"),
                               QCoreApplication::translate("qtpaths", "Prints user readable name for <type>."), QStringLiteral("type"));
    parser.addOption(display);

    QCommandLineOption testmode(QStringList() << QStringLiteral("testmode") << QStringLiteral("test-mode"),
                                QCoreApplication::translate("qtpaths", "Use paths specific for unit testing."));
    parser.addOption(testmode);

    QCommandLineOption qtversion(QStringLiteral("qt-version"), QCoreApplication::translate("qtpaths", "Qt version."));
    parser.addOption(qtversion);

    QCommandLineOption installprefix(QStringLiteral("install-prefix"), QCoreApplication::translate("qtpaths", "Installation prefix for Qt."));
    parser.addOption(installprefix);

    QCommandLineOption bindir(QStringList() << QStringLiteral("binaries-dir") << QStringLiteral("binaries-directory"),
                              QCoreApplication::translate("qtpaths", "Location of Qt executables."));
    parser.addOption(bindir);

    QCommandLineOption plugindir(QStringList() << QStringLiteral("plugin-dir") << QStringLiteral("plugin-directory"),
                                 QCoreApplication::translate("qtpaths", "Location of Qt plugins."));
    parser.addOption(plugindir);

    parser.process(app);

    QStandardPaths::enableTestMode(parser.isSet(testmode));

    QStringList results;
    if (parser.isSet(qtversion)) {
        QString qtversionstring = QString::fromLatin1(qVersion());
        results << qtversionstring;
    }

    if (parser.isSet(installprefix)) {
        QString path = QLibraryInfo::location(QLibraryInfo::PrefixPath);
        results << path;
    }

    if (parser.isSet(bindir)) {
        QString path = QLibraryInfo::location(QLibraryInfo::BinariesPath);
        results << path;
    }

    if (parser.isSet(plugindir)) {
        QString path = QLibraryInfo::location(QLibraryInfo::PluginsPath);
        results << path;
    }

    if (parser.isSet(types)) {
        QStringList typesList = ::types();
        results << typesList.join('\n');
    }

    if (parser.isSet(display)) {
        QStandardPaths::StandardLocation location = parseLocationOrError(parser.value(display));
        QString text = QStandardPaths::displayName(location);
        results << text;
    }

    if (parser.isSet(paths)) {
        QStandardPaths::StandardLocation location = parseLocationOrError(parser.value(paths));
        QStringList paths = QStandardPaths::standardLocations(location);
        results << paths.join(pathsep);
    }

    if (parser.isSet(writablePath)) {
        QStandardPaths::StandardLocation location = parseLocationOrError(parser.value(writablePath));
        QString path = QStandardPaths::writableLocation(location);
        results << path;
    }

    if (parser.isSet(findExe)) {
        QString searchitem = searchStringOrError(&parser);
        QString path = QStandardPaths::findExecutable(searchitem);
        results << path;
    }

    if (parser.isSet(locateDir)) {
        QStandardPaths::StandardLocation location = parseLocationOrError(parser.value(locateDir));
        QString searchitem = searchStringOrError(&parser);
        QString path = QStandardPaths::locate(location, searchitem, QStandardPaths::LocateDirectory);
        results << path;
    }

    if (parser.isSet(locateFile)) {
        QStandardPaths::StandardLocation location = parseLocationOrError(parser.value(locateFile));
        QString searchitem = searchStringOrError(&parser);
        QString path = QStandardPaths::locate(location, searchitem, QStandardPaths::LocateFile);
        results << path;
    }

    if (parser.isSet(locateDirs)) {
        QStandardPaths::StandardLocation location = parseLocationOrError(parser.value(locateDirs));
        QString searchitem = searchStringOrError(&parser);
        QStringList paths = QStandardPaths::locateAll(location, searchitem, QStandardPaths::LocateDirectory);
        results << paths.join(pathsep);
    }

    if (parser.isSet(locateFiles)) {
        QStandardPaths::StandardLocation location = parseLocationOrError(parser.value(locateFiles));
        QString searchitem = searchStringOrError(&parser);
        QStringList paths = QStandardPaths::locateAll(location, searchitem, QStandardPaths::LocateFile);
        results << paths.join(pathsep);
    }
    if (results.isEmpty()) {
        parser.showHelp();
    } else if (results.size() == 1) {
        const QString &item = results.first();
        message(item);
        if (item.isEmpty())
            return EXIT_FAILURE;
    } else {
        QString errorMessage = QCoreApplication::translate("qtpaths", "Several options given, only one is supported at a time.");
        error(errorMessage);
    }
    return EXIT_SUCCESS;
}