5 // Created by Claudio Bisegni on
21/06/07.
6 // Copyright
2007 INFN
- National Institute of Nuclear Physics.
All rights reserved.
11 #include
<sys
/types.h
>
14 static AuthUtil
*sharedAuthUtil
= nil
;
16 @implementation AuthUtil
21 if (sharedAuthUtil
== nil
) {
22 [[self alloc
] init
]; // assignment not done here
25 return sharedAuthUtil
;
28 + (id
)allocWithZone
:(NSZone
*)zone
31 if (sharedAuthUtil
== nil
) {
32 sharedAuthUtil
= [super allocWithZone
:zone
];
33 return sharedAuthUtil
; // assignment and
return on first allocation
36 return nil
; //on subsequent allocation attempts
return nil
39 - (id
)copyWithZone
:(NSZone
*)zone
44 // -------------------------------------------------------------------------------
46 // -------------------------------------------------------------------------------
49 authorizationRef
= nil
;
53 // -------------------------------------------------------------------------------
55 // -------------------------------------------------------------------------------
58 OSStatus status
= noErr
;
59 AuthorizationFlags flags
;
60 AuthorizationItem myItems
= {kAuthorizationRightExecute
, 0, NULL, 0};
61 AuthorizationRights myRights
= {1, &myItems
};
62 flags
= kAuthorizationFlagDefaults | kAuthorizationFlagInteractionAllowed | kAuthorizationFlagPreAuthorize | kAuthorizationFlagExtendRights
;
64 /*if(authorizationRef) {
68 // chek
if autorization is valid
for and old password request
69 status
= AuthorizationCopyRights
(authorizationRef
, &myRights
, NULL, flags
, NULL );
71 if (status
!= errAuthorizationSuccess
) {
73 flags
= kAuthorizationFlagDefaults
;
74 if(!authorizationRef
){
75 status
= AuthorizationCreate
(NULL, kAuthorizationEmptyEnvironment
, flags
, &authorizationRef
);
76 if (status
!= errAuthorizationSuccess
) {
81 flags
= kAuthorizationFlagDefaults | kAuthorizationFlagInteractionAllowed | kAuthorizationFlagPreAuthorize | kAuthorizationFlagExtendRights
;
82 status
= AuthorizationCopyRights
(authorizationRef
, &myRights
, NULL, flags
, NULL );
84 if (status
!= errAuthorizationSuccess
) {
85 AuthorizationFree
(authorizationRef
, kAuthorizationFlagDefaults
);
94 // -------------------------------------------------------------------------------
96 // -------------------------------------------------------------------------------
100 if(authorizationRef
){
101 status
= AuthorizationFree
(authorizationRef
, kAuthorizationFlagDefaults
);
102 authorizationRef
= 0L;
104 return status
== noErr
;
108 // -------------------------------------------------------------------------------
110 // -------------------------------------------------------------------------------
111 -(AuthorizationRef
) authorization
113 return authorizationRef
;
116 // -------------------------------------------------------------------------------
118 // -------------------------------------------------------------------------------
119 -(NSData
*) extFormAuth
{
120 AuthorizationExternalForm extAuth
; // external authorization
121 NSData
*authorizationData
= nil
;
122 if(AuthorizationMakeExternalForm
([self authorization
], &extAuth
))
124 NSLog
(@"Could not create external authorization form."
);
127 authorizationData
= [NSData dataWithBytes
:&extAuth
length:sizeof
(AuthorizationExternalForm
)];
128 return authorizationData
;
131 // -------------------------------------------------------------------------------
133 // -------------------------------------------------------------------------------
134 -(OSStatus
) execUnixCommand
:(const
char*) commandPath args
:(const
char*[])args output
:(NSMutableString
*)output
136 OSStatus status
= noErr
;
137 FILE
*commandOutIn
= NULL;
141 status
= AuthorizationExecuteWithPrivileges
(authorizationRef
, commandPath
, kAuthorizationFlagDefaults
, (char *const
*)args
, &commandOutIn
);
142 if (status
== errAuthorizationSuccess
&& commandOutIn
){
145 int bytesRead
= read
(fileno
(commandOutIn
), buff
, sizeof
(buff
));
146 if (bytesRead
< 1) break;
147 //write
(fileno
(stdout
), buff
, bytesRead
);
149 [output appendString
:[NSString stringWithCString
:buff encoding
:NSUTF8StringEncoding
]];
154 fflush
(commandOutIn
);
155 fclose(commandOutIn
);
157 // whait
for comand finish
158 pid
= wait
( &pidStatus
);
159 if (pid
== -1 ||
!WIFEXITED
( pidStatus
))
161 NSLog
( @"
Error: problem with wait pid status
: %d", pidStatus );
172 - (unsigned
)retainCount
174 return UINT_MAX
; //denotes an object that cannot be released