Run shell script as root user (without sudo) using setuid

In the most Linux distributions, setuid doesn’t work on shell scripts because of the security issues associated with it.

But you can use the setuid on binary files and from there you can execute the shell script as root user. And the following steps will help you to do that.

1) Copy the following C program to a file named “runasroot.c” and this program simply runs a script which is passed as command-line argument.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {                                              
  if (argc != 2) {                                                              
    printf("Usage: %s /path/to/script\n", argv[0]);                             
    printf("Note: '/path/to/script' can be either relative or absolute path.\nE\
xamples:\n%s ./build.sh \n%s /tmp/test.sh\n", argv[0], argv[0]);                
  } else {                                                                      
    printf("Running the script '%s' as root user...\n", argv[1]);               
    setuid(0);                                                                  
    system(argv[1]);                                                            
  }                                                                             
   return 0;                                                                    
}

2) Compile it.

gcc runasroot.c -o runasroot

3) Change the ownership of the compiled executable binary to root

sudo chown root:root runasroot

4) Change the permissions on the binary file with setuid flag

sudo chmod 4755 runasroot

5) Run the script using the executable binary file

./runasroot script.sh

Examples :

./runasroot ./build.sh 
./runasroot /tmp/test.sh

You can even copy this ‘runasroot’ executable file to your home bin directory (i.e., in ‘/home/user/bin‘) so that you can use it anywhere in your scripts. And you can use the ‘runasroot‘ command (without any password prompt) instead ofsudo‘ in your commands. If you want to pass parameters to the commands, you should use single/double quotes to surround it.

runasroot 'fdisk -l'

instead of

sudo fdisk -l

Warning: Make sure you don’t place this script in any common directory (like /usr/bin or /opt) in a multi-user environments.

How to fix the wireless network issues in Fedora?

Issue 1 : NetworkManager asks password each time (even password is saved) when you connect to enterprise wireless network.

Does you fedora (NetworkManager) asks the password each time even when you saved it? I’m also faced it with my ThinkPad Edge (Fedora 20) and found a workaround to fix it.

Continue reading “How to fix the wireless network issues in Fedora?”

Maven Multi-Module Structure for Enterprise Java Projects (Best Practices)

Many times, I am trying to resolve several basic or complex Maven /  Java EE  project structure issues, on my day work. In order to provide the solutions,  I often end up experimenting with the project structure and it’s effectiveness.

It’s worth investing some time on application structure for big projects (especially Java EE). If not, it’s cannot be changed at the end.

It’s important to understand the application structure and the underlying build tools when you start working on a project.

Maven might be wise choice because it’s helps you build a better project quickly and effectively.


Maven helps in the following areas :

  • Making the build process easy
  • Providing a uniform build system
  • Providing quality project information
  • Providing guidelines for best practices development
  • Allowing transparent migration to new features

Best practices (maven’s) for development

  • Keeping your test source code in a separate, but parallel source tree
  • Using test case naming conventions to locate and execute tests
  • Have test cases setup their environment and don’t rely on customizing the build for test preparation.

Maven is designed to be flexible, but only to an extent. So, using maven in your project means, you need to reorganize your project (if not in good shape) and adhere to the maven’s conventions.

Random Quote :

“It is good to break conventions in your life and in your coding, but never with Maven.”


 Organizing Style

There are number of ways you can organize your project’s sub-modules.

  • Single Bulk Module
  • Module by Class Type
  • Module by Functionality Area

1) All the modules can be grouped together as a single bulk project. But it’s not recommended anyway.

2) Module by class type : Project can be splitted into sub-projects/modules. And each module are used to group together all classes that comprise, but are not limited to, a layer in the project’s architecture. For example, a module could contain all classes that make up the project’s service or persistence layers or a module could contain all the model class (i.e., jpa or entity beans).

3) Modules by functionality area : Project’s sub-modules are organized by functional area (component based) i.e, vertical slice of the application including model beans, service layer, repositories etc..

Example for ‘Module by class type’:

  • app-model
  • app-utils
  • app-repository
  • app-services
  • app-web
  • app-ear

Example for ‘Module by functionality’:

  • app-utils
  • app-user
  • app-audit
  • app-auth
  • app-integration
  • app-web
  • app-ear

It’s always necessary for each module to have it’s own unit tests.

Pros and Cons of Class Type :

+ Pretty maintainable in that you always known where to find stuff
+ Build order is very straight forward
Circular dependencies
Classes with totally different responsibilities are all mixed up together making it difficult to re-use functionality in other projects

Pros and Cons of Functionality Type :

+ Modules are highly decoupled
+ It’s far simpler to re-use a functional area of code in other projects
Build order might be complex because of its dependencies

