2 * This file is part of the OpenPTS project.
4 * The Initial Developer of the Original Code is International
5 * Business Machines Corporation. Portions created by IBM
6 * Corporation are Copyright (C) 2010 International Business
7 * Machines Corporation. All Rights Reserved.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the Common Public License as published by
11 * IBM Corporation; either version 1 of the License, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * Common Public License for more details.
19 * You should have received a copy of the Common Public License
20 * along with this program; if not, a copy can be viewed at
21 * http://www.opensource.org/licenses/cpl1.0.php.
26 * \brief UUID wrapper (Generic part, OPENPTS_UUID)
27 * @author Seiji Munetoh <munetoh@users.sourceforge.jp>
29 * cleanup 2011-10-07 SM
35 * Program UUID Description When Files
36 * ---------------------------------------------------------------------------------------------------
37 * ptsc CID Colelctor ID System install => /var/lib/openpts/uuid (UUID of sign key)
38 * RM RM ID RM Gen xid in RM, path /var/lib/openpts/$UUID/rm_files
39 * RunID ID of this daemon Daemon start => /var/lib/openpts/run_uuid TODO ptsc requires uuid file
40 * ---------------------------------------------------------------------------------------------------
41 * openpts VID Verifier ID 1st run => 'HOME/.openpts/uuid
42 * CID Colelctor ID Enrollment => 'HOME/.openpts/$UUID (dir name)
43 * RM Colelctor RM ID Enrollment => 'HOME/.openpts/$UUID/rm_uuid
44 * NEWRM Colelctor New RM ID Update => 'HOME/.openpts/$UUID/newrm_uuid
45 * OLDRM Colelctor Old RM ID Update => 'HOME/.openpts/$UUID/oldrm_uuid
46 * ---------------------------------------------------------------------------------------------------
48 * Unit Test: check_uuid.c
57 #include <sys/types.h>
68 #define SEP_LINE "------------------------------------------------------------------------------------"
71 /******************************/
73 /******************************/
76 * Create new OPENPTS_UUID, no contents
78 * @return OPENPTS_UUID
80 OPENPTS_UUID *newOpenptsUuid() {
83 uuid = xmalloc(sizeof(OPENPTS_UUID)); // BYTE[16]
87 memset(uuid, 0, sizeof(OPENPTS_UUID));
93 * Create new OPENPTS_UUID, with contents
95 * @return OPENPTS_UUID
97 OPENPTS_UUID *newOpenptsUuid2(PTS_UUID *pts_uuid) {
100 uuid = xmalloc(sizeof(OPENPTS_UUID)); // BYTE[16]
104 memset(uuid, 0, sizeof(OPENPTS_UUID));
106 uuid->uuid = xmalloc_assert(16);
107 memcpy(uuid->uuid, pts_uuid, 16);
109 uuid->str = getStringOfUuid(uuid->uuid);
110 uuid->time = getDateTimeOfUuid(uuid->uuid);
111 uuid->status = OPENPTS_UUID_UUID_ONLY;
117 * init UUID from file
118 * status = OPENPTS_UUID_EMPTY
119 * @return OPENPTS_UUID
121 OPENPTS_UUID *newOpenptsUuidFromFile(char * filename) {
125 uuid = newOpenptsUuid();
130 /* set the filename */
131 uuid->filename = smalloc_assert(filename);
133 /* load the filename */
134 rc = readOpenptsUuidFile(uuid);
135 if (rc != PTS_SUCCESS) {
136 ERROR("newOpenptsUuidFromFile() - readOpenptsUuidFile() fail rc=%d\n", rc);
137 freeOpenptsUuid(uuid);
147 void freeOpenptsUuid(OPENPTS_UUID *uuid) {
150 ERROR("null input\n");
154 if (uuid->filename != NULL) {
155 xfree(uuid->filename);
157 if (uuid->uuid != NULL) {
160 if (uuid->str != NULL) {
163 if (uuid->time != NULL) {
173 * @retval PTS_SUCCESS
174 * @retval PTS_INTERNAL_ERROR
176 int genOpenptsUuid(OPENPTS_UUID *uuid) {
178 ASSERT(NULL != uuid, "uuid is NULL\n");
180 /* check the status */
181 if (uuid->status == OPENPTS_UUID_EMPTY) {
182 // hold UUID only, no binding with the file
183 uuid->status = OPENPTS_UUID_UUID_ONLY;
184 } else if (uuid->status == OPENPTS_UUID_FILENAME_ONLY) {
185 // TODO Re genenation happen, before load the UUID from file
186 DEBUG("genOpenptsUuid() %s filled, before load the UUID from file\n", uuid->str);
187 uuid->status = OPENPTS_UUID_FILLED;
188 } else if (uuid->status == OPENPTS_UUID_FILLED) {
189 // TODO Re genenation happen
190 uuid->status = OPENPTS_UUID_CHANGED;
191 ERROR("genOpenptsUuid() %s - changed\n", uuid->str);
192 } else if (uuid->status == OPENPTS_UUID_CHANGED) {
193 // TODO Re genenation happen
194 uuid->status = OPENPTS_UUID_CHANGED;
195 ERROR("genOpenptsUuid() %s - changed again\n", uuid->str);
196 } else if (uuid->status == OPENPTS_UUID_UUID_ONLY) {
197 // TODO Re genenation happen
198 uuid->status = OPENPTS_UUID_UUID_ONLY;
199 ERROR("genOpenptsUuid() %s - changed again (no binding to the file)\n", uuid->str);
201 ERROR("genOpenptsUuid() - bad status\n");
206 if (uuid->uuid != NULL) {
209 if (uuid->str != NULL) {
212 if (uuid->time != NULL) {
217 uuid->uuid = newUuid();
218 uuid->str = getStringOfUuid(uuid->uuid);
219 uuid->time = getDateTimeOfUuid(uuid->uuid);
222 DEBUG("genOpenptsUuid() - %s\n", uuid->str);
228 * read UUID from file(uuid->filename), and fill OPENPTS_UUID
230 * @retval PTS_SUCCESS
231 * @retval PTS_INTERNAL_ERROR
233 int readOpenptsUuidFile(OPENPTS_UUID *uuid) {
234 int rc = PTS_SUCCESS;
242 return PTS_INTERNAL_ERROR;
244 if (uuid->filename == NULL) {
246 return PTS_INTERNAL_ERROR;
249 DEBUG("readOpenptsUuidFile() : %s\n", uuid->filename);
251 // TODO check UUID status?
252 if (uuid->status == OPENPTS_UUID_FILENAME_ONLY) {
255 // reload UUID from same? file
256 DEBUG("reload UUID, current UUID=%s, filename=%s\n",
257 uuid->str, uuid->filename);
261 if (uuid->uuid != NULL) {
264 if (uuid->str != NULL) {
267 if (uuid->time != NULL) {
272 if ((fp = fopen(uuid->filename, "r")) == NULL) {
273 // DEBUG("readUuidFile - UUID File %s open was failed\n", filename);
274 /* we don't want double free errors - we may have already freed these up
275 above. this was a genuine issue that caused multiple pointers to
276 reference the same area of memory. */
280 return PTS_DENIED; // TODO
284 memset(line, 0, BUF_SIZE);
287 if (fgets(line, BUF_SIZE, fp) != NULL) {
289 /* remove LR at the end otherwise getUuidFromString() go bad */
290 for (i = 0; i < BUF_SIZE; i++) {
291 if (line[i] == 0x0a) {
297 uuid->uuid = getUuidFromString(line);
298 if (uuid->uuid == NULL) {
299 ERROR("readUuidFile() - UUID is NULL, file %s\n", uuid->filename);
300 rc = PTS_INTERNAL_ERROR;
303 uuid->str = getStringOfUuid(uuid->uuid);
304 if (uuid->str == NULL) {
305 ERROR("readUuidFile() - STR UUID is NULL, file %s\n", uuid->filename);
306 rc = PTS_INTERNAL_ERROR;
309 uuid->time = getDateTimeOfUuid(uuid->uuid);
310 if (uuid->time == NULL) {
311 ERROR("readUuidFile() - TIME UUID is NULL, file %s\n", uuid->filename);
312 rc = PTS_INTERNAL_ERROR;
315 uuid->status = OPENPTS_UUID_FILLED;
317 fprintf(stderr, NLS(MS_OPENPTS, OPENPTS_UUID_READ_FAILED, "Failed to read the UUID file\n"));
327 * @retval PTS_SUCCESS
328 * @retval PTS_INTERNAL_ERROR
330 int writeOpenptsUuidFile(OPENPTS_UUID *uuid, int overwrite) {
333 int mode = S_IRUSR | S_IWUSR | S_IRGRP;
337 ERROR("writeOpenptsUuidFile() - uuid == NULL\n");
338 return PTS_INTERNAL_ERROR;
340 if (uuid->filename == NULL) {
341 ERROR("writeOpenptsUuidFile() - uuid->filename == NULL\n");
342 return PTS_INTERNAL_ERROR;
344 if ((uuid->status != OPENPTS_UUID_FILLED) && (uuid->status != OPENPTS_UUID_CHANGED)) {
345 ERROR("writeOpenptsUuidFile() - uuid->status = %d (!= FILLED or CHANGED)\n", uuid->status);
346 // 1 => OPENPTS_UUID_FILENAME_ONLY, UUID is missing
347 return PTS_INTERNAL_ERROR;
349 if (uuid->str == NULL) {
350 ERROR("writeOpenptsUuidFile() - uuid->str == NULL\n");
351 return PTS_INTERNAL_ERROR;
355 if (overwrite == 1) {
357 if ((fp = fopen(uuid->filename, "w")) == NULL) {
358 fprintf(stderr, NLS(MS_OPENPTS, OPENPTS_UUID_FILE_OPEN_FAILED,
359 "Failed to open UUID file %s\n"), uuid->filename);
360 return PTS_INTERNAL_ERROR;
364 if ((fd = open(uuid->filename, O_CREAT | O_EXCL | O_WRONLY, mode)) == -1) {
365 if (errno == EEXIST) {
366 /* exist, keep the current UUID file */
367 fprintf(stderr, NLS(MS_OPENPTS, OPENPTS_UUID_FILE_EXISTS,
368 "The UUID file '%s' already exists\n"), uuid->filename);
369 // return PTS_SUCCESS; // TODO
370 return OPENPTS_FILE_EXISTS;
372 fprintf(stderr, NLS(MS_OPENPTS, OPENPTS_UUID_FILE_OPEN_FAILED,
373 "Failed to open UUID file %s\n"), uuid->filename);
374 return PTS_INTERNAL_ERROR;
377 if ((fp = fdopen(fd, "w")) == NULL) {
378 fprintf(stderr, NLS(MS_OPENPTS, OPENPTS_UUID_FILE_OPEN_FAILED,
379 "Failed to open UUID file %s\n"), uuid->filename);
380 return PTS_INTERNAL_ERROR;
385 fprintf(fp, "%s", uuid->str);
387 fclose(fp); // this close fd also
389 DEBUG("writeOpenptsUuidFile() %s -> %s\n", uuid->str, uuid->filename);