You may have noticed that both of them are not 100% perfect.

Also there are few modules which are repeated in both these types in the project. Because it’s always wise to keep the necessary things together i.e., creating a separate database module allows you to change your project’s database implementation fairly easily, which may make testing easier in some circumstances and likewise, having a separate web module allows you to re-skin your code easily.

At the end of the day, it really depends on the need of the project and it’s existing structure. And you can also mix-match both these types, if helps to solve your problem effectively.

GNOME 3.6: GNOME Online Accounts and Google two-factor authentication

Kparal's Fedora Blog

goa-panel In GNOME 3.4 (Fedora 17), GNOME Online Accounts (GOA) worked great with Google two-factor authentication (you really should enable that, if you value your data). In GNOME 3.6 (Fedora 18) it works no more, and it might be fixed in GNOME 3.8. When developers break some existing functionality for the sake of “progress”, but don’t bother fixing it or providing an alternative way before an official release, I always feel a bit… disenchanted.

Fortunately you can work around the broken code.

  1. Open Seahorse, filter your passwords for “GOA”, you should see one or two items of “Gnome Online Accounts password” type. Delete them.
  2. Re-login to Gnome session.
  3. Open Online Accounts and log in to your Google account. It will fail.
  4. Create an application-specific password for your Google account in the web browser.
  5. Open Seahorse, filter your password for “GOA”, you should see a single item. Open it and display…

View original post 84 more words

Generating and Compiling Java Classes using Java Compiler API

Steps :

  • Construct JavaFileObject from any source (i.e, here, from sourceCode String) and make it to iterable object
  • Get the Java System Compiler
  • Create a compiler task using Compiler.getTask(), (if required create the task with diagnostic collection to get compilation errors)
  • Execute the Compiler Task (compile it).

Source Code :

import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import java.util.Locale;

import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

public class CompileDynamicClasses {

static String sourceCode = "class DynamicClass{" + "public static void main (String args[]){"
+ "System.out.println (\"Hello, Dynamic Class!\");" + "}" + "}";

public static void main(String[] args) {

// Construct a JavaFileObject from source code
SimpleJavaFileObject fileObject = new DynamicJavaSourceCodeObject("DynamicClass",
sourceCode);

Continue reading "Generating and Compiling Java Classes using Java Compiler API"

[Nexus 7] AOSP ROM – Jelly Bean 4.2.2 with franco kernel

I built a AOSP ROM based on latest version of pure Android open source project (AOSP JB 4.2.2). And I replaced the default kernel with franco kernel (r47) which is known for its performance.

Warning : Built for Nexus 7 wifi only model (grouper).

Features: Pure Android Experience + Better performance with franco kernel + Stable

Refer franco.Kernel (r47) features : http://forum.xda-developers.com/show…63&postcount=1

Download Link :

AOSP 4.2.2 ROM : https://dl.dropbox.com/u/29388500/AO…ramsrib-v2.zip

GAPPS : http://goo.im/gapps/gapps-jb-20130301-signed.zip

Instructions :
* Backup first.
* Flash this AOSP 4.2.2 ROM.
* Flash Google apps. (It is not optional anymore!)
* Do ‘Wipe data/factory reset’ in recovery.
* Reboot & enjoy.

Every custom modification to devices have risks. Flash this at your own risk!

Screenshots :

About Android
Default Home Screen
Default Apps
Home Screen

 

 

 

 

 

 

 

For more details, checkout my actual thread at XDA developers :
http://forum.xda-developers.com/showpost.php?p=38739972&postcount=1

How to fix postgresql – ‘could not create shared memory segment: Invalid argument (shmget) ‘ error?

I recently updated my Postgresql’s ‘max_connections’ to 450 ( in postgresql.conf) which makes my postgresql server to fail during start-up :


FATAL: could not create shared memory segment: Invalid argument
DETAIL: Failed system call was shmget(key=5432001, size=35274752, 03600).
HINT: This error usually means that PostgreSQL's request for a shared memory segment exceeded your kernel's SHMMAX parameter. You can ...currently 454).
If the request size is already small, it's possible that it is less than your kernel's SHMMIN parameter, in which case raising the reque...s called for.
The PostgreSQL documentation contains more information about shared memory configuration.
[FAILED]

This is because of your shared memory.

1) Check your maximum shared memory :
$ sudo cat /proc/sys/kernel/shmmax
33554432

You need to increase the shared memory to fix this.

2) To fix it temporarily, run this command :
$ sudo sysctl -w kernel.shmmax=134217728

3) To fix this permenantly, set this in your configuration:
$ sudo vi /etc/sysctl.conf
kernel.shmmax = 134217728

4) Now start your pgsql server, It will work